Set custom payload to packet object in python

197 views
Skip to first unread message

Sakaki Mirai

unread,
Aug 29, 2018, 3:39:06 AM8/29/18
to ns-3-users
Hi all,

I'm sorry English is not my first language, so my English is broken.

I'm writing a simulation script in python and I want to set custom payload to ns.network.Packet object.

In C++, we can achieve it by this code.

Ptr<Packet> p = Create<Packet> (buffer, size);

but in python, I couldn't find any method like this.

I could create zero-filled payload by this code.

from ns.network import Packet
packet = Packet(1024)

I want to set custom payload in this packet object.

Any advice and suggestions will be greatly appriciated. Please help me.

Sakaki Mirai

unread,
Sep 1, 2018, 11:34:42 AM9/1/18
to ns-3-users
After few days investigation, I resolved the problem.

First, I had to enable Python API Scanning Support (./waf --apiscan=all).

To do so, I rebuild ns-3.

Here is Dockerfile to set up the build environment.

FROM ubuntu:16.04

# Install requirements
RUN apt-get update
RUN apt-get -y install gcc g++ python python-dev python-pip python-setuptools mercurial git \
    openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar \
    gdb valgrind dia uncrustify flex bison libfl-dev tcpdump sqlite sqlite3 libsqlite3-dev \
    libxml2 libxml2-dev cmake libc6-dev libc6-dev-i386 libclang-dev \
    libgtk2.0-0 libgtk2.0-dev vtun lxc libboost-signals-dev libboost-filesystem-dev \
    llvm-3.9 clang-3.9 llvm-3.9-dev clang-3.9-dev
RUN pip install -U pip && \
    python -m pip install wheel && \
    python -m pip install cxxfilt sphinx pygccxml

# Build CastXML
RUN mkdir -p /repos/CastXML-build
WORKDIR /repos
WORKDIR /repos/CastXML-build
RUN cmake ../CastXML
RUN make
RUN make install

# Install PyBindGen
WORKDIR /repos
WORKDIR /repos/pybindgen
RUN python setup.py install

# Build ns-3
ADD ns-allinone-3.27-edited /ns-allinone-3.27
WORKDIR /ns-allinone-3.27/ns-3.27
RUN ./waf configure
RUN ./waf build
RUN ./waf --apiscan=network --no32bit-scan

CMD ./waf shell


Second, I edited ns-2.37/src/network/paket.cc and its header.

(I'm sorry I'm not familiar to C++ so my code may be really bad...)

packet.cc

$ diff ns-allinone-3.27-edited/ns-3.27/src/network/model/packet.cc ns-allinone-3.27/ns-3.27/src/network/model/packet.cc
197,210d196
< Packet::Packet (const char *str, uint32_t size)
<   : m_buffer (),
<     m_byteTagList (),
<     m_packetTagList (),
<     m_metadata (static_cast<uint64_t> (Simulator::GetSystemId ()) << 32 | m_globalUid, size),
<     m_nixVector (0)
< {
<   m_globalUid++;
<   m_buffer.AddAtStart (size);
<   Buffer::Iterator i = m_buffer.Begin ();
<   uint8_t *buffer = (uint8_t*) str;
<   i.Write (buffer, size);
< }
<
374,382d359
< std::string
< Packet::GetString () const
< {
<   uint8_t *buffer = new uint8_t[m_buffer.GetSize()];
<   m_buffer.CopyData (buffer, m_buffer.GetSize());
<   std::string data = std::string((char*)buffer);
<   return data;
< }
<

packet.h

$ diff ns-allinone-3.27-edited/ns-3.27/src/network/model/packet.h ns-allinone-3.27/ns-3.27/src/network/model/packet.h
287,288d286
<
<   Packet (const char *str, uint32_t size);
422,423d419
<   std::string GetString () const;
<

Then, I executed ./waf --apiscan=network --no32bit-scan

Finally, I could set and get packet contents.

# ./waf shell
# python
>>> from ns.network import Packet
>>> packet = Packet("aaa", 1024)
>>> packet.GetString()
'aaa'

Maybe my workaround has many problems but I want success pattern.

Any advice for this workaround is welcome.

I was helped a lot by many posts in this group. Many thanks for this community members.


2018年8月29日水曜日 16時39分06秒 UTC+9 Sakaki Mirai:
Reply all
Reply to author
Forward
0 new messages