A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("Ipv4RawSocketImpl");
17 
18 namespace ns3 {
19 
20 NS_OBJECT_ENSURE_REGISTERED (Ipv4RawSocketImpl);
21 
22 TypeId
24 {
25  static TypeId tid = TypeId ("ns3::Ipv4RawSocketImpl")
26  .SetParent<Socket> ()
27  .AddAttribute ("Protocol", "Protocol number to match.",
28  UintegerValue (0),
29  MakeUintegerAccessor (&Ipv4RawSocketImpl::m_protocol),
30  MakeUintegerChecker<uint16_t> ())
31  .AddAttribute ("IcmpFilter",
32  "Any icmp header whose type field matches a bit in this filter is dropped. Type must be less than 32.",
33  UintegerValue (0),
34  MakeUintegerAccessor (&Ipv4RawSocketImpl::m_icmpFilter),
35  MakeUintegerChecker<uint32_t> ())
36  //
37  // from raw (7), linux, returned length of Send/Recv should be
38  //
39  // | IP_HDRINC on | off |
40  // ----------+---------------+-------------+-
41  // Send(Ipv4)| hdr + payload | payload |
42  // Recv(Ipv4)| hdr + payload | hdr+payload |
43  // ----------+---------------+-------------+-
44  .AddAttribute ("IpHeaderInclude",
45  "Include IP Header information (a.k.a setsockopt (IP_HDRINCL)).",
46  BooleanValue (false),
47  MakeBooleanAccessor (&Ipv4RawSocketImpl::m_iphdrincl),
48  MakeBooleanChecker ())
49  ;
50  return tid;
51 }
52 
54 {
55  NS_LOG_FUNCTION (this);
57  m_node = 0;
60  m_protocol = 0;
61  m_shutdownSend = false;
62  m_shutdownRecv = false;
63 }
64 
65 void
67 {
68  NS_LOG_FUNCTION (this << node);
69  m_node = node;
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION (this);
76  m_node = 0;
78 }
79 
82 {
83  NS_LOG_FUNCTION (this);
84  return m_err;
85 }
86 
89 {
90  NS_LOG_FUNCTION (this);
91  return NS3_SOCK_RAW;
92 }
93 
94 Ptr<Node>
96 {
97  NS_LOG_FUNCTION (this);
98  return m_node;
99 }
100 int
102 {
103  NS_LOG_FUNCTION (this << address);
104  if (!InetSocketAddress::IsMatchingType (address))
105  {
107  return -1;
108  }
110  m_src = ad.GetIpv4 ();
111  return 0;
112 }
113 int
115 {
116  NS_LOG_FUNCTION (this);
118  return 0;
119 }
120 int
122 {
123  NS_LOG_FUNCTION (this);
124  return (-1);
125 }
126 int
128 {
129  NS_LOG_FUNCTION (this << address);
130  address = InetSocketAddress (m_src, 0);
131  return 0;
132 }
133 int
135 {
136  NS_LOG_FUNCTION (this);
137  Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
138  if (ipv4 != 0)
139  {
140  ipv4->DeleteRawSocket (this);
141  }
142  return 0;
143 }
144 int
146 {
147  NS_LOG_FUNCTION (this);
148  m_shutdownSend = true;
149  return 0;
150 }
151 int
153 {
154  NS_LOG_FUNCTION (this);
155  m_shutdownRecv = true;
156  return 0;
157 }
158 int
160 {
161  NS_LOG_FUNCTION (this << address);
162  if (!InetSocketAddress::IsMatchingType (address))
163  {
165  return -1;
166  }
168  m_dst = ad.GetIpv4 ();
169  return 0;
170 }
171 int
173 {
174  NS_LOG_FUNCTION (this);
176  return -1;
177 }
178 uint32_t
180 {
181  NS_LOG_FUNCTION (this);
182  return 0xffffffff;
183 }
184 int
186 {
187  NS_LOG_FUNCTION (this << p << flags);
189  return SendTo (p, flags, to);
190 }
191 int
193  const Address &toAddress)
194 {
195  NS_LOG_FUNCTION (this << p << flags << toAddress);
196  if (!InetSocketAddress::IsMatchingType (toAddress))
197  {
199  return -1;
200  }
201  if (m_shutdownSend)
202  {
203  return 0;
204  }
206  Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
207  Ipv4Address dst = ad.GetIpv4 ();
208  Ipv4Address src = m_src;
209  if (ipv4->GetRoutingProtocol ())
210  {
211  Ipv4Header header;
212  if (!m_iphdrincl)
213  {
214  header.SetDestination (dst);
215  header.SetProtocol (m_protocol);
216  }
217  else
218  {
219  p->RemoveHeader (header);
220  dst = header.GetDestination ();
221  src = header.GetSource ();
222  }
223  SocketErrno errno_ = ERROR_NOTERROR; //do not use errno as it is the standard C last error number
224  Ptr<Ipv4Route> route;
225  Ptr<NetDevice> oif = m_boundnetdevice; //specify non-zero if bound to a source address
226  if (!oif && src != Ipv4Address::GetAny ())
227  {
228  int32_t index = ipv4->GetInterfaceForAddress (src);
229  NS_ASSERT (index >= 0);
230  oif = ipv4->GetNetDevice (index);
231  NS_LOG_LOGIC ("Set index " << oif << "from source " << src);
232  }
233 
234  // TBD-- we could cache the route and just check its validity
235  route = ipv4->GetRoutingProtocol ()->RouteOutput (p, header, oif, errno_);
236  if (route != 0)
237  {
238  NS_LOG_LOGIC ("Route exists");
239  if (!m_iphdrincl)
240  {
241  ipv4->Send (p, route->GetSource (), dst, m_protocol, route);
242  }
243  else
244  {
245  ipv4->SendWithHeader (p, header, route);
246  }
247  NotifyDataSent (p->GetSize ());
249  return p->GetSize ();
250  }
251  else
252  {
253  NS_LOG_DEBUG ("dropped because no outgoing route.");
254  return -1;
255  }
256  }
257  return 0;
258 }
259 uint32_t
261 {
262  NS_LOG_FUNCTION (this);
263  uint32_t rx = 0;
264  for (std::list<Data>::const_iterator i = m_recv.begin (); i != m_recv.end (); ++i)
265  {
266  rx += (i->packet)->GetSize ();
267  }
268  return rx;
269 }
271 Ipv4RawSocketImpl::Recv (uint32_t maxSize, uint32_t flags)
272 {
273  NS_LOG_FUNCTION (this << maxSize << flags);
274  Address tmp;
275  return RecvFrom (maxSize, flags, tmp);
276 }
278 Ipv4RawSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags,
279  Address &fromAddress)
280 {
281  NS_LOG_FUNCTION (this << maxSize << flags << fromAddress);
282  if (m_recv.empty ())
283  {
284  return 0;
285  }
286  struct Data data = m_recv.front ();
287  m_recv.pop_front ();
289  fromAddress = inet;
290  if (data.packet->GetSize () > maxSize)
291  {
292  Ptr<Packet> first = data.packet->CreateFragment (0, maxSize);
293  if (!(flags & MSG_PEEK))
294  {
295  data.packet->RemoveAtStart (maxSize);
296  }
297  m_recv.push_front (data);
298  return first;
299  }
300  return data.packet;
301 }
302 
303 void
305 {
306  NS_LOG_FUNCTION (this << protocol);
307  m_protocol = protocol;
308 }
309 
310 bool
312 {
313  NS_LOG_FUNCTION (this << *p << ipHeader << incomingInterface);
314  if (m_shutdownRecv)
315  {
316  return false;
317  }
318 
319  Ptr<NetDevice> boundNetDevice = Socket::GetBoundNetDevice();
320  if (boundNetDevice)
321  {
322  if (boundNetDevice != incomingInterface->GetDevice())
323  {
324  return false;
325  }
326  }
327 
328  NS_LOG_LOGIC ("src = " << m_src << " dst = " << m_dst);
329  if ((m_src == Ipv4Address::GetAny () || ipHeader.GetDestination () == m_src) &&
330  (m_dst == Ipv4Address::GetAny () || ipHeader.GetSource () == m_dst) &&
331  ipHeader.GetProtocol () == m_protocol)
332  {
333  Ptr<Packet> copy = p->Copy ();
334  // Should check via getsockopt ()..
335  if (IsRecvPktInfo ())
336  {
337  Ipv4PacketInfoTag tag;
338  copy->RemovePacketTag (tag);
339  tag.SetRecvIf (incomingInterface->GetDevice ()->GetIfIndex ());
340  copy->AddPacketTag (tag);
341  }
342  if (m_protocol == 1)
343  {
344  Icmpv4Header icmpHeader;
345  copy->PeekHeader (icmpHeader);
346  uint8_t type = icmpHeader.GetType ();
347  if (type < 32 &&
348  ((uint32_t(1) << type) & m_icmpFilter))
349  {
350  // filter out icmp packet.
351  return false;
352  }
353  }
354  copy->AddHeader (ipHeader);
355  struct Data data;
356  data.packet = copy;
357  data.fromIp = ipHeader.GetSource ();
358  data.fromProtocol = ipHeader.GetProtocol ();
359  m_recv.push_back (data);
360  NotifyDataRecv ();
361  return true;
362  }
363  return false;
364 }
365 
366 bool
368 {
369  NS_LOG_FUNCTION (this << allowBroadcast);
370  if (!allowBroadcast)
371  {
372  return false;
373  }
374  return true;
375 }
376 
377 bool
379 {
380  NS_LOG_FUNCTION (this);
381  return true;
382 }
383 
384 } // 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 "...
Hold a bool native type.
Definition: boolean.h:38
Ptr< Packet > packet
Packet data.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
void NotifyDataRecv(void)
Notify through the callback (if set) that some data have been received.
Definition: socket.cc:304
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:174
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:841
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:61
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:170
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:744
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
void SetProtocol(uint8_t num)
Definition: ipv4-header.cc:278
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
Doxygen 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:363
a polymophic address class
Definition: address.h:86
virtual int Close(void)
Close a socket.
Ptr< NetDevice > GetBoundNetDevice()
Returns socket's bound netdevice, if any.
Definition: socket.cc:350
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:353
bool m_shutdownSend
Flag to shutdown send capability.
Hold an unsigned integer type.
Definition: uinteger.h:46
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:284
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:233
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:277
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.
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)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: socket.cc:314
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Ptr< NetDevice > m_boundnetdevice
the device this socket is bound to (might be null).
Definition: socket.h:911
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:848
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
uint16_t m_protocol
Protocol.
uint8_t GetType(void) const
Get ICMP type.
Definition: icmpv4.cc:120
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
uint32_t m_icmpFilter
ICMPv4 filter specification.
Ptr< T > GetObject(void) const
Definition: object.h:362
SocketType
Enumeration of the possible socket types.
Definition: socket.h:104
a unique identifier for an interface.
Definition: type-id.h:49
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:294
uint16_t fromProtocol
Protocol used.
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
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:253
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.