Implement Simple TCP with NS3 - ns-3

hi,
I'm new to NS3, I'm trying to modify first.cc to do TCP rather than UDP.
I didn't get any error, but no data exchange happened as well.
Could anyone help me on this?˜˜
many thanks˜~
here is the code: in the main:
NS_LOG_INFO ("Creating Topology");
Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));
NodeContainer nodes;
nodes.Create (2);
//
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("100Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("20ms"));
//
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("109.11.12.0", "255.255.255.0");//address setting
Ipv4InterfaceContainer interfaces = address.Assign (devices);
OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address
(InetSocketAddress (Ipv4Address ("255.255.255.0"), 10)));
onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onOffHelper.SetAttribute ("DataRate",StringValue ("2Mbps"));
onOffHelper.SetAttribute ("PacketSize",UintegerValue(2000));
ApplicationContainer app = onOffHelper.Install (nodes.Get (0));
// Start the application
app.Start (Seconds (10.0));
app.Stop (Seconds (100.0));
// Create an optional packet sink to receive packets
PacketSinkHelper sink ("ns3::TcpSocketFactory",Address
(InetSocketAddress (Ipv4Address::GetAny (), 10)));
app = sink.Install (nodes.Get(1));
app.Start (Seconds (1.0));
app.Stop (Seconds (100.0));
pointToPoint.EnablePcapAll ("testtcp");

ok,I think it is solved now, first, the IP was wrong at onoff helper, they should be the same as Ipv4AddressHelper.
then the app start time is wrong, and the config onoff application code are not needed.
here is the code now: it might not be very right, but at least I can read result from it now.
NodeContainer nodes;
nodes.Create (2); //creat 2 nodes they are p2p
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("2Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("20ms"));
NetDeviceContainer devices;// put nodes in ndc
devices = pointToPoint.Install (nodes);
////give them an address
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("109.11.12.0", "255.255.255.0");//address setting
Ipv4InterfaceContainer interfaces = address.Assign (devices);
//sink for reciever????
PacketSinkHelper sink ("ns3::TcpSocketFactory",Address
(InetSocketAddress (Ipv4Address::GetAny (), 10)));
//set a node as reciever
ApplicationContainer app = sink.Install (nodes.Get(0));
app.Start (Seconds (1.0));
app.Stop (Seconds (10.0));
OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address
(InetSocketAddress (Ipv4Address ("109.11.12.1"), 10)));
onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onOffHelper.SetAttribute ("DataRate",StringValue ("2Mbps"));
onOffHelper.SetAttribute ("PacketSize",UintegerValue(1280));
// ApplicationContainer
app = onOffHelper.Install (nodes.Get (1));
// Start the application
app.Start (Seconds (1.0));
app.Stop (Seconds (10.0));
pointToPoint.EnablePcapAll ("testtcp");
Simulator::Run ();

Are you really trying to send TCP traffic to ip address 255.255.255.0 ? That is unlikely to work ever. Maybe you should try instead 109.11.12.1

Actually TCP is not meant to support Broadcasting that is why some protocol opt to use UDP rather that TCP, to overcome such problem . For instance, Bootp which makes its own way on UDP since the initial state of the protocol is not supported with TCP.

Related

STM32 - Can't receive msg from multicast address

I have a client/server LWIP program, I want to use multicast features so I used IGMP library did the following setting like this:
Setting .IOC
Enable LWIP_IGMP
Enable LWIP_MULTICAST_TX_OPTION
Enable LWIP_IP4
in ethernet.c
netif->flags |= NETIF_FLAG_IGMP
in stm32f7xx_hal_eth.c (ETH_MACDMAConfig)
macinit.PromiscuousMode = ETH_PROMISCUOUS_MODE_ENABLE;
macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_NONE;
and i implemented code like this :
void UDP_Multicast_init(void *arg)
{
struct ip4_addr ipgroup, localIP;
struct udp_pcb *upcb;
char msg[] = "hello";
struct pbuf* p;
p = pbuf_alloc(PBUF_TRANSPORT,sizeof(msg),PBUF_RAM);
memcpy (p->payload, msg, sizeof(msg));
IP4_ADDR(&ipgroup, 224, 224, 0, 1); //Multicast IP address.
IP4_ADDR(&localIP, 192, 168, 1, 99); //Interface IP address
#if LWIP_IGMP
igmp_joingroup((ip4_addr_t *)(&localIP),(ip4_addr_t *)(&ipgroup));
#endif
upcb = ( struct udp_pcb*) udp_new();
MulticastStart(); //binding connection to the port 10
udp_recv(upcb, UDP_callback, NULL);
udp_sendto(upcb,p,&ipgroup,10);
}
void UDP_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port)
{
printf("test");
}
and I try to see in wireshark, iplocal successfully joins into the multicast address, and sends a data to the multicast address.
enter image description here
but the callback function cannot be executed.
is there something missing??
thanks for the response.

How to receive data over Ethernet using LWIP, UDP

I'm trying to send data to and from my computer and an STM32H745 over Ethernet using LwIP and UDP. I have successfully configured the card and right now I can send data from the card to a Python script running on the computer. However, I don't understand how udp_recv works <udp,lwip> or how to receive data with UDP on LwIP in general, and I can't find examples that do just that. Where is the data being received? Should I even use udp_recv?
In the main loop I run MX_LWIP_Process, which runs ethernetif_input which somehow handles the received data, but I don't understand where it puts it.
Below is the main code, just for reference.
const char* message = "a";
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_1); // orange
ip_addr_t PC_IPADDR;
IP_ADDR4(&PC_IPADDR, 192, 168, 1, 200);
u16_t port = 8000;
struct udp_pcb* my_udp = udp_new();
struct pbuf* udp_buffer = NULL;
/* Infinite loop */
for (;; )
{
MX_LWIP_Process();
HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_1); // orange
HAL_Delay(1000);
udp_buffer = pbuf_alloc(PBUF_TRANSPORT, strlen(message), PBUF_RAM);
if (udp_buffer != NULL)
{
memcpy(udp_buffer->payload, message, strlen(message));
udp_sendto(my_udp, udp_buffer,&PC_IPADDR, port);
pbuf_free(udp_buffer);
}
//udp_recv (struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
}
udp_recv() does not actually receive UDP datagrams (despite its name). It registers a callback function that will then be called by MX_LWIP_Process() when a datagram has been buffered. It would better be called udp_set_recv_callback(), but it is what it is.
To that end you should call it once before your executive loop:
udp_bind( my_udp, IP_ADDR_ANY, port ) ;
udp_recv( my_udp, udp_receive_callback, NULL ) ;
/* Infinite loop */
for (;; )
{
// Run the CubeMX LwIP stack
MX_LWIP_Process() ;
...
}
Where udp_receive_callback is a function that will be invoked on receipt of a datagram:
void udp_receive_callback( void* arg, // User argument - udp_recv `arg` parameter
struct udp_pcb* upcb, // Receiving Protocol Control Block
struct pbuf* p, // Pointer to Datagram
const ip_addr_t* addr, // Address of sender
u16_t port ) // Sender port
{
// Process datagram here (non-blocking code)
...
// Must free receive pbuf before return
pbuf_free(p);
}
Examples include:
https://gist.github.com/iwanbk/1399729
https://github.com/STMicroelectronics/STM32CubeF2/blob/master/Projects/STM322xG_EVAL/Applications/LwIP/LwIP_UDP_Echo_Client/Src/udp_echoclient.c
Documentation can be found at https://www.nongnu.org/lwip/2_0_x/group__udp__raw.html

How do I simulate swarm movement of drones with ns-3?

First of all, I'm still an absolute beginner in ns-3. I am trying to simulate swarm movement of drones using ns-3. I've gotten some source codes/tutorial from youtube which is partly what I want. So far, the codes are simulating FANET(Flying Ad Hoc Network) together with a 3D Gauss Markov Mobility model. I'm thinking that maybe I can add to these codes to also simulate swarm movement together.
Problem is I don't know where to begin and also have not been able to find much resources online. Could someone maybe give some ideas as to what I should do or maybe provide relevant resources? I will attach the codes that I have right now.
Reference: https://www.nsnam.com/2020/11/flying-adhoc-network-simulation-fanet.html
#include "ns3/point-to-point-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include <fstream>
#include <string>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wifi-module.h"
#include "ns3/aodv-helper.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("Mob");
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer c;
c.Create (20); //20 wireless nodes
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
//80211a, 80211b, 80211n, 2.4g and 5G, 80211ac, 80211ax is also supported.80211p (VANETs, WAVE)
WifiMacHelper mac;
mac.SetType ("ns3::AdhocWifiMac");
//AdhocWifiMac, StaWifiMac, ApWifiMac
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue ("OfdmRate54Mbps"));
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");
wifiPhy.SetChannel (wifiChannel.Create ());
NetDeviceContainer cDevices = wifi.Install (wifiPhy, mac, c);
//
NS_LOG_INFO ("Enabling AODV routing on all backbone nodes");
AodvHelper aodv;
//AODV protocol is being using FANETs.
InternetStackHelper internet;
internet.SetRoutingHelper (aodv); // has effect on the next Install ()
internet.Install (c);
//
// Assign IPv4 addresses to the device drivers (actually to the associated
// IPv4 interfaces) we just created.
//
Ipv4AddressHelper ipAddrs;
ipAddrs.SetBase ("192.168.0.0", "255.255.255.0");
Ipv4InterfaceContainer cInterfaces;
cInterfaces=ipAddrs.Assign (cDevices);
//Mobility Model -3D
MobilityHelper mobility;
mobility.SetMobilityModel ("ns3::GaussMarkovMobilityModel",
"Bounds", BoxValue (Box (0, 100, 0, 100, 0, 100)),
"TimeStep", TimeValue (Seconds (0.5)),
"Alpha", DoubleValue (0.85),
"MeanVelocity", StringValue ("ns3::UniformRandomVariable[Min=800|Max=1200]"),
"MeanDirection", StringValue ("ns3::UniformRandomVariable[Min=0|Max=6.283185307]"),
"MeanPitch", StringValue ("ns3::UniformRandomVariable[Min=0.05|Max=0.05]"),
"NormalVelocity", StringValue ("ns3::NormalRandomVariable[Mean=0.0|Variance=0.0|Bound=0.0]"),
"NormalDirection", StringValue ("ns3::NormalRandomVariable[Mean=0.0|Variance=0.2|Bound=0.4]"),
"NormalPitch", StringValue ("ns3::NormalRandomVariable[Mean=0.0|Variance=0.02|Bound=0.04]"));
mobility.SetPositionAllocator ("ns3::RandomBoxPositionAllocator",
"X", StringValue ("ns3::UniformRandomVariable[Min=0|Max=100]"),
"Y", StringValue ("ns3::UniformRandomVariable[Min=0|Max=100]"),
"Z", StringValue ("ns3::UniformRandomVariable[Min=0|Max=100]"));
mobility.Install (c);
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (c.Get(0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (cInterfaces.GetAddress(0), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (c.Get(1));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
wifiPhy.EnablePcapAll ("Fanet3D"); //Packet Capture.
//Network Animation using NetAnim.
AnimationInterface anim("Fanet3D.xml");
//Ascii Trace Metrics can be processed using Tracemetrics Software.
AsciiTraceHelper ascii;
wifiPhy.EnableAsciiAll(ascii.CreateFileStream("Fanet3D.tr"));
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}

In UDP P2P protocol the server instance is not responding

The code is mainly trying to create two instances of server on two different ports and two instances of client also. Each client is having a p2p connection with a dedicated server. No concurrent communication is considered. The problem in the code is the server nodes are not responding. I am a beginner in ns3. The question may be very trivial. Please suggest me ways to solve the problem. Also I have used Flow Monitor to measure the throughput.
int main(int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse(argc, argv);
Time::SetResolution(Time::NS);
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes1,nodes2;
nodes1.Create(2);
nodes2.Create(2);
PointToPointHelper pointToPoint1,pointToPoint2;
pointToPoint1.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
pointToPoint1.SetChannelAttribute("Delay", StringValue("2ms"));
pointToPoint2.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
pointToPoint2.SetChannelAttribute("Delay", StringValue("2ms"));
NetDeviceContainer devices1,devices2;
devices1 = pointToPoint1.Install(nodes1);
devices2 = pointToPoint2.Install(nodes2);
InternetStackHelper stack1,stack2;
stack1.Install(nodes1);//
stack2.Install(nodes2);
Ipv4AddressHelper address1;
address1.SetBase("10.1.1.0", "255.255.255.0");
Ipv4AddressHelper address2;
address2.SetBase("10.5.4.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces1 = address1.Assign(devices1);
Ipv4InterfaceContainer interfaces2 = address2.Assign(devices2);
UdpEchoServerHelper echoServer1(8889),echoServer2(8850);
ApplicationContainer serverApps1 = echoServer1.Install(nodes1.Get(0));
ApplicationContainer serverApps2 = echoServer2.Install(nodes2.Get(0));
serverApps1.Start(Seconds(1.0));
serverApps1.Stop(Seconds(10.0));
serverApps2.Start(Seconds(1.0));
serverApps2.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient1(interfaces1.GetAddress(1), 8889),echoClient2(interfaces2.GetAddress(1), 8850);
echoClient1.SetAttribute("MaxPackets", UintegerValue(1));
echoClient1.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient1.SetAttribute("PacketSize", UintegerValue(1024));
echoClient2.SetAttribute("MaxPackets", UintegerValue(1));
echoClient2.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient2.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer clientApps1 = echoClient1.Install (nodes1.Get (1));
clientApps1.Start (Seconds (2.0));
clientApps1.Stop (Seconds (10.0));
ApplicationContainer clientApps2 = echoClient2.Install (nodes2.Get (1));
clientApps2.Start (Seconds (2.0));
clientApps2.Stop (Seconds (10.0));
FlowMonitorHelper flowmon ;
Ptr<FlowMonitor> monitor = flowmon.InstallAll( ) ;
pointToPoint1.EnablePcapAll("prog1");
pointToPoint2.EnablePcapAll("prog2");
Simulator::Stop(Seconds(50.0) ) ;
Simulator::Run();
monitor->CheckForLostPackets() ;
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier()) ;
std :: map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ( ) ;
for (std ::map<FlowId,FlowMonitor::FlowStats >::const_iterator iter = stats.begin();iter!= stats.end( );++iter) //map iterator
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow ( iter->first ) ;
if ((t.sourceAddress==Ipv4Address ("10.1.1.1") && t.destinationAddress==Ipv4Address("10.1.1.2")))
{
std:: cout<<"Flow ID : " <<iter->first<<"source address" << t.sourceAddress<<"destination address"<<t.destinationAddress<<"\n" ;
std :: cout<<"Transmitted Packets ="<<iter->second.txPackets<<"\n" ;
std :: cout<<"Received Packets = "<<iter->second.rxPackets<<"\n" ;
std :: cout<<"Throughput is :" <<iter->second.rxBytes*8.0/(iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds())/1024/1024<<"Mbps.\n" ;
}
}
Simulator::Destroy ();
return 0;
}

(UDP) GVCP BroadCast not functioning

Environment: Windows Socket Programming using VC++ 2010
GVCP : GigE Vision Control Protocol
GVCP = UDP+(GVCP Header Data+Payload Data). so basically on top its a UDP only
for Detecting GigE Sensor (Camera) need to first Broadcast a GVCP packet (containing Gvcp Payload data), using Broadcast address 255.255.255.255
but i am able to broadcast only by 192.168.1.255 (as seen on wire-shark) when i change broadcast address 255.255.255.255 nothing is visible on wire-shark nor on other machine
so problem is not able to broadcast using IP 255.255.255.255 using UDP/WinSock
able to start broadcasting the GVCP packet its just a socket creation error the correct one is below
//---------------------DATA SENDER------------------------------
struct sockaddr_in Sender_addr;
int Sender_addrlen = sizeof(Sender_addr);
Sender_addr.sin_family = AF_INET;
Sender_addr.sin_port = htons(CAMPORT); //BROADCAST_PORT);
Sender_addr.sin_addr.s_addr = inet_addr("255.255.255.255"); //Broadcast
IP Here");
//---------------------DATA RECEIVER----------------------------
struct sockaddr_in Recv_addr;
int Recv_addrlen = sizeof(Recv_addr);
Recv_addr.sin_family = AF_INET;
Recv_addr.sin_port = htons(PCPORT);
Recv_addr.sin_addr.s_addr = INADDR_ANY;
if(bind(sock,(sockaddr*)&Recv_addr,sizeof(Recv_addr))<0)
{
perror("bind");
_getch;
closesocket(sock);
}
//and then send command for GVCP packet (GVCP packet Structure is )
TxBuff[0] = 0x42;
TxBuff[1] = 0x01;
TxBuff[2] = 0x00;
TxBuff[3] = 0x02;
TxBuff[4] = 0x00;
TxBuff[5] = 0x00;
TxBuff[6] = 0x00;
TxBuff[7] = 0x02;
if(sendto(sock,TxBuff,TxBuffSize,0,(struct sockaddr
*)&Sender_addr,sizeof(Sender_addr)) <0)
{
perror("send: error ");
_getch();
closesocket(sock);
}