I have one socket that is waiting for OSPF Hello packets (224.0.0.5) on the different interfaces of my machine, except loopback interface. At this point, I receive multicast OSPF packets from two different interfaces on the same socket.
class mcast(object):
def __init__(self):
self.bufsize = BUFSIZE
def create(self, MCAST_GROUP, PROTO):
self.mcast_group = MCAST_GROUP
self.proto = PROTO
s = socket(AF_INET, SOCK_RAW, self.proto)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.bind((self.mcast_group, self.proto))
netinterfaces = getIPAllInterfaces().items()
for x in range(0, len(netinterfaces)):
if netinterfaces[x][0] == 'lo':
continue
else:
mcast = inet_aton(self.mcast_group) + inet_aton(netinterfaces[x][1])
s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, str(mcast))
return s
def recv(self, s):
self.s = s
return self.s.recvfrom(self.bufsize)
The point now is that I need to know in which interface I received the packet because the treatment of the packet is different.
How to know from which interface did I received the multicast packet?
EDIT: from Getting the IP of the interface that received a recvfrom() UDP packet (Microsoft) here "there is no way to know the receiving IP when a single listening socket is bound to multiple IPs.(...) create a separate listening socket for each IP." Does anyone know a different solution?