Friday, July 10, 2009

Multicast Application with Multihomed Support (C# .NET Sample)

In the previous post we’ve reviewed the multicast addressing technology and examined some of its pitfalls.

In this post we’ll dive through the implementation of a simple application that allows sending and receiving multicast data over the LAN, while enabling the selection of the network interfaces (IPEndPoint) through which the multicast traffic will be sent and received.

As illustrated in the previous post, setting the network interface is crucial when the host is connected to two or more networks or have two or more network cards installed and enabled.

The application source code can be downloaded from here.



The MulticastConfiguration class encapsulates the configuration entered by the user. It exposes two factory methods that allow instantiating the class for single network hosts and for multi network hosts. The 1st factory method requires only the multicast address/port of the sender and  the receiver, while the 2nd factory method also requires the unicast address of the network card of the sender and  the receiver.

Code Snippet
  1.     class MulticastConfiguration
  2.     {
  3.         readonly IPAddress m_SendMulticastAddress;
  4.         readonly int m_SendPort;
  5.         readonly int m_ReceivePort;
  6.         readonly IPAddress m_ReceiveMulticastAddress;
  7.         // Multihomed Support
  8.         readonly IPAddress m_SendNetworkCardAddress = IPAddress.Any;
  9.         readonly int m_SendNetworkCardPort;
  10.         readonly IPAddress m_ReceiveNetworkCardAddress = IPAddress.Any;
  11.         public MulticastConfiguration(
  12.             IPAddress sendMulticastAddress,
  13.             int sendPort,
  14.             IPAddress receiveMulticastAddress,
  15.             int receivePort)
  16.         {
  17.             m_SendMulticastAddress = sendMulticastAddress;
  18.             m_SendPort = sendPort;
  19.             m_ReceiveMulticastAddress = receiveMulticastAddress;
  20.             m_ReceivePort = receivePort;
  21.         }
  22.         public MulticastConfiguration(
  23.             IPAddress sendMulticastAddress,
  24.             int sendPort,
  25.             IPAddress receiveMulticastAddress,
  26.             int receivePort,
  27.             IPAddress sendExplicitSourceAddressm,
  28.             int sendExplicitSourcePort,
  29.             IPAddress receiveExplicitSourceAddress)
  30.             : this(
  31.             sendMulticastAddress,
  32.             sendPort,
  33.             receiveMulticastAddress,
  34.             receivePort)
  35.         {
  36.             m_SendNetworkCardAddress = sendExplicitSourceAddressm;
  37.             m_SendNetworkCardPort = sendExplicitSourcePort;
  38.             m_ReceiveNetworkCardAddress = receiveExplicitSourceAddress;
  39.         }
  40.         public static MulticastConfiguration CreateForSingleInterfaceNetwork(
  41.             IPAddress sendMulticastAddress,
  42.             int sendPort,
  43.             IPAddress receiveMulticastAddress,
  44.             int receivePort)
  45.         {
  46.             return new MulticastConfiguration(
  47.                 sendMulticastAddress,
  48.                 sendPort,
  49.                 receiveMulticastAddress,
  50.                 receivePort);
  51.         }
  52.         public static MulticastConfiguration CreateForMultiHomedNetwork(
  53.             IPAddress sendMulticastAddress,
  54.             int sendPort,
  55.             IPAddress receiveMulticastAddress,
  56.             int receivePort,
  57.             IPAddress sendExplicitSourceAddressm,
  58.             int sendExplicitSourcePort,
  59.             IPAddress receiveExplicitSourceAddress)
  60.         {
  61.             return new MulticastConfiguration(
  62.                 sendMulticastAddress,
  63.                 sendPort,
  64.                 receiveMulticastAddress,
  65.                 receivePort,
  66.                 sendExplicitSourceAddressm,
  67.                 sendExplicitSourcePort,
  68.                 receiveExplicitSourceAddress);
  69.         }
  70.         public IPAddress SendMulticastAddress
  71.         {
  72.             get { return m_SendMulticastAddress; }
  73.         }
  74.         public int SendPort
  75.         {
  76.             get { return m_SendPort; }
  77.         }
  78.         public IPAddress ReceiveMulticastAddress
  79.         {
  80.             get { return m_ReceiveMulticastAddress; }
  81.         }
  82.         public int ReceivePort
  83.         {
  84.             get { return m_ReceivePort; }
  85.         }
  86.         public IPAddress SendNetworkCardAddress
  87.         {
  88.             get { return m_SendNetworkCardAddress; }
  89.         }
  90.         public int SendNetworkCardPort
  91.         {
  92.             get { return m_SendNetworkCardPort; }
  93.         }
  94.         public IPAddress ReceiveNetworkCardAddress
  95.         {
  96.             get { return m_ReceiveNetworkCardAddress; }
  97.         }
  98.     }


The TransportAgent class is in charge of creating the sockets that receive and send the data, joining to the multicast group (i.e. sending IGMP package to the router) and binding both sockets to the appropriate network interface.

Code Snippet
  1.     class TransportAgent
  2.     {
  3.         static readonly IPEndPoint AnyAddress = new IPEndPoint(IPAddress.Any, 0);
  4.         private readonly byte[] m_objectStateBuffer = new byte[8192];
  5.         private readonly MulticastConfiguration m_dataEntity;
  6.         private readonly Socket m_sendSocket;
  7.         private readonly Socket m_receiveSocket;
  8.         private  bool m_opened;
  9.         public TransportAgent(
  10.             MulticastConfiguration entity,
  11.             EventHandler<BufferReceivedEventArgs> messageReceived)
  12.             : this(entity)
  13.         {
  14.             MessageReceived += messageReceived;
  15.         }
  16.         public TransportAgent(MulticastConfiguration entity)
  17.         {
  18.             m_dataEntity = entity;
  20.             m_sendSocket = CreateUdpSocket();
  21.             EndPoint sendEndPoint = new IPEndPoint(
  22.                 m_dataEntity.SendNetworkCardAddress,
  23.                 m_dataEntity.SendNetworkCardPort);
  25.             // define the address of the network card from
  26.             // which the multicast data will be sent. 
  27.             m_sendSocket.Bind(sendEndPoint);
  28.             m_receiveSocket = CreateUdpSocket();
  30.             EndPoint receiveEndPoint = new IPEndPoint(
  31.                 m_dataEntity.ReceiveNetworkCardAddress,
  32.                 m_dataEntity.ReceivePort);
  33.             // define the address of the network card from
  34.             // which the multicast data will be received. 
  35.             m_receiveSocket.Bind(receiveEndPoint);
  37.             // Send the IGMP message to the router, ask to
  38.             // goin to the multicast group
  39.             JoinMulticast(m_receiveSocket);
  40.             BeginReceive();
  41.             m_opened = true;
  42.         }
  43.         public event EventHandler<BufferReceivedEventArgs> MessageReceived = delegate { };
  44.         public void Close()
  45.         {
  46.             if (!m_opened)
  47.             {
  48.                 return;
  49.             }
  50.             DropMulticast(m_receiveSocket);
  51.             m_receiveSocket.Close();
  52.             m_sendSocket.Close();
  53.             m_opened = false;
  54.         }
  55.         public void Send(byte[] message)
  56.         {
  57.             StateObject stateObject = new StateObject(
  58.                 m_sendSocket, message);
  59.             IPEndPoint endPoint = new IPEndPoint(
  60.                 m_dataEntity.SendMulticastAddress,
  61.                 m_dataEntity.SendPort);
  62.             int offset = 0;
  64.             m_sendSocket.BeginSendTo(
  65.                 message,
  66.                 offset,
  67.                 message.Length,
  68.                 SocketFlags.None,
  69.                 endPoint,
  70.                 OnSend,
  71.                 stateObject);           
  72.         }
  73.         private void OnSend(IAsyncResult ar)
  74.         {
  75.             int bytesCount = m_sendSocket.EndSendTo(ar);
  76.             Trace.WriteLine(
  77.                 string.Format("Messages Sent, Bytes: {0}",
  78.                 bytesCount));
  79.         }
  80.         private void BeginReceive()
  81.         {
  82.             Socket receiveSocket = m_receiveSocket;
  83.             if (receiveSocket == null) return;
  85.             EndPoint remoteEP = AnyAddress;
  86.             StateObject stateObject = new StateObject(
  87.                 receiveSocket, m_objectStateBuffer);
  88.             int offset = 0;
  89.             receiveSocket.BeginReceiveFrom(
  90.                 stateObject.Buffer,
  91.                 offset,
  92.                 stateObject.Buffer.Length,
  93.                 SocketFlags.None,
  94.                 ref remoteEP,
  95.                 OnReceive,
  96.                 stateObject);
  97.         }
  98.         private void OnReceive(IAsyncResult result)
  99.         {
  100.             EndPoint remoteEP = AnyAddress;
  101.             try
  102.             {
  103.                 int bufferSize = m_receiveSocket.EndReceiveFrom(
  104.                     result, ref remoteEP);
  105.                 StateObject stateObject =
  106.                     (StateObject)result.AsyncState;
  108.                 BufferReceivedEventArgs e = new BufferReceivedEventArgs(
  109.                     stateObject.Buffer, bufferSize);
  111.                 MessageReceived(this, e);
  113.                 BeginReceive();
  114.             }
  115.             catch
  116.             {
  117.                 Trace.WriteLine("Disconnection");
  118.             }
  119.         }
  120.         private static Socket CreateUdpSocket()
  121.         {
  122.             Socket socket = new Socket(
  123.                 AddressFamily.InterNetwork,
  124.                 SocketType.Dgram,
  125.                 ProtocolType.Udp);
  127.             return socket;
  128.         }
  129.         private void JoinMulticast(Socket socket)
  130.         {
  131.             MulticastOption multicastOption = GetMulticastOption();
  133.             socket.SetSocketOption(
  134.                 SocketOptionLevel.IP,
  135.                 SocketOptionName.MulticastTimeToLive,
  136.                 64);
  138.             socket.SetSocketOption(
  139.                 SocketOptionLevel.IP,
  140.                 SocketOptionName.AddMembership,
  141.                 multicastOption);
  142.         }
  143.         private void DropMulticast(Socket socket)
  144.         {
  145.             MulticastOption multicastOption = GetMulticastOption();
  147.             socket.SetSocketOption(
  148.                 SocketOptionLevel.IP,
  149.                 SocketOptionName.MulticastTimeToLive,
  150.                 64);
  152.             socket.SetSocketOption(
  153.                 SocketOptionLevel.IP,
  154.                 SocketOptionName.DropMembership,
  155.                 multicastOption);
  156.         }
  157.         private MulticastOption GetMulticastOption()
  158.         {
  159.             IPAddress ip = m_dataEntity.ReceiveMulticastAddress;
  160.             // MulticastOption can be constructed with the
  161.             // overload that allows setting the address of
  162.             // the network card from which the IGMP package
  163.             // will be sent to the router.
  164.             MulticastOption mo = new MulticastOption(ip);
  165.             return mo;
  166.         }
  167.     }


All the host has to do is to read the configuration from the UI, instantiate the configuration class, and instantiate the transport class - injecting it with the configuration instance and with delegate to OnMessageReceived callback that will be called when ever data arrive.

Code Snippet
  1.     MulticastConfiguration multicastConfiguration = ReadConfiguration();
  2.     m_TransportAgent = new TransportAgent(
  3.         multicastConfiguration, OnMessageReceived);
Sending Multicast Data
Code Snippet
  1.     ASCIIEncoding asciiEncoding = new ASCIIEncoding();
  2.     string message = m_textBoxMessage.Text;
  3.     byte[] bytes = asciiEncoding.GetBytes(message);
  4.     m_TransportAgent.Send(bytes);
Receiving Multicast Data
Code Snippet
  1.     private void OnMessageReceived(object sender, BufferReceivedEventArgs e)
  2.     {
  3.         string message = Encoding.ASCII.GetString(e.Buffer);
  4.         Print(message);
  5.     }


My choice for network protocol analyzer is Wireshark which is the best analyzer available today, it’s released under the GNU General Public License (GPL) so it can be used freely.

To get started, download the source code and run the multicast tester application, download Wireshark, run it and start a capture session,


In order to get a feeling about the way that multicast addressing works – let’s review the IGMP message that is sent to the router when the receiver socket joins to the multicast group (see TransportAgent line 166).

Enter ‘igmp’ in Wireshark filter.


In  the multicast tester application, press ‘Connect’. The ‘network card interface’ in the receiver configuration is set to my local machine upper network card address.



As a result, IGMP package was sent to the network, requesting it to add network interface with address (which is my upper network card address) to the multicast group Consequently, the network will deliver any multicast data sent to group to address

Now, In the multicast tester application, press ‘disconnect’.


As a result, IGMP package was sent to the network, requesting it to remove the network interface from the group.

Sending Multicast Data – Single Network

Enter ‘udp’ in Wireshark filter.

In  the multicast tester application, leave the ‘Network Card Address’ and ‘Network Card Port’ in the sender configuration blank, press ‘Connect’, enter some string to the message text box and press ‘Send’


As a result, the string that was specified (in this case ‘aviad’) is sent form the network card, port 3704. Since we didn’t specify the network card address and port – the operation system took the liberty to select network card and port.

Sending Multicast Data – Multihomed Network

In  the multicast tester application, set the sender ‘Network Card Address’ to one of your network cards address and set the ‘Network Card Port’ to 1234, press ‘Connect’, enter some string to the message text box and press ‘Send’.



As a result, the string is sent form the network card, port 1234. Since we did specify the network card address and port – the network card and the port from which the string was sent were NOT the selection of the operation system. Even though in the previous case the operation system selected the same network card that we specified, in cases where there are more than 1 enabled and active network cards in the machine - you must specify the appropriate network address or your multicast may be delivered though the wrong network.

Tuesday, July 7, 2009

Multicast Addressing Pitfalls

The IP multicast model has been described as follows: “You put packets in at one end, and the network conspires to deliver them to anyone who asks”. That sounds pretty simple indeed, but as you spend more time with applications that deliver multicast services you find that in many cases you will ‘put packets in at one end, look at the network protocol analyzer, and see nothing that you expect’. The truth of the matter is that getting multicast applications to work as expected requires deep understanding of the network topology and the underlying routing devices.

This post reviews the multicast addressing technology along with in detail illustrations of some of the pitfalls that are often encountered when deploying multicast applications.

In my next post you can find walkthrough of C# sample application that allows sending and receiving multicast data over the network.

How does it Work?

In multicast applications, the sender defines IP multicast group address (from to to which is send data packets. Receivers inform the network that they are interested in receiving data packets sent to a certain group by sending Internet Group Management Protocol (IGMP) package to the closest network node (router, IGMP querier). The node is in charge of maintaining multicast distribution trees such that data packets sent to a multicast group reach all receivers which have joined the group.


Why Use Multicast?

The great thing about multicast addressing is that in order to distribute a message to multiple receivers - the sender have to dispatch only one message over the wire, while it’s the network (e.g. intermediary routers) responsibility to copy and distribute the message to all the receivers in the group. In case there aren’t any receivers that had joined the group – the network drops the message.  The fact that the sender is unaware of the receivers greatly improves the application ability to scale to a large receiver population

The Pitfalls

Developing application that sends and receives multicast streams over the network is considered a fairly simple task. You can get pretty fast to the point where everything seems to work fine in the integration labs, were all the PCs communicate through a single network card and connected through few well known switches. The problems start when the application first meet the client network that is often spitted by VLANs and filled with all kinds of Cisco goodness. Unavoidably, you’ll find yourself steering at the network sniffer trying to figure out why you don’t see packages from group X even though the IGMP package was sent to the router, or why you do see packages from group Y - even though your application had never requested to join this particular group (No IGMP package was sent).

Where is my Multicast?

Multihomed Network

In case a host is connected to two or more networks or have two or more network cards installed and enabled – senders must explicitly state to which network interface (represented by the unicast IP address of the network card) they want to send the multicast traffic, and receivers must explicitly state from which network interface they want to receive multicast traffic. If they don’t – the operation system takes the liberty to choose the network interface, and the multicast traffic has 50% chance of reaching to the intended destination.


A common mistake that developers make is not binding the socket used for sending multicast traffic to the appropriate network card endpoint, or binding the socket used for receiving multicast to the address Any (

This mistake is so common because developers often assume that UDP unicast socket and UDP multicast sockets that are being used to send data can be implemented alike, and that sockets that are being used to receive multicast data can bind to IPAddress.Any ( This is true when the host has single network card, but not true for host that lives in multihomed network.


In case the machine firewall is turned on, and the firewall is NOT configured to relay transports from a certain multicast group (and corresponding UDP port) – your application will not receive messages from that group.


Inter VLAN

In case the sender and the receivers don’t sit on the same VLAN, the receivers will not receive messages from the sender.


Switch doesn’t Support IGMP Snooping

In case the sender is connected to a switch that support IGMP snooping (which means that it relay multicast messages only through ports which sent IGMP message), and the receiver is connected to another switch that doesn’t support IGMP snooping - the multicast relaying mechanism will "breaks down" in the absence of an mrouter port, thus the receiver will not receive the messages from the sender.


If you want a fix for this solution, you must have the switches somehow learn or know of an mrouter port. When the switches know their mrouter port, the right Switch (that doesn’t support IGMP snooping) relays out the IGMP report that it receives through its mrouter port. From the perspective of the left Switch, it received merely another IGMP report. The left switch adds that port into its IGMP snooping table and begins sending out multicast traffic on that port as well. At this point, the right Switch receive the multicast traffic, and the application works as expected.

Why am I getting this Multicast?

Broadcasting Multicast Traffic

Switches that cannot understand multicast addresses usually broadcast multicast traffic to all the members of a LAN. Machines that are connected to such switches will see multicast traffic from groups that they’ve never registered to.


Port Mirroring

In case the machine is connected to a router port that is configured as ‘port mirroring’ – the machine will receive multicast streams form all the groups.


Port mirroring, also known as a roving analysis port, is a method of monitoring network traffic that forwards a copy of each incoming and outgoing packet from one port of a network switch to another port where the packet can be studied. A network administrator uses port mirroring as a diagnostic tool or debugging feature, especially when fending off an attack. It enables the administrator to keep close track of switch performance and alter it if necessary. Port mirroring can be managed locally or remotely.

Multicasting over WAN

Source-specific multicast (SSM) make multicasting packages over the WAN eligible by reducing the amount of multicast routing information that the network must maintain. With SSM receivers supply the sender’s source address to the routers as a part of joining the group (by setting the receiver socket option MCAST_JOIN_SOURCE_GROUP).

A great presentation (ppt) that discusses multicast addressing can be downloaded from here.