A Discrete-Event Network Simulator
API
udp-echo-client.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 #include "ns3/log.h"
19 #include "ns3/ipv4-address.h"
20 #include "ns3/ipv6-address.h"
21 #include "ns3/nstime.h"
22 #include "ns3/inet-socket-address.h"
23 #include "ns3/inet6-socket-address.h"
24 #include "ns3/socket.h"
25 #include "ns3/simulator.h"
26 #include "ns3/socket-factory.h"
27 #include "ns3/packet.h"
28 #include "ns3/uinteger.h"
29 #include "ns3/trace-source-accessor.h"
30 #include "udp-echo-client.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("UdpEchoClientApplication");
35 
36 NS_OBJECT_ENSURE_REGISTERED (UdpEchoClient);
37 
38 TypeId
40 {
41  static TypeId tid = TypeId ("ns3::UdpEchoClient")
43  .SetGroupName("Applications")
44  .AddConstructor<UdpEchoClient> ()
45  .AddAttribute ("MaxPackets",
46  "The maximum number of packets the application will send",
47  UintegerValue (100),
49  MakeUintegerChecker<uint32_t> ())
50  .AddAttribute ("Interval",
51  "The time to wait between packets",
52  TimeValue (Seconds (1.0)),
54  MakeTimeChecker ())
55  .AddAttribute ("RemoteAddress",
56  "The destination Address of the outbound packets",
57  AddressValue (),
60  .AddAttribute ("RemotePort",
61  "The destination port of the outbound packets",
62  UintegerValue (0),
64  MakeUintegerChecker<uint16_t> ())
65  .AddAttribute ("PacketSize", "Size of echo data in outbound packets",
66  UintegerValue (100),
69  MakeUintegerChecker<uint32_t> ())
70  .AddTraceSource ("Tx", "A new packet is created and is sent",
72  "ns3::Packet::TracedCallback")
73  .AddTraceSource ("Rx", "A packet has been received",
75  "ns3::Packet::TracedCallback")
76  .AddTraceSource ("TxWithAddresses", "A new packet is created and is sent",
78  "ns3::Packet::TwoAddressTracedCallback")
79  .AddTraceSource ("RxWithAddresses", "A packet has been received",
81  "ns3::Packet::TwoAddressTracedCallback")
82  ;
83  return tid;
84 }
85 
87 {
88  NS_LOG_FUNCTION (this);
89  m_sent = 0;
90  m_socket = 0;
91  m_sendEvent = EventId ();
92  m_data = 0;
93  m_dataSize = 0;
94 }
95 
97 {
98  NS_LOG_FUNCTION (this);
99  m_socket = 0;
100 
101  delete [] m_data;
102  m_data = 0;
103  m_dataSize = 0;
104 }
105 
106 void
108 {
109  NS_LOG_FUNCTION (this << ip << port);
110  m_peerAddress = ip;
111  m_peerPort = port;
112 }
113 
114 void
116 {
117  NS_LOG_FUNCTION (this << addr);
118  m_peerAddress = addr;
119 }
120 
121 void
123 {
124  NS_LOG_FUNCTION (this);
126 }
127 
128 void
130 {
131  NS_LOG_FUNCTION (this);
132 
133  if (m_socket == 0)
134  {
135  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
138  {
139  if (m_socket->Bind () == -1)
140  {
141  NS_FATAL_ERROR ("Failed to bind socket");
142  }
144  }
145  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
146  {
147  if (m_socket->Bind6 () == -1)
148  {
149  NS_FATAL_ERROR ("Failed to bind socket");
150  }
152  }
154  {
155  if (m_socket->Bind () == -1)
156  {
157  NS_FATAL_ERROR ("Failed to bind socket");
158  }
160  }
162  {
163  if (m_socket->Bind6 () == -1)
164  {
165  NS_FATAL_ERROR ("Failed to bind socket");
166  }
168  }
169  else
170  {
171  NS_ASSERT_MSG (false, "Incompatible address type: " << m_peerAddress);
172  }
173  }
174 
176  m_socket->SetAllowBroadcast (true);
177  ScheduleTransmit (Seconds (0.));
178 }
179 
180 void
182 {
183  NS_LOG_FUNCTION (this);
184 
185  if (m_socket != 0)
186  {
187  m_socket->Close ();
189  m_socket = 0;
190  }
191 
193 }
194 
195 void
196 UdpEchoClient::SetDataSize (uint32_t dataSize)
197 {
198  NS_LOG_FUNCTION (this << dataSize);
199 
200  //
201  // If the client is setting the echo packet data size this way, we infer
202  // that she doesn't care about the contents of the packet at all, so
203  // neither will we.
204  //
205  delete [] m_data;
206  m_data = 0;
207  m_dataSize = 0;
208  m_size = dataSize;
209 }
210 
211 uint32_t
213 {
214  NS_LOG_FUNCTION (this);
215  return m_size;
216 }
217 
218 void
219 UdpEchoClient::SetFill (std::string fill)
220 {
221  NS_LOG_FUNCTION (this << fill);
222 
223  uint32_t dataSize = fill.size () + 1;
224 
225  if (dataSize != m_dataSize)
226  {
227  delete [] m_data;
228  m_data = new uint8_t [dataSize];
229  m_dataSize = dataSize;
230  }
231 
232  memcpy (m_data, fill.c_str (), dataSize);
233 
234  //
235  // Overwrite packet size attribute.
236  //
237  m_size = dataSize;
238 }
239 
240 void
241 UdpEchoClient::SetFill (uint8_t fill, uint32_t dataSize)
242 {
243  NS_LOG_FUNCTION (this << fill << dataSize);
244  if (dataSize != m_dataSize)
245  {
246  delete [] m_data;
247  m_data = new uint8_t [dataSize];
248  m_dataSize = dataSize;
249  }
250 
251  memset (m_data, fill, dataSize);
252 
253  //
254  // Overwrite packet size attribute.
255  //
256  m_size = dataSize;
257 }
258 
259 void
260 UdpEchoClient::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
261 {
262  NS_LOG_FUNCTION (this << fill << fillSize << dataSize);
263  if (dataSize != m_dataSize)
264  {
265  delete [] m_data;
266  m_data = new uint8_t [dataSize];
267  m_dataSize = dataSize;
268  }
269 
270  if (fillSize >= dataSize)
271  {
272  memcpy (m_data, fill, dataSize);
273  m_size = dataSize;
274  return;
275  }
276 
277  //
278  // Do all but the final fill.
279  //
280  uint32_t filled = 0;
281  while (filled + fillSize < dataSize)
282  {
283  memcpy (&m_data[filled], fill, fillSize);
284  filled += fillSize;
285  }
286 
287  //
288  // Last fill may be partial
289  //
290  memcpy (&m_data[filled], fill, dataSize - filled);
291 
292  //
293  // Overwrite packet size attribute.
294  //
295  m_size = dataSize;
296 }
297 
298 void
300 {
301  NS_LOG_FUNCTION (this << dt);
303 }
304 
305 void
307 {
308  NS_LOG_FUNCTION (this);
309 
311 
312  Ptr<Packet> p;
313  if (m_dataSize)
314  {
315  //
316  // If m_dataSize is non-zero, we have a data buffer of the same size that we
317  // are expected to copy and send. This state of affairs is created if one of
318  // the Fill functions is called. In this case, m_size must have been set
319  // to agree with m_dataSize
320  //
321  NS_ASSERT_MSG (m_dataSize == m_size, "UdpEchoClient::Send(): m_size and m_dataSize inconsistent");
322  NS_ASSERT_MSG (m_data, "UdpEchoClient::Send(): m_dataSize but no m_data");
323  p = Create<Packet> (m_data, m_dataSize);
324  }
325  else
326  {
327  //
328  // If m_dataSize is zero, the client has indicated that it doesn't care
329  // about the data itself either by specifying the data size by setting
330  // the corresponding attribute or by not calling a SetFill function. In
331  // this case, we don't worry about it either. But we do allow m_size
332  // to have a value different from the (zero) m_dataSize.
333  //
334  p = Create<Packet> (m_size);
335  }
336  Address localAddress;
337  m_socket->GetSockName (localAddress);
338  // call to the trace sinks before the packet is actually sent,
339  // so that tags added to the packet can be sent as well
340  m_txTrace (p);
342  {
344  }
346  {
348  }
349  m_socket->Send (p);
350  ++m_sent;
351 
353  {
354  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
356  }
358  {
359  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
361  }
363  {
364  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
366  }
368  {
369  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
371  }
372 
373  if (m_sent < m_count)
374  {
376  }
377 }
378 
379 void
381 {
382  NS_LOG_FUNCTION (this << socket);
383  Ptr<Packet> packet;
384  Address from;
385  Address localAddress;
386  while ((packet = socket->RecvFrom (from)))
387  {
389  {
390  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
391  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
393  }
394  else if (Inet6SocketAddress::IsMatchingType (from))
395  {
396  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
397  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
399  }
400  socket->GetSockName (localAddress);
401  m_rxTrace (packet);
402  m_rxTraceWithAddresses (packet, from, localAddress);
403  }
404 }
405 
406 } // Namespace ns3
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_txTraceWithAddresses
Callbacks for tracing the packet Tx events, includes source and destination addresses.
an Inet address class
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
TracedCallback< Ptr< const Packet > > m_txTrace
Callbacks for tracing the packet Tx events.
uint32_t m_dataSize
packet payload size (must be equal to m_size)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
Ptr< Socket > m_socket
Socket.
virtual void DoDispose(void)
Destructor implementation.
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#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.
#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
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:346
Callback< R > MakeNullCallback(void)
Definition: callback.h:1635
void SetRemote(Address ip, uint16_t port)
set the remote address and port
virtual void StopApplication(void)
Application specific shutdown code.
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:59
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
void ScheduleTransmit(Time dt)
Schedule the next packet transmission.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: address.h:278
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
The base class for all ns3 applications.
Definition: application.h:60
AttributeValue implementation for Time.
Definition: nstime.h:1076
uint8_t * m_data
packet payload data
Hold an unsigned integer type.
Definition: uinteger.h:44
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
static bool IsMatchingType(const Address &address)
Ptr< Node > GetNode() const
Definition: application.cc:104
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
A Udp Echo client.
virtual void StartApplication(void)
Application specific startup code.
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.
uint16_t GetPort(void) const
uint32_t m_count
Maximum number of packets the application will send.
static TypeId GetTypeId(void)
Get the type ID.
Time m_interval
Packet inter-send time.
Ptr< const AttributeChecker > MakeAddressChecker(void)
Definition: address.cc:172
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: nstime.h:1077
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
void SetDataSize(uint32_t dataSize)
Set the data size of the packet (the number of bytes that are sent as data to the server)...
void Send(void)
Send a packet.
AttributeValue implementation for Address.
Definition: address.h:278
EventId m_sendEvent
Event to send the next packet.
An identifier for simulation events.
Definition: event-id.h:53
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callbacks for tracing the packet Rx events, includes source and destination addresses.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
Address m_peerAddress
Remote peer address.
static bool IsMatchingType(const Address &addr)
If the address match.
TracedCallback< Ptr< const Packet > > m_rxTrace
Callbacks for tracing the packet Rx events.
uint32_t m_sent
Counter for sent packets.
static Ipv4Address ConvertFrom(const Address &address)
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.
uint32_t m_size
Size of the sent packet.
uint32_t GetDataSize(void) const
Get the number of data bytes that will be sent to the server.
uint16_t GetPort(void) const
Get the port.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
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)
uint16_t m_peerPort
Remote peer port.
Ipv4Address GetIpv4(void) const
void SetFill(std::string fill)
Set the data fill of the packet (what is sent as data to the server) to the zero-terminated contents ...
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:824
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.