PDA

View Full Version : UDP broadcasts - multiple interfaces.



sBoff
30th October 2012, 03:14
Hi all,

I am in a situation where there is some legacy code that broadcasts on 255.255.255.255 (this cant be changed for various reasons).

The sending device has now been updated to have two network interfaces.

We want the broadcast to be sent from the original interface, not the new one. Is there a way we can specify which interface should be used to send the broadcast? I thought binding to the original interface may solve the issue but I haven't been successful in getting it to work - should it work?

m_pDataSocket->bind(originalInterfaceIPAddress, m_DataPort, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
We need to specify which interface to send the broadcast from because we only want the receiver to accept data that originated from the original interface.
We use the code below and then check if the 'Sender' variable is in the acceptable subnet. The problem is we we cant control what interface the broadcast is being sent from this check fails as 'Sender' has the IP address of the senders new interface.

bytesRead = m_pDataSocket->readDatagram(ByteArray.data(), ByteArray.size(), &Sender, &SenderPort);
Long story short, is there anyway to specify which network interface a broadcast UDB packet should be sent from?

I hope this make sence,
Regards

ChrisW67
30th October 2012, 04:55
If you are sending to the local IP broadcast address then you are asking the operating system to deliver it to all local network hosts. The operating system must send it out all interfaces in order to reach all local hosts that might need to receive it.

If you have control of the sender source code you want to be sending out only an IP sub-network broadcast and not a global broadcast. That is if you have two interfaces:


IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default router 0.0.0.0 UG 1 0 0 eth0
loopback localhost 255.0.0.0 UG 0 0 0 lo
192.168.1.0 * 255.255.255.0 U 1 0 0 eth0
192.168.2.0 * 255.255.255.0 U 1 0 0 eth1

You can broadcast to hosts on eth0 using 192.168.1.255, or eth1 using 192.168.2.255

sBoff
30th October 2012, 19:24
Chris, thanks for your reply.

What you said about the local IP broadcast being sent from all interfaces makes sense. I had never thought about it that way.



If you have control of the sender source code you want to be sending out only an IP sub-network broadcast and not a global broadcast. That is if you have two interfaces:
...
You can broadcast to hosts on eth0 using 192.168.1.255, or eth1 using 192.168.2.255

This was one of my original thoughts as well. I will mention it again and see if there is any real reason we cant approach it this way.

I should mention that this is on a Linux device and to be precise it has one network interface with multiple aliases - I am not sure the implications of this.

Any further thoughts/comments would be very much appreciated.

Regards,
Dan

ChrisW67
30th October 2012, 23:17
I should mention that this is on a Linux device and to be precise it has one network interface with multiple aliases - I am not sure the implications of this.
Yes and no. A subnet directed IP broadcast will still result in an Ethernet broadcast on the single network interface. However, only IP clients in the sub network targeted by the IP broadcast will act on the IP packet, others will simply ignore it because it is not addressed to them. The sender IP address of the subnet direct IP broadcast packets with be the host's address on that subnet.

sBoff
1st November 2012, 20:29
Thanks for your input and help Chris. :)

In the end we left it as a local IP broadcast but bound (QUdpSocket::bind()) to the interface we wanted to send from. This worked however as a result we needed to add an additional socket to receive data (i.e. you cant broadcast from a bound socket and also receive on that socket). Previously we were using the same socket for sending and receiving.

Regards