A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-echo-server.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-server.h"
7
9
10#include "ns3/address-utils.h"
11#include "ns3/boolean.h"
12#include "ns3/inet-socket-address.h"
13#include "ns3/inet6-socket-address.h"
14#include "ns3/ipv4-address.h"
15#include "ns3/ipv6-address.h"
16#include "ns3/log.h"
17#include "ns3/nstime.h"
18#include "ns3/packet.h"
19#include "ns3/simulator.h"
20#include "ns3/socket-factory.h"
21#include "ns3/socket.h"
22#include "ns3/udp-socket.h"
23#include "ns3/uinteger.h"
24
25namespace ns3
26{
27
28NS_LOG_COMPONENT_DEFINE("UdpEchoServerApplication");
29
31
34{
35 static TypeId tid =
36 TypeId("ns3::UdpEchoServer")
38 .SetGroupName("Applications")
39 .AddConstructor<UdpEchoServer>()
40 .AddAttribute("Tos",
41 "The Type of Service used to send IPv4 packets. "
42 "All 8 bits of the TOS byte are set (including ECN bits).",
46 .AddAttribute("EnableSeqTsEchoHeader",
47 "Enable use of SeqTsEchoHeader for sequence number and timestamps",
48 BooleanValue(false),
51 .AddTraceSource("RxWithAddresses",
52 "A packet has been received",
54 "ns3::Packet::TwoAddressTracedCallback");
55 return tid;
56}
57
65
70
71void
73{
74 NS_LOG_FUNCTION(this);
75
76 auto local = m_local;
77 if (local.IsInvalid())
78 {
80 }
81 if (m_socket->Bind(local) == -1)
82 {
83 NS_FATAL_ERROR("Failed to bind socket");
84 }
86 {
88 if (udpSocket)
89 {
90 // equivalent to setsockopt (MCAST_JOIN_GROUP)
91 udpSocket->MulticastJoinGroup(0, m_local);
92 }
93 else
94 {
95 NS_FATAL_ERROR("Error: Failed to join multicast group");
96 }
97 }
98 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
99 m_socket->SetRecvCallback(MakeCallback(&UdpEchoServer::HandleRead, this));
100
101 if (m_local.IsInvalid())
102 {
104 if (m_socket6->Bind(local) == -1)
105 {
106 NS_FATAL_ERROR("Failed to bind socket");
107 }
108 if (addressUtils::IsMulticast(local))
109 {
111 if (udpSocket)
112 {
113 // equivalent to setsockopt (MCAST_JOIN_GROUP)
114 udpSocket->MulticastJoinGroup(0, local);
115 }
116 else
117 {
118 NS_FATAL_ERROR("Error: Failed to join multicast group");
119 }
120 }
121 m_socket6->SetRecvCallback(MakeCallback(&UdpEchoServer::HandleRead, this));
122 }
123}
124
125void
127{
128 NS_LOG_FUNCTION(this << socket);
129
130 Address from;
131 while (auto packet = socket->RecvFrom(from))
132 {
133 Address localAddress;
134 socket->GetSockName(localAddress);
136 m_rxTrace(packet, from);
137 m_rxTraceWithAddresses(packet, from, localAddress);
139 {
140 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server received "
141 << packet->GetSize() << " bytes from "
142 << InetSocketAddress::ConvertFrom(from).GetIpv4() << " port "
143 << InetSocketAddress::ConvertFrom(from).GetPort());
144 }
146 {
147 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server received "
148 << packet->GetSize() << " bytes from "
149 << Inet6SocketAddress::ConvertFrom(from).GetIpv6() << " port "
150 << Inet6SocketAddress::ConvertFrom(from).GetPort());
151 }
152
153 packet->RemoveAllPacketTags();
154 packet->RemoveAllByteTags();
155
157 {
158 SeqTsEchoHeader header;
159 packet->RemoveHeader(header);
160 header.SetTsEchoReply(header.GetTsValue());
161 header.SetTsValue(Simulator::Now());
162 packet->AddHeader(header);
163 }
164
165 NS_LOG_LOGIC("Echoing packet");
166 socket->SendTo(packet, 0, from);
167
169 {
170 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server sent "
171 << packet->GetSize() << " bytes to "
172 << InetSocketAddress::ConvertFrom(from).GetIpv4() << " port "
173 << InetSocketAddress::ConvertFrom(from).GetPort());
174 }
176 {
177 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server sent "
178 << packet->GetSize() << " bytes to "
179 << Inet6SocketAddress::ConvertFrom(from).GetIpv6() << " port "
180 << Inet6SocketAddress::ConvertFrom(from).GetPort());
181 }
182 }
183}
184
185} // Namespace ns3
a polymophic address class
Definition address.h:114
An Inet6 address class.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
an Inet address class
static bool IsMatchingType(const Address &address)
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
static Ipv4Address GetAny()
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Packet header to carry sequence number and two timestamps.
void SetTsValue(Time ts)
Set the sender's time value.
void SetTsEchoReply(Time ts)
Upon SeqTsEchoHeader reception, the host answers via echoing back the received timestamp.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
ns3::TracedCallback< Ptr< const Packet > > m_rxTraceWithoutAddress
Callbacks for tracing the packet Rx events.
Ptr< Socket > m_socket6
IPv6 Socket (used if only port is specified).
TypeId m_protocolTid
Protocol TypeId value.
SinkApplication(uint16_t defaultPort=0)
Constructor.
Address m_local
Local address to bind to (address and port).
Ptr< Socket > m_socket
Socket (IPv4 or IPv6, depending on local address).
uint32_t m_port
Local port to bind to.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
@ S
second
Definition nstime.h:106
a unique identifier for an interface.
Definition type-id.h:50
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:874
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
A Udp Echo server.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callbacks for tracing the packet Rx events, includes source and destination addresses.
static constexpr uint16_t DEFAULT_PORT
default port
uint8_t m_tos
The packets Type of Service.
static TypeId GetTypeId()
Get the type ID.
bool m_enableSeqTsEchoHeader
Enable or disable use of SeqTsEchoHeader.
void DoStartApplication() override
Application specific startup code for child subclasses.
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
Hold an unsigned integer type.
Definition uinteger.h:34
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:690
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:281
#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:274
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
bool IsMulticast(const Address &ad)
Address family-independent test for a multicast address.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:643
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition boolean.h:70