A Discrete-Event Network Simulator
API
udp-echo-server.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright 2007 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "ns3/log.h"
20 #include "ns3/ipv4-address.h"
21 #include "ns3/ipv6-address.h"
22 #include "ns3/address-utils.h"
23 #include "ns3/nstime.h"
24 #include "ns3/inet-socket-address.h"
25 #include "ns3/inet6-socket-address.h"
26 #include "ns3/socket.h"
27 #include "ns3/udp-socket.h"
28 #include "ns3/simulator.h"
29 #include "ns3/socket-factory.h"
30 #include "ns3/packet.h"
31 #include "ns3/uinteger.h"
32 
33 #include "udp-echo-server.h"
34 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("UdpEchoServerApplication");
38 
39 NS_OBJECT_ENSURE_REGISTERED (UdpEchoServer);
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::UdpEchoServer")
46  .SetGroupName("Applications")
47  .AddConstructor<UdpEchoServer> ()
48  .AddAttribute ("Port", "Port on which we listen for incoming packets.",
49  UintegerValue (9),
51  MakeUintegerChecker<uint16_t> ())
52  .AddTraceSource ("Rx", "A packet has been received",
54  "ns3::Packet::TracedCallback")
55  .AddTraceSource ("RxWithAddresses", "A packet has been received",
57  "ns3::Packet::TwoAddressTracedCallback")
58  ;
59  return tid;
60 }
61 
63 {
64  NS_LOG_FUNCTION (this);
65 }
66 
68 {
69  NS_LOG_FUNCTION (this);
70  m_socket = 0;
71  m_socket6 = 0;
72 }
73 
74 void
76 {
77  NS_LOG_FUNCTION (this);
79 }
80 
81 void
83 {
84  NS_LOG_FUNCTION (this);
85 
86  if (m_socket == 0)
87  {
88  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
91  if (m_socket->Bind (local) == -1)
92  {
93  NS_FATAL_ERROR ("Failed to bind socket");
94  }
96  {
97  Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket);
98  if (udpSocket)
99  {
100  // equivalent to setsockopt (MCAST_JOIN_GROUP)
101  udpSocket->MulticastJoinGroup (0, m_local);
102  }
103  else
104  {
105  NS_FATAL_ERROR ("Error: Failed to join multicast group");
106  }
107  }
108  }
109 
110  if (m_socket6 == 0)
111  {
112  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
115  if (m_socket6->Bind (local6) == -1)
116  {
117  NS_FATAL_ERROR ("Failed to bind socket");
118  }
119  if (addressUtils::IsMulticast (local6))
120  {
121  Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket6);
122  if (udpSocket)
123  {
124  // equivalent to setsockopt (MCAST_JOIN_GROUP)
125  udpSocket->MulticastJoinGroup (0, local6);
126  }
127  else
128  {
129  NS_FATAL_ERROR ("Error: Failed to join multicast group");
130  }
131  }
132  }
133 
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this);
142 
143  if (m_socket != 0)
144  {
145  m_socket->Close ();
147  }
148  if (m_socket6 != 0)
149  {
150  m_socket6->Close ();
152  }
153 }
154 
155 void
157 {
158  NS_LOG_FUNCTION (this << socket);
159 
160  Ptr<Packet> packet;
161  Address from;
162  Address localAddress;
163  while ((packet = socket->RecvFrom (from)))
164  {
165  socket->GetSockName (localAddress);
166  m_rxTrace (packet);
167  m_rxTraceWithAddresses (packet, from, localAddress);
169  {
170  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
171  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
173  }
174  else if (Inet6SocketAddress::IsMatchingType (from))
175  {
176  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
177  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
179  }
180 
181  packet->RemoveAllPacketTags ();
182  packet->RemoveAllByteTags ();
183 
184  NS_LOG_LOGIC ("Echoing packet");
185  socket->SendTo (packet, 0, from);
186 
188  {
189  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server sent " << packet->GetSize () << " bytes to " <<
190  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
192  }
193  else if (Inet6SocketAddress::IsMatchingType (from))
194  {
195  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server sent " << packet->GetSize () << " bytes to " <<
196  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
198  }
199  }
200 }
201 
202 } // Namespace ns3
uint16_t m_port
Port on which we listen for incoming packets.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
an Inet address class
Ipv4Address GetIpv4(void) const
static Ipv4Address GetAny(void)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
bool IsMulticast(const Address &ad)
Address family-independent test for a multicast address.
virtual void StartApplication(void)
Application specific startup code.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
virtual int GetSockName(Address &address) const =0
Get socket address.
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
Callback< R > MakeNullCallback(void)
Definition: callback.h:1635
TracedCallback< Ptr< const Packet > > m_rxTrace
Callbacks for tracing the packet Rx events.
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< Socket > m_socket6
IPv6 Socket.
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:880
The base class for all ns3 applications.
Definition: application.h:60
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< Node > GetNode() const
Definition: application.cc:104
Address m_local
local multicast address
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
virtual void DoDispose(void)
Destructor implementation.
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callbacks for tracing the packet Rx events, includes source and destination addresses.
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
static bool IsMatchingType(const Address &addr)
If the address match.
uint16_t GetPort(void) const
Get the port.
virtual void StopApplication(void)
Application specific shutdown code.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
A Udp Echo server.
uint16_t GetPort(void) const
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
static TypeId GetTypeId(void)
Get the type ID.
virtual int Close(void)=0
Close a socket.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
static bool IsMatchingType(const Address &address)
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:824
Ptr< Socket > m_socket
IPv4 Socket.