<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Smörgåsbord &#187; socat</title>
	<atom:link href="http://smorgasbord.gavagai.nl/tags/socat/feed/" rel="self" type="application/rss+xml" />
	<link>http://smorgasbord.gavagai.nl</link>
	<description>Ambachtelijk bereide beschouwingen.</description>
	<lastBuildDate>Fri, 06 Jan 2012 21:30:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>DNS over UDP over TCP over SSH over HTTPS-proxy with SOCAT &#8211; a reader question</title>
		<link>http://smorgasbord.gavagai.nl/2010/10/dns-over-udp-over-tcp-over-ssh-over-https-proxy-with-socat-a-reader-question/</link>
		<comments>http://smorgasbord.gavagai.nl/2010/10/dns-over-udp-over-tcp-over-ssh-over-https-proxy-with-socat-a-reader-question/#comments</comments>
		<pubDate>Sun, 31 Oct 2010 18:40:08 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[socat]]></category>
		<category><![CDATA[tunneling]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=1177</guid>
		<description><![CDATA[A while ago I received a question from a reader. He was having some trouble with a pretty interesting setup. As I was rather intrigued I spent some time to figure it out — it&#8217;s solved, but I&#8217;m not 100% sure on the theory. So if you are, please comment.
the question

from	Yves &#60;theYinYeti@yeti.selfip.net&#62;
to	wicher@gavagai.eu
date	Wed, Sep 15, 2010 [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I received a question from a reader. He was having some trouble with a pretty interesting setup. As I was rather intrigued I spent some time to figure it out — it&#8217;s solved, but I&#8217;m not 100% sure on the theory. So if <b>you are</b>, please comment.</p>
<h2>the question</h2>
<blockquote><p>
from	Yves &lt;theYinYeti@yeti.selfip.net&gt;<br />
to	wicher@gavagai.eu<br />
date	Wed, Sep 15, 2010 at 16:06<br />
subject	Socat</p>
<p>Dear mister Minnaard,</p>
<p>I hope you don&#8217;t mind me asking you for help. By chance, I saw your blog post:<br />
<a href="http://smorgasbord.gavagai.nl/2010/01/socat-access-your-x-servers-domain-socket-over-tcp/">http://smorgasbord.gavagai.nl/2010/01/socat-access-your-x-servers-domain-socket-over-tcp/</a></p>
<p>It happens I have a problem with socat on my Sheevaplug server, and it seems you have good knowledge of this software.</p>
<p>Following <a href="http://zarb.org/~gc/html/udp-in-ssh-tunneling.html">http://zarb.org/~gc/html/udp-in-ssh-tunneling.html</a>, I use socat to tunnel DNS through SSH, so as to have transparent DNS resolution, to be used with a transparent proxy (NAT rules + tinyproxy, backed by cntlmd, backed by corporate proxy).</p>
<p>My problem is that the number of socat PIDs on both the client and the server keeps growing. I&#8217;ve had floods of “accept: too many open files” messages, and I suspect socat is the culprit (at this point, a ps -ef shows almost a thousand of socat PIDs!); especially since the transparent DNS stops working after this flood of messages.</p>
<p>More specifically, on the client, I run:</p>
<ul>
<li><code>dnsmasq</code> with “<code>server=127.0.0.1#1053</code>” as a back-end</li>
<li><code>
<pre>socat -ly udp4-listen:1053,reuseaddr,fork tcp:127.0.0.1:1054</pre>
<p></code></li>
<li><code>
<pre>ssh -L 1053:208.67.222.222:53 -L 1054:127.0.0.1:1054 \
-Tfx remoteuser@remoteserver \
'socat -lf /dev/null tcp4-listen:1054,reuseaddr,fork \
UDP:208.67.222.222:53'</pre>
<p></code>(208.67.222.222 is OpenDNS)
</li>
</ul>
<p>Besides, on localhost (local client), ssh is configured this way:<br />
<code><br />
Host remoteserver<br />
Port 443<br />
ServerAliveInterval 60<br />
ProxyCommand /usr/bin/proxytunnel -v -p corporateproxy:8080 -P login:passwd -d %h:%p<br />
</code><br />
Do you have an idea on how I could make socat behave better?<br />
I thank you beforehand for any advice you could provide.<br />
Sincerely,</p>
<p>Yves.
</p></blockquote>
<h2>the reply</h2>
<p>from	Wicher Minnaard &lt;wicher@gavagai.eu&gt;<br />
to	Yves &lt;theYinYeti@yeti.selfip.net&gt;<br />
date	Thu, Sep 16, 2010 at 02:19<br />
subject	Re: Socat</p>
<p>Dear mister Yves,</p>
<p>On Wed, Sep 15, 2010 at 16:06, Yves &lt;yves@yeti.selfip.net&gt; wrote:</p>
<blockquote><p>
Dear mister Minnaard,</p>
<p>I hope you don&#8217;t mind me asking you for help. By chance, I saw your blog post:<br />
http://smorgasbord.gavagai.nl/2010/01/socat-access-your-x-servers-domain-socket-over-tcp/</p>
<p>It happens I have a problem with socat on my Sheevaplug server, and it seems<br />
you have good knowledge of this software.
</p></blockquote>
<p>Thank you for your compliment. Actually, I do not know socat all too well, but I know a thing or two about networking.</p>
<blockquote><p>
Following http://zarb.org/~gc/html/udp-in-ssh-tunneling.html, I use socat to tunnel DNS through SSH, so as to have transparent DNS resolution, to be used with a transparent proxy (NAT rules + tinyproxy, backed by cntlmd, backed by<br />
corporate proxy).
</p></blockquote>
<p>Sounds like just another day at the office ;-)<br />
Interesting setup.</p>
<blockquote><p>
My problem is that the number of socat PIDs on both the client and the server keeps growing. I&#8217;ve had floods of “accept: too many open files&#8221; messages, and I suspect socat is the culprit (at this point, a ps -ef shows almost a thousand of socat PIDs!); especially since the transparent DNS stops working after this flood of messages.</p>
<ul>
<li><code>dnsmasq</code> with “<code>server=127.0.0.1#1053</code>” as a back-end</li>
<li><code>
<pre>socat -ly udp4-listen:1053,reuseaddr,fork tcp:127.0.0.1:1054</pre>
<p></code></li>
<li><code>
<pre>ssh -L 1053:208.67.222.222:53 -L 1054:127.0.0.1:1054 \
-Tfx remoteuser@remoteserver \
'socat -lf /dev/null tcp4-listen:1054,reuseaddr,fork \
UDP:208.67.222.222:53'</pre>
<p></code>(208.67.222.222 is OpenDNS)
</li>
</ul>
</blockquote>
<p>Sidenote: Google provides a solid DNS service nowadays. I don&#8217;t know what they do with the data, though the same could be said about OpenDNS.<br />
See http://code.google.com/speed/public-dns/</p>
<blockquote><p>
<code><br />
Host remoteserver<br />
Port 443<br />
ServerAliveInterval 60<br />
ProxyCommand /usr/bin/proxytunnel -v -p corporateproxy:8080 -P login:passwd -d %h:%p<br />
</code>
</p></blockquote>
<p>So it&#8217;s DNS over UDP over TCP over SSH over proxied HTTPS, or something. I&#8217;m losing track of all the layers here.</p>
<blockquote><p>
Do you have an idea on how I could make socat behave better?<br />
I thank you beforehand for any advice you could provide.
</p></blockquote>
<p>Well you got me curious now.<br />
I managed to reproduce your problem in a simplified setup:</p>
<p>- on host w00tage, setup the nameserver façade:<br />
<code>socat udp4-listen:1053,reuseaddr tcp:localhost:1054</code></p>
<p>- from host w00tage, tunnel the TCP connection over SSH to the host hosting the to-be-proxied nameserver:<br />
<code>ssh -L 1054:localhost:1054 priv</code></p>
<p>- now, on this host priv, connect the TCP socket to the UDP socket where the nameserver is listening<br />
<code>socat tcp4-listen:1054,fork,reuseaddr UDP:localhost:53</code></p>
<p>- on w00tage, do queries on localhost:1054:<br />
<code>dig @localhost -p1053 google.fr</code></p>
<p>Dig is part of the bind-tools package and greatly helps in diagnosing DNS problems.<br />
The first dig will probably return. Subsequent digs may take seconds to return. And they leave stray socat processes behind on both w00tage and priv. Kill the strays on w00tage, and the ones on priv dissolve. So the local ones (on w00tage) tie up the remote ones. The latter are not the problem, just a symptom. You can keep on querying, doing more digs, until your file descriptors run out.</p>
<p>A stab at what is going on here: UDP is stateless, and socat is ignorant of protocol data (as it should be). A DNS query takes one UDP packet. But socat does not know we&#8217;re doing DNS. And since UDP is stateless, there&#8217;s no &#8216;bye&#8217; in the conversation. So socat doesn&#8217;t know when to stop waiting for more. Dig does, though &#8211; it recognizes a DNS answer when it sees one. So dig returns (but why the delays? socat buffers and timeouts maybe?) but socat doesn&#8217;t. It&#8217;s still waiting for more.<br />
Now since we&#8217;re forking socat for every new independent socketpair opened (which is what happens when you do a query), we get a lot of those hanging socats.</p>
<p>I had a look at the URL you sent me, http://zarb.org/~gc/html/udp-in-ssh-tunneling.html, to see how they are getting it to work and it appears there is a &#8216;UDP-RECVFROM&#8217; address type in socat. From its manpage:<br />
————<br />
UDP-RECVFROM:
<port>
Creates  a  UDP socket on
<port> [UDP service] using UDP/IP version 4<br />
or 6 depending on option pf.  It receives one packet from an<br />
unspecified peer and may send one or more answer packets to that peer.<br />
This mode is particularly useful with fork option where each arriving<br />
packet &#8211; from arbitrary peers &#8211; is handled by its own sub process.<br />
This allows a behaviour similar to  typical  UDP  based  servers like<br />
ntpd or named. This address works well with socat UDP-SENDTO address<br />
peers.<br />
————</p>
<p>Hey, explicit mentioning of nameservers there. Receive one packet, don&#8217;t wait for more, send it off, collect answer, send that back.<br />
On host w00tage, I run the façade thusly:</p>
<p><code>socat udp4-recvfrom:1053,fork,reuseaddr tcp:localhost:1054</code></p>
<p>And that appears to work indeed. But wait, how does socat (on priv) know when to stop trying to collect answer packets?<br />
I think it&#8217;s socat timeouts, but I&#8217;m not 100% sure. What leads me to think that it&#8217;s timeouts is that when I stress-test this setup from w00tage:</p>
<p><code>while true; do dig @localhost -p1053 sheeva; done</code><br />
(for &#8217;sheeva&#8217; the nameserver never has to do recursion)<br />
I have about 30 forked-off query-listeners when I do a <code>pgrep socat | wc -l</code> on w00tage. But I have about 400 on priv. That number goes up and down and the pids change, so they do not *accumulate* but rather seem to hang about until they decide their job is done. But to be sure I&#8217;d have to study the source. Or ask the socat mailing list.</p>
<p>Anyway, problem solved!</p>
<p>I was happy to help you and I got some exercise out of it. Any reason why you didn&#8217;t post this question as a comment on my blog? Maybe it would be a bit off-topic, I can see that.<br />
Would you mind if I published this?</p>
<p>Regards and good luck, Wicher Minnaard.</p>
<h2>it works</h2>
<blockquote><p>
from	Yves &lt;theYinYeti@yeti.selfip.net&gt;<br />
to	wicher@gavagai.eu<br />
date	Thu, Sep 16, 2010 at 10:01<br />
subject	Re: Socat</p>
<p>Dear mister Minnaard,</p>
<p>All in all, I was just careless in adapting the “alternative solution with socat” to my setup.</p>
<p>I thank you for your analysis and tests; I learnt a few things following them. And I certainly wouldn&#8217;t mind at all if you were to publish this.<br />
However, if you do so, please change the e-mail to “theYinYeti@yeti.selfip.net” (the Sheevaplug :-) ), and my name to simply “Yves”; thank you. As for the web site (I see there&#8217;s a field for it), it is <a href="http://yeti.selfip.net/gablin.php">“http://yeti.selfip.net/gablin.php”</a> (a bit lame, but I may find time someday to update it…)</p>
<p>As you said, I had the feeling this was a bit off-topic. But now, I see how this can shed some light on advanced socat usage.<br />
&#8211;<br />
Regards,</p>
<p>Yves.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2010/10/dns-over-udp-over-tcp-over-ssh-over-https-proxy-with-socat-a-reader-question/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SOCAT: access your X server&#8217;s domain socket over TCP</title>
		<link>http://smorgasbord.gavagai.nl/2010/01/socat-access-your-x-servers-domain-socket-over-tcp/</link>
		<comments>http://smorgasbord.gavagai.nl/2010/01/socat-access-your-x-servers-domain-socket-over-tcp/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 18:09:11 +0000</pubDate>
		<dc:creator>Wicher</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[socat]]></category>
		<category><![CDATA[X11]]></category>
		<category><![CDATA[xauth]]></category>
		<category><![CDATA[xhost]]></category>

		<guid isPermaLink="false">http://smorgasbord.gavagai.nl/?p=761</guid>
		<description><![CDATA[Access remote X11 servers that have their TCP socket disabled
This happens to me regularly. Someone brings a machine along and I want to display some app, running on my machine, on their display. Networked X11 to the rescue, you say? No, their X11 server is started with &#8216;-nolisten TCP&#8217; wich is the default on most [...]]]></description>
			<content:encoded><![CDATA[<h3>Access remote X11 servers that have their TCP socket disabled</h3>
<p>This happens to me regularly. Someone brings a machine along and I want to display some app, running on my machine, on their display. Networked X11 to the rescue, you say? No, their X11 server is started with &#8216;-nolisten TCP&#8217; wich is the default on most modern Linux distros. Sadly, the TCP socket can&#8217;t be enabled &#8216;in-flight&#8217; — if you decide you <strong>do</strong> fancy a TCP socket after all, you&#8217;ll have to restart your X server which may be a pain if you&#8217;re in the middle of something (besides, restarting is just plain uncool).<br />
But there is a way to expose the Unix domain socket as a TCP socket, with the help of <a href="http://www.dest-unreach.org/socat/">socat</a>. The following examples all use bash, so if you run a different shell (if you don&#8217;t know, you probably aren&#8217;t) you may need to define environment variables differently.</p>
<h4>Braindead Proof of Concept (BPOC)</h4>
<p>Situation: You want to display an application running on a machine called <code>w00t</code> on another machine, called <code>bling</code>. There&#8217;s an X11 server running on bling, but it&#8217;s not configured to listen on any TCP socket. DNS is properly setup, so if you ping w00t from bling, you get replies from bling&#8217;s IP, and vice versa.</p>
<ol>
<li>On bling, find the domain socket of bling&#8217;s X11 server. Have a look in <code>/tmp/.X11-unix/</code>. The socket&#8217;s name usually reflects its X server display number (which you can determine by running <code>echo $DISPLAY</code> in an xterm).</li>
<li>On bling, run something along the lines of<br />
<code>socat TCP-LISTEN:6066 UNIX-CONNECT:/tmp/.X11-unix/X0</code><br />
This will open up TCP port 6066 on all of bling&#8217;s network interfaces, connecting it to the Unix domain socket of the X server.</li>
<li>In an xterm on bling, run <code>xhost +</code>. You have now opened up your X11 server to the whole wide world, a silly thing to do. Anyone with access to the TCP socket can now read your keystrokes, read your window contents, click your mouse buttons&#8230;</li>
<li>In an xterm on w00t, run <code>DISPLAY="bling:66" xclock</code>. You may have noticed that 66 = 6066 &#8211; 6000 and indeed, by convention the TCP port number for a certain display is its display number + 6000. Anyhow&#8230;. yay, a clock! It&#8217;s displayed on bling, but running on w00t.</li>
</ol>
<h4>Improvements</h4>
<ul>
<li>You may have noticed that in the BPOC, you can use the display on bling only once. <code>socat</code> will allow only one client, and will exit once that client exits. In some situations, you may consider that a feature (it&#8217;s a one-time access grant), but in others you may not. If you want a reusable TCP socket, run something along the lines of<br />
<code>socat TCP-listen:6066,fork,reuseaddr UNIX-CONNECT:/tmp/.X11-unix/X0</code> which forks off a socat process for every TCP connection.
</li>
<li>You may not want to expose a TCP socket on all interfaces. Maybe you only want to expose a socket on the LAN interface, or on the localhost interface (and wrap the packets in an SSH tunnel). Well, you can, using the &#8216;bind&#8217; option:<br />
<code>socat TCP-LISTEN:6066,bind=localhost UNIX-CONNECT:/tmp/.X11-unix/X0</code><br />
Now tunnel it over SSH. On w00t, run <code>ssh -L 6011:localhost:6023 bling</code>. Now localhost:6011 on woot is actually localhost:6023 on bling which is actually /tmp/.X11-unix/X0 on bling. So on w00t you can  start an xclock with its display on bling by running <code>DISPLAY="localhost:11" xclock</code>.
</li>
<li><code>xhost +</code> from the BPOC is braindead indeed. There are a couple things you could have done instead, there are good ways of tightening up your authorization scheme.
<ul>
<li>First off, you don&#8217;t really need to run <code>xhost +</code> if you properly set up X11 cookies, which you should. <a href="http://tldp.org/HOWTO/Remote-X-Apps-6.html#ss6.2">Here are some examples on using the xauth scheme</a>, but take note: <code>xauth generate</code> will probably not work on recent X11 releases since the XSECURITY extension is disabled by default. Just use the same cookies on the client and the server.
</li>
<li>Run <code>xhost +w00t</code>. That&#8217;s host-based authentication, which is stupid, but not as stupid as no authorization at all. Any user on w00t can now connect.
</li>
<li>Suppose that on bling (of course!) you&#8217;d run <code>xhost +SI:localuser:theuser</code> with &#8216;theuser&#8217; being the userID of the unix-user running the socat instance. Now from the point of view of the X server, any client connecting through socat will be coming from &#8216;theuser&#8217; and will therefore be allowed access. Entertaining, but not much different from just running <code>xhost +</code>. It is something to keep in mind though! Many distros by default add the unix-user that started the X server to the authorization list. That user does not need a cookie. If you run socat as that user you will have the effect of running <code>xhost +</code> even if you run <code>xhost -</code>.
</li>
<li>Just run a nested X11 server, such as Xnest or Xephyr. This way you put untrusted users in a sandbox, preventing them from snooping your keyboard and windows. It&#8217;s the X11 equivalent of a chroot.
</li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://smorgasbord.gavagai.nl/2010/01/socat-access-your-x-servers-domain-socket-over-tcp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

