【问题标题】:Communicating Through Protocol Layers With INET Packet使用 INET 数据包通过协议层进行通信
【发布时间】:2021-08-24 17:29:35
【问题描述】:

我对接收方的接收数据包感到不安。 请帮我想办法。 在SENDER端,我将到达网络层的数据包(通过Udp协议从UdpBasicApp传来)封装如下:

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());
}

在 RECEIVER 端,我尝试获取 SENDER 的网络地址,如下所示:

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

当我打印 l3 时, 输出为 DEBUG: THE SOURCE NETWORK ADDRESS IS : &lt;none&gt;

怎么了? 如何通过收到的数据包访问 SENDER 网络地址?

提前非常感谢。 我将不胜感激

【问题讨论】:

    标签: c++ omnet++ inet


    【解决方案1】:

    请求标签是您添加到将信息发送到较低 OSI 层的数据包中的内容。在接收端,协议层将使用 indicator tags 注释数据包,以便 OSI 上层可以在需要时获取该信息。您正在向传入数据包添加一个空请求标签,所以难怪它是空的。您需要的是从数据包中获取L3AddressInd 标签并从中提取源地址:

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

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

    取决于数据包的接收方式。

    【讨论】: