From 77ca76e8e997dc80ed246e2cedaabf35013eb7dd Mon Sep 17 00:00:00 2001 From: Craig Dowell Date: Wed, 12 Sep 2007 15:23:25 -0700 Subject: [PATCH] start of udp echo applications --HG-- rename : src/applications/onoff-application.cc => src/applications/onoff/onoff-application.cc rename : src/applications/onoff-application.h => src/applications/onoff/onoff-application.h rename : src/applications/packet-sink.cc => src/applications/packet-sink/packet-sink.cc rename : src/applications/packet-sink.h => src/applications/packet-sink/packet-sink.h --- examples/udp-echo.cc | 200 ++++++++++++++++++ examples/wscript | 4 + samples/wscript | 2 +- .../{ => onoff}/onoff-application.cc | 0 .../{ => onoff}/onoff-application.h | 0 src/applications/{ => onoff}/wscript | 8 +- .../{ => packet-sink}/packet-sink.cc | 0 .../{ => packet-sink}/packet-sink.h | 0 src/applications/packet-sink/wscript | 12 ++ src/applications/udp-echo/udp-echo-client.cc | 192 +++++++++++++++++ src/applications/udp-echo/udp-echo-client.h | 70 ++++++ src/applications/udp-echo/wscript | 12 ++ src/applications/waf | 1 - src/internet-node/wscript | 2 +- src/wscript | 4 +- 15 files changed, 498 insertions(+), 9 deletions(-) create mode 100644 examples/udp-echo.cc rename src/applications/{ => onoff}/onoff-application.cc (100%) rename src/applications/{ => onoff}/onoff-application.h (100%) rename src/applications/{ => onoff}/wscript (66%) rename src/applications/{ => packet-sink}/packet-sink.cc (100%) rename src/applications/{ => packet-sink}/packet-sink.h (100%) create mode 100644 src/applications/packet-sink/wscript create mode 100644 src/applications/udp-echo/udp-echo-client.cc create mode 100644 src/applications/udp-echo/udp-echo-client.h create mode 100644 src/applications/udp-echo/wscript delete mode 100755 src/applications/waf diff --git a/examples/udp-echo.cc b/examples/udp-echo.cc new file mode 100644 index 000000000..5a0280695 --- /dev/null +++ b/examples/udp-echo.cc @@ -0,0 +1,200 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +// Network topology +// +// n0 n1 n2 n3 +// | | | | +// ================= +// LAN +// +// - CBR/UDP flows from n0 to n1 and from n3 to n0 +// - DropTail queues +// - Tracing of queues and packet receptions to file "csma-one-subnet.tr" + +#include "ns3/command-line.h" +#include "ns3/default-value.h" +#include "ns3/ptr.h" +#include "ns3/debug.h" +#include "ns3/simulator.h" +#include "ns3/nstime.h" +#include "ns3/data-rate.h" +#include "ns3/ascii-trace.h" +#include "ns3/pcap-trace.h" +#include "ns3/internet-node.h" +#include "ns3/csma-channel.h" +#include "ns3/csma-net-device.h" +#include "ns3/csma-topology.h" +#include "ns3/csma-ipv4-topology.h" +#include "ns3/eui48-address.h" +#include "ns3/ipv4-address.h" +#include "ns3/inet-socket-address.h" +#include "ns3/ipv4.h" +#include "ns3/socket.h" +#include "ns3/ipv4-route.h" +#include "ns3/udp-echo-client.h" + +using namespace ns3; + +NS_DEBUG_COMPONENT_DEFINE ("UdpEcho"); + +int +main (int argc, char *argv[]) +{ +// +// Users may find it convenient to turn on explicit debugging +// for selected modules; the below lines suggest how to do this +// +#if 1 + DebugComponentEnable("CsmaOneSubnet"); + + DebugComponentEnable("Object"); + DebugComponentEnable("Queue"); + DebugComponentEnable("DropTailQueue"); + DebugComponentEnable("Channel"); + DebugComponentEnable("CsmaChannel"); + DebugComponentEnable("CsmaNetDevice"); + DebugComponentEnable("Ipv4L3Protocol"); + DebugComponentEnable("NetDevice"); + DebugComponentEnable("PacketSocket"); + DebugComponentEnable("OnOffApplication"); + DebugComponentEnable("UdpSocket"); + DebugComponentEnable("UdpL4Protocol"); + DebugComponentEnable("Ipv4L3Protocol"); + DebugComponentEnable("Ipv4StaticRouting"); + DebugComponentEnable("Ipv4Interface"); + DebugComponentEnable("ArpIpv4Interface"); + DebugComponentEnable("Ipv4LoopbackInterface"); + DebugComponentEnable("UdpEchoClient"); +#endif +// +// Set up default values for the simulation. Use the DefaultValue::Bind() +// technique to tell the system what subclass of Queue to use. The Bind +// command command tells the queue factory which class to instantiate when the +// queue factory is invoked in the topology code +// + DefaultValue::Bind ("Queue", "DropTailQueue"); +// +// Allow the user to override any of the defaults and the above Bind() at +// run-time, via command-line arguments +// + CommandLine::Parse (argc, argv); +// +// Explicitly create the nodes required by the topology (shown above). +// + NS_DEBUG("Create nodes."); + Ptr n0 = Create (); + Ptr n1 = Create (); + Ptr n2 = Create (); + Ptr n3 = Create (); + + NS_DEBUG("Create channels."); +// +// Explicitly create the channels required by the topology (shown above). +// + Ptr lan = CsmaTopology::CreateCsmaChannel( + DataRate(5000000), MilliSeconds(2)); + + NS_DEBUG("Build Topology."); +// +// Now fill out the topology by creating the net devices required to connect +// the nodes to the channels and hooking them up. AddIpv4CsmaNetDevice will +// create a net device, add a MAC address (in memory of the pink flamingo) and +// connect the net device to a nodes and also to a channel. the +// AddIpv4CsmaNetDevice method returns a net device index for the net device +// created on the node. Interpret nd0 as the net device we created for node +// zero. +// + uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, + Eui48Address("08:00:2e:00:00:00")); + + uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, + Eui48Address("08:00:2e:00:00:01")); + + uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, + Eui48Address("08:00:2e:00:00:02")); + + uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, + Eui48Address("08:00:2e:00:00:03")); + + NS_DEBUG ("nd0 = " << nd0); + NS_DEBUG ("nd1 = " << nd1); + NS_DEBUG ("nd2 = " << nd2); + NS_DEBUG ("nd3 = " << nd3); +// +// We've got the "hardware" in place. Now we need to add IP addresses. +// + NS_DEBUG("Assign IP Addresses."); +// +// XXX BUGBUG +// Need a better way to get the interface index. The point-to-point topology +// as implemented can't return the index since it creates interfaces on both +// sides (i.e., it does AddIpv4Addresses, not AddIpv4Address). We need a +// method on Ipv4 to find the interface index corresponding to a given ipv4 +// address. +// +// Assign IP addresses to the net devices and associated interfaces +// on the lan. The AddIpv4Address method returns an Ipv4 interface index +// which we do not need here. +// + CsmaIpv4Topology::AddIpv4Address (n0, nd0, Ipv4Address("10.1.1.1"), + Ipv4Mask("255.255.255.0")); + + CsmaIpv4Topology::AddIpv4Address (n1, nd1, Ipv4Address("10.1.1.2"), + Ipv4Mask("255.255.255.0")); + + CsmaIpv4Topology::AddIpv4Address (n2, nd2, Ipv4Address("10.1.1.3"), + Ipv4Mask("255.255.255.0")); + + CsmaIpv4Topology::AddIpv4Address (n3, nd3, Ipv4Address("10.1.1.4"), + Ipv4Mask("255.255.255.0")); +// +// Create a UdpEchoClient application to send UDP datagrams from node zero to +// node 1. +// + NS_DEBUG("Create Applications."); + Ptr client = Create (n0,"10.1.1.2", 80, + 1, Seconds(1.), 1024); +// +// Tell the application when to start and stop. +// + client->Start(Seconds(1.0)); + client->Stop (Seconds(10.0)); +// +// Configure tracing of all enqueue, dequeue, and NetDevice receive events. +// Trace output will be sent to the file "csma-one-subnet.tr" +// + NS_DEBUG("Configure Tracing."); + AsciiTrace asciitrace ("csma-one-subnet.tr"); + asciitrace.TraceAllNetDeviceRx (); + asciitrace.TraceAllQueues (); +// +// Also configure some tcpdump traces; each interface will be traced. +// The output files will be named: +// csma-one-subnet.pcap-- +// and can be read by the "tcpdump -r" command (use "-tt" option to +// display timestamps correctly) +// + PcapTrace pcaptrace ("csma-one-subnet.pcap"); + pcaptrace.TraceAllIp (); +// +// Now, do the actual simulation. +// + NS_DEBUG("Run Simulation."); + Simulator::Run (); + Simulator::Destroy (); + NS_DEBUG("Done."); +} diff --git a/examples/wscript b/examples/wscript index 0c887cad0..a1f16e29b 100644 --- a/examples/wscript +++ b/examples/wscript @@ -14,6 +14,10 @@ def build(bld): ['csma', 'internet-node']) obj.source = 'csma-one-subnet.cc' + obj = bld.create_ns3_program('udp-echo', + ['csma', 'internet-node']) + obj.source = 'udp-echo.cc' + obj = bld.create_ns3_program('csma-broadcast', ['csma', 'internet-node']) obj.source = 'csma-broadcast.cc' diff --git a/samples/wscript b/samples/wscript index d8733131a..e89fbe9b8 100644 --- a/samples/wscript +++ b/samples/wscript @@ -26,7 +26,7 @@ def build(bld): obj.source = 'main-test.cc' obj = bld.create_ns3_program('main-simple', - ['node', 'internet-node', 'applications']) + ['node', 'internet-node', 'onoff']) obj.source = 'main-simple.cc' obj = bld.create_ns3_program('main-default-value', diff --git a/src/applications/onoff-application.cc b/src/applications/onoff/onoff-application.cc similarity index 100% rename from src/applications/onoff-application.cc rename to src/applications/onoff/onoff-application.cc diff --git a/src/applications/onoff-application.h b/src/applications/onoff/onoff-application.h similarity index 100% rename from src/applications/onoff-application.h rename to src/applications/onoff/onoff-application.h diff --git a/src/applications/wscript b/src/applications/onoff/wscript similarity index 66% rename from src/applications/wscript rename to src/applications/onoff/wscript index 760f76e18..c1bfe4c52 100644 --- a/src/applications/wscript +++ b/src/applications/onoff/wscript @@ -1,14 +1,12 @@ ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- def build(bld): - obj = bld.create_ns3_module('applications', ['node']) - obj.source = [ + module = bld.create_ns3_module('onoff', ['core']) + module.source = [ 'onoff-application.cc', - 'packet-sink.cc', ] - headers = bld.create_obj('ns3header') headers.source = [ 'onoff-application.h', - 'packet-sink.h', ] + diff --git a/src/applications/packet-sink.cc b/src/applications/packet-sink/packet-sink.cc similarity index 100% rename from src/applications/packet-sink.cc rename to src/applications/packet-sink/packet-sink.cc diff --git a/src/applications/packet-sink.h b/src/applications/packet-sink/packet-sink.h similarity index 100% rename from src/applications/packet-sink.h rename to src/applications/packet-sink/packet-sink.h diff --git a/src/applications/packet-sink/wscript b/src/applications/packet-sink/wscript new file mode 100644 index 000000000..332b6e4a1 --- /dev/null +++ b/src/applications/packet-sink/wscript @@ -0,0 +1,12 @@ +## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- + +def build(bld): + module = bld.create_ns3_module('packet-sink', ['node']) + module.source = [ + 'packet-sink.cc', + ] + headers = bld.create_obj('ns3header') + headers.source = [ + 'packet-sink.h', + ] + diff --git a/src/applications/udp-echo/udp-echo-client.cc b/src/applications/udp-echo/udp-echo-client.cc new file mode 100644 index 000000000..317a91065 --- /dev/null +++ b/src/applications/udp-echo/udp-echo-client.cc @@ -0,0 +1,192 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright 2007 University of Washington + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "ns3/debug.h" +#include "ns3/ipv4-address.h" +#include "ns3/nstime.h" +#include "ns3/inet-socket-address.h" +#include "ns3/socket.h" +#include "ns3/simulator.h" +#include "ns3/socket-factory.h" +#include "ns3/packet.h" + +#include "udp-echo-client.h" + +#if 0 +int sock; /* Socket descriptor */ +struct sockaddr_in echoServAddr; /* Echo server address */ +struct sockaddr_in fromAddr; /* Source address of echo */ +unsigned short echoServPort; /* Echo server port */ +unsigned int fromSize; /* In-out of address size for recvfrom() */ +char *servIP; /* IP address of server */ +char *echoString; /* String to send to echo server */ +char echoBuffer[ECHOMAX+1]; /* Buffer for receiving echoed string */ +int echoStringLen; /* Length of string to echo */ +int respStringLen; /* Length of received response */ + +if ((echoStringLen = strlen(echoString)) > ECHOMAX) /* Check input length */ + DieWithError("Echo word too long"); + +if (argc == 4) + echoServPort = atoi(argv[3]); /* Use given port, if any */ + else + echoServPort = 7; /* 7 is the well-known port for the echo service */ + +/* Create a datagram/UDP socket */ +if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) + DieWithError("socket() failed"); + +/* Construct the server address structure */ +memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ +echoServAddr.sin_family = AF_INET; /* Internet addr family */ +echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */ +echoServAddr.sin_port = htons(echoServPort); /* Server port */ + +/* Send the string to the server */ +if (sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *) + &echoServAddr, sizeof(echoServAddr)) != echoStringLen) + DieWithError("sendto() sent a different number of bytes than expected"); + +/* Recv a response */ +fromSize = sizeof(fromAddr); +if ((respStringLen = recvfrom(sock, echoBuffer, ECHOMAX, 0, + (struct sockaddr *) &fromAddr, &fromSize)) != echoStringLen) + DieWithError("recvfrom() failed"); + +if (echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr) + { + fprintf(stderr,"Error: received a packet from unknown source.\n"); + exit(1); + } + +/* null-terminate the received data */ +echoBuffer[respStringLen] = '\0'; +printf("Received: %s\n", echoBuffer); /* Print the echoed arg */ + +close(sock); +exit(0); +#endif + +namespace ns3 { + +NS_DEBUG_COMPONENT_DEFINE ("UdpEchoClient"); + +UdpEchoClient::UdpEchoClient ( + Ptr n, + Ipv4Address serverAddress, + uint16_t serverPort, + uint32_t count, + Time interval, + uint32_t size) +: + Application(n) +{ + NS_DEBUG ("UdpEchoClient::UdpEchoClient (" << n << ", " << serverAddress << + ", " << serverPort << ", " << count << ", " << interval << + ", " << size << ")"); + + Construct (n, serverAddress, serverPort, count, interval, size); +} + +UdpEchoClient::~UdpEchoClient() +{ + NS_DEBUG ("UdpEchoClient::~UdpEchoClient ()"); +} + +void +UdpEchoClient::Construct ( + Ptr n, + Ipv4Address serverAddress, + uint16_t serverPort, + uint32_t count, + Time interval, + uint32_t size) +{ + NS_DEBUG ("UdpEchoClient::Construct (" << n << ", " << serverAddress << + ", " << serverPort << ", " << count << ", " << interval << + ", " << size << ")"); + + m_node = n; + m_serverAddress = serverAddress; + m_serverPort = serverPort; + m_count = count; + m_interval = interval; + m_size = size; + + m_sent = 0; + m_socket = 0; + m_peer = InetSocketAddress (serverAddress, serverPort); + m_sendEvent = EventId (); +} + +void +UdpEchoClient::DoDispose (void) +{ + NS_DEBUG ("UdpEchoClient::DoDispose ()"); + Application::DoDispose (); +} + +void +UdpEchoClient::StartApplication (void) +{ + NS_DEBUG ("UdpEchoClient::StartApplication ()"); + + if (!m_socket) + { + InterfaceId iid = InterfaceId::LookupByName ("Udp"); + Ptr socketFactory = + GetNode ()->QueryInterface (iid); + m_socket = socketFactory->CreateSocket (); + m_socket->Bind (); + m_socket->Connect (m_peer); + } + + StopApplication (); + ScheduleTransmit (Seconds(0.)); +} + +void +UdpEchoClient::StopApplication () +{ + NS_DEBUG ("UdpEchoClient::StopApplication ()"); +} + +void +UdpEchoClient::ScheduleTransmit (Time dt) +{ + NS_DEBUG ("UdpEchoClient::ScheduleTransmit (" << dt << ")"); + m_sendEvent = Simulator::Schedule(dt, &UdpEchoClient::Send, this); +} + +void +UdpEchoClient::Send (void) +{ + NS_DEBUG ("UdpEchoClient::Send ()"); + + NS_ASSERT (m_sendEvent.IsExpired ()); + + Packet p (m_size); + m_socket->Send (p); + ++m_sent; + + if (m_sent < m_count) + { + ScheduleTransmit (m_interval); + } +} + +} // Namespace ns3 diff --git a/src/applications/udp-echo/udp-echo-client.h b/src/applications/udp-echo/udp-echo-client.h new file mode 100644 index 000000000..7c5f2478a --- /dev/null +++ b/src/applications/udp-echo/udp-echo-client.h @@ -0,0 +1,70 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright 2007 University of Washington + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __UDP_ECHO_CLIENT_H__ +#define __UDP_ECHO_CLIENT_H__ + +#include "ns3/application.h" +#include "ns3/event-id.h" +#include "ns3/ptr.h" + +namespace ns3 { + +class Address; +class Socket; +class Packet; + +class UdpEchoClient : public Application +{ +public: + UdpEchoClient (Ptr n, Ipv4Address serverAddr, uint16_t serverPort, + uint32_t count, Time interval, uint32_t size); + + virtual ~UdpEchoClient (); + +protected: + virtual void DoDispose (void); + +private: + void Construct (Ptr n, Ipv4Address serverAddr, uint16_t serverPort, + uint32_t count, Time interval, uint32_t size); + + virtual void StartApplication (void); + virtual void StopApplication (void); + + void ScheduleTransmit (Time dt); + void Send (void); + + Ptr m_node; + Ipv4Address m_serverAddress; + uint16_t m_serverPort; + uint32_t m_count; + Time m_interval; + uint32_t m_size; + + uint32_t m_sent; + Ptr m_socket; + Address m_peer; + EventId m_sendEvent; + +}; + +} // namespace ns3 + +#endif // __UDP_ECHO_CLIENT_H__ + diff --git a/src/applications/udp-echo/wscript b/src/applications/udp-echo/wscript new file mode 100644 index 000000000..00cbaa3c5 --- /dev/null +++ b/src/applications/udp-echo/wscript @@ -0,0 +1,12 @@ +## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- + +def build(bld): + module = bld.create_ns3_module('udp-echo', ['internet-node']) + module.source = [ + 'udp-echo-client.cc', + ] + headers = bld.create_obj('ns3header') + headers.source = [ + 'udp-echo-client.h', + ] + diff --git a/src/applications/waf b/src/applications/waf deleted file mode 100755 index 3dcf598bc..000000000 --- a/src/applications/waf +++ /dev/null @@ -1 +0,0 @@ -exec "`dirname "$0"`"/../../waf "$@" \ No newline at end of file diff --git a/src/internet-node/wscript b/src/internet-node/wscript index a5f5ec6ef..3bb60d1bb 100644 --- a/src/internet-node/wscript +++ b/src/internet-node/wscript @@ -2,7 +2,7 @@ def build(bld): - obj = bld.create_ns3_module('internet-node', ['node', 'applications']) + obj = bld.create_ns3_module('internet-node', ['node']) obj.source = [ 'internet-node.cc', 'ipv4-l4-demux.cc', diff --git a/src/wscript b/src/wscript index 859021e66..27c0154c9 100644 --- a/src/wscript +++ b/src/wscript @@ -18,7 +18,9 @@ all_modules = ( 'internet-node', 'devices/point-to-point', 'devices/csma', - 'applications', + 'applications/onoff', + 'applications/packet-sink', + 'applications/udp-echo', 'routing/global-routing', 'mobility', )