1

Backstory:
I have a wireless device which creates it's own SSID, assigns itself an IP address using auto-ip, and begins broadcasting discovery information to 255.255.255.255. (unfortunately, it does not easily support multicast)

What I'm trying to do:
I need to be able to receive the discovery information, then send configuration information to the device. The problem is, with auto-ip, the "IP negotiation" process can take minutes on Windows, etc (during which time I can see the broadcasts and can even send broadcast information back to the device).

So I enumerate all connected network interfaces (can't directly tell which will be used to talk to the device), create a DatagramSocket for each of their addresses, then start listening. If I receive the discovery information via a particular socket, I know I can use that same socket to send data back to the device. This works on Windows.

The problem:
On Linux and OSX, the following code does not receive broadcast packets:

byte[] addr = {(byte)169, (byte)254, (byte)6, (byte)215};  
DatagramSocket foo = new DatagramSocket(new InetSocketAddress(InetAddress.getByAddress(addr), PORT_NUM));  
while (true)  
{  
byte[] buf = new byte[256];  
DatagramPacket pct = new DatagramPacket(buf, buf.length);  
foo.receive(pct);  
System.out.println( IoBuffer.wrap(buf).getHexDump() );  
}

In order to receive broadcast packets (on Linux/OSX), I need to create my DatagramSocket using:
DatagramSocket foo = new DatagramSocket(PORT_NUM);

However, when I then use this socket to send data back to the device, the packet is routed by the OS (I'm assuming) and since the interface of interest may be in the middle of auto-ip negotiation, fails.

Thoughts on the following?

  • How to get the "working" Windows behavior to happen on Linux/OSX
  • A better way to handle this process

Thanks in advance!

Arsinio
  • 11
  • 1
  • I don't understand how your Linux machine's network interface would be "in the middle of auto-ip negotiation", how does that machine get it's IP addresses? Static or DHCP? The machine should have it's IP address independent of the wireless device? Or do all your machines get their IP addresses via auto-ip? Is auto-ip really http://en.wikipedia.org/wiki/Zero-configuration_networking? – HeatfanJohn May 14 '13 at 23:42
  • @HeatfanJohn I believe auto-IP (Automatic Private IP Addressing (APIPA)) and ZeroConf are one in the same. During the IP negotiation process, the OS randomly selects IP addresses and probes them to make sure there are no conflicts. That process can take a few minutes. During that time, I can still send and receive broadcasts using that interface _if_ I'm using the previously bound socket. However, using the "wildcard" DatagramSocket, the OS will not route the packets to this device. – Arsinio May 14 '13 at 23:55

1 Answers1

0

I do not think this is the problem with the code. Have you checked if OSX/Linux has correctly allowed those address / port number through their firewalls? I had this simple problem too in the past =P..

FYI, there is a nice technology called Zero-configuration which was built to solve this problem. It is very easy to learn so I recommend you to having a look at that as well.

Good luck.

Jason
  • 1,298
  • 1
  • 16
  • 27
  • I wish I could use ZeroConf!! Unfortunately the device doesn't support that kind of thing. I know the firewall settings are good because when I replace the DatagramSocket instantiation with the wildcard instantiation, I can see the broadcasts. – Arsinio May 14 '13 at 23:56