A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
42 TypeId
44 {
45  static TypeId tid = TypeId ("ns3::UdpEchoServer")
47  .AddConstructor<UdpEchoServer> ()
48  .AddAttribute ("Port", "Port on which we listen for incoming packets.",
49  UintegerValue (9),
50  MakeUintegerAccessor (&UdpEchoServer::m_port),
51  MakeUintegerChecker<uint16_t> ())
52  ;
53  return tid;
54 }
55 
57 {
58  NS_LOG_FUNCTION (this);
59 }
60 
62 {
63  NS_LOG_FUNCTION (this);
64  m_socket = 0;
65  m_socket6 = 0;
66 }
67 
68 void
70 {
71  NS_LOG_FUNCTION (this);
73 }
74 
75 void
77 {
78  NS_LOG_FUNCTION (this);
79 
80  if (m_socket == 0)
81  {
82  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
85  m_socket->Bind (local);
87  {
88  Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket);
89  if (udpSocket)
90  {
91  // equivalent to setsockopt (MCAST_JOIN_GROUP)
92  udpSocket->MulticastJoinGroup (0, m_local);
93  }
94  else
95  {
96  NS_FATAL_ERROR ("Error: Failed to join multicast group");
97  }
98  }
99  }
100 
101  if (m_socket6 == 0)
102  {
103  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
106  m_socket6->Bind (local6);
107  if (addressUtils::IsMulticast (local6))
108  {
109  Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket6);
110  if (udpSocket)
111  {
112  // equivalent to setsockopt (MCAST_JOIN_GROUP)
113  udpSocket->MulticastJoinGroup (0, local6);
114  }
115  else
116  {
117  NS_FATAL_ERROR ("Error: Failed to join multicast group");
118  }
119  }
120  }
121 
124 }
125 
126 void
128 {
129  NS_LOG_FUNCTION (this);
130 
131  if (m_socket != 0)
132  {
133  m_socket->Close ();
135  }
136  if (m_socket6 != 0)
137  {
138  m_socket6->Close ();
140  }
141 }
142 
143 void
145 {
146  NS_LOG_FUNCTION (this << socket);
147 
148  Ptr<Packet> packet;
149  Address from;
150  while ((packet = socket->RecvFrom (from)))
151  {
153  {
154  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
155  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
157  }
158  else if (Inet6SocketAddress::IsMatchingType (from))
159  {
160  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
161  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
163  }
164 
165  packet->RemoveAllPacketTags ();
166  packet->RemoveAllByteTags ();
167 
168  NS_LOG_LOGIC ("Echoing packet");
169  socket->SendTo (packet, 0, from);
170 
172  {
173  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server sent " << packet->GetSize () << " bytes to " <<
174  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
176  }
177  else if (Inet6SocketAddress::IsMatchingType (from))
178  {
179  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server sent " << packet->GetSize () << " bytes to " <<
180  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
182  }
183  }
184 }
185 
186 } // 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:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
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.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
uint32_t GetSize(void) const
Definition: packet.h:650
#define NS_LOG_INFO(msg)
Definition: log.h:298
Callback< R > MakeNullCallback(void)
Definition: callback.h:1395
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
a polymophic address class
Definition: address.h:86
Ptr< Socket > m_socket6
IPv6 Socket.
void RemoveAllPacketTags(void)
Remove all packet tags.
Definition: packet.cc:869
The base class for all ns3 applications.
Definition: application.h:61
Hold an unsigned integer type.
Definition: uinteger.h:46
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:1238
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)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: application.cc:83
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
static InetSocketAddress ConvertFrom(const Address &address)
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:361
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.
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.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
static bool IsMatchingType(const Address &address)
static TypeId LookupByName(std::string name)
Definition: type-id.cc:536
Ptr< Socket > m_socket
IPv4 Socket.