Search code examples
c++omnet++inet

Communicating Through Protocol Layers With INET Packet


I'm troubling with the reception packet at the receiver side. Please help me to find a way. At the SENDER side, I encapsulate the data packet (which comes from UdpBasicApp through Udp protocol) when it arrives at Network Layer as follows:

void Sim::encapsulate(Packet *packet) {
    cModule *iftModule = findModuleByPath("SensorNetwork.sink.interfaceTable");
    IInterfaceTable *ift = check_and_cast<IInterfaceTable *>(iftModule);
    auto *ie = ift->findFirstNonLoopbackInterface();
    mySinkMacAddr  = ie->getMacAddress();
    mySinkNetwAddr = ie->getNetworkAddress();
    interfaceId = ie->getInterfaceId();

    //Set Source and Destination Mac and Network Address.
    packet->addTagIfAbsent<MacAddressReq>()->setSrcAddress(myMacAddr);
    packet->addTagIfAbsent<MacAddressReq>()->setDestAddress(mySinkMacAddr);
    packet->addTagIfAbsent<L3AddressReq>()->setSrcAddress(myNetwAddr);
    packet->addTagIfAbsent<L3AddressReq>()->setDestAddress(mySinkNetwAddr);

    packet->addTagIfAbsent<InterfaceReq>()->setInterfaceId(interfaceId);

    //Attaches a "control info" structure (object) to the down message or packet.
    packet->addTagIfAbsent<PacketProtocolTag>()->setProtocol(&getProtocol());
    packet->addTagIfAbsent<DispatchProtocolInd>()->setProtocol(&getProtocol());
}

At the RECEIVER side, I try to get the Network Address of the SENDER as follows :

auto l3 = packet->addTagIfAbsent<L3AddressReq>()->getSrcAddress();
EV_DEBUG << "THE SOURCE NETWORK ADDRESS IS : " <<l3<<endl;

And when I print l3, the output is DEBUG: THE SOURCE NETWORK ADDRESS IS : <none>

What is wrong ? How can I access to the SENDER network Address through the received packet ?

Many thanks in advance. I will be grateful


Solution

  • Request tags are things that you add to a packet sending information down to lower OSI layers. On receiving end, protocol layers will annotate the packet with indicator tags so upper OSI layers can get that information if needed. You are adding an empty request tag to an incoming packet, so no wonder it is empty. What you need is get the L3AddressInd tag from the packet and extract the source address from there:

    L3Address srcAddr = packet->getTag<L3AddressInd>()->getSrcAddress();
    

    or

    MacAddress srcAddr = packet->getTag<MacAddressInd>()->getSrcAddress();
    

    depending on how the packet was received.