Disabling self-reception of UDP broadcasts
Asked Answered
H

3

5

I wish to know is there any way I can disable the UDP broadcast packet from the node A to not received by node A itself.

For braodcast I am simply using INADDR_BROADCAST and on the receiver side I am using AI_PASSIVE | AI_NUMERICHOST.

Hodgkin answered 21/12, 2010 at 12:29 Comment(0)
A
4

No, this is fundamental property of broadcasting - every host on the subnet, including the sender, will have to process the packet all the way up the network stack. You options are:

  • Switch to multicast. This is preferred since multicast reduces the load on the whole network compared to broadcast, and because you can explicitly control multicast loopback with the IP_MULTICAST_LOOP socket option.
  • Don't bind(2) the destination port on the sending machine. This works but is sort of kludgy since it puts restrictions on application design and/or deployment.
Ask answered 23/12, 2010 at 4:30 Comment(4)
Another option is to look at the src IP of each incoming packet and simply ignore the packets that are from local IP(s).Shocker
Yes, but with multicast this is done on the hardware/kernel level.Ask
since each application is doing broadcast and listining to others broadcast to fixed numbet port. It can not be done.Hodgkin
@Remy :- My current implementation is like this only. I wish to avoid this scenario.Hodgkin
B
2

Bind to interface, not just address.

  #include <net/if.h>
  #include <socket.h>

  struct ifreq interface;
  strcpy(interface.ifr_ifrn.ifrn_name, "eth0");

  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &interface, sizeof(interface));

  //... bind(fd,...) ...

This way data that didn't arrive at the interface specified (but originated from it instead) will not be received.

Beshrew answered 16/11, 2011 at 14:50 Comment(0)
A
2

Here are results of my experiments with Python's socket library. Whether UDP broadcaster receives messages sent by itself is dependent on what address will you bind broadcasting socket to. For greater clarity, broadcaster's IP address was 192.168.2.1.

  • When binding to '192.168.2.255' or '' (empty address), broadcaster receives messages sent by itself
  • When binding to '192.168.2.1', '255.255.255.255' or '<broadcast>', broadcaster will NOT receive messages sent by itself

Receiver received broadcasted UDP messages in all these cases.

P.S. Tested on Python 2.7.9, OS Raspbian 8 (adaptation of Debian for Raspberry Pi), Linux kernel 4.4.38

Aggressor answered 20/4, 2017 at 14:48 Comment(2)
what is '<broadcast>'? Is that a hostname, alias, etc? I keep seeing it but no one says what it is.Guileless
@Guileless I guess that is a constant to mark if a connection is a broadcast or not. CMIIWIndividuation

© 2022 - 2024 — McMap. All rights reserved.