A Discrete-Event Network Simulator
API
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 
35 namespace ns3
36 {
37 
38 class Ipv4EndPoint;
39 class Ipv6EndPoint;
40 class Node;
41 class Packet;
42 class UdpL4Protocol;
43 class Ipv6Header;
44 class Ipv6Interface;
45 
71 class UdpSocketImpl : public UdpSocket
72 {
73  public:
78  static TypeId GetTypeId();
82  UdpSocketImpl();
83  ~UdpSocketImpl() override;
84 
89  void SetNode(Ptr<Node> node);
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;
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 
141  friend class UdpSocketFactory;
142  // invoked by Udp class
143 
148  int FinishBind();
149 
158  void ForwardUp(Ptr<Packet> packet,
159  Ipv4Header header,
160  uint16_t port,
161  Ptr<Ipv4Interface> incomingInterface);
162 
171  void ForwardUp6(Ptr<Packet> packet,
172  Ipv6Header header,
173  uint16_t port,
174  Ptr<Ipv6Interface> incomingInterface);
175 
182  void Destroy();
183 
190  void Destroy6();
191 
195  void DeallocateEndPoint();
196 
202  int DoSend(Ptr<Packet> p);
211  int DoSendTo(Ptr<Packet> p, Ipv4Address daddr, uint16_t dport, uint8_t tos);
219  int DoSendTo(Ptr<Packet> p, Ipv6Address daddr, uint16_t dport);
220 
230  void ForwardIcmp(Ipv4Address icmpSource,
231  uint8_t icmpTtl,
232  uint8_t icmpType,
233  uint8_t icmpCode,
234  uint32_t icmpInfo);
235 
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
260 
262  uint16_t m_defaultPort;
264 
268  bool m_connected;
270 
271  std::queue<std::pair<Ptr<Packet>, Address>> m_deliveryQueue;
272  uint32_t m_rxAvailable;
273 
274  // Socket attributes
275  uint32_t m_rcvBufSize;
277  int32_t m_ipMulticastIf;
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
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
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.