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