I am trying to send UDP packets from Network Simulator 3 (NS-3) to a Multi-threaded python server but I am unable to receive the packet on the server side.
Both NS3 and Python Server are running on the same machine.
I am able to ping the server from the NS3 node(device).
The network/setup configuration and code are as follows:
I created a tap interface with IP address 10.0.0.1/255.255.255.0 and gave it to the server. The server is listening on 10.0.0.1/5566. I tested it using Netcat and it's working as expected.
NS3 program creates 2 nodes (10.0.1.3/4). The node 10.0.1.3 behaves as a tap interface while running the emulation. Node 10.0.1.4 is behaving as a client and sends UDP packets. In the Wireshark, I can see the packet has been sent from Client Node in NS3 to the server IP.
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("TAPPacketSend Test");
//
// Packet Send
//
void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
{
NS_LOG_INFO ("SendStuff () called ...");
Ptr<Packet> p = Create<Packet> (reinterpret_cast<uint8_t const *> ("I am long 20 bytes!"), 20);
p->AddPaddingAtEnd (100);
sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
return;
}
int
main (int argc, char *argv[])
{
NS_LOG_INFO ("Ping Emulation Example with TAP");
std::string deviceName ("tap0");
std::string remote ("10.0.0.1"); //Python Server
std::string network ("10.0.1.0");
std::string mask ("255.255.255.0");
std::string pi ("yes");
Ipv4Address remoteIp (remote.c_str ());
Ipv4Address tapNetwork (network.c_str ());
Ipv4Mask tapMask (mask.c_str ());
bool modePi = ( pi == "yes" ? true : false);
GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
NS_LOG_INFO ("Create Node");
Ptr<Node> node = CreateObject<Node> ();
Ipv4AddressHelper addresses;
addresses.SetBase (tapNetwork, tapMask,"0.0.0.3");
Ipv4Address tapIp = addresses.NewAddress ();
NS_LOG_INFO ("Create Device");
TapFdNetDeviceHelper helper;
helper.SetDeviceName (deviceName);
helper.SetModePi (modePi);
helper.SetTapIpv4Address (tapIp);
helper.SetTapIpv4Mask (tapMask);
NetDeviceContainer devices = helper.Install (node);
Ptr<NetDevice> device = devices.Get (0);
NS_LOG_INFO ("Add Internet Stack");
InternetStackHelper internetStackHelper;
internetStackHelper.Install (node);
NS_LOG_INFO ("Create IPv4 Interface");
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
uint32_t interface = ipv4->AddInterface (device);
Ipv4Address devIp = addresses.NewAddress ();
Ipv4InterfaceAddress address = Ipv4InterfaceAddress (devIp, tapMask);
ipv4->AddAddress (interface, address);
ipv4->SetMetric (interface, 1);
ipv4->SetUp (interface);
Ipv4StaticRoutingHelper ipv4RoutingHelper;
Ptr<Ipv4StaticRouting> staticRouting = ipv4RoutingHelper.GetStaticRouting (ipv4);
staticRouting->SetDefaultRoute (tapIp, interface);
Ptr<Socket> srcSocket = Socket::CreateSocket (node, TypeId::LookupByName ("ns3::UdpSocketFactory"));
srcSocket->Bind ();
srcSocket->BindToNetDevice (device);
LogComponentEnableAll (LOG_PREFIX_TIME);
Ipv4Address dstAddr = remoteIp;
uint16_t dstPort = 5566;
for (uint32_t i = 0; i < 20; i++) {
Simulator::Schedule (Seconds (1 + i * 0.5), &SendStuff, srcSocket, dstAddr, dstPort);
}
helper.EnablePcap ("fd-tap-packet", device, true);
NS_LOG_INFO ("Run Emulation.");
Simulator::Stop (Seconds (60.0));
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
}
Is there a reason why can't i receive the packets on pyhton server? is it even possible?