2

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?

  • Look at the source address on the IP packet. It will be the address of the sending router, which must be on the network of one of your interfaces. The OSPF multicast is a link-local multicast, meaning it cannot be sent off the local link, so the source address will be in the same network as one of your interfaces. OSPF Hello packets also contain the IP address and network mask of the sending router. – Ron Maupin Oct 31 '17 at 13:40
  • Great idea! Thanks @RonMaupin – André Pires Oct 31 '17 at 14:40

0 Answers0