title: Loopback in multicast
body: <p>This question is about sending and receiving multicasts inside the same host, while simultaneously sending it to other hosts.</p>
  
  <p>Even after hours of googling, I have not been able to sort out, how multicast datagrams are routed within the same host..!</p>
  
  <p>Here comes a detailed description of the problem:</p>
  
  <p>Linux box "A" is cable-connected to a switch/router (let's call the switch/router as "R").</p>
  
  <p>In linux box A, I have a process (A0) that sends UDP packets to multicast address "224.0.0.0", port 5000.</p>
  
  <p>Likewise, in the same box A, I have two processes (A1 and A2), both connecting to 224.0.0.0, port 5000 and consuming UDP packets.</p>
  
  <p>Now, how does the kernel in box A manage the routing?</p>
  
  <p>So, A0 sends a datagram which is consumed by A1 and A2.  Does such a datagram do a round-trip A --> R --> A ?</p>
  
  <p>.. or is the kernel routing in "A" smart enough to avoid this unnecessary round-trip? (i.e. datagram is sent by A0 and consumed right away by A1 and A2, never leaving A).</p>
  
  <p>Of course, one can ensure that multicast datagram never leave A, by creating and enforcing a loopback device for multicast:</p>
  
  <pre><code>sudo ifconfig lo multicast
  sudo route add -net 224.0.0.0 netmask 240.0.0.0 dev lo
  </code></pre>
  
  <p>But now, if I want to consume the UDP datagram simultaneously in another linux box, say, "B", there is no way they arrive there.</p>
  
  <p>So, ideally, (1) datagram consumed in A should never leave A (i.e. no round trip A --> R --> A), while (2) datagram consumed in B should go normally as A --> R --> B.  I'd like to achieve these two things simultaneously.</p>
  
  <p>Any way to achieve this, for example, with the "route" command ?</p>
  
  <p>Sockets is processes A1 and A2 have the following flags set:</p>
  
  <pre><code>SO_REUSEADDR
  SO_REUSEPORT
  </code></pre>
  
  <p>While I have checked that process A0 (the one that sends multicast datagram) has the following flag on:</p>
  
  <pre><code>IP_MULTICAST_LOOP
  </code></pre>