Search code examples
omnet++inet

Broadcasting from application layer (or a lower layer) [INET 4.1.0]


So here is what I want to do:

Broadcast some information to my neighbours via UDP (wireless scenario) without having to specify an IP address (a generic one is fine). The port will be the same on all nodes. The incoming packet should be accepted by the neighbouring nodes and handed up to the application layer.

I've read this question but I was not able to find the mentioned parameter. I also couldn't find out how to bind a socket to a specific interface by looking at the developers guide of inet.

So my question is: Is it even possible to broadcast from Application Layer?

If it is: Which parameters do I have to set?

If it is not possible: Do I have to go to a lower layer? I have seen the the pure ethernet examples in inet (inet/applications/ethernet/) but how can I apply this to a wireless scenario?


Solution

  • So I found it out myself. I'm posting an answer here to help other people encountering the same problem.

    I solved the problem by setting several udp socket options. I set the following (some of them are not necessary but I found them to be useful):

    socket.setMulticastOutputInterface(101);
    socket.setMulticastLoop(false);
    socket.bind(port);
    L3Address address;
    L3AddressResolver().tryResolve("224.0.0.1", address);
    socket.joinMulticastGroup(address);
    

    setMulticastOutputInterface(101): This sets the interface (by id) which should be used to broadcast (multicast) my messages.

    setMulticastLoop(false): I found this to be useful to prevent ipv4 layer to sent the multicast to my loopback device as well. This is not necessary though.

    bind(port): This is to only receive udp packets with the given destination port. Not necessary too.

    joinMulticastGroup(address): This is necessary to accept the packets with the given multicast address.

    I imagined the solution to be somewhat different but this is fine for my use case. I tried to stay as far away from lower layer broadcasting as possible to avoid being dependent on a specific lower layer protocol. Check out the source code comments for more info. Hope this helps someone.