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.

392 lines
15 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!-- source: doc/socat-multicast.html -->
  2. <html><head>
  3. <title>IP Multicasting with Socat</title>
  4. <link rel="stylesheet" type="text/css" href="dest-unreach.css">
  5. </head>
  6. <body>
  7. <h1>IP Multicasting with Socat</h1>
  8. <h2>Introduction</h2>
  9. <p>
  10. Multicasting (and broadcasting which is also discussed in this article)
  11. provides a means to direct a single packet to more than one host. Special
  12. addresses are defined for this purpose and are handled specially by network
  13. adapters, networking hardware, and IP stacks.
  14. </p>
  15. <p>
  16. IPv4 specifications provide broadcasting and multicasting; IPv6 provides
  17. multicasting but replaces broadcasting by special multicast modes. UNIX domain
  18. sockets do not know broadcasting or multicasting.
  19. </p>
  20. <p>
  21. The following examples use UDP/IPv4 only. However, they can easily be
  22. adapted for raw IPv4 sockets. IPv6 multicasting has not yet been successfully
  23. used with socat; please contact the author if you have positive experiences or
  24. ideas that go beyond <tt>IPV6_ADD_MEMBERSHIP</tt>.
  25. </p>
  26. <p>
  27. All multicast examples presented in this document use multicast address
  28. 224.1.0.1; it can be replaced by any valid IPv4 multicast address (except
  29. <a href="#ALLSYSTEMS">all-systems</a>).
  30. </p>
  31. <p>
  32. We assume a local network with address 192.168.10.0 and mask 255.255.255.0; an
  33. eventual "client" has 192.168.10.1, example "server" and example peer have
  34. 192.168.10.2 in all examples. Change these addresses and mask to your own
  35. requirements.
  36. </p>
  37. <p>
  38. All the following examples work bidirectionally except when otherwise noticed.
  39. For "clients" we just use <tt>STDIO</tt>, and for "servers" we use <tt>EXEC:hostname</tt> which
  40. ingores its input but shows us which host the reply comes from. Replace these
  41. socat addresses with what is appropriate for your needs (e.g. shell script
  42. invocations). Port 6666 can be replaced with any other port (but for ports &lt;
  43. 1024 root privilege might be required).
  44. </p>
  45. <p>
  46. Different kinds of broadcast addresses exist: 255.255.255.255 is local network
  47. only; for the IPv4 network 192.168.10.0/24 the "official" broadcast address
  48. is 192.168.10.255; the network address 192.168.10.0 is also interpreted as
  49. broadcast by some hosts. The two latter forms are routed by gateways. In the
  50. following examples we only use broadcast address 192.168.10.255.
  51. </p>
  52. <h2>Example 1: Multicast client and servers</h2>
  53. <p>This example builds something like a "supervisor" or "client" that
  54. communicates with a set of "servers". The supervisor may send packets to the
  55. multicast address, and the servers may send response packets. Note that the
  56. servers would also respond to other clients' requests.</p>
  57. <p>Multicast server:</p>
  58. <span class="frame"><span class="shell">
  59. socat UDP4-RECVFROM:6666,ip-add-membership=224.1.0.1:192.168.10.2,fork EXEC:hostname
  60. </span></span>
  61. <p>
  62. This command receives multicast packets addressed to 224.1.0.1 and forks a
  63. child process for each. The child processes may each send one or more reply
  64. packets back to the particular sender. 192.168.10.2 means the address of the
  65. interface where multicasts should be received.
  66. Run this command on a number of hosts, and they will all respond in
  67. parallel.</p>
  68. <p>Multicast client:</p>
  69. <span class="frame"><span class="shell">
  70. socat STDIO UDP4-DATAGRAM:224.1.0.1:6666,range=192.168.10.0/24
  71. </span></span>
  72. <p>
  73. This process transfers data from stdin to the multicast address, and transfers
  74. packets received from the local network to stdout. It does not matter in which
  75. direction the first data is passed.
  76. A packet from the network is accepted by the IP stack for our socket if:
  77. <ul>
  78. <li>it is an incoming UDP/IPv4 packet</li>
  79. <li>its target port matches the local port assigned to the socket (random)</li>
  80. <li>its target address matches one of the hosts local addresses or the any-host
  81. multicast address</li>
  82. </ul>
  83. Of these packets, socat handles only those matching the following criteria:
  84. <ul>
  85. <li>the source address is within the given range</li>
  86. <li>the source port is 6666</li>
  87. </ul>
  88. </p>
  89. <h2>Example 2: Broadcast client and servers</h2>
  90. <p>Broadcast server:</p>
  91. <span class="frame"><span class="shell">
  92. socat UDP4-RECVFROM:6666,broadcast,fork EXEC:hostname
  93. </span></span>
  94. <p>
  95. This command receives packets addressed to a local broadcast address and forks
  96. a child process for each. The child processes may each send one or more reply
  97. packets back to the particular sender.
  98. Run this command on a number of hosts, and they will all respond in
  99. parallel.</p>
  100. <p>Broadcast client:</p>
  101. <span class="frame"><span class="shell">
  102. socat STDIO UDP4-DATAGRAM:192.168.10.255:6666,broadcast,range=192.168.10.0/24
  103. </span></span>
  104. <p>
  105. This process transfers data from stdin to the broadcast address, and transfers
  106. packets received from the local network to stdout. It does not matter in which
  107. direction the first data is passed.
  108. A packet from the network is accepted by the IP stack for our socket if:
  109. <ul>
  110. <li>it is an incoming UDP/IPv4 packet</li>
  111. <li>its target port matches the local port assigned to the socket (6666)</li>
  112. <li>its target address matches one of the hosts local addresses or the any-host
  113. multicast address, or a local broadcast address</li>
  114. </ul>
  115. Of these packets, socat handles only those matching the following criteria:
  116. <ul>
  117. <li>the source address is within the given range</li>
  118. <li>the source port is 6666</li>
  119. </ul>
  120. </p>
  121. <p>The <tt>broadcast</tt> option is only required for sending or receiving
  122. local broadcasts.</p>
  123. <h2>Example 3: Multicast peers</h2>
  124. <p>It is possible to combine multicast sender and receiver in one socat
  125. address. This allows to start processes on different hosts on the local network
  126. that will communicate symmetrically, so each process can send messages that are
  127. received by all the other ones.</p>
  128. <span class="frame"><span class="shell">
  129. socat STDIO UDP4-DATAGRAM:224.1.0.1:6666,bind=:6666,range=192.168.10.0/24,ip-add-membership=224.1.0.1:192.168.10.2
  130. </span></span>
  131. <p>
  132. This command is valid for host 192.168.10.2; adapt this address to the
  133. particular interface addresses of the hosts.
  134. </p>
  135. <p>
  136. Starting this process opens a socket on port 6666 that will receive packets
  137. directed to multicast address 224.1.0.1. Only packets with matching source
  138. address and source port 6666 will be handled though. When this process sends
  139. data to the network the packets will be addressed to 224.1.0.1:6666 and have a
  140. source address of 192.168.10.2:6666, matching the accept criteria of the peers
  141. on the local network.
  142. </p>
  143. <p>Note: this command receives the packets it just has sent; add option
  144. <tt>ip-multicast-loop=0</tt> if this in undesired.</p>
  145. <h2>Example 4: Broadcast peers</h2>
  146. <p>Just as with multicast, it is possible to combine broadcast sender and
  147. receiver in one socat address.</p>
  148. <span class="frame"><span class="shell">
  149. socat STDIO UDP4-DATAGRAM:255.255.255.255:6666,bind=:6666,range=192.168.10.0/24,broadcast
  150. </span></span>
  151. <p>
  152. Starting this process opens a socket on port 6666 that will receive packets
  153. directed to a local broadcast addresses. Only packets with matching source
  154. address and source port 6666 will be handled though. When this process sends
  155. data to the network the packets will be addressed to 255.255.255.255:6666 and
  156. have a source address of 192.168.10.2:6666, matching the accept criteria of
  157. the peers on the local network.
  158. </p>
  159. <p>Note: this command receives the packets it just has sent; there does not
  160. seem to exist a simple way to prevent this.</p>
  161. <h2>Troubleshooting</h2>
  162. <p>
  163. If you do not get an error message during operation, but the packets do not
  164. reach the target processes, use <tt>tcpdump</tt> to see if the packets have the
  165. correct source and destination addresses and ports, and if they leave and enter
  166. the hosts as expected.
  167. </p>
  168. <p>
  169. The following subsections discuss some typical sources of trouble.
  170. </p>
  171. <h3>IP filters</h3>
  172. <p>
  173. If you do not succeed in receiving multicast or broadcast packets, check if
  174. iptables are activated on the receiving or sending host. They might be
  175. configured to disallow this traffic.
  176. </p>
  177. <h3>Do not bind()</h3>
  178. <p>
  179. When using multicast communications, you should not bind the sockets to a
  180. specific IP address. It seems that the (Linux) IP stack compares the
  181. destination address with the bind address, not taking care of the multicast
  182. property of the incoming packet.
  183. </p>
  184. <h3>Routing</h3>
  185. <p>
  186. When you receive an error like:</p>
  187. <table border="1" bgcolor="#e08080"><tr><td><tt>... E sendto(3, 0x80c2e44, 4,
  188. 0, AF=2 224.1.0.1:6666, 16): Network is unreachable</tt></td></tr></table>
  189. <p>you have a routing problem. The (Linux) IP stack seems to handle multicast
  190. addresses just like unicast addresses when determining their route (interface
  191. and gateway), i.e. the routing table needs an entry that somehow matches the
  192. target address. </p>
  193. <p>
  194. For the same reason, multicast packets will probably leave your host on the
  195. interface with the default route if it is specified.</p>
  196. <p>
  197. Set a multicast/broadcast route with the following command (Linux):</p>
  198. <span class="frame"><span class="shell">
  199. route add -net 224.0.0.0/3 gw 192.168.10.2
  200. </span></span>
  201. <a name="ALLSYSTEMS"></a>
  202. <h3>ALL-SYSTEMS multicast address</h3>
  203. <p>
  204. <tt>224.0.0.1</tt> is the all-systems multicast address: all
  205. datagram sockets appear to be automatically member of this group on all
  206. interfaces. This membership cannot be dropped on Linux (you need iptables to
  207. filter packets).
  208. </p>
  209. <h2>(In)Security</h2>
  210. <p>When you use the above examples you should understand that all datagram
  211. sockets without exception accept all packets that are directly addressed to
  212. them;
  213. the multi- and broadcast receiving features are just extensions to this
  214. functionality. socat currently has no means to handle incoming packets
  215. differently whether they are addressed to unicast, multicast, or broadcast
  216. addresses. However, for EXEC'd scripts socat can provide this info in
  217. environment variables.
  218. </p>
  219. <p>Authentication or encryption are not available.</p>
  220. <p>It is very easy to fake the source address of UDP (or raw IP) packets. You
  221. should understand whether your network is protected from address spoofing
  222. attacks.</p>
  223. <p>Broadcast and multicast traffic can trivially be received by <em>any</em>
  224. host on the local network.</p>
  225. <h2>History</h2>
  226. Starting with version 1.5.0, socat provides a set of address types that
  227. allow various operations on datagram oriented sockets:
  228. <dl>
  229. <dt>SENDTO</dt><dd>send packets to a remote socket and receive packet from this
  230. remote socket only</dd>
  231. <dt>RECV</dt><dd>receive all packets that arrive on the local socket, but do
  232. not reply</dd>
  233. <dt>RECVFROM</dt><dd>receive all packets that arrive on the local socket, and
  234. reply using child processes</dd>
  235. </dl>
  236. <p>
  237. These modes already enable several different client/server oriented operations.
  238. Moreover, the SENDTO addresses can send to multicast and broadcast addresses
  239. (the latter requires the <tt>broadcast</tt> option though). RECV and RECVFROM
  240. also would accept packets addressed to a local broadcast address (with option
  241. <tt>broadcast</tt>) or the all-systems multicast address.
  242. </p>
  243. <p>
  244. These address types had, however, two major caveats:
  245. <ul>
  246. <li>Missing control of multicast group membership in the RECV and RECVFROM
  247. addresses</li>
  248. <li>The SENDTO address would never accept a reply to a broadcast or multicast
  249. addressed packet because the source address of incoming replies would not match
  250. the target address of the sent packet.
  251. </ul>
  252. </p>
  253. <h3>New Features in socat 1.6.0</h3>
  254. <p>
  255. socat version 1.6.0 addresses these problems and provides a new more generic
  256. datagram address type (*-DATAGRAM) and the new address option IP-ADD-MEMBERSHIP.
  257. </p>
  258. <p>
  259. Please note that the new features could not be successfully tested on IPv6;
  260. these sections thus apply to IPv4 only.
  261. </p>
  262. <h3>New Features in socat 1.7.0</h3>
  263. <p>
  264. socat version 1.7.0 helps to find more information about incoming packets in
  265. environment variables that can be used in scripts or programs invoked by
  266. socat. The option <tt>ip-pktinfo</tt> (on non-BSD systems)
  267. or <tt>ip-recvdstaddr</tt> (on BSD systems) is required to get basic
  268. information about incoming packets.
  269. </p>
  270. <p>
  271. Example: Start a receiver of the following form (tried on Linux):
  272. </p>
  273. <span class="frame"><span class="shell">
  274. socat -u UDP-RECVFROM:8888,reuseaddr,ip-add-membership=224.1.0.1:192.168.10.2,ip-pktinfo,fork SYSTEM:export
  275. </span></span>
  276. <p>
  277. Then send a multicast packet from the client:
  278. </p>
  279. <span class="frame"><span class="shell">
  280. echo |socat -u STDIO UDP-DATAGRAM:224.1.0.1:8888
  281. </span></span>
  282. <p>
  283. On the server the following text should appear (only interesting lines shown):
  284. </p>
  285. <pre>
  286. export SOCAT_IP_DSTADDR="224.1.0.1"
  287. export SOCAT_IP_IF="eth0"
  288. export SOCAT_IP_LOCADDR="192.168.10.2"
  289. export SOCAT_PEERADDR="192.168.10.1"
  290. export SOCAT_PEERPORT="41159"
  291. </pre>
  292. <p>
  293. <tt>SOCAT_IP_IF</tt> shows the interface where the packet entered the server;
  294. <tt>SOCAT_IP_LOCADDR</tt> shows the IP address of this interface;
  295. <tt>SOCAT_IP_DSTADDR</tt> shows the target address of the packet;
  296. <tt>SOCAT_PEERADDR</tt> and <tt>SOCAT_PEERPORT</tt> are the client socket
  297. values.
  298. </p>
  299. <h2>More info about socat datagrams</h2>
  300. <h3>Links regarding this tutorial</h3>
  301. <a href="socat.html#ADDRESS_UDP4_DATAGRAM">address UDP4-DATAGRAM</a><br>
  302. <a href="socat.html#ADDRESS_UDP4_RECVFROM">address UDP4-RECVFROM</a><br>
  303. <a href="socat.html#OPTION_RANGE">option range</a><br>
  304. <a href="socat.html#OPTION_SO_BROADCAST">option broadcast</a><br>
  305. <a href="socat.html#OPTION_IP_ADD_MEMBERSHIP">option ip-add-membership</a><br>
  306. <a href="socat.html#OPTION_FORK">option fork</a><br>
  307. <a href="socat.html#OPTION_BIND">option bind</a><br>
  308. <h3>Other datagram addresses</h3>
  309. <a href="socat.html#ADDRESS_UDP4_RECV">address UDP4-RECV</a>: pure datagram receiver<br>
  310. <a href="socat.html#ADDRESS_UDP4_SENDTO">address UDP4-SENDTO</a>: communicate
  311. with one peer address<br>
  312. <a href="socat.html#ADDRESS_UDP4_LISTEN">address UDP4-LISTEN</a>: pseudo stream server<br>
  313. <a href="socat.html#ADDRESS_UDP4_CONNECT">address UDP4-CONNECT</a>: pseudo stream client<br>
  314. <h3>Related socat option groups</h3>
  315. <a href="socat.html#GROUP_IP">IP options</a><br>
  316. <a href="socat.html#GROUP_SOCKET">socket options</a><br>
  317. <a href="socat.html#GROUP_FD">file descriptor options</a><br>
  318. <a href="socat.html#GROUP_RANGE">range options</a><br>
  319. <a href="socat.html#GROUP_CHILD">child process options</a><br>
  320. <h2>References</h2>
  321. <a href="http://www.dest-unreach.org/socat">socat home page</a><br>
  322. <a href="socat.html">socat man page</a><br>
  323. <a href="http://en.wikipedia.org/wiki/Multicast">multicasting on Wikipedia</a><br>
  324. <a href="http://en.wikipedia.org/wiki/Broadcast_address">broadcasting on Wikipedia</a><br>
  325. <p>
  326. <small>This document was last modified in May 2009.</small><br>
  327. <small>Copyright: Gerhard Rieger 2007-2009</small><br>
  328. <small>License: <a href="http://www.fsf.org/licensing/licenses/fdl.html">GNU Free Documentation License (FDL)</a></small>
  329. </p>
  330. </body>
  331. </html>