[Xorp-hackers] Constructing packets

Victor Faion vfaion at gmail.com
Thu Feb 19 09:34:06 PST 2009


I was trying to construct a simple packet and send it over a socket. I
defined the header like this:

typedef struct bpsfhdr {
  uint8_t   type;
  uint32_t  src;
} bpsfhdr;

Then to construct it and send it I did this just as a test:

vector<uint8_t> data;
data.reserve(sizeof(bpsfhdr));
bpsfhdr hdr;
hdr.type = 42;
hdr.src = ip.addr();
memcpy(&data[0], &hdr, sizeof(bpsfhdr));
socketCl.send_send(dst, sockid, data,
  callback(this, &Neighbour::send_cb));

I didn't really know how to put the data inside the vector. For
example if I use the copy_out() function of the IPv4 class I thought
the IP address would overflow if I just had a uint8_t field in the
struct. Is there a higher level way of adding data into the packets
passed into send_send, for example, adding a variable length string?
Can I have for example a field of type string inside the struct? I
think this is where I am going wrong, but I looked at a few examples
in other XORP processes and it was done in a similar way.

To receive/umarshall the packet in socket4_user_0_1_recv_event I used
the AlignData class:

AlignData<struct bpsfhdr> aligned(pdata);
const struct bpsfhdr* pkt = aligned.payload();
uint8_t type = pkt->type;
IPv4 src(pkt->src);
XLOG_INFO("type = %u src = %s", type, src.str().c_str());

But when I run this I get the following error:

xorp_fea: asyncio.cc:450: void AsyncFileWriter::add_data(const
std::vector<uint8_t, std::allocator<uint8_t> >&, const
ref_ptr<XorpCallback4<void, AsyncFileOperator::Event, const uint8_t*,
size_t, size_t> >&): Assertion `data.size() != 0' failed.
[ 2009/02/19 17:15:58  ERROR xorp_rtrmgr:30765 RTRMGR +754
module_manager.cc done_cb ] Command
"/root/project/xorp1.6/install/fea/xorp_fea": terminated with signal
6; aborted with a core dump.


Cheers,
Victor



More information about the Xorp-hackers mailing list