raw sockets

320 views
Skip to first unread message

Norman

unread,
Dec 6, 2008, 8:01:07 PM12/6/08
to ns-3-users
Hello,
I wonder to know if it is possible to create in ns3 (v3.2.1) a sort of
raw socket? I basically would like to build L3 packet from scratch.
For example:
Ptr<Packet> p = Create<Packet> (1000);
Ipv4Header ipv4hdr;
xHeader x;
ipv4hdr.SetSource (Ipv4Address ("192.168.0.1"));
ipv4hdr.SetDestination (Ipv4Address ("192.168.0.2"));
x.SetSourcePort(1025);
x.SetDestinationPort (9);
p->AddHeader (x);
p->AddHeader (ipv4hdr);

How can I send such a packet?

Thanks,

--
Norman

Gustavo Carneiro

unread,
Dec 7, 2008, 12:29:42 PM12/7/08
to ns-3-...@googlegroups.com


2008/12/7 Norman <baz.n...@yahoo.com>

Two ways you can send the packet:

  1. Directly call netdevice->Send (packet, nextHopMacAddress, 0x0800);

  2. Use a "packet socket" (see PacketSocketFactory).

This assumes you don't need the IPv4 routing, i.e. you already know the outgoing netdevice and next hop MAC address.
 
--
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert

Norman

unread,
Dec 7, 2008, 1:58:16 PM12/7/08
to ns-3-users
> Two ways you can send the packet:
>
> 1. Directly call netdevice->Send (packet, nextHopMacAddress, 0x0800);
>
> 2. Use a "packet socket" (see PacketSocketFactory).
>
> This assumes you don't need the IPv4 routing, i.e. you already know the
> outgoing netdevice and next hop MAC address.

Hi Gustavo,
Thanks for your replay.

I have to admit that I've tried before Send method from NetDevice
but had some problems with ARP, I will try once again!
Using a PacketSocketFactory is new for me, I'm going to investigate
this way right now!
Thanks for that!

Beside those those approaches your kindly described I believe that
there is a 3rd one
i.e. a Send method from IPv4L3Protocol object, but it doesn't work for
me for some reasons.

Example:
Ptr<Ipv4L3Protocol> ipv4;
Ptr<Packet> p = Create<Packet> (1000);
Ipv4Header ipv4hdr;
xHeader x;
ipv4hdr.SetSource (Ipv4Address ("1.0.1.73"));
ipv4hdr.SetDestination (Ipv4Address ("5.2.5.1"));
x.SetSourcePort(9);
x.SetDestinationPort (9);
p->AddHeader (x);
p->AddHeader (ipv4hdr);

ipv4 = node->GetObject<Ipv4L3Protocol>();
Ipv4Address s("1.0.1.73");
Ipv4Address d("5.2.5.1");
ipv4->Send(p, s, d, MYPROTID);

However this doesn't work (based on debugging messages), the other
side receives a sort of malformed packet i.e. dport is set to 20
instead of 9:

2168000ns Ipv4EndPointDemux:Lookup(0x8073a38, 5.2.5.1, 20, 1.0.1.73,
17664, 0x80760d0)
2168000ns Ipv4EndPointDemux:Lookup(): Looking up endpoint for
destination address 5.2.5.1
2168000ns Ipv4EndPointDemux:Lookup(): Looking at endpoint dport=9
daddr=0.0.0.0 sport=0 saddr=0.0.0.0
2168000ns Ipv4EndPointDemux:Lookup(): Skipping endpoint 0xbffd51e0
because endpoint dport 9 does not match packet dport 20

Do you have any idea what is going wrong?
[ My protocol "x" is based on UDP (the only difference so far is a
protocol number),
I didn't touch anything else yet, so "SetDestinationPort" method
should work ]

Thanks,

--
Norman

Gustavo Carneiro

unread,
Dec 7, 2008, 2:10:40 PM12/7/08
to ns-3-...@googlegroups.com


2008/12/7 Norman <baz.n...@yahoo.com>

Probably you just don't have any socket in the receiving node listening in the correct port?

Anyway this would not work.  If you check the source you will notice that Ipv4L3Protocol::Send adds another IPv4 header.  So you end up with two consecutive IP headers.
 


Do you have any idea what is going wrong?
[ My protocol "x" is based on UDP (the only difference so far is a
protocol number),
I didn't touch anything else yet, so "SetDestinationPort" method
should work ]

Thanks,

--
Norman



Norman

unread,
Dec 7, 2008, 2:23:49 PM12/7/08
to ns-3-users

> Anyway this would not work. If you check the source you will notice that
> Ipv4L3Protocol::Send adds another IPv4 header. So you end up with two
> consecutive IP headers.

That was the issue!!!! Once I commented out:
//p->AddHeader (ipv4hdr);
it works right now!

However I need to be able to define my customized IP header or more
precisely
add one option, so I guess there is not escape from NetDevice->Send
() ;-)

btw
What do you exactly mean by SocketFactory (method nr 2)?
Is it possible to create a sort of PF_PACKET?

Thanks,

--
Norman

Providence SALUMU MUNGA

unread,
Dec 8, 2008, 3:55:13 AM12/8/08
to ns-3-...@googlegroups.com, baz.n...@yahoo.com

Hi,

Le Sun, 7 Dec 2008 11:23:49 -0800 (PST),
Norman <baz.n...@yahoo.com> a écrit :

> btw
> What do you exactly mean by SocketFactory (method nr 2)?
> Is it possible to create a sort of PF_PACKET?

You need to make use of PacketSocketFactory class.

Attached is simple example that shows the use of PF_PACKET like messages
in a wifi environment.
You can also find it in examples/wifi-packet-socket.cc from
http://code.nsnam.org/salumu/ns-3-mih (./waf --run wifi-packet-socket).

Hope it will help you start over more complex examples.

Regards,

--
Providence SALUMU M.

Doctorant, Département Logiciels-Réseaux
TELECOM SudParis
9 rue Charles Fourier, 91011 Evry Cedex

Tel: +33 1 60 76 44 65
Mob: +33 6 84 54 94 85
Fax: +33 1 60 76 47 11
http://www-lor.int-evry.fr/

"If you can't be strong, be clever, and make peace with someone who is
strong. But it's always better to be strong yourself! Always!" -- B.O.
LowLow.

wifi-packet-socket.cc

Mathieu Lacage

unread,
Dec 8, 2008, 4:55:53 AM12/8/08
to ns-3-...@googlegroups.com, baz.n...@yahoo.com
On Mon, 2008-12-08 at 09:55 +0100, Providence SALUMU MUNGA wrote:
> Hi,
>
> Le Sun, 7 Dec 2008 11:23:49 -0800 (PST),
> Norman <baz.n...@yahoo.com> a écrit :
>
> > btw
> > What do you exactly mean by SocketFactory (method nr 2)?
> > Is it possible to create a sort of PF_PACKET?
>
> You need to make use of PacketSocketFactory class.
>
> Attached is simple example that shows the use of PF_PACKET like messages
> in a wifi environment.
> You can also find it in examples/wifi-packet-socket.cc from
> http://code.nsnam.org/salumu/ns-3-mih (./waf --run wifi-packet-socket).
>
> Hope it will help you start over more complex examples.

I should point out that the latest version of ns-3 also allows you to
create ip raw sockets (as opposed to packet raw sockets):

Ptr<Socket> socket = Socket::CreateSocket ("ns3::Ipv4RawSocketFactory");

These should behave just the same as a normal linux-style raw socket.
That is, you can use them to receive ipv4 traffic and send ipv4 traffic
and the ip stack will use arp if needed. The exact raw socket logic is
located in src/internet-stack/ipv4-raw-socket-impl.cc

best regards,
Mathieu

Providence SALUMU MUNGA

unread,
Dec 8, 2008, 12:18:52 PM12/8/08
to ns-3-...@googlegroups.com
Le Mon, 08 Dec 2008 10:55:53 +0100,
Mathieu Lacage <mathieu...@sophia.inria.fr> a écrit :

> I should point out that the latest version of ns-3 also allows you to
> create ip raw sockets (as opposed to packet raw sockets):
>
> Ptr<Socket> socket = Socket::CreateSocket
> ("ns3::Ipv4RawSocketFactory");

Thank you Mathieu for point out this new one, it is much easier indeed.

--
Providence SALUMU M.

Doctorant, Département Logiciels-Réseaux
TELECOM SudParis

"If you can't be strong, be clever, and make peace with someone who is

Norman

unread,
Dec 8, 2008, 5:50:04 PM12/8/08
to ns-3-users
Thanks for your suggestion!

--
Norman

Norman

unread,
Dec 8, 2008, 5:51:02 PM12/8/08
to ns-3-users
Hi Mathieu,

Raw IP sockets sounds great!;-)
I'm going to test it!

Many Thanks,

--
Norman

Reply all
Reply to author
Forward
0 new messages