A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-socket-impl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#ifndef UDP_SOCKET_IMPL_H
9#define UDP_SOCKET_IMPL_H
10
11#include "icmpv4.h"
12#include "ipv4-interface.h"
13#include "udp-socket.h"
14
15#include "ns3/callback.h"
16#include "ns3/ipv4-address.h"
17#include "ns3/ptr.h"
18#include "ns3/socket.h"
19#include "ns3/traced-callback.h"
20
21#include <queue>
22#include <stdint.h>
23
24namespace ns3
25{
26
27class Ipv4EndPoint;
28class Ipv6EndPoint;
29class Node;
30class Packet;
31class UdpL4Protocol;
32class Ipv6Header;
33class Ipv6Interface;
34
35/**
36 * @ingroup socket
37 * @ingroup udp
38 *
39 * @brief A sockets interface to UDP
40 *
41 * This class subclasses ns3::UdpSocket, and provides a socket interface
42 * to ns3's implementation of UDP.
43 *
44 * For IPv4 packets, the TOS is set according to the following rules:
45 * - if the socket is connected, the TOS set for the socket is used
46 * - if the socket is not connected, the TOS specified in the destination address
47 * passed to SendTo is used, while the TOS set for the socket is ignored
48 * In both cases, a SocketIpTos tag is only added to the packet if the resulting
49 * TOS is non-null. The Bind and Connect operations set the TOS for the
50 * socket to the value specified in the provided address.
51 * If the TOS determined for a packet (as described above) is not null, the
52 * packet is assigned a priority based on that TOS value (according to the
53 * Socket::IpTos2Priority function). Otherwise, the priority set for the
54 * socket is assigned to the packet. Setting a TOS for a socket also sets a
55 * priority for the socket (according to the Socket::IpTos2Priority function).
56 * A SocketPriority tag is only added to the packet if the resulting priority
57 * is non-null.
58 */
59
61{
62 public:
63 /**
64 * @brief Get the type ID.
65 * @return the object TypeId
66 */
67 static TypeId GetTypeId();
68 /**
69 * Create an unbound udp socket.
70 */
72 ~UdpSocketImpl() override;
73
74 /**
75 * @brief Set the associated node.
76 * @param node the node
77 */
78 void SetNode(Ptr<Node> node);
79 /**
80 * @brief Set the associated UDP L4 protocol.
81 * @param udp the UDP L4 protocol
82 */
83 void SetUdp(Ptr<UdpL4Protocol> udp);
84
85 SocketErrno GetErrno() const override;
86 SocketType GetSocketType() const override;
87 Ptr<Node> GetNode() const override;
88 int Bind() override;
89 int Bind6() override;
90 int Bind(const Address& address) override;
91 int Close() override;
92 int ShutdownSend() override;
93 int ShutdownRecv() override;
94 int Connect(const Address& address) override;
95 int Listen() override;
96 uint32_t GetTxAvailable() const override;
97 int Send(Ptr<Packet> p, uint32_t flags) override;
98 int SendTo(Ptr<Packet> p, uint32_t flags, const Address& address) override;
99 uint32_t GetRxAvailable() const override;
100 Ptr<Packet> Recv(uint32_t maxSize, uint32_t flags) override;
101 Ptr<Packet> RecvFrom(uint32_t maxSize, uint32_t flags, Address& fromAddress) override;
102 int GetSockName(Address& address) const override;
103 int GetPeerName(Address& address) const override;
104 int MulticastJoinGroup(uint32_t interfaceIndex, const Address& groupAddress) override;
105 int MulticastLeaveGroup(uint32_t interfaceIndex, const Address& groupAddress) override;
106 void BindToNetDevice(Ptr<NetDevice> netdevice) override;
107 bool SetAllowBroadcast(bool allowBroadcast) override;
108 bool GetAllowBroadcast() const override;
109 void Ipv6JoinGroup(Ipv6Address address,
111 std::vector<Ipv6Address> sourceAddresses) override;
112
113 private:
114 // Attributes set through UdpSocket base class
115 void SetRcvBufSize(uint32_t size) override;
116 uint32_t GetRcvBufSize() const override;
117 void SetIpMulticastTtl(uint8_t ipTtl) override;
118 uint8_t GetIpMulticastTtl() const override;
119 void SetIpMulticastIf(int32_t ipIf) override;
120 int32_t GetIpMulticastIf() const override;
121 void SetIpMulticastLoop(bool loop) override;
122 bool GetIpMulticastLoop() const override;
123 void SetMtuDiscover(bool discover) override;
124 bool GetMtuDiscover() const override;
125
126 /**
127 * @brief UdpSocketFactory friend class.
128 * @relates UdpSocketFactory
129 */
130 friend class UdpSocketFactory;
131 // invoked by Udp class
132
133 /**
134 * Finish the binding process
135 * @returns 0 on success, -1 on failure
136 */
137 int FinishBind();
138
139 /**
140 * @brief Called by the L3 protocol when it received a packet to pass on to TCP.
141 *
142 * @param packet the incoming packet
143 * @param header the packet's IPv4 header
144 * @param port the remote port
145 * @param incomingInterface the incoming interface
146 */
147 void ForwardUp(Ptr<Packet> packet,
148 Ipv4Header header,
149 uint16_t port,
150 Ptr<Ipv4Interface> incomingInterface);
151
152 /**
153 * @brief Called by the L3 protocol when it received a packet to pass on to TCP.
154 *
155 * @param packet the incoming packet
156 * @param header the packet's IPv6 header
157 * @param port the remote port
158 * @param incomingInterface the incoming interface
159 */
160 void ForwardUp6(Ptr<Packet> packet,
161 Ipv6Header header,
162 uint16_t port,
163 Ptr<Ipv6Interface> incomingInterface);
164
165 /**
166 * @brief Kill this socket by zeroing its attributes (IPv4)
167 *
168 * This is a callback function configured to m_endpoint in
169 * SetupCallback(), invoked when the endpoint is destroyed.
170 */
171 void Destroy();
172
173 /**
174 * @brief Kill this socket by zeroing its attributes (IPv6)
175 *
176 * This is a callback function configured to m_endpoint in
177 * SetupCallback(), invoked when the endpoint is destroyed.
178 */
179 void Destroy6();
180
181 /**
182 * @brief Deallocate m_endPoint and m_endPoint6
183 */
184 void DeallocateEndPoint();
185
186 /**
187 * @brief Send a packet
188 * @param p packet
189 * @returns 0 on success, -1 on failure
190 */
191 int DoSend(Ptr<Packet> p);
192 /**
193 * @brief Send a packet to a specific destination and port (IPv4)
194 * @param p packet
195 * @param daddr destination address
196 * @param dport destination port
197 * @param tos ToS
198 * @returns 0 on success, -1 on failure
199 */
200 int DoSendTo(Ptr<Packet> p, Ipv4Address daddr, uint16_t dport, uint8_t tos);
201 /**
202 * @brief Send a packet to a specific destination and port (IPv6)
203 * @param p packet
204 * @param daddr destination address
205 * @param dport destination port
206 * @returns 0 on success, -1 on failure
207 */
208 int DoSendTo(Ptr<Packet> p, Ipv6Address daddr, uint16_t dport);
209
210 /**
211 * @brief Called by the L3 protocol when it received an ICMP packet to pass on to TCP.
212 *
213 * @param icmpSource the ICMP source address
214 * @param icmpTtl the ICMP Time to Live
215 * @param icmpType the ICMP Type
216 * @param icmpCode the ICMP Code
217 * @param icmpInfo the ICMP Info
218 */
219 void ForwardIcmp(Ipv4Address icmpSource,
220 uint8_t icmpTtl,
221 uint8_t icmpType,
222 uint8_t icmpCode,
223 uint32_t icmpInfo);
224
225 /**
226 * @brief Called by the L3 protocol when it received an ICMPv6 packet to pass on to TCP.
227 *
228 * @param icmpSource the ICMP source address
229 * @param icmpTtl the ICMP Time to Live
230 * @param icmpType the ICMP Type
231 * @param icmpCode the ICMP Code
232 * @param icmpInfo the ICMP Info
233 */
234 void ForwardIcmp6(Ipv6Address icmpSource,
235 uint8_t icmpTtl,
236 uint8_t icmpType,
237 uint8_t icmpCode,
238 uint32_t icmpInfo);
239
240 // Connections to other layers of TCP/IP
241 Ipv4EndPoint* m_endPoint; //!< the IPv4 endpoint
242 Ipv6EndPoint* m_endPoint6; //!< the IPv6 endpoint
243 Ptr<Node> m_node; //!< the associated node
244 Ptr<UdpL4Protocol> m_udp; //!< the associated UDP L4 protocol
246 m_icmpCallback; //!< ICMP callback
248 m_icmpCallback6; //!< ICMPv6 callback
249
250 Address m_defaultAddress; //!< Default address
251 uint16_t m_defaultPort; //!< Default port
252 TracedCallback<Ptr<const Packet>> m_dropTrace; //!< Trace for dropped packets
253
254 mutable SocketErrno m_errno; //!< Socket error code
255 bool m_shutdownSend; //!< Send no longer allowed
256 bool m_shutdownRecv; //!< Receive no longer allowed
257 bool m_connected; //!< Connection established
258 bool m_allowBroadcast; //!< Allow send broadcast packets
259
260 std::queue<std::pair<Ptr<Packet>, Address>> m_deliveryQueue; //!< Queue for incoming packets
261 uint32_t m_rxAvailable; //!< Number of available bytes to be received
262
263 // Socket attributes
264 uint32_t m_rcvBufSize; //!< Receive buffer size
265 uint8_t m_ipMulticastTtl; //!< Multicast TTL
266 int32_t m_ipMulticastIf; //!< Multicast Interface
267 bool m_ipMulticastLoop; //!< Allow multicast loop
268 bool m_mtuDiscover; //!< Allow MTU discovery
269};
270
271} // namespace ns3
272
273#endif /* UDP_SOCKET_IMPL_H */
a polymophic address class
Definition address.h:90
Callback template class.
Definition callback.h:422
Ipv4 addresses are stored in host order in this class.
A representation of an internet endpoint/connection.
Packet header for IPv4.
Definition ipv4-header.h:23
Describes an IPv6 address.
A representation of an IPv6 endpoint/connection.
Packet header for IPv6.
Definition ipv6-header.h:24
Smart pointer class similar to boost::intrusive_ptr.
Ptr< Packet > Recv()
Read a single packet from the socket.
Definition socket.cc:163
SocketType
Enumeration of the possible socket types.
Definition socket.h:96
Ipv6MulticastFilterMode
Enumeration of the possible filter of a socket.
Definition socket.h:132
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition socket.h:73
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:48
API to create UDP socket instances.
(abstract) base class of all UdpSockets
Definition udp-socket.h:37
A sockets interface to UDP.
void SetUdp(Ptr< UdpL4Protocol > udp)
Set the associated UDP L4 protocol.
uint32_t GetTxAvailable() const override
Returns the number of bytes which can be sent in a single call to Send.
void SetIpMulticastTtl(uint8_t ipTtl) override
Set the IP multicast TTL.
bool m_allowBroadcast
Allow send broadcast packets.
int MulticastJoinGroup(uint32_t interfaceIndex, const Address &groupAddress) override
Corresponds to socket option MCAST_JOIN_GROUP.
static TypeId GetTypeId()
Get the type ID.
bool GetIpMulticastLoop() const override
Get the IP multicast loop capability.
bool GetMtuDiscover() const override
Get the MTU discover capability.
int GetPeerName(Address &address) const override
Get the peer address of a connected socket.
void Destroy()
Kill this socket by zeroing its attributes (IPv4)
void ForwardUp6(Ptr< Packet > packet, Ipv6Header header, uint16_t port, Ptr< Ipv6Interface > incomingInterface)
Called by the L3 protocol when it received a packet to pass on to TCP.
int DoSendTo(Ptr< Packet > p, Ipv4Address daddr, uint16_t dport, uint8_t tos)
Send a packet to a specific destination and port (IPv4)
Callback< void, Ipv6Address, uint8_t, uint8_t, uint8_t, uint32_t > m_icmpCallback6
ICMPv6 callback.
void BindToNetDevice(Ptr< NetDevice > netdevice) override
Bind a socket to specific device.
int Close() override
Close a socket.
int FinishBind()
Finish the binding process.
bool m_connected
Connection established.
void ForwardIcmp(Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo)
Called by the L3 protocol when it received an ICMP packet to pass on to TCP.
int Listen() override
Listen for incoming connections.
Callback< void, Ipv4Address, uint8_t, uint8_t, uint8_t, uint32_t > m_icmpCallback
ICMP callback.
SocketErrno m_errno
Socket error code.
uint8_t m_ipMulticastTtl
Multicast TTL.
int GetSockName(Address &address) const override
Get socket address.
Ipv4EndPoint * m_endPoint
the IPv4 endpoint
int32_t m_ipMulticastIf
Multicast Interface.
void SetIpMulticastIf(int32_t ipIf) override
Set the IP multicast interface.
void SetRcvBufSize(uint32_t size) override
Set the receiving buffer size.
uint8_t GetIpMulticastTtl() const override
Get the IP multicast TTL.
SocketType GetSocketType() const override
std::queue< std::pair< Ptr< Packet >, Address > > m_deliveryQueue
Queue for incoming packets.
uint32_t m_rxAvailable
Number of available bytes to be received.
Ptr< Node > GetNode() const override
Return the node this socket is associated with.
TracedCallback< Ptr< const Packet > > m_dropTrace
Trace for dropped packets.
void Ipv6JoinGroup(Ipv6Address address, Socket::Ipv6MulticastFilterMode filterMode, std::vector< Ipv6Address > sourceAddresses) override
Joins a IPv6 multicast group.
uint32_t m_rcvBufSize
Receive buffer size.
Address m_defaultAddress
Default address.
UdpSocketImpl()
Create an unbound udp socket.
int Bind6() override
Allocate a local IPv6 endpoint for this socket.
uint16_t m_defaultPort
Default port.
bool m_shutdownSend
Send no longer allowed.
int Send(Ptr< Packet > p, uint32_t flags) override
Send data (or dummy data) to the remote host.
bool m_ipMulticastLoop
Allow multicast loop.
int SendTo(Ptr< Packet > p, uint32_t flags, const Address &address) override
Send data to a specified peer.
int32_t GetIpMulticastIf() const override
Get the IP multicast interface.
Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress) override
Read a single packet from the socket and retrieve the sender address.
bool m_mtuDiscover
Allow MTU discovery.
bool m_shutdownRecv
Receive no longer allowed.
void SetNode(Ptr< Node > node)
Set the associated node.
bool GetAllowBroadcast() const override
Query whether broadcast datagram transmissions are allowed.
void ForwardUp(Ptr< Packet > packet, Ipv4Header header, uint16_t port, Ptr< Ipv4Interface > incomingInterface)
Called by the L3 protocol when it received a packet to pass on to TCP.
int Connect(const Address &address) override
Initiate a connection to a remote host.
Ptr< UdpL4Protocol > m_udp
the associated UDP L4 protocol
void SetMtuDiscover(bool discover) override
Set the MTU discover capability.
Ipv6EndPoint * m_endPoint6
the IPv6 endpoint
int MulticastLeaveGroup(uint32_t interfaceIndex, const Address &groupAddress) override
Corresponds to socket option MCAST_LEAVE_GROUP.
void Destroy6()
Kill this socket by zeroing its attributes (IPv6)
void DeallocateEndPoint()
Deallocate m_endPoint and m_endPoint6.
int Bind() override
Allocate a local IPv4 endpoint for this socket.
int ShutdownSend() override
SocketErrno GetErrno() const override
Get last error number.
int DoSend(Ptr< Packet > p)
Send a packet.
uint32_t GetRxAvailable() const override
Return number of bytes which can be returned from one or multiple calls to Recv.
bool SetAllowBroadcast(bool allowBroadcast) override
Configure whether broadcast datagram transmissions are allowed.
void SetIpMulticastLoop(bool loop) override
Set the IP multicast loop capability.
Ptr< Node > m_node
the associated node
void ForwardIcmp6(Ipv6Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo)
Called by the L3 protocol when it received an ICMPv6 packet to pass on to TCP.
uint32_t GetRcvBufSize() const override
Get the receiving buffer size.
int ShutdownRecv() override
uint16_t port
Definition dsdv-manet.cc:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.