简体   繁体   English

addByteLength()不添加字节以在omnet ++中发送消息

[英]addByteLength() not adding bytes to send message in omnet++

I wrote a function for my own simulation model to create and send ARP requests. 我为自己的仿真模型编写了一个函数,用于创建和发送ARP请求。 Here in the below code I have successfully created the ARP packet and set the byte size to 28. Now when I tried to encapsulate this created packet inside the link layer frame tempLinkLayerARPRequestFrame 在下面的代码中,我已经成功创建了ARP数据包并将字节大小设置为28。现在,当我尝试将创建的数据包封装在链接层帧tempLinkLayerARPRequestFrame

                tempIPARPRequest = new IPv4ARP("IPv4ARP Request");
                tempIPARPRequest->setSourceIP(tempIPPacket->getSourceIP());
                tempIPARPRequest->setDestinationIP(
                        tempIPPacket->getDestinationIP());
                tempIPARPRequest->addByteLength(28);
                // encapsulate it 
                tempLinkLayerARPRequestFrame= new LinkLayerFrame("LinkARP Request");
                tempLinkLayerARPRequestFrame->setField1(this->Field1);
                tempLinkLayerARPRequestFrame->setField2(this->Field2);


                tempLinkLayerARPRequestFrame->setFrameType(0); //an ARP packet
                tempLinkLayerARPRequestFrame->addByteLength(30); //30Bytes
                tempLinkLayerARPRequestFrame->encapsulate(tempIPARPRequest);
                tempLinkLayerARPRequestFrame->setKind(0);

                send(tempLinkLayerARPRequestFrame->dup(), "toBelowSendQ");
                ARPRequestedCache[tempIPPacket->getDestinationIP()] = true;

But I see that the transmitted frame is of size 28 bytes instead of 28 + 30 = 58 bytes. 但是我看到传输的帧的大小为28字节,而不是28 + 30 = 58字节。 Why is it happening or the encapsulate function is not appending the bytes? 为什么会发生这种情况,或者封装函数未附加字节?

Updated 更新

The IPv4ARP msg file is: IPv4ARP msg文件是:

NED File: NED文件:

packet IPv4ARP {
    string SourceIP;
    string DestinationIP;
}

IPv4ARP_m.h: IPv4ARP_m.h:

//
// Generated file, do not edit! Created by nedtool 4.6 from IPv4ARP.msg.
//

#ifndef _IPV4ARP_M_H_
#define _IPV4ARP_M_H_

#include <omnetpp.h>

// nedtool version check
#define MSGC_VERSION 0x0406
#if (MSGC_VERSION!=OMNETPP_VERSION)
#    error Version mismatch! Probably this file was generated by an earlier version of nedtool: 'make clean' should help.
#endif



/**
 * Class generated from <tt>IPv4ARP.msg:19</tt> by nedtool.
 * <pre>
 * //
 * // TODO generated message class
 * //
 * packet IPv4ARP
 * {
 *     string SourceIP;
 *     string DestinationIP;
 * }
 * </pre>
 */
class IPv4ARP : public ::cPacket
{
  protected:
    opp_string SourceIP_var;
    opp_string DestinationIP_var;

  private:
    void copy(const IPv4ARP& other);

  protected:
    // protected and unimplemented operator==(), to prevent accidental usage
    bool operator==(const IPv4ARP&);

  public:
    IPv4ARP(const char *name=NULL, int kind=0);
    IPv4ARP(const IPv4ARP& other);
    virtual ~IPv4ARP();
    IPv4ARP& operator=(const IPv4ARP& other);
    virtual IPv4ARP *dup() const {return new IPv4ARP(*this);}
    virtual void parsimPack(cCommBuffer *b);
    virtual void parsimUnpack(cCommBuffer *b);

    // field getter/setter methods
    virtual const char * getSourceIP() const;
    virtual void setSourceIP(const char * SourceIP);
    virtual const char * getDestinationIP() const;
    virtual void setDestinationIP(const char * DestinationIP);
};

inline void doPacking(cCommBuffer *b, IPv4ARP& obj) {obj.parsimPack(b);}
inline void doUnpacking(cCommBuffer *b, IPv4ARP& obj) {obj.parsimUnpack(b);}


#endif // ifndef _IPV4ARP_M_H_

IPv4ARP_m.cc IPv4ARP_m.cc

//
// Generated file, do not edit! Created by nedtool 4.6 from IPv4ARP.msg.
//

// Disable warnings about unused variables, empty switch stmts, etc:
#ifdef _MSC_VER
#  pragma warning(disable:4101)
#  pragma warning(disable:4065)
#endif

#include <iostream>
#include <sstream>
#include "IPv4ARP_m.h"

USING_NAMESPACE


// Another default rule (prevents compiler from choosing base class' doPacking())
template<typename T>
void doPacking(cCommBuffer *, T& t) {
    throw cRuntimeError("Parsim error: no doPacking() function for type %s or its base class (check .msg and _m.cc/h files!)",opp_typename(typeid(t)));
}

template<typename T>
void doUnpacking(cCommBuffer *, T& t) {
    throw cRuntimeError("Parsim error: no doUnpacking() function for type %s or its base class (check .msg and _m.cc/h files!)",opp_typename(typeid(t)));
}




// Template rule for outputting std::vector<T> types
template<typename T, typename A>
inline std::ostream& operator<<(std::ostream& out, const std::vector<T,A>& vec)
{
    out.put('{');
    for(typename std::vector<T,A>::const_iterator it = vec.begin(); it != vec.end(); ++it)
    {
        if (it != vec.begin()) {
            out.put(','); out.put(' ');
        }
        out << *it;
    }
    out.put('}');

    char buf[32];
    sprintf(buf, " (size=%u)", (unsigned int)vec.size());
    out.write(buf, strlen(buf));
    return out;
}

// Template rule which fires if a struct or class doesn't have operator<<
template<typename T>
inline std::ostream& operator<<(std::ostream& out,const T&) {return out;}

Register_Class(IPv4ARP);

IPv4ARP::IPv4ARP(const char *name, int kind) : ::cPacket(name,kind)
{
    this->SourceIP_var = 0;
    this->DestinationIP_var = 0;
}

IPv4ARP::IPv4ARP(const IPv4ARP& other) : ::cPacket(other)
{
    copy(other);
}

IPv4ARP::~IPv4ARP()
{
}

IPv4ARP& IPv4ARP::operator=(const IPv4ARP& other)
{
    if (this==&other) return *this;
    ::cPacket::operator=(other);
    copy(other);
    return *this;
}

void IPv4ARP::copy(const IPv4ARP& other)
{
    this->SourceIP_var = other.SourceIP_var;
    this->DestinationIP_var = other.DestinationIP_var;
}

void IPv4ARP::parsimPack(cCommBuffer *b)
{
    ::cPacket::parsimPack(b);
    doPacking(b,this->SourceIP_var);
    doPacking(b,this->DestinationIP_var);
}

void IPv4ARP::parsimUnpack(cCommBuffer *b)
{
    ::cPacket::parsimUnpack(b);
    doUnpacking(b,this->SourceIP_var);
    doUnpacking(b,this->DestinationIP_var);
}

const char * IPv4ARP::getSourceIP() const
{
    return SourceIP_var.c_str();
}

void IPv4ARP::setSourceIP(const char * SourceIP)
{
    this->SourceIP_var = SourceIP;
}

const char * IPv4ARP::getDestinationIP() const
{
    return DestinationIP_var.c_str();
}

void IPv4ARP::setDestinationIP(const char * DestinationIP)
{
    this->DestinationIP_var = DestinationIP;
}

class IPv4ARPDescriptor : public cClassDescriptor
{
  public:
    IPv4ARPDescriptor();
    virtual ~IPv4ARPDescriptor();

    virtual bool doesSupport(cObject *obj) const;
    virtual const char *getProperty(const char *propertyname) const;
    virtual int getFieldCount(void *object) const;
    virtual const char *getFieldName(void *object, int field) const;
    virtual int findField(void *object, const char *fieldName) const;
    virtual unsigned int getFieldTypeFlags(void *object, int field) const;
    virtual const char *getFieldTypeString(void *object, int field) const;
    virtual const char *getFieldProperty(void *object, int field, const char *propertyname) const;
    virtual int getArraySize(void *object, int field) const;

    virtual std::string getFieldAsString(void *object, int field, int i) const;
    virtual bool setFieldAsString(void *object, int field, int i, const char *value) const;

    virtual const char *getFieldStructName(void *object, int field) const;
    virtual void *getFieldStructPointer(void *object, int field, int i) const;
};

Register_ClassDescriptor(IPv4ARPDescriptor);

IPv4ARPDescriptor::IPv4ARPDescriptor() : cClassDescriptor("IPv4ARP", "cPacket")
{
}

IPv4ARPDescriptor::~IPv4ARPDescriptor()
{
}

bool IPv4ARPDescriptor::doesSupport(cObject *obj) const
{
    return dynamic_cast<IPv4ARP *>(obj)!=NULL;
}

const char *IPv4ARPDescriptor::getProperty(const char *propertyname) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    return basedesc ? basedesc->getProperty(propertyname) : NULL;
}

int IPv4ARPDescriptor::getFieldCount(void *object) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    return basedesc ? 2+basedesc->getFieldCount(object) : 2;
}

unsigned int IPv4ARPDescriptor::getFieldTypeFlags(void *object, int field) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    if (basedesc) {
        if (field < basedesc->getFieldCount(object))
            return basedesc->getFieldTypeFlags(object, field);
        field -= basedesc->getFieldCount(object);
    }
    static unsigned int fieldTypeFlags[] = {
        FD_ISEDITABLE,
        FD_ISEDITABLE,
    };
    return (field>=0 && field<2) ? fieldTypeFlags[field] : 0;
}

const char *IPv4ARPDescriptor::getFieldName(void *object, int field) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    if (basedesc) {
        if (field < basedesc->getFieldCount(object))
            return basedesc->getFieldName(object, field);
        field -= basedesc->getFieldCount(object);
    }
    static const char *fieldNames[] = {
        "SourceIP",
        "DestinationIP",
    };
    return (field>=0 && field<2) ? fieldNames[field] : NULL;
}

int IPv4ARPDescriptor::findField(void *object, const char *fieldName) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    int base = basedesc ? basedesc->getFieldCount(object) : 0;
    if (fieldName[0]=='S' && strcmp(fieldName, "SourceIP")==0) return base+0;
    if (fieldName[0]=='D' && strcmp(fieldName, "DestinationIP")==0) return base+1;
    return basedesc ? basedesc->findField(object, fieldName) : -1;
}

const char *IPv4ARPDescriptor::getFieldTypeString(void *object, int field) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    if (basedesc) {
        if (field < basedesc->getFieldCount(object))
            return basedesc->getFieldTypeString(object, field);
        field -= basedesc->getFieldCount(object);
    }
    static const char *fieldTypeStrings[] = {
        "string",
        "string",
    };
    return (field>=0 && field<2) ? fieldTypeStrings[field] : NULL;
}

const char *IPv4ARPDescriptor::getFieldProperty(void *object, int field, const char *propertyname) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    if (basedesc) {
        if (field < basedesc->getFieldCount(object))
            return basedesc->getFieldProperty(object, field, propertyname);
        field -= basedesc->getFieldCount(object);
    }
    switch (field) {
        default: return NULL;
    }
}

int IPv4ARPDescriptor::getArraySize(void *object, int field) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    if (basedesc) {
        if (field < basedesc->getFieldCount(object))
            return basedesc->getArraySize(object, field);
        field -= basedesc->getFieldCount(object);
    }
    IPv4ARP *pp = (IPv4ARP *)object; (void)pp;
    switch (field) {
        default: return 0;
    }
}

std::string IPv4ARPDescriptor::getFieldAsString(void *object, int field, int i) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    if (basedesc) {
        if (field < basedesc->getFieldCount(object))
            return basedesc->getFieldAsString(object,field,i);
        field -= basedesc->getFieldCount(object);
    }
    IPv4ARP *pp = (IPv4ARP *)object; (void)pp;
    switch (field) {
        case 0: return oppstring2string(pp->getSourceIP());
        case 1: return oppstring2string(pp->getDestinationIP());
        default: return "";
    }
}

bool IPv4ARPDescriptor::setFieldAsString(void *object, int field, int i, const char *value) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    if (basedesc) {
        if (field < basedesc->getFieldCount(object))
            return basedesc->setFieldAsString(object,field,i,value);
        field -= basedesc->getFieldCount(object);
    }
    IPv4ARP *pp = (IPv4ARP *)object; (void)pp;
    switch (field) {
        case 0: pp->setSourceIP((value)); return true;
        case 1: pp->setDestinationIP((value)); return true;
        default: return false;
    }
}

const char *IPv4ARPDescriptor::getFieldStructName(void *object, int field) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    if (basedesc) {
        if (field < basedesc->getFieldCount(object))
            return basedesc->getFieldStructName(object, field);
        field -= basedesc->getFieldCount(object);
    }
    switch (field) {
        default: return NULL;
    };
}

void *IPv4ARPDescriptor::getFieldStructPointer(void *object, int field, int i) const
{
    cClassDescriptor *basedesc = getBaseClassDescriptor();
    if (basedesc) {
        if (field < basedesc->getFieldCount(object))
            return basedesc->getFieldStructPointer(object, field, i);
        field -= basedesc->getFieldCount(object);
    }
    IPv4ARP *pp = (IPv4ARP *)object; (void)pp;
    switch (field) {
        default: return NULL;
    }
}

In the presented code you are sending tempIPoIBARPRequest , but you didn't show this variable in code. 在显示的代码中,您正在发送tempIPoIBARPRequest ,但未在代码中显示此变量。 And the packet tempLinkLayerARPRequestFrame , you expect to have 58 bytes is not sent anywhere. 而您希望有58个字节的数据包tempLinkLayerARPRequestFrame不会发送到任何地方。

To check actual size of tempLinkLayerARPRequestFrame try: 要检查tempLinkLayerARPRequestFrame实际大小, tempLinkLayerARPRequestFrame尝试:

EV << "size of tempLinkLayerARPRequestFrame is " << tempLinkLayerARPRequestFrame->getByteLength() << " bytes \n";

It shows that tempLinkLayerARPRequestFrame has size equal to 58 bytes. 它显示tempLinkLayerARPRequestFrame大小等于58个字节。

Core4Inet has a customization of Tkenv for console output. Core4Inet具有针对控制台输出的Tkenv定制。

The cPacket indeed gets encapsulated and the size too gets added. 确实封装了cPacket,并且增加了大小。 Whenever you reference the Core4Inet project it shows both the encapsulated cPacket's size as well as the parent cPacket's size [Just adjacent to it]. 每当您引用Core4Inet项目时,它都会显示封装的cPacket的大小以及父cPacket的大小[仅与其相邻]。 Check the console output properly [ If there is a horizontal scroll bar, then use it ]. 正确检查控制台输出[ 如果有水平滚动条,请使用 ]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM