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 
23 TypeId
25 {
26  static TypeId tid = TypeId ("ns3::Ipv4RawSocketImpl")
27  .SetParent<Socket> ()
28  .AddAttribute ("Protocol", "Protocol number to match.",
29  UintegerValue (0),
30  MakeUintegerAccessor (&Ipv4RawSocketImpl::m_protocol),
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),
35  MakeUintegerAccessor (&Ipv4RawSocketImpl::m_icmpFilter),
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),
48  MakeBooleanAccessor (&Ipv4RawSocketImpl::m_iphdrincl),
49  MakeBooleanChecker ())
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);
138  Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
139  if (ipv4 != 0)
140  {
141  ipv4->DeleteRawSocket (this);
142  }
143  return 0;
144 }
145 int
147 {
148  NS_LOG_FUNCTION (this);
149  m_shutdownSend = true;
150  return 0;
151 }
152 int
154 {
155  NS_LOG_FUNCTION (this);
156  m_shutdownRecv = true;
157  return 0;
158 }
159 int
161 {
162  NS_LOG_FUNCTION (this << address);
163  if (!InetSocketAddress::IsMatchingType (address))
164  {
166  return -1;
167  }
169  m_dst = ad.GetIpv4 ();
170  return 0;
171 }
172 int
174 {
175  NS_LOG_FUNCTION (this);
177  return -1;
178 }
179 uint32_t
181 {
182  NS_LOG_FUNCTION (this);
183  return 0xffffffff;
184 }
185 int
187 {
188  NS_LOG_FUNCTION (this << p << flags);
190  return SendTo (p, flags, to);
191 }
192 int
194  const Address &toAddress)
195 {
196  NS_LOG_FUNCTION (this << p << flags << toAddress);
197  if (!InetSocketAddress::IsMatchingType (toAddress))
198  {
200  return -1;
201  }
202  if (m_shutdownSend)
203  {
204  return 0;
205  }
207  Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
208  Ipv4Address dst = ad.GetIpv4 ();
209  Ipv4Address src = m_src;
210  if (ipv4->GetRoutingProtocol ())
211  {
212  Ipv4Header header;
213  if (!m_iphdrincl)
214  {
215  header.SetDestination (dst);
216  header.SetProtocol (m_protocol);
217  }
218  else
219  {
220  p->RemoveHeader (header);
221  dst = header.GetDestination ();
222  src = header.GetSource ();
223  }
224  SocketErrno errno_ = ERROR_NOTERROR; //do not use errno as it is the standard C last error number
225  Ptr<Ipv4Route> route;
226  Ptr<NetDevice> oif = m_boundnetdevice; //specify non-zero if bound to a source address
227  if (!oif && src != Ipv4Address::GetAny ())
228  {
229  int32_t index = ipv4->GetInterfaceForAddress (src);
230  NS_ASSERT (index >= 0);
231  oif = ipv4->GetNetDevice (index);
232  NS_LOG_LOGIC ("Set index " << oif << "from source " << src);
233  }
234 
235  // TBD-- we could cache the route and just check its validity
236  route = ipv4->GetRoutingProtocol ()->RouteOutput (p, header, oif, errno_);
237  if (route != 0)
238  {
239  NS_LOG_LOGIC ("Route exists");
240  if (!m_iphdrincl)
241  {
242  ipv4->Send (p, route->GetSource (), dst, m_protocol, route);
243  }
244  else
245  {
246  ipv4->SendWithHeader (p, header, route);
247  }
248  NotifyDataSent (p->GetSize ());
250  return p->GetSize ();
251  }
252  else
253  {
254  NS_LOG_DEBUG ("dropped because no outgoing route.");
255  return -1;
256  }
257  }
258  return 0;
259 }
260 uint32_t
262 {
263  NS_LOG_FUNCTION (this);
264  uint32_t rx = 0;
265  for (std::list<Data>::const_iterator i = m_recv.begin (); i != m_recv.end (); ++i)
266  {
267  rx += (i->packet)->GetSize ();
268  }
269  return rx;
270 }
272 Ipv4RawSocketImpl::Recv (uint32_t maxSize, uint32_t flags)
273 {
274  NS_LOG_FUNCTION (this << maxSize << flags);
275  Address tmp;
276  return RecvFrom (maxSize, flags, tmp);
277 }
279 Ipv4RawSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags,
280  Address &fromAddress)
281 {
282  NS_LOG_FUNCTION (this << maxSize << flags << fromAddress);
283  if (m_recv.empty ())
284  {
285  return 0;
286  }
287  struct Data data = m_recv.front ();
288  m_recv.pop_front ();
290  fromAddress = inet;
291  if (data.packet->GetSize () > maxSize)
292  {
293  Ptr<Packet> first = data.packet->CreateFragment (0, maxSize);
294  if (!(flags & MSG_PEEK))
295  {
296  data.packet->RemoveAtStart (maxSize);
297  }
298  m_recv.push_front (data);
299  return first;
300  }
301  return data.packet;
302 }
303 
304 void
306 {
307  NS_LOG_FUNCTION (this << protocol);
308  m_protocol = protocol;
309 }
310 
311 bool
313 {
314  NS_LOG_FUNCTION (this << *p << ipHeader << incomingInterface);
315  if (m_shutdownRecv)
316  {
317  return false;
318  }
319 
320  Ptr<NetDevice> boundNetDevice = Socket::GetBoundNetDevice();
321  if (boundNetDevice)
322  {
323  if (boundNetDevice != incomingInterface->GetDevice())
324  {
325  return false;
326  }
327  }
328 
329  NS_LOG_LOGIC ("src = " << m_src << " dst = " << m_dst);
330  if ((m_src == Ipv4Address::GetAny () || ipHeader.GetDestination () == m_src) &&
331  (m_dst == Ipv4Address::GetAny () || ipHeader.GetSource () == m_dst) &&
332  ipHeader.GetProtocol () == m_protocol)
333  {
334  Ptr<Packet> copy = p->Copy ();
335  // Should check via getsockopt ()..
336  if (IsRecvPktInfo ())
337  {
338  Ipv4PacketInfoTag tag;
339  copy->RemovePacketTag (tag);
340  tag.SetRecvIf (incomingInterface->GetDevice ()->GetIfIndex ());
341  copy->AddPacketTag (tag);
342  }
343  if (m_protocol == 1)
344  {
345  Icmpv4Header icmpHeader;
346  copy->PeekHeader (icmpHeader);
347  uint8_t type = icmpHeader.GetType ();
348  if (type < 32 &&
349  ((uint32_t(1) << type) & m_icmpFilter))
350  {
351  // filter out icmp packet.
352  return false;
353  }
354  }
355  copy->AddHeader (ipHeader);
356  struct Data data;
357  data.packet = copy;
358  data.fromIp = ipHeader.GetSource ();
359  data.fromProtocol = ipHeader.GetProtocol ();
360  m_recv.push_back (data);
361  NotifyDataRecv ();
362  return true;
363  }
364  return false;
365 }
366 
367 bool
369 {
370  NS_LOG_FUNCTION (this << allowBroadcast);
371  if (!allowBroadcast)
372  {
373  return false;
374  }
375  return true;
376 }
377 
378 bool
380 {
381  NS_LOG_FUNCTION (this);
382  return true;
383 }
384 
385 } // 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)
Definition: log.h:345
Hold a bool native type.
Definition: boolean.h:38
Ptr< Packet > packet
Packet data.
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
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)
Definition: assert.h:64
uint8_t GetProtocol(void) const
Definition: ipv4-header.cc:272
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
void SetNode(Ptr< Node > node)
Set the node associated with this socket.
uint32_t GetSize(void) const
Definition: packet.h:650
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
NS_LOG_COMPONENT_DEFINE("Ipv4RawSocketImpl")
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:364
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: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: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:285
Ipv4Address GetSource(void) const
Definition: ipv4-route.cc:56
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
Ptr< Packet > Copy(void) const
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)
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:315
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)
Definition: log.h:289
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
uint32_t m_icmpFilter
ICMPv4 filter specification.
Ptr< T > GetObject(void) const
Definition: object.h:361
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:295
uint16_t fromProtocol
Protocol used.
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
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.