Results 1 to 5 of 5

Thread: UDP broadcasts - multiple interfaces.

  1. #1
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Smile UDP broadcasts - multiple interfaces.

    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?
    Qt Code:
    1. m_pDataSocket->bind(originalInterfaceIPAddress, m_DataPort, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. bytesRead = m_pDataSocket->readDatagram(ByteArray.data(), ByteArray.size(), &Sender, &SenderPort);
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: UDP broadcasts - multiple interfaces.

    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:
    Qt Code:
    1. IP routing table
    2. Destination Gateway Genmask Flags Metric Ref Use Iface
    3. default router 0.0.0.0 UG 1 0 0 eth0
    4. loopback localhost 255.0.0.0 UG 0 0 0 lo
    5. 192.168.1.0 * 255.255.255.0 U 1 0 0 eth0
    6. 192.168.2.0 * 255.255.255.0 U 1 0 0 eth1
    To copy to clipboard, switch view to plain text mode 
    You can broadcast to hosts on eth0 using 192.168.1.255, or eth1 using 192.168.2.255

  3. The following user says thank you to ChrisW67 for this useful post:

    sBoff (30th October 2012)

  4. #3
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Default Re: UDP broadcasts - multiple interfaces.

    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

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: UDP broadcasts - multiple interfaces.

    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.

  6. #5
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Default Re: UDP broadcasts - multiple interfaces.

    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

Similar Threads

  1. Object and multiple inheritance for interfaces
    By brcain in forum Qt Programming
    Replies: 8
    Last Post: 29th June 2021, 15:29
  2. Replies: 2
    Last Post: 22nd October 2010, 15:52
  3. multiple exported interfaces in one plugin - how to?
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2009, 19:20
  4. Qt Designer for web interfaces
    By rbp in forum Qt Tools
    Replies: 3
    Last Post: 14th October 2009, 02:01
  5. QUdpSocket - Multiple Network Interfaces
    By dbrmik in forum Qt Programming
    Replies: 0
    Last Post: 24th April 2009, 13:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.