Change remote host address in PacketSocket

215 views
Skip to first unread message

Fábio Oliveira

unread,
Jul 28, 2014, 1:47:53 PM7/28/14
to ns-3-...@googlegroups.com
Hi,

I'm trying to use PacketSocket to send packets in a Application. Firstly, I'm sending a broadcast using the following configurations:

PacketSocketHelper help;
help.Install(this->GetNode());
TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
m_socket = Socket::CreateSocket(this->GetNode(), tid);
m_address.SetSingleDevice(this->GetNode()->GetDevice(0)->GetIfIndex());
m_address.SetPhysicalAddress(m_broadcast);      //  m_broadcast is Mac broadcast address
m_address.SetProtocol(0x8ff0);
if (Mac48Address::IsMatchingType(m_broadcast) == true) {
    m_socket->SetAllowBroadcast(true);
    m_socket->Connect(m_address);
}

At this point in the code, if I write m_socket->Send(packet) it forwards the broadcast correctly.


However, if I change the destination address of the socket to another mac address, the socket ignores this and keeps sending it in broadcast
For example:

m_address.SetSingleDevice(this->GetNode()->GetDevice(0)->GetIfIndex());
m_address.SetPhysicalAddress(remote);    //remote is MAC address of remote host
m_address.SetProtocol(0x8ff0);
if (Mac48Address::IsMatchingType(remote) == true) {
    m_socket->Connect(m_address);
}


I hope you can help me.
Best regards,

Fábio Oliveira

Tommaso Pecorella

unread,
Jul 28, 2014, 3:32:34 PM7/28/14
to ns-3-...@googlegroups.com
Gi,

you have to Close () the socket before using again a Connect (). Else the socket will just throw a (silent) error.

Cheers,
T

Fábio Oliveira

unread,
Jul 28, 2014, 4:25:46 PM7/28/14
to ns-3-...@googlegroups.com
I tried that but after Close the socket, it is no longer available and no more packet is sent.
Have I call some more instructions after Close the socket?

I tried:
m_socket->Close();

m_address.SetSingleDevice(this->GetNode()->GetDevice(0)->GetIfIndex());
m_address.SetPhysicalAddress(remote);    //remote is MAC address of remote host
m_address.SetProtocol(0x8ff0);
if (Mac48Address::IsMatchingType(remote) == true) {
    m_socket->Connect(m_address);
}

I also tried:

TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
m_socket = Socket::CreateSocket(this->GetNode(), tid);
m_address.SetSingleDevice(this->GetNode()->GetDevice(0)->GetIfIndex());
m_address.SetPhysicalAddress(remote);    //remote is MAC address of remote host
m_address.SetProtocol(0x8ff0);
if (Mac48Address::IsMatchingType(remote) == true) {
    m_socket->Connect(m_address);
}

But no more packet is sent.



--
You received this message because you are subscribed to a topic in the Google Groups "ns-3-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ns-3-users/2sJBuHQkWz8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ns-3-users+...@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at http://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/d/optout.

Tommaso Pecorella

unread,
Jul 28, 2014, 4:45:29 PM7/28/14
to ns-3-...@googlegroups.com
Hi,

that's a bug, thanks.

As a temporary fix, please comment out the lines:
  m_shutdownSend = true;
  m_shutdownRecv
= true;
in PacketSocket::Close (void)

A more elaborate and foolproof patch will be made soon™.


Cheers,

T.

Fábio Oliveira

unread,
Jul 28, 2014, 5:07:08 PM7/28/14
to ns-3-...@googlegroups.com
Hi,

I commented that lines but I still can not send packets after Close the socket.

Regards,
Fábio Oliveira

Tommaso Pecorella

unread,
Jul 29, 2014, 1:38:34 PM7/29/14
to ns-3-...@googlegroups.com
Hi Fábio,

mind sending me a test program ?

Thanks,

T.

Tommaso Pecorella

unread,
Jul 29, 2014, 2:30:25 PM7/29/14
to ns-3-...@googlegroups.com
Hi Fábio,

after a check, I found that reopening a closed PacketSocket isn't that obvious. And performing a 2nd Connect isn't obvious either.
It seems that the socket is made to be used, closed and destroyed.

Tbh it's not a big surprise, PacketSocket are really extremely low-level things, and they're not really meant to be used as a general purpose socket.

One possible solution could be to use TWO PacketSockets, but you'll have to use different "Protocols" in the addresses. Plus, the underlying NetDevice will have to respect the protocol, and that's not granted (e.g., lr-wpan doesn't).

If you can't use two PacketSockets, I can try to modify the actual PacketSocket adding a "reset", but it's more or less equivalent to perform a Close () and then using a completely new PacketSocket.
Indeed, this seems to be the "normal" system: Close the old, trash it and create a new one.

Cheers,

T.

PS: note that if you don't Close the old one, bad things will happen.

Fábio Oliveira

unread,
Jul 29, 2014, 4:32:26 PM7/29/14
to ns-3-...@googlegroups.com
Hi Tommaso,

I'm working in creating a Master/Slaves scenario and I'm trying simulate applications in that scenario.
So, firstly the Slaves have to communicate with Master and then Slaves in the topology may have to communicate with each other. Therefore, in each "Slave" I will have to change the remote address in the socket N times.

Based on your information I can have a problem here in my work. What you think is the best solution for my problem?

Regards,
Fábio



Konstantinos

unread,
Jul 29, 2014, 4:34:51 PM7/29/14
to ns-3-...@googlegroups.com
Hi Fabio,

Each time you create a new connection, you could (should ?) open a new socket. So, there is no need to 'change' the remote address of the socket N times, you just need to create N sockets.
To unsubscribe from this group and all its topics, send an email to ns-3-users+unsubscribe@googlegroups.com.

Fábio Oliveira

unread,
Jul 29, 2014, 5:05:36 PM7/29/14
to ns-3-...@googlegroups.com
Hi Konstantinos,

I tried the code below but still no sending packets after create new socket. Am I doing something wrong?

//m_socket->Close();

TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
Ptr<Socket> socket = Socket::CreateSocket(this->GetNode(), tid);
PacketSocketAddress address;
address.SetSingleDevice(this->GetNode()->GetDevice(0)->GetIfIndex());
address.SetPhysicalAddress(remote);
address.SetProtocol(0x8ff0);
socket->Connect(address);
socket->Send(packet);
//socket->Close();

Note that "remote" is correctly defined. I checked that.
m_socket is the first socket used. I tried also with lines uncommented.

Regards,
Fábio


To unsubscribe from this group and all its topics, send an email to ns-3-users+...@googlegroups.com.

Tommaso Pecorella

unread,
Jul 29, 2014, 5:28:08 PM7/29/14
to ns-3-...@googlegroups.com
Hi Fábio,

about this code, I see no error (but I may have missed it). Try enabling the PacketSocket logs and see if there's an error message popping up.

About using multiple sockets... I have an alternative idea, but it does have a drawback.
First a clarification: PacketSockets are really not user-friendly. The SetSingleDevice acts as a send *and* receive BindToNetDevice.
With a "SingleDevice" specified, a PacketSocket will receive stuff ONLY from that device and will send packets ONLY to that device.
Without, the opposite will happen. A packet sent will be sent on ALL the devices at the same time (PacketSockets don't have routing).

Now, if you can live with the *receive* broadcast thing, you can simply one one socket. How ?
int PacketSocket::SendTo (Ptr<Packet> p, uint32_t flags, const Address &address)

Guess what? "address" is the destination address (it must be a PacketSocketAddress). The "flags" field is ignored, set it to zero.

Sometimes it's good to K.I.S.S. (Keep It Simple and Stupid).

Cheers,

T.

PS: Still, I think that closing a PacketSocket shouldn't put it in a dead state. I opened a bug for this.

Hi Fabio,

To unsubscribe from this group and all its topics, send an email to ns-3-users+...@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at http://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/d/optout.

Fábio Oliveira

unread,
Jul 29, 2014, 5:52:18 PM7/29/14
to ns-3-...@googlegroups.com
Hi Tommaso,

It works!! Thank you for all your help. Thanks also to Konstantinos.
PacketSocket is really not user-friendly. This problem was driving me crazy.
Now I can continue my work. Thank you again!

Best regards,
Fábio
Reply all
Reply to author
Forward
0 new messages