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
7#include "udp-echo-server.h"
8
9#include "ns3/address-utils.h"
10#include "ns3/inet-socket-address.h"
11#include "ns3/inet6-socket-address.h"
12#include "ns3/ipv4-address.h"
13#include "ns3/ipv6-address.h"
14#include "ns3/log.h"
15#include "ns3/nstime.h"
16#include "ns3/packet.h"
17#include "ns3/simulator.h"
18#include "ns3/socket-factory.h"
19#include "ns3/socket.h"
20#include "ns3/udp-socket.h"
21#include "ns3/uinteger.h"
22
23namespace ns3
24{
25
26NS_LOG_COMPONENT_DEFINE("UdpEchoServerApplication");
27
28NS_OBJECT_ENSURE_REGISTERED(UdpEchoServer);
29
30TypeId
32{
33 static TypeId tid =
34 TypeId("ns3::UdpEchoServer")
36 .SetGroupName("Applications")
37 .AddConstructor<UdpEchoServer>()
38 .AddAttribute("Tos",
39 "The Type of Service used to send IPv4 packets. "
40 "All 8 bits of the TOS byte are set (including ECN bits).",
44 .AddTraceSource("Rx",
45 "A packet has been received",
47 "ns3::Packet::TracedCallback")
48 .AddTraceSource("RxWithAddresses",
49 "A packet has been received",
51 "ns3::Packet::TwoAddressTracedCallback");
52 return tid;
53}
54
56 : SinkApplication(DEFAULT_PORT),
57 m_socket{nullptr},
58 m_socket6{nullptr}
59{
60 NS_LOG_FUNCTION(this);
61}
62
64{
65 NS_LOG_FUNCTION(this);
66 m_socket = nullptr;
67 m_socket6 = nullptr;
68}
69
70void
72{
73 NS_LOG_FUNCTION(this);
74
75 if (!m_socket)
76 {
77 auto tid = TypeId::LookupByName("ns3::UdpSocketFactory");
79 auto local = m_local;
80 if (local.IsInvalid())
81 {
83 }
84 if (m_socket->Bind(local) == -1)
85 {
86 NS_FATAL_ERROR("Failed to bind socket");
87 }
89 {
91 if (udpSocket)
92 {
93 // equivalent to setsockopt (MCAST_JOIN_GROUP)
94 udpSocket->MulticastJoinGroup(0, m_local);
95 }
96 else
97 {
98 NS_FATAL_ERROR("Error: Failed to join multicast group");
99 }
100 }
101 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
103 }
104
105 if (m_local.IsInvalid() && !m_socket6)
106 {
107 // local address is not specified, so create another socket to also listen to all IPv6
108 // addresses
109 auto tid = TypeId::LookupByName("ns3::UdpSocketFactory");
112 if (m_socket6->Bind(local) == -1)
113 {
114 NS_FATAL_ERROR("Failed to bind socket");
115 }
116 if (addressUtils::IsMulticast(local))
117 {
119 if (udpSocket)
120 {
121 // equivalent to setsockopt (MCAST_JOIN_GROUP)
122 udpSocket->MulticastJoinGroup(0, local);
123 }
124 else
125 {
126 NS_FATAL_ERROR("Error: Failed to join multicast group");
127 }
128 }
130 }
131}
132
133void
135{
136 NS_LOG_FUNCTION(this);
137
138 if (m_socket)
139 {
140 m_socket->Close();
142 }
143 if (m_socket6)
144 {
145 m_socket6->Close();
147 }
148}
149
150void
152{
153 NS_LOG_FUNCTION(this << socket);
154
155 Address from;
156 while (auto packet = socket->RecvFrom(from))
157 {
158 Address localAddress;
159 socket->GetSockName(localAddress);
160 m_rxTrace(packet);
161 m_rxTraceWithAddresses(packet, from, localAddress);
163 {
164 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server received "
165 << packet->GetSize() << " bytes from "
166 << InetSocketAddress::ConvertFrom(from).GetIpv4() << " port "
168 }
170 {
171 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server received "
172 << packet->GetSize() << " bytes from "
173 << Inet6SocketAddress::ConvertFrom(from).GetIpv6() << " port "
175 }
176
177 packet->RemoveAllPacketTags();
178 packet->RemoveAllByteTags();
179
180 NS_LOG_LOGIC("Echoing packet");
181 socket->SendTo(packet, 0, from);
182
184 {
185 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server sent "
186 << packet->GetSize() << " bytes to "
187 << InetSocketAddress::ConvertFrom(from).GetIpv4() << " port "
189 }
191 {
192 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server sent "
193 << packet->GetSize() << " bytes to "
194 << Inet6SocketAddress::ConvertFrom(from).GetIpv6() << " port "
196 }
197 }
198}
199
200} // Namespace ns3
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
Ptr< Node > GetNode() const
An Inet6 address class.
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.
an Inet address class
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 Ipv4Address GetAny()
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Base class for sink applications.
Address m_local
Local address to bind to (address and port)
uint32_t m_port
Local port to bind to.
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
Definition socket.cc:423
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.
@ S
second
Definition nstime.h:105
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
A Udp Echo server.
void StartApplication() override
Application specific startup code.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_rxTraceWithAddresses
Callbacks for tracing the packet Rx events, includes source and destination addresses.
Ptr< Socket > m_socket6
IPv6 Socket (used if only port is specified)
uint8_t m_tos
The packets Type of Service.
static TypeId GetTypeId()
Get the type ID.
void StopApplication() override
Application specific shutdown code.
TracedCallback< Ptr< const Packet > > m_rxTrace
Callbacks for tracing the packet Rx events.
Ptr< Socket > m_socket
Socket.
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
Hold an unsigned integer type.
Definition uinteger.h:34
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_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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< 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.
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
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580