You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
15 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <html><head>
  2. <title>Generic sockets with Socat</title>
  3. <link rel="stylesheet" type="text/css" href="dest-unreach.css">
  4. </head>
  5. <body>
  6. <h1>Generic sockets with Socat</h1>
  7. <h2>Introduction</h2>
  8. <p>Beginning with version 1.7.0 socat provides means to freely control
  9. important aspects of socket handling. This allows to experiment with socket
  10. types and protocols that are not explicitely implemented in socat.
  11. </p>
  12. <p>The related socat features fall into three major categories:<p>
  13. <ul>
  14. <li>address options for changing socket parameters while using common
  15. socket types:
  16. <tt>pf (protocol-family), so-type (socktype), so-prototype (protocol)</tt>
  17. </li>
  18. <li>address options for setting arbitrary socket options:
  19. <tt>setsockopt-int, setsockopt-string, setsockopt-bin</tt></li>
  20. <li>address types for passing almost arbitrary parameters and address data to
  21. the standard system calls:
  22. <tt>socket-connect, socket-listen, socket-sendto, socket-recv,
  23. socket-recvfrom, socket-datagram</tt></li>
  24. </ul>
  25. <p>In practice this gives you two possibilities:</p>
  26. <p>If you want to cope with sockets staying within the usual domains ( =
  27. protocol families = address families) which are IPv4, IPv6, UNIX/local, and
  28. raw interface for socat 1.7.0, it is sufficient to learn about a couple of
  29. <a href="#GENERIC_OPTIONS">address options</a> that allow to change default
  30. parameters, and to apply generic socket options.</p>
  31. <p>For other address families socat provides <a
  32. href="#GENERIC_ADDRESSES">generic socket addresses</a>.
  33. </p>
  34. <a name="GENERIC_OPTIONS"></a>
  35. <h2>Generic socket options</h2>
  36. <h3>Example 1: DCCP communication</h3>
  37. <p>A relatively new communication protocol has been introduced in the Internet
  38. community for which no socat address type has been implemented up to version
  39. 1.7.0
  40. (see <a href="http://www.ietf.org/html.charters/dccp-charter.html">IETF's
  41. Datagram Congestion Control Protocol</a>
  42. and <a href="http://www.linuxfoundation.org/en/Net:DCCP#python_support">Linux
  43. foundation Net:DCCP</a> for more info). Taken that the
  44. operating system implements DCCP, it is possible to use this protocol
  45. with socat while just employing standard socket addresses and some options.
  46. </p>
  47. <p>A simple server that accepts a DCCP connection, passes the arriving data to a
  48. subprocess for converting upper case to lower case characters, and then
  49. returns it to the client:
  50. </p>
  51. <span class="frame"><span class="shell">
  52. socat TCP4-LISTEN:4096,reuseaddr,type=6,prototype=33 exec:'tr A-Z a-z',pty,raw,echo=0
  53. </span></span>
  54. <p>A simple client that sends some upper case characters to the server via DCCP
  55. and prints what the server returns:
  56. </p>
  57. <span class="frame"><span class="shell">
  58. echo ABCD |socat - TCP4-CONNECT:localhost:4096,type=6,prototype=33
  59. </span></span>
  60. <p>We choose the TCP4 addresses as base because it best matches the DCCP
  61. requirements:
  62. <ol>
  63. <li>DCCP is (here) based on IPv4</li>
  64. <li>DCCP is stream oriented and uses <tt>connect()</tt> and <tt>listen();
  65. accept()</tt> calls</li>
  66. <li>DCCP protocol uses ports</li>
  67. </ol>
  68. </p>
  69. <p>Option <tt><a href="socat.html#OPTION_SO_TYPE">type</a>=6</tt> changes TCP's
  70. <tt>SOCK_STREAM</tt> parameter to <tt>SOCK_DCCP</tt>, and <tt>
  71. <a href="socat.html#OPTION_SO_PROTOTYPE">prototype</a>=33</tt> replaces the
  72. default <tt>IPPROTO_TCP</tt> with <tt>IPPROTO_DCCP</tt>.
  73. </p>
  74. <p>DCCP has an important parameter, the service code. It provides another
  75. multiplexing layer beyond the protocol ports. The Linux implementation of DCCP
  76. allows to set this parameter with code like <tt>setsocktopt(fd, SOL_DCCP,
  77. DCCP_SOCKOPT_SERVICE, {1}, sizeof(int))</tt>. The equivalent generic socat
  78. option is: <tt><a href="socat.html#OPTION_SETSOCKOPT_INT">setsockopt-int</a>=269:2:1</tt> for service code 1.
  79. If the service codes on server and client do not match the <tt>connect()</tt>
  80. operation fails with error:<p>
  81. <table border="1" bgcolor="e08080"><tr><td><tt>... E connect(3, AF=2 127.0.0.1:4096, 16): Invalid request code</tt></td></tr></table>
  82. <p>Please note that this examples works with IPv6 as well, you just need to
  83. replace the TCP4 words with TCP6, and the IPv4 socket address with an
  84. appropriate IPv6 socket address, e.g. <tt>[::1]</tt>!
  85. </p>
  86. <a name="GENERIC_ADDRESSES"></a>
  87. <h2>Generic socket addresses</h2>
  88. <p>socat's generic socket addresses are a more comprehensive mechanism that
  89. allows to deal with protocol families whose socket addresses are not supported
  90. by socat - no semantical parsing, no structured assignment to the struct
  91. components are available. Instead, the socket address records for binding and
  92. connecting/sending are specified in unstructured hexadecimal form. The
  93. following example demonstrates this by performing simple data transfer over
  94. raw AppleTalk protocol.
  95. </p>
  96. <p>Note: I do not have any knowledge about AppleTalk. I just managed to
  97. configure my Linux host to tolerate the creation of a receiving and a sending
  98. socket. Don't blame me nor ask me for support if it does not work for you.
  99. </p>
  100. <a name="EXAMPLE_APPLETALK"></a>
  101. <h3>Enabling AppleTalk protocol</h3>
  102. <p>Install the <tt>netatalk</tt> package. Check that <tt>/etc/netatalk/atalkd.conf</tt>
  103. has an entry like <tt>eth0 -phase 2 -net 0-65534 -addr 65280.243</tt>. The
  104. last part is an arbitrary (?) host address, some of the following values must
  105. fit it. Make sure the <tt>atalkd</tt> daemon is running. Run the AppleTalk
  106. ping command:
  107. </p>
  108. <span class="frame"><span class="shell">
  109. aecho 65280.243
  110. </span></span>
  111. <p>If you get an error like:
  112. </p>
  113. <table border="1" bgcolor="#e08080"><tr><td><tt>Device or resource busy</tt></td></tr></table>
  114. <p>then try to restart <tt>atalkd</tt>:</p>
  115. <span class="frame"><span class="shell">
  116. /etc/init.d/atalkd restart
  117. </span></span>
  118. <p>When <tt>aecho</tt> works like <tt>ping</tt> you are ready for the next step.
  119. </p>
  120. <h3>Example 2: AppleTalk datagram communication</h3>
  121. <p>We start a socat process with a receiver and echo service:
  122. </p>
  123. <span class="frame"><span class="shell">
  124. socat SOCKET-RECVFROM:5:2:0:x40x00x0000x00x00x0000000000000000 PIPE
  125. </span></span>
  126. <p>Then, in another shell on the same host, we start a client socket process
  127. that sends data to the server and gets the answer:
  128. </p>
  129. <span class="frame"><span class="shell">
  130. echo ABCD |socat - SOCKET-DATAGRAM:5:2:0:x40x00xff00xf3x00x0000000000000000
  131. </span></span>
  132. <p>The client process should print the data.
  133. </p>
  134. <p>How did this work? The generic socat address has just used the system call
  135. parameters that were provided on command line, without knowing anything about
  136. AppleTalk sockets and protocol. The values 5, 2, and 0 are directly used for
  137. the <tt>socket()</tt> call: they specify the domain (<tt>PF_APPLETALK=5</tt>),
  138. socket type (<tt>SOCK_DGRAM=2</tt>), and no protocol (0) - values for Linux.
  139. The long hex strings define the socket addresses. They can only be constructed
  140. with knowledge of the underlying structure. In
  141. <tt>/usr/include/linux/atalk.h</tt> we find the following declarations:
  142. </p>
  143. <pre>
  144. struct atalk_addr {
  145. __be16 s_net;
  146. __u8 s_node;
  147. };
  148. struct sockaddr_at {
  149. sa_family_t sat_family;
  150. __u8 sat_port;
  151. struct atalk_addr sat_addr;
  152. char sat_zero[8];
  153. </pre>
  154. <p>After rolling out <tt>atalk_addr</tt> and considering implicit padding by the
  155. C programming language we get the following byte map:
  156. </p>
  157. <table border="1">
  158. <tr><th>component</th><th>offset</th><th>length</th><th>value</th><th>meaning</th></tr>
  159. <tr><td>sat_family</td><td>0</td><td>2</td><td>x0005</td><td>address family</td></tr>
  160. <tr><td>sat_port</td><td>2</td><td>1</td><td>x40</td><td>port</td></tr>
  161. <tr><td>-</td><td>3</td><td>1</td><td>x00</td><td>padding</td></tr>
  162. <tr><td>sat_addr.s_net</td><td>4</td><td>2</td><td>xff00</td><td>network address</td></tr>
  163. <tr><td>sat_addr.s_node</td><td>6</td><td>1</td><td>xf3</td><td>node address</td></tr>
  164. <tr><td>-</td><td>7</td><td>1</td><td>x00</td><td>padding</td></tr>
  165. <tr><td>sat_zero</td><td>8</td><td>8</td><td>x0000000000000000</td><td>padding</td></tr>
  166. </table>
  167. <p>Note that hexadecimal ff00 is the same as decimal 65280, and hexadecimal xf3
  168. is the same as decimal 243 - these are the numbers specified in
  169. <tt>atalkd.conf</tt>.
  170. </p>
  171. <p>The address family component must be omitted from the socket address because
  172. it is added by socat implicitely. The resulting hexadecimal representation of
  173. the target socket address is therefore:
  174. </p>
  175. <tt>x40x00xff00xf3x00x0000000000000000</tt>
  176. <p>The receiver just has to specify the port, so its bind address data is:
  177. </p>
  178. <tt>x40x00x0000x00x00x0000000000000000</tt>
  179. <h2>Parameters for well known socket types</h2>
  180. <p>Finding the correct parameters and socket addresses is not always trivial.
  181. Therefore this section provides tables with the parameters of common socket
  182. types. Some of these types are directly implemented by socat (and other
  183. programs). Establishing interoperability between a directly implemented
  184. socket and a generic socket might be your first step before entering unknown
  185. ground.</p>
  186. <h3>Socket parameters</h3>
  187. <h4>Table: parameter names for "well known" sockets:</h4>
  188. <table border=1>
  189. <tr><th>name</th><th>domain</th><th>socktype</th><th>protocol</th><th> </th><th>level</th><th>remark</th></tr>
  190. <tr><td>UDP4</td><td>PF_INET</td><td>SOCK_DGRAM</td><td>IPPROTO_UDP</td><td> </td><td>SOL_UDP</td><td></td></tr>
  191. <tr><td>UDP6</td><td>PF_INET6</td><td>SOCK_DGRAM</td><td>IPPROTO_UDP</td><td> </td><td>SOL_UDP</td><td></td></tr>
  192. <tr><td>raw IPv4</td><td>PF_INET</td><td>SOCK_RAW</td><td>IPPROTO_RAW</td><td> </td><td>SOL_IP</td><td></td></tr>
  193. <tr><td>raw IPv6</td><td>PF_INET6</td><td>SOCK_RAW</td><td>IPPROTO_RAW</td><td> </td><td>SOL_IPV6</td><td></td></tr>
  194. <tr><td>UNIX</td><td>PF_LOCAL</td><td>SOCK_DGRAM</td><td>0</td><td> </td><td>SOL_SOCKET</td><td> </td></tr>
  195. <tr><td>PACKET</td><td>PF_PACKET</td><td>SOCK_RAW</td><td>768</td><td></td><td>SOL_PACKET</td><td>tcpdump (include layer 2 header)</td></tr>
  196. <tr><td>PACKET</td><td>PF_PACKET</td><td>SOCK_DGRAM</td><td>768</td><td></td><td>SOL_PACKET</td><td>no level 2 header</td></tr>
  197. <tr><td>SCTP4</td><td>PF_INET</td><td>SOCK_SEQPACKET</td><td>IPPROTO_SCTP</td><td> </td><td>SOL_SCTP</td><td> </td></tr>
  198. </table>
  199. <h4>Table: parameter values:</h4>
  200. <table border=1>
  201. <tr><th>name</th><th>Linux</th><th>FreeBSD</th><th>NetBSD</th><th>OpenBSD</th><th>Solaris</th><th>AIX</th><th>Cygwin</th><th>Mac OS X</th><th>HP-UX</th></tr>
  202. <tr><td>PF_LOCAL</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td></tr>
  203. <tr><td>PF_INET</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td></tr>
  204. <tr><td>PF_APPLETALK</td><td>5</td><td>16</td><td>16</td><td>16</td><td>16</td><td>16</td><td>16</td><td>16</td><td>16</td></tr>
  205. <tr><td>PF_INET6</td><td>10</td><td>28</td><td>24</td><td>24</td><td>26</td><td>24</td><td>-</td><td>30</td><td>22</td></tr>
  206. <tr><td>PF_PACKET</td><td>17</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>
  207. <tr><td>SOCK_STREAM</td><td>1</td><td>1</td><td>1</td><td>1</td><td>2</td><td>1</td><td>1</td><td>1</td><td>1</td></tr>
  208. <tr><td>SOCK_DGRAM</td><td>2</td><td>2</td><td>2</td><td>2</td><td>1</td><td>2</td><td>2</td><td>2</td><td>2</td></tr>
  209. <tr><td>SOCK_RAW</td><td>3</td><td>3</td><td>3</td><td>3</td><td>4</td><td>3</td><td>3</td><td>3</td><td>3</td></tr>
  210. <tr><td>SOCK_SEQPACKET</td><td>5</td><td>5</td><td>5</td><td>5</td><td>6</td><td>5</td><td>5</td><td>5</td><td>5</td></tr>
  211. <tr><td>SOCK_DCCP</td><td>(6)</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>
  212. <tr><td>SOCK_PACKET</td><td>10</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>
  213. <tr><td>IPPROTO_IP</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
  214. <tr><td>IPPROTO_TCP</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td></tr>
  215. <tr><td>IPPROTO_UDP</td><td>17</td><td>17</td><td>17</td><td>17</td><td>17</td><td>17</td><td>17</td><td>17</td><td>17</td></tr>
  216. <tr><td>IPPROTO_DCCP</td><td>33</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>
  217. <tr><td>IPPROTO_SCTP</td><td>132</td><td>132</td><td>-</td><td>-</td><td>132</td><td>132</td><td>-</td><td>-</td><td>-</td></tr>
  218. <tr><td>IPPROTO_RAW</td><td>255</td><td>255</td><td>255</td><td>255</td><td>255</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>
  219. <tr><td>SOL_SOCKET</td><td>1</td><td>65535</td><td>65535</td><td>65535</td><td>65535</td><td>65535</td><td>65535</td><td>65535</td><td>65535</td></tr>
  220. <tr><td>SOL_IP</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
  221. <tr><td>SOL_TCP</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td></tr>
  222. <tr><td>SOL_UDP</td><td>17</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>17</td><td>-</td><td>-</td></tr>
  223. <tr><td>SOL_IPV6</td><td>41</td><td>41</td><td>41</td><td>41</td><td>41</td><td>41</td><td>-</td><td>41</td><td>41</td></tr>
  224. <tr><td>SOL_PACKET</td><td>263</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>
  225. <tr><td>SOL_DCCP</td><td>269</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>
  226. </table>
  227. <h3>Socket address specifications</h3>
  228. <p>These hexadecimal data define socket addresses for local and remote sockets,
  229. and for bind and range options. The basis is the <tt>struct sockaddr_*</tt> for
  230. the respective address family that should be declared in the C include files.
  231. Please keep in mind that their first two bytes (<tt>sa_family</tt> and - on BSD
  232. - <tt>sa_len</tt>) are implicitely prepended by socat.</p>
  233. <h4>Linux on 32bit Intel:</h4>
  234. <table border=1>
  235. <tr><th>name</th><th>socket address type (without leading address family)</th><th>binary specification</th></tr>
  236. <tr><td>IPv4</td><td>2 bytes port, 4 bytes IPv4 addr, 8 bytes 0</td><td>x0016
  237. x7f000001 x0000000000000000</td></tr>
  238. <tr><td>IPv6</td><td>2 bytes port, 4 bytes flowinfo, 16 bytes IPv6 addr, 4 bytes scope-id</td><td>x0016 x00000000 x0102030405060708090a0b0c0d0e0f x00000000</td></tr>
  239. <tr><td>UNIX</td><td>variable length path name, 0 terminated</td><td>x2f746d702f736f636b00</td></tr>
  240. <tr><td>PACKET</td><td>2 bytes protocol (0x0003), interface index as int in host byte order, 8 bytes 0</td><td>x0003 x02000000 x0000000000000000</td></tr>
  241. </table>
  242. <p>For AppleTalk see above <a href="#EXAMPLE_APPLETALK">example</a>.</p>
  243. <h4>Solaris on 32bit Intel:</h4>
  244. <table border=1>
  245. <tr><th>name</th><th>socket address type (without leading address family)</th><th>binary specification</th></tr>
  246. <tr><td>IPv6</td><td>2 bytes port, 4 bytes flowinfo, 16 bytes IPv6 addr, 4
  247. bytes scope-id, 4 bytes src-id</td><td>x0016 x00000000 x0102030405060708090a0b0c0d0e0f x00000000 x00000000</td></tr>
  248. </table>
  249. <h3>Forever - play on...</h3>
  250. <p>Eager to experiment with exotic socket types? Run nmap's protocol scan and
  251. see what is available on your system:
  252. </p>
  253. <span class="frame"><span class="shell">
  254. nmap -sO localhost
  255. </span></span>
  256. <p>
  257. <small>Copyright: Gerhard Rieger 2008</small><br>
  258. <small>License: <a href="http://www.fsf.org/licensing/licenses/fdl.html">GNU Free Documentation License (FDL)</a></small>
  259. </p>
  260. </body>
  261. </html>