[Twisted-Python] Can I get the interface's IP addr from where the datagram is received?

Hi all,
I'm writting a udp server which may have multiple instances listening on multiple network interfaces, I need to get the network interface's address when datagrame received:
class MyUdpServer(DatagramProtocol): def datagramReceived(self, data, addr): print "The client's addr is", addr print "My addr is", ???
How can I retrieve the IP? Thanks in advance!
- Eric

Eric Hsu wrote:
I'm writting a udp server which may have multiple instances listening on multiple network interfaces, I need to get the network interface's address when datagrame received:
class MyUdpServer(DatagramProtocol): def datagramReceived(self, data, addr): print "The client's addr is", addr print "My addr is", ???
How can I retrieve the IP? Thanks in advance!
Assumption: you really need to know the address the packet was sent to, and not just "(one of) my IP address(es)".
1) the portable, ugly way: iterate all local addresses, bind to each separately, map socket fd -> IP address when receiving. Note that modern OSes allow more than one address per interface. Sucks a lot if anything changes during runtime, too.
2) the Linux-specific, nice way: see IP_PKTINFO in ip(7), recvmsg(2), cmsg(3), etc. For python, use eunuchs.recvmsg, see http://www.inoi.fi/open/trac/eunuchs

2005/7/6, Tommi Virtanen tv@twistedmatrix.com:
Assumption: you really need to know the address the packet was sent to, and not just "(one of) my IP address(es)".
Hi Tommi, thank you for your reply!
I'm having my server running on a machine which has two ethernet interfaces, one has an intranet ip address like "192.168.3.4", another one has a public internet ip address like "202.71.3.4".
I want my server to provide public services to the outside. One of its function is to allocate some ports for the clients, once the allocation has been done, the server has to send back an address pair (ip:port) to the client.
I have written a method:
def getServerIP(): return socket.getaddrinfo(socket.gethostname(), None)[0][4][0]
It worked properly several days ago and returned me the public address (202.71.3.4) of that machine, however, yesterday, it kept returning the intranet address (192.168.3.4) which was useless for the clients on the internet. I can't figure out why...
I've tried to use the self.transport.getHost(), however, the result was something like this: IPv4Address(UDP, '0.0.0.0', 6677). What's wrong with this method?
Is it so hard to know the ip address from which the data my server received?
I'm very new to twisted and not a native english speaker, so please bear with me :p
- Eric
participants (2)
-
Eric Hsu
-
Tommi Virtanen