1

I have a socket according to:

self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(('0.0.0.0', 67)) # Listen on port 67

Now, the socket listening on all interfaces at port 67. I wait for some message:

message, address = self.socket.recvfrom(8192) # Blocking

When a message is received, I would like to send a broadcast response at port 68 on the same interface that was receiving the message.

The question is: Is there some way to inspect the message and determine which network interface that was receiving the message, and if it is, how can I send a response and provide that interface for transport?

Running at Windows, Python 3.4.

kungcc
  • 1,832
  • 5
  • 25
  • 48
  • Possibily related - [How to tell which interface the socket received the message from?](http://stackoverflow.com/questions/603577/how-to-tell-which-interface-the-socket-received-the-message-from) – jweyrich May 26 '15 at 12:54

1 Answers1

1

You can't. You could only distinguish between the interfaces if you'd bind to each IP address separately (which would open a socket for each interface).

EDIT: as @jweyrich pointed out in the comments below, on Windows a socket option called IP_PKTINFO (of course Linux also) could provide the information - if it implemented

knitti
  • 6,817
  • 31
  • 42
  • You can, strictly talking about UDP - see [IP_PKTINFO](http://linux.die.net/man/7/ip) – jweyrich May 26 '15 at 12:58
  • indeed, my bad - [for windows](https://msdn.microsoft.com/en-us/library/windows/desktop/hh285668%28v=vs.85%29.aspx) - Just not sure this would work over the Python's socket abstraction - I haven't read the code to know if it internally uses `WSARecvMsg`. – jweyrich May 26 '15 at 13:03
  • OpenBSD doesn't have it, NetBSD has issues (http://mail-index.netbsd.org/tech-net/2013/09/22/msg004252.html), OSX didn't last time I looked (10.7), IOS??? .... – knitti May 26 '15 at 13:10
  • OP is running on Windows, if he doesn't care about portability, the socket option may solve the problem - yet I doubt Python's implementation does use `WSARecvMsg` internally on Windows. – jweyrich May 26 '15 at 13:12
  • @jweyrich, thanks, I missed that part, thats *my bad* actually. If you don't post this as an answer, I'll update my own in a few minutes. – knitti May 26 '15 at 13:15
  • No worries. I didn't notice that bit as well, at least not until your 1st reply. Feel free to update your answer. If it works, it's still necessary to parse that extra information (since Python's doesn't provide the necessary structures) - A quick search revealed [someone already did it](http://carnivore.it/2012/10/12/python3.3_sendmsg_and_recvmsg). – jweyrich May 26 '15 at 13:21