A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-echo-client.cc
Go to the documentation of this file.
1/*
2 * Copyright 2007 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6#include "udp-echo-client.h"
7
8#include "ns3/address-utils.h"
9#include "ns3/log.h"
10#include "ns3/nstime.h"
11#include "ns3/packet.h"
12#include "ns3/simulator.h"
13#include "ns3/socket-factory.h"
14#include "ns3/socket.h"
15#include "ns3/trace-source-accessor.h"
16#include "ns3/uinteger.h"
17
18namespace ns3
19{
20
21NS_LOG_COMPONENT_DEFINE("UdpEchoClientApplication");
22
23NS_OBJECT_ENSURE_REGISTERED(UdpEchoClient);
24
25TypeId
27{
28 static TypeId tid =
29 TypeId("ns3::UdpEchoClient")
31 .SetGroupName("Applications")
32 .AddConstructor<UdpEchoClient>()
33 .AddAttribute(
34 "MaxPackets",
35 "The maximum number of packets the application will send (zero means infinite)",
36 UintegerValue(100),
39 .AddAttribute("Interval",
40 "The time to wait between packets",
41 TimeValue(Seconds(1.0)),
44 .AddAttribute(
45 "RemoteAddress",
46 "The destination Address of the outbound packets",
49 (void(UdpEchoClient::*)(const Address&)) &
50 UdpEchoClient::SetRemote, // this is needed to indicate which version of the
51 // function overload to use
55 "Replaced by Remote in ns-3.44.")
56 .AddAttribute("RemotePort",
57 "The destination port of the outbound packets",
62 "Replaced by Remote in ns-3.44.")
63 .AddAttribute(
64 "PacketSize",
65 "Size of echo data in outbound packets",
66 UintegerValue(100),
69 .AddTraceSource("Tx",
70 "A new packet is created and is sent",
72 "ns3::Packet::TracedCallback")
73 .AddTraceSource("Rx",
74 "A packet has been received",
76 "ns3::Packet::TracedCallback")
77 .AddTraceSource("TxWithAddresses",
78 "A new packet is created and is sent",
80 "ns3::Packet::TwoAddressTracedCallback")
81 .AddTraceSource("RxWithAddresses",
82 "A packet has been received",
84 "ns3::Packet::TwoAddressTracedCallback");
85 return tid;
86}
87
89 : m_dataSize{0},
90 m_data{nullptr},
91 m_sent{0},
92 m_socket{nullptr},
93 m_peerPort{},
94 m_sendEvent{}
95{
96 NS_LOG_FUNCTION(this);
97}
98
100{
101 NS_LOG_FUNCTION(this);
102 m_socket = nullptr;
103
104 delete[] m_data;
105 m_data = nullptr;
106 m_dataSize = 0;
107}
108
109void
111{
112 NS_LOG_FUNCTION(this << ip << port);
113 SetRemote(ip);
114 SetPort(port);
115}
116
117void
119{
120 NS_LOG_FUNCTION(this << addr);
121 if (!addr.IsInvalid())
122 {
123 m_peer = addr;
124 if (m_peerPort)
125 {
127 }
128 }
129}
130
133{
134 return m_peer;
135}
136
137void
139{
140 NS_LOG_FUNCTION(this << port);
141 if (m_peer.IsInvalid())
142 {
143 // save for later
145 return;
146 }
148 {
150 }
151}
152
153uint16_t
170
171void
173{
174 NS_LOG_FUNCTION(this);
175
176 if (!m_socket)
177 {
178 auto tid = TypeId::LookupByName("ns3::UdpSocketFactory");
180 NS_ABORT_MSG_IF(m_peer.IsInvalid(), "Remote address not properly set");
181 if (!m_local.IsInvalid())
182 {
187 "Incompatible peer and local address IP version");
188 if (m_socket->Bind(m_local) == -1)
189 {
190 NS_FATAL_ERROR("Failed to bind socket");
191 }
192 }
193 else
194 {
196 {
197 if (m_socket->Bind() == -1)
198 {
199 NS_FATAL_ERROR("Failed to bind socket");
200 }
201 }
203 {
204 if (m_socket->Bind6() == -1)
205 {
206 NS_FATAL_ERROR("Failed to bind socket");
207 }
208 }
209 else
210 {
211 NS_ASSERT_MSG(false, "Incompatible address type: " << m_peer);
212 }
213 }
214 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
218 }
219
221}
222
223void
225{
226 NS_LOG_FUNCTION(this);
227
228 if (m_socket)
229 {
230 m_socket->Close();
232 m_socket = nullptr;
233 }
234
236}
237
238void
240{
241 NS_LOG_FUNCTION(this << dataSize);
242
243 //
244 // If the client is setting the echo packet data size this way, we infer
245 // that she doesn't care about the contents of the packet at all, so
246 // neither will we.
247 //
248 delete[] m_data;
249 m_data = nullptr;
250 m_dataSize = 0;
251 m_size = dataSize;
252}
253
256{
257 NS_LOG_FUNCTION(this);
258 return m_size;
259}
260
261void
262UdpEchoClient::SetFill(std::string fill)
263{
264 NS_LOG_FUNCTION(this << fill);
265
266 uint32_t dataSize = fill.size() + 1;
267
268 if (dataSize != m_dataSize)
269 {
270 delete[] m_data;
271 m_data = new uint8_t[dataSize];
272 m_dataSize = dataSize;
273 }
274
275 memcpy(m_data, fill.c_str(), dataSize);
276
277 //
278 // Overwrite packet size attribute.
279 //
280 m_size = dataSize;
281}
282
283void
284UdpEchoClient::SetFill(uint8_t fill, uint32_t dataSize)
285{
286 NS_LOG_FUNCTION(this << fill << dataSize);
287 if (dataSize != m_dataSize)
288 {
289 delete[] m_data;
290 m_data = new uint8_t[dataSize];
291 m_dataSize = dataSize;
292 }
293
294 memset(m_data, fill, dataSize);
295
296 //
297 // Overwrite packet size attribute.
298 //
299 m_size = dataSize;
300}
301
302void
303UdpEchoClient::SetFill(uint8_t* fill, uint32_t fillSize, uint32_t dataSize)
304{
305 NS_LOG_FUNCTION(this << fill << fillSize << dataSize);
306 if (dataSize != m_dataSize)
307 {
308 delete[] m_data;
309 m_data = new uint8_t[dataSize];
310 m_dataSize = dataSize;
311 }
312
313 if (fillSize >= dataSize)
314 {
315 memcpy(m_data, fill, dataSize);
316 m_size = dataSize;
317 return;
318 }
319
320 //
321 // Do all but the final fill.
322 //
323 uint32_t filled = 0;
324 while (filled + fillSize < dataSize)
325 {
326 memcpy(&m_data[filled], fill, fillSize);
327 filled += fillSize;
328 }
329
330 //
331 // Last fill may be partial
332 //
333 memcpy(&m_data[filled], fill, dataSize - filled);
334
335 //
336 // Overwrite packet size attribute.
337 //
338 m_size = dataSize;
339}
340
341void
347
348void
350{
351 NS_LOG_FUNCTION(this);
352
354
355 Ptr<Packet> p;
356 if (m_dataSize)
357 {
358 //
359 // If m_dataSize is non-zero, we have a data buffer of the same size that we
360 // are expected to copy and send. This state of affairs is created if one of
361 // the Fill functions is called. In this case, m_size must have been set
362 // to agree with m_dataSize
363 //
365 "UdpEchoClient::Send(): m_size and m_dataSize inconsistent");
366 NS_ASSERT_MSG(m_data, "UdpEchoClient::Send(): m_dataSize but no m_data");
368 }
369 else
370 {
371 //
372 // If m_dataSize is zero, the client has indicated that it doesn't care
373 // about the data itself either by specifying the data size by setting
374 // the corresponding attribute or by not calling a SetFill function. In
375 // this case, we don't worry about it either. But we do allow m_size
376 // to have a value different from the (zero) m_dataSize.
377 //
379 }
380 Address localAddress;
381 m_socket->GetSockName(localAddress);
382 // call to the trace sinks before the packet is actually sent,
383 // so that tags added to the packet can be sent as well
384 m_txTrace(p);
385 m_txTraceWithAddresses(p, localAddress, m_peer);
386 m_socket->Send(p);
387 ++m_sent;
388
390 {
391 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " client sent " << m_size
392 << " bytes to " << InetSocketAddress::ConvertFrom(m_peer).GetIpv4()
394 }
396 {
397 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " client sent " << m_size
398 << " bytes to " << Inet6SocketAddress::ConvertFrom(m_peer).GetIpv6()
400 }
401
402 if (m_sent < m_count || m_count == 0)
403 {
405 }
406}
407
408void
410{
411 NS_LOG_FUNCTION(this << socket);
412 Address from;
413 while (auto packet = socket->RecvFrom(from))
414 {
416 {
417 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " client received "
418 << packet->GetSize() << " bytes from "
419 << InetSocketAddress::ConvertFrom(from).GetIpv4() << " port "
421 }
423 {
424 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " client received "
425 << packet->GetSize() << " bytes from "
426 << Inet6SocketAddress::ConvertFrom(from).GetIpv6() << " port "
428 }
429 Address localAddress;
430 socket->GetSockName(localAddress);
431 m_rxTrace(packet);
432 m_rxTraceWithAddresses(packet, from, localAddress);
433 }
434}
435
436} // Namespace ns3
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
AttributeValue implementation for Address.
Definition address.h:275
Ptr< Node > GetNode() const
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition event-id.cc:58
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
uint16_t GetPort() const
Get the port.
static bool IsMatchingType(const Address &addr)
If the address match.
Ipv6Address GetIpv6() const
Get the IPv6 address.
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
static bool IsMatchingType(const Address &address)
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition simulator.cc:274
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
Definition socket.cc:423
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int GetSockName(Address &address) const =0
Get socket address.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition socket.cc:117
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:61
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Base class for source applications.
Address m_local
Local address to bind to.
uint8_t m_tos
The packets Type of Service.
Address m_peer
Peer address.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
@ S
second
Definition nstime.h:105
AttributeValue implementation for Time.
Definition nstime.h:1395
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
@ DEPRECATED
Attribute or trace source is deprecated; user is warned.
Definition type-id.h:64
A Udp Echo client.
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 ...
Time m_interval
Packet inter-send time.
void Send()
Send a packet.
void StopApplication() override
Application specific shutdown code.
Ptr< Socket > m_socket
Socket.
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
static constexpr uint16_t DEFAULT_PORT
default port
EventId m_sendEvent
Event to send the next packet.
uint32_t m_size
Size of the sent packet.
void SetRemote(const Address &ip, uint16_t port)
set the remote address and port
uint32_t GetDataSize() const
Get the number of data bytes that will be sent to the server.
uint16_t GetPort() const
Get the remote port (temporary function until deprecated attributes are removed)
uint32_t m_count
Maximum number of packets the application will send.
std::optional< uint16_t > m_peerPort
Remote peer port (deprecated) // NS_DEPRECATED_3_44.
static TypeId GetTypeId()
Get the type ID.
uint8_t * m_data
packet payload data
void SetPort(uint16_t port)
Set the remote port (temporary function until deprecated attributes are removed)
uint32_t m_sent
Counter for sent packets.
void StartApplication() override
Application specific startup code.
void ScheduleTransmit(Time dt)
Schedule the next packet transmission.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_txTraceWithAddresses
Callbacks for tracing the packet Tx events, includes source and destination addresses.
TracedCallback< Ptr< const Packet > > m_txTrace
Callbacks for tracing the packet Tx events.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callbacks for tracing the packet Rx events, includes source and destination addresses.
void SetDataSize(uint32_t dataSize)
Set the data size of the packet (the number of bytes that are sent as data to the server).
TracedCallback< Ptr< const Packet > > m_rxTrace
Callbacks for tracing the packet Rx events.
Address GetRemote() const
Get the remote address (temporary function until deprecated attributes are removed)
uint32_t m_dataSize
packet payload size (must be equal to m_size)
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
Ptr< const AttributeChecker > MakeAddressChecker()
Definition address.cc:169
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:275
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:1396
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1416
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
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:35
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Address ConvertToSocketAddress(const Address &address, uint16_t port)
Convert IPv4/IPv6 address with port to a socket address.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684