Alex Rivera | Logout

How to ignore your own broadcast udp packets

Asked 2009-11-19T09:49:33.860
12

For the following I'm assuming one network card.

I have a component of my program which is designed to let others in the subnet know of its existence. For this, I've implemented a solution where whenever the program starts up (and periodically afterwards) it sends a broadcast to INADDR_BROADCAST - whoever listens on the required port will remember where it came from for later use.

The problem with this is that I don't want to remember my own broadcasts. I thought that in theory this would be easy to do - simply find out the local ip and compare to what you get in recvfrom.

However, I've found it difficult to get the local IP: getaddrinfo with NULL returns 127.0.0.1, getaddrinfo with the hostname returns the public ip. Can anyone point me in the direction of finding the actual subnet ip ? I think I must be missing something very obvious here but well... I'm still missing it :)

Note: I've read other SO questions on broadcasts, in particular this one: UDP-Broadcast on all interfaces but I haven't gotten round to the multiple interface issue yet.

Edit
Report

1 Answer

2

On linux, you can get the IP address of a given interface using ioctl with the SIOCGIFADDR option. I don't think this works for Windows, though. For that, you'll have to do something goofy like this.

answered 2009-11-19T19:53:48.050

Your Answer