gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 22:52:46 25 4
gpt4 key购买 nike

我为自己的仿真模型编写了一个函数来创建和发送 ARP 请求。在下面的代码中,我已经成功创建了 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;

但我看到传输的帧大小为 28 字节,而不是 28 + 30 = 58 字节。为什么会发生这种情况,或者封装函数没有附加字节?

已更新

IPv4ARP 消息文件是:

NED 文件:

packet IPv4ARP {
string SourceIP;
string DestinationIP;
}

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

//
// 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;
}
}

最佳答案

Core4Inet 为控制台输出定制了 Tkenv

cPacket 确实被封装了,大小也被添加了。每当您引用 Core4Inet 项目时,它都会显示封装的 cPacket 的大小以及父 cPacket 的大小 [就在它旁边]。正确检查控制台输出[如果有水平滚动条,则使用它]。

关于c++ - addByteLength() 不添加字节以在 omnet++ 中发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35918330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com