A Discrete-Event Network Simulator
API
ipv4-raw-socket-impl.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 #include <netinet/in.h>
3 #include <sys/socket.h>
4 #include <sys/types.h>
5 #include "ipv4-raw-socket-impl.h"
6 #include "ipv4-l3-protocol.h"
7 #include "icmpv4.h"
8 #include "ns3/ipv4-packet-info-tag.h"
9 #include "ns3/inet-socket-address.h"
10 #include "ns3/node.h"
11 #include "ns3/packet.h"
12 #include "ns3/uinteger.h"
13 #include "ns3/boolean.h"
14 #include "ns3/log.h"
15 
16 namespace ns3 {
17 
18 NS_LOG_COMPONENT_DEFINE ("Ipv4RawSocketImpl");
19 
20 NS_OBJECT_ENSURE_REGISTERED (Ipv4RawSocketImpl);
21 
22 TypeId
24 {
25  static TypeId tid = TypeId ("ns3::Ipv4RawSocketImpl")
26  .SetParent<Socket> ()
27  .SetGroupName ("Internet")
28  .AddAttribute ("Protocol", "Protocol number to match.",
29  UintegerValue (0),
31  MakeUintegerChecker<uint16_t> ())
32  .AddAttribute ("IcmpFilter",
33  "Any icmp header whose type field matches a bit in this filter is dropped. Type must be less than 32.",
34  UintegerValue (0),
36  MakeUintegerChecker<uint32_t> ())
37  //
38  // from raw (7), linux, returned length of Send/Recv should be
39  //
40  // | IP_HDRINC on | off |
41  // ----------+---------------+-------------+-
42  // Send(Ipv4)| hdr + payload | payload |
43  // Recv(Ipv4)| hdr + payload | hdr+payload |
44  // ----------+---------------+-------------+-
45  .AddAttribute ("IpHeaderInclude",
46  "Include IP Header information (a.k.a setsockopt (IP_HDRINCL)).",
47  BooleanValue (false),
50  ;
51  return tid;
52 }
53 
55 {
56  NS_LOG_FUNCTION (this);
58  m_node = 0;
61  m_protocol = 0;
62  m_shutdownSend = false;
63  m_shutdownRecv = false;
64 }
65 
66 void
68 {
69  NS_LOG_FUNCTION (this << node);
70  m_node = node;
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION (this);
77  m_node = 0;
79 }
80 
83 {
84  NS_LOG_FUNCTION (this);
85  return m_err;
86 }
87 
90 {
91  NS_LOG_FUNCTION (this);
92  return NS3_SOCK_RAW;
93 }
94 
95 Ptr<Node>
97 {
98  NS_LOG_FUNCTION (this);
99  return m_node;
100 }
101 int
103 {
104  NS_LOG_FUNCTION (this << address);
105  if (!InetSocketAddress::IsMatchingType (address))
106  {
108  return -1;
109  }
111  m_src = ad.GetIpv4 ();
112  return 0;
113 }
114 int
116 {
117  NS_LOG_FUNCTION (this);
119  return 0;
120 }
121 int
123 {
124  NS_LOG_FUNCTION (this);
125  return (-1);
126 }
127 int
129 {
130  NS_LOG_FUNCTION (this << address);
131  address = InetSocketAddress (m_src, 0);
132  return 0;
133 }
134 int
136 {
137  NS_LOG_FUNCTION (this << address);
138 
139  if (m_dst == Ipv4Address::GetAny ())
140  {
142  return -1;
143  }
144 
145  address = InetSocketAddress (m_dst, 0);
146 
147  return 0;
148 }
149 int
151 {
152  NS_LOG_FUNCTION (this);
153  Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
154  if (ipv4 != 0)
155  {
156  ipv4->DeleteRawSocket (this);
157  }
158  return 0;
159 }
160 int
162 {
163  NS_LOG_FUNCTION (this);
164  m_shutdownSend = true;
165  return 0;
166 }
167 int
169 {
170  NS_LOG_FUNCTION (this);
171  m_shutdownRecv = true;
172  return 0;
173 }
174 int
176 {
177  NS_LOG_FUNCTION (this << address);
178  if (!InetSocketAddress::IsMatchingType (address))
179  {
181  return -1;
182  }
184  m_dst = ad.GetIpv4 ();
185  return 0;
186 }
187 int
189 {
190  NS_LOG_FUNCTION (this);
192  return -1;
193 }
194 uint32_t
196 {
197  NS_LOG_FUNCTION (this);
198  return 0xffffffff;
199 }
200 int
202 {
203  NS_LOG_FUNCTION (this << p << flags);
205  return SendTo (p, flags, to);
206 }
207 int
209  const Address &toAddress)
210 {
211  NS_LOG_FUNCTION (this << p << flags << toAddress);
212  if (!InetSocketAddress::IsMatchingType (toAddress))
213  {
215  return -1;
216  }
217  if (m_shutdownSend)
218  {
219  return 0;
220  }
222  Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
223  Ipv4Address dst = ad.GetIpv4 ();
224  Ipv4Address src = m_src;
225  if (ipv4->GetRoutingProtocol ())
226  {
227  Ipv4Header header;
228  if (!m_iphdrincl)
229  {
230  header.SetDestination (dst);
231  header.SetProtocol (m_protocol);
232  }
233  else
234  {
235  p->RemoveHeader (header);
236  dst = header.GetDestination ();
237  src = header.GetSource ();
238  }
239  SocketErrno errno_ = ERROR_NOTERROR; //do not use errno as it is the standard C last error number
240  Ptr<Ipv4Route> route;
241  Ptr<NetDevice> oif = m_boundnetdevice; //specify non-zero if bound to a source address
242  if (!oif && src != Ipv4Address::GetAny ())
243  {
244  int32_t index = ipv4->GetInterfaceForAddress (src);
245  NS_ASSERT (index >= 0);
246  oif = ipv4->GetNetDevice (index);
247  NS_LOG_LOGIC ("Set index " << oif << "from source " << src);
248  }
249 
250  // TBD-- we could cache the route and just check its validity
251  route = ipv4->GetRoutingProtocol ()->RouteOutput (p, header, oif, errno_);
252  if (route != 0)
253  {
254  NS_LOG_LOGIC ("Route exists");
255  uint32_t pktSize = p->GetSize ();
256  if (!m_iphdrincl)
257  {
258  ipv4->Send (p, route->GetSource (), dst, m_protocol, route);
259  }
260  else
261  {
262  pktSize += header.GetSerializedSize ();
263  ipv4->SendWithHeader (p, header, route);
264  }
265  NotifyDataSent (pktSize);
267  return pktSize;
268  }
269  else
270  {
271  NS_LOG_DEBUG ("dropped because no outgoing route.");
272  return -1;
273  }
274  }
275  return 0;
276 }
277 uint32_t
279 {
280  NS_LOG_FUNCTION (this);
281  uint32_t rx = 0;
282  for (std::list<Data>::const_iterator i = m_recv.begin (); i != m_recv.end (); ++i)
283  {
284  rx += (i->packet)->GetSize ();
285  }
286  return rx;
287 }
289 Ipv4RawSocketImpl::Recv (uint32_t maxSize, uint32_t flags)
290 {
291  NS_LOG_FUNCTION (this << maxSize << flags);
292  Address tmp;
293  return RecvFrom (maxSize, flags, tmp);
294 }
296 Ipv4RawSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags,
297  Address &fromAddress)
298 {
299  NS_LOG_FUNCTION (this << maxSize << flags << fromAddress);
300  if (m_recv.empty ())
301  {
302  return 0;
303  }
304  struct Data data = m_recv.front ();
305  m_recv.pop_front ();
307  fromAddress = inet;
308  if (data.packet->GetSize () > maxSize)
309  {
310  Ptr<Packet> first = data.packet->CreateFragment (0, maxSize);
311  if (!(flags & MSG_PEEK))
312  {
313  data.packet->RemoveAtStart (maxSize);
314  }
315  m_recv.push_front (data);
316  return first;
317  }
318  return data.packet;
319 }
320 
321 void
323 {
324  NS_LOG_FUNCTION (this << protocol);
325  m_protocol = protocol;
326 }
327 
328 bool
330 {
331  NS_LOG_FUNCTION (this << *p << ipHeader << incomingInterface);
332  if (m_shutdownRecv)
333  {
334  return false;
335  }
336 
337  Ptr<NetDevice> boundNetDevice = Socket::GetBoundNetDevice();
338  if (boundNetDevice)
339  {
340  if (boundNetDevice != incomingInterface->GetDevice())
341  {
342  return false;
343  }
344  }
345 
346  NS_LOG_LOGIC ("src = " << m_src << " dst = " << m_dst);
347  if ((m_src == Ipv4Address::GetAny () || ipHeader.GetDestination () == m_src) &&
348  (m_dst == Ipv4Address::GetAny () || ipHeader.GetSource () == m_dst) &&
349  ipHeader.GetProtocol () == m_protocol)
350  {
351  Ptr<Packet> copy = p->Copy ();
352  // Should check via getsockopt ()..
353  if (IsRecvPktInfo ())
354  {
355  Ipv4PacketInfoTag tag;
356  copy->RemovePacketTag (tag);
357  tag.SetRecvIf (incomingInterface->GetDevice ()->GetIfIndex ());
358  copy->AddPacketTag (tag);
359  }
360  if (m_protocol == 1)
361  {
362  Icmpv4Header icmpHeader;
363  copy->PeekHeader (icmpHeader);
364  uint8_t type = icmpHeader.GetType ();
365  if (type < 32 &&
366  ((uint32_t(1) << type) & m_icmpFilter))
367  {
368  // filter out icmp packet.
369  return false;
370  }
371  }
372  copy->AddHeader (ipHeader);
373  struct Data data;
374  data.packet = copy;
375  data.fromIp = ipHeader.GetSource ();
376  data.fromProtocol = ipHeader.GetProtocol ();
377  m_recv.push_back (data);
378  NotifyDataRecv ();
379  return true;
380  }
381  return false;
382 }
383 
384 bool
386 {
387  NS_LOG_FUNCTION (this << allowBroadcast);
388  if (!allowBroadcast)
389  {
390  return false;
391  }
392  return true;
393 }
394 
395 bool
397 {
398  NS_LOG_FUNCTION (this);
399  return true;
400 }
401 
402 } // namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
Ipv4Address m_src
Source address.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)
Send data to a specified peer.
an Inet address class
Ipv4Address GetIpv4(void) const
void SetDestination(Ipv4Address destination)
Definition: ipv4-header.cc:298
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
AttributeValue implementation for Boolean.
Definition: boolean.h:34
Ptr< Packet > packet
Packet data.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
void NotifyDataRecv(void)
Notify through the callback (if set) that some data have been received.
Definition: socket.cc:305
virtual bool SetAllowBroadcast(bool allowBroadcast)
Configure whether broadcast datagram transmissions are allowed.
Ptr< Packet > Recv(void)
Read a single packet from the socket.
Definition: socket.cc:175
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:81
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:824
virtual int Send(Ptr< Packet > p, uint32_t flags)
Send data (or dummy data) to the remote host.
Ipv4Address GetDestination(void) const
Definition: ipv4-header.cc:304
static TypeId GetTypeId(void)
Get the type ID of this class.
Ipv4Address fromIp
Source address.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
uint8_t GetProtocol(void) const
Definition: ipv4-header.cc:272
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
void SetNode(Ptr< Node > node)
Set the node associated with this socket.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:786
virtual uint32_t GetRxAvailable(void) const
Return number of bytes which can be returned from one or multiple calls to Recv.
Ipv4Address GetSource(void) const
Definition: ipv4-header.cc:291
SocketType
Enumeration of the possible socket types.
Definition: socket.h:104
void SetProtocol(uint8_t num)
Definition: ipv4-header.cc:278
virtual void DoDispose(void)
Destructor implementation.
Introspection did not find any typical Config paths.
Definition: icmpv4.h:33
bool IsRecvPktInfo() const
Get status indicating whether enable/disable packet information to socket.
Definition: socket.cc:364
a polymophic address class
Definition: address.h:90
virtual int Close(void)
Close a socket.
Ptr< NetDevice > GetBoundNetDevice()
Returns socket's bound netdevice, if any.
Definition: socket.cc:351
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
Definition: packet.cc:228
Packet header for IPv4.
Definition: ipv4-header.h:31
A low-level Socket API based loosely on the BSD Socket API.
Definition: socket.h:66
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:340
bool m_shutdownSend
Flag to shutdown send capability.
Hold an unsigned integer type.
Definition: uinteger.h:44
Ipv4Address m_dst
Destination address.
void NotifyDataSent(uint32_t size)
Notify through the callback (if set) that some data have been sent.
Definition: socket.cc:285
Ipv4Address GetSource(void) const
Definition: ipv4-route.cc:56
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:278
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)
Read a single packet from the socket and retrieve the sender address.
virtual int Bind6()
Allocate a local IPv6 endpoint for this socket.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
virtual int Bind()
Allocate a local IPv4 endpoint for this socket.
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
std::list< struct Data > m_recv
Packet waiting to be processed.
virtual int ShutdownRecv(void)
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
virtual enum Socket::SocketType GetSocketType(void) const
Get socket type (NS3_SOCK_RAW)
virtual bool GetAllowBroadcast() const
Query whether broadcast datagram transmissions are allowed.
virtual void DoDispose(void)
Destructor implementation.
Definition: socket.cc:315
virtual int GetPeerName(Address &address) const
Get the peer address of a connected socket.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ptr< NetDevice > m_boundnetdevice
the device this socket is bound to (might be null).
Definition: socket.h:967
virtual uint32_t GetTxAvailable(void) const
Returns the number of bytes which can be sent in a single call to Send.
Ptr< NetDevice > GetDevice(void) const
virtual int Listen(void)
Listen for incoming connections.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:831
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
uint16_t m_protocol
Protocol.
uint8_t GetType(void) const
Get ICMP type.
Definition: icmpv4.cc:121
IPv4 raw data and additional information.
bool ForwardUp(Ptr< const Packet > p, Ipv4Header ipHeader, Ptr< Ipv4Interface > incomingInterface)
Forward up to receive method.
virtual Ptr< Node > GetNode(void) const
Return the node this socket is associated with.
tuple address
Definition: first.py:37
Definition: first.py:1
uint32_t m_icmpFilter
ICMPv4 filter specification.
virtual uint32_t GetSerializedSize(void) const
Definition: ipv4-header.cc:375
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:45
a unique identifier for an interface.
Definition: type-id.h:58
bool m_shutdownRecv
Flag to shutdown receive capability.
void NotifySend(uint32_t spaceAvailable)
Notify through the callback (if set) that some data have been sent.
Definition: socket.cc:295
uint16_t fromProtocol
Protocol used.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
void SetProtocol(uint16_t protocol)
Set protocol field.
static bool IsMatchingType(const Address &address)
virtual enum Socket::SocketErrno GetErrno() const
Get last error number.
bool m_iphdrincl
Include IP Header information (a.k.a setsockopt (IP_HDRINCL))
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257
virtual int ShutdownSend(void)
enum Socket::SocketErrno m_err
Last error number.
virtual int GetSockName(Address &address) const
Get socket address.
void SetRecvIf(uint32_t ifindex)
Set the tag's receiving interface.