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("Port",
39 "Port on which we listen for incoming packets.",
43 .AddAttribute("Tos",
44 "The Type of Service used to send IPv4 packets. "
45 "All 8 bits of the TOS byte are set (including ECN bits).",
49 .AddTraceSource("Rx",
50 "A packet has been received",
52 "ns3::Packet::TracedCallback")
53 .AddTraceSource("RxWithAddresses",
54 "A packet has been received",
56 "ns3::Packet::TwoAddressTracedCallback");
57 return tid;
58}
59
64
66{
67 NS_LOG_FUNCTION(this);
68 m_socket = nullptr;
69 m_socket6 = nullptr;
70}
71
72void
74{
75 NS_LOG_FUNCTION(this);
76
77 if (!m_socket)
78 {
79 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
82 if (m_socket->Bind(local) == -1)
83 {
84 NS_FATAL_ERROR("Failed to bind socket");
85 }
87 {
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)
102 {
103 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
106 if (m_socket6->Bind(local6) == -1)
107 {
108 NS_FATAL_ERROR("Failed to bind socket");
109 }
110 if (addressUtils::IsMulticast(local6))
111 {
113 if (udpSocket)
114 {
115 // equivalent to setsockopt (MCAST_JOIN_GROUP)
116 udpSocket->MulticastJoinGroup(0, local6);
117 }
118 else
119 {
120 NS_FATAL_ERROR("Error: Failed to join multicast group");
121 }
122 }
123 }
124
125 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
128}
129
130void
132{
133 NS_LOG_FUNCTION(this);
134
135 if (m_socket)
136 {
137 m_socket->Close();
139 }
140 if (m_socket6)
141 {
142 m_socket6->Close();
144 }
145}
146
147void
149{
150 NS_LOG_FUNCTION(this << socket);
151
152 Ptr<Packet> packet;
153 Address from;
154 Address localAddress;
155 while ((packet = socket->RecvFrom(from)))
156 {
157 socket->GetSockName(localAddress);
158 m_rxTrace(packet);
159 m_rxTraceWithAddresses(packet, from, localAddress);
161 {
162 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server received "
163 << packet->GetSize() << " bytes from "
164 << InetSocketAddress::ConvertFrom(from).GetIpv4() << " port "
166 }
168 {
169 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server received "
170 << packet->GetSize() << " bytes from "
171 << Inet6SocketAddress::ConvertFrom(from).GetIpv6() << " port "
173 }
174
175 packet->RemoveAllPacketTags();
176 packet->RemoveAllByteTags();
177
178 NS_LOG_LOGIC("Echoing packet");
179 socket->SendTo(packet, 0, from);
180
182 {
183 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server sent "
184 << packet->GetSize() << " bytes to "
185 << InetSocketAddress::ConvertFrom(from).GetIpv4() << " port "
187 }
189 {
190 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " server sent "
191 << packet->GetSize() << " bytes to "
192 << Inet6SocketAddress::ConvertFrom(from).GetIpv6() << " port "
194 }
195 }
196}
197
198} // Namespace ns3
a polymophic address class
Definition address.h:90
The base class for all ns3 applications.
Definition application.h:51
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
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.
uint16_t m_port
Port on which we listen for incoming packets.
Address m_local
local multicast address
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.
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
IPv4 Socket.
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
Hold an unsigned integer type.
Definition uinteger.h:34
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.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
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< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580