A Discrete-Event Network Simulator
API
ipv6-raw-socket-impl.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2009 Strasbourg University
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include "ns3/inet6-socket-address.h"
25 #include "ns3/node.h"
26 #include "ns3/packet.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/log.h"
29 #include "ns3/ipv6-route.h"
30 #include "ns3/ipv6-routing-protocol.h"
31 #include "ns3/ipv6-packet-info-tag.h"
32 
33 #include "ipv6-l3-protocol.h"
34 #include "ipv6-raw-socket-impl.h"
35 #include "icmpv6-header.h"
36 #include "icmpv6-l4-protocol.h"
37 
38 namespace ns3
39 {
40 
41 NS_LOG_COMPONENT_DEFINE ("Ipv6RawSocketImpl");
42 
43 NS_OBJECT_ENSURE_REGISTERED (Ipv6RawSocketImpl);
44 
46 {
47  static TypeId tid = TypeId ("ns3::Ipv6RawSocketImpl")
48  .SetParent<Socket> ()
49  .AddAttribute ("Protocol", "Protocol number to match.",
50  UintegerValue (0),
52  MakeUintegerChecker<uint16_t> ())
53  ;
54  return tid;
55 }
56 
58 {
61  m_node = 0;
64  m_protocol = 0;
65  m_shutdownSend = false;
66  m_shutdownRecv = false;
68 }
69 
71 {
72 }
73 
75 {
77  m_node = 0;
79 }
80 
82 {
83  NS_LOG_FUNCTION (this << node);
84  m_node = node;
85 }
86 
88 {
89  return m_node;
90 }
91 
93 {
95  return m_err;
96 }
97 
99 {
100  return NS3_SOCK_RAW;
101 }
102 
104 {
105  NS_LOG_FUNCTION (this << address);
106 
107  if (!Inet6SocketAddress::IsMatchingType (address))
108  {
110  return -1;
111  }
113  m_src = ad.GetIpv6 ();
114  return 0;
115 }
116 
118 {
121  return 0;
122 }
123 
125 {
126  return(Bind());
127 }
128 
130 {
132  address = Inet6SocketAddress (m_src, 0);
133  return 0;
134 }
135 
137 {
140 
141  if (ipv6)
142  {
143  ipv6->DeleteRawSocket (this);
144  }
145  return 0;
146 }
147 
149 {
151  m_shutdownSend = true;
152  return 0;
153 }
154 
156 {
158  m_shutdownRecv = true;
159  return 0;
160 }
161 
163 {
164  NS_LOG_FUNCTION (this << address);
165 
166  if (!Inet6SocketAddress::IsMatchingType (address))
167  {
169  return -1;
170  }
171 
173  m_dst = ad.GetIpv6 ();
174  return 0;
175 }
176 
178 {
181  return -1;
182 }
183 
184 int Ipv6RawSocketImpl::Send (Ptr<Packet> p, uint32_t flags)
185 {
186  NS_LOG_FUNCTION (this << p << flags);
188  return SendTo (p, flags, to);
189 }
190 
191 int Ipv6RawSocketImpl::SendTo (Ptr<Packet> p, uint32_t flags, const Address& toAddress)
192 {
193  NS_LOG_FUNCTION (this << p << flags << toAddress);
194 
195  if (!Inet6SocketAddress::IsMatchingType (toAddress))
196  {
198  return -1;
199  }
200 
201  if (m_shutdownSend)
202  {
203  return 0;
204  }
205 
208  Ipv6Address dst = ad.GetIpv6 ();
209 
210  if (ipv6->GetRoutingProtocol ())
211  {
212  Ipv6Header hdr;
213  hdr.SetDestinationAddress (dst);
215  Ptr<Ipv6Route> route = 0;
216  Ptr<NetDevice> oif (0); /*specify non-zero if bound to a source address */
217 
218  if (!m_src.IsAny ())
219  {
220  int32_t index = ipv6->GetInterfaceForAddress (m_src);
221  NS_ASSERT (index >= 0);
222  oif = ipv6->GetNetDevice (index);
223  }
224 
225  route = ipv6->GetRoutingProtocol ()->RouteOutput (p, hdr, oif, err);
226 
227  if (route)
228  {
229  NS_LOG_LOGIC ("Route exists");
231  {
232  /* calculate checksum here for ICMPv6 echo request (sent by ping6)
233  * as we cannot determine source IPv6 address at application level
234  */
235  uint8_t type;
236  p->CopyData (&type, sizeof(type));
238  {
239  Icmpv6Echo hdr (1);
240  p->RemoveHeader (hdr);
242  p->AddHeader (hdr);
243  }
244  }
245 
246  if (m_src.IsAny ())
247  {
248  ipv6->Send (p, route->GetSource (), dst, m_protocol, route);
249  }
250  else
251  {
252  ipv6->Send (p, m_src, dst, m_protocol, route);
253  }
254  // Return only payload size (as Linux does).
255  return p->GetSize () - hdr.GetSerializedSize ();
256  }
257  else
258  {
259  NS_LOG_DEBUG ("No route, dropped!");
260  }
261  }
262  return 0;
263 }
264 
265 Ptr<Packet> Ipv6RawSocketImpl::Recv (uint32_t maxSize, uint32_t flags)
266 {
267  NS_LOG_FUNCTION (this << maxSize << flags);
268  Address tmp;
269  return RecvFrom (maxSize, flags, tmp);
270 }
271 
272 Ptr<Packet> Ipv6RawSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags, Address& fromAddress)
273 {
274  NS_LOG_FUNCTION (this << maxSize << flags << fromAddress);
275 
276  if (m_data.empty ())
277  {
278  return 0;
279  }
280 
281  /* get packet */
282  struct Data data = m_data.front ();
283  m_data.pop_front ();
284  fromAddress = Inet6SocketAddress (data.fromIp, data.fromProtocol);
285  if (data.packet->GetSize () > maxSize)
286  {
287  Ptr<Packet> first = data.packet->CreateFragment (0, maxSize);
288  if (!(flags & MSG_PEEK))
289  {
290  data.packet->RemoveAtStart (maxSize);
291  }
292  m_data.push_front (data);
293  return first;
294  }
295 
296  return data.packet;
297 }
298 
300 {
302  return 0xffffffff;
303 }
304 
306 {
308  uint32_t rx = 0;
309 
310  for (std::list<Data>::const_iterator it = m_data.begin (); it != m_data.end (); ++it)
311  {
312  rx+= (it->packet)->GetSize ();
313  }
314 
315  return rx;
316 }
317 
319 {
320  NS_LOG_FUNCTION (this << *p << hdr << device);
321 
322  if (m_shutdownRecv)
323  {
324  return false;
325  }
326 
327  Ptr<NetDevice> boundNetDevice = Socket::GetBoundNetDevice();
328  if (boundNetDevice)
329  {
330  if (boundNetDevice != device)
331  {
332  return false;
333  }
334  }
335 
336  if ((m_src == Ipv6Address::GetAny () || hdr.GetDestinationAddress () == m_src) &&
337  (m_dst == Ipv6Address::GetAny () || hdr.GetSourceAddress () == m_dst) &&
338  hdr.GetNextHeader () == m_protocol)
339  {
340  Ptr<Packet> copy = p->Copy ();
341 
343  {
344  /* filter */
345  Icmpv6Header icmpHeader;
346  copy->PeekHeader (icmpHeader);
347  uint8_t type = icmpHeader.GetType ();
348 
349  if (Icmpv6FilterWillBlock(type))
350  {
351  /* packet filtered */
352  return false;
353  }
354  }
355 
356  // Should check via getsockopt ()..
357  if (IsRecvPktInfo ())
358  {
359  Ipv6PacketInfoTag tag;
360  copy->RemovePacketTag (tag);
361  tag.SetRecvIf (device->GetIfIndex ());
362  copy->AddPacketTag (tag);
363  }
364 
365  copy->AddHeader (hdr);
366  struct Data data;
367  data.packet = copy;
368  data.fromIp = hdr.GetSourceAddress ();
369  data.fromProtocol = hdr.GetNextHeader ();
370  m_data.push_back (data);
371  NotifyDataRecv ();
372  return true;
373  }
374  return false;
375 }
376 
377 bool
379 {
380  if (!allowBroadcast)
381  {
382  return false;
383  }
384  return true;
385 }
386 
387 bool
389 {
390  return true;
391 }
392 
393 void
395 {
396  memset(&m_icmpFilter, 0xff, sizeof(icmpv6Filter));
397 }
398 
399 void
401 {
402  memset(&m_icmpFilter, 0x00, sizeof(icmpv6Filter));
403 }
404 
405 void
407 {
408  (m_icmpFilter.icmpv6Filt[(type) >> 5]) |= (uint32_t(1) << ((type) & 31));
409 }
410 
411 void
413 {
414  (m_icmpFilter.icmpv6Filt[(type) >> 5]) &= ~(uint32_t(1) << ((type) & 31));
415 }
416 
417 bool
419 {
420  return (((m_icmpFilter.icmpv6Filt[(type) >> 5]) & (uint32_t(1) << ((type) & 31))) != 0);
421 }
422 
423 bool
425 {
426  return (((m_icmpFilter.icmpv6Filt[(type) >> 5]) & (uint32_t(1) << ((type) & 31))) == 0);
427 }
428 
429 } /* namespace ns3 */
430 
void SetNode(Ptr< Node > node)
Set the node associated with this socket.
bool IsAny() const
If the IPv6 address is the "Any" address.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: ipv6-header.cc:143
uint8_t GetNextHeader(void) const
Get the next header.
Definition: ipv6-header.cc:81
void CalculatePseudoHeaderChecksum(Ipv6Address src, Ipv6Address dst, uint16_t length, uint8_t protocol)
Calculate pseudo header checksum for IPv6.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
Introspection did not find any typical Config paths.
Definition: ipv6-header.h:33
#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
virtual void DoDispose()
Dispose object.
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
void NotifyDataRecv(void)
Notify through the callback (if set) that some data have been received.
Definition: socket.cc:304
virtual uint32_t GetSerializedSize() const
Get the serialized size.
Ipv6Address fromIp
Source address.
uint32_t icmpv6Filt[8]
ICMPv6 filter specification.
Ipv6Address m_src
Source address.
Ptr< Packet > Recv(void)
Read a single packet from the socket.
Definition: socket.cc:174
IPv6 layer implementation.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:836
bool m_shutdownSend
Flag to shutdown send capability.
uint8_t GetType() const
Get the type field.
#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
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
uint16_t fromProtocol
Protocol used.
Ptr< Packet > packet
Packet data.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
SocketType
Enumeration of the possible socket types.
Definition: socket.h:104
static TypeId GetTypeId()
Get the type ID of this class.
bool ForwardUp(Ptr< const Packet > p, Ipv6Header hdr, Ptr< NetDevice > device)
Forward up to receive method.
virtual enum Socket::SocketType GetSocketType() const
Get socket type (NS3_SOCK_RAW)
virtual enum Socket::SocketErrno GetErrno() const
Get last error number.
bool IsRecvPktInfo() const
Get status indicating whether enable/disable packet information to socket.
Definition: socket.cc:363
virtual int Send(Ptr< Packet > p, uint32_t flags)
Send data (or dummy data) to the remote host.
a polymophic address class
Definition: address.h:90
virtual int Bind()
Allocate a local IPv4 endpoint for this socket.
bool m_shutdownRecv
Flag to shutdown receive capability.
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
A low-level Socket API based loosely on the BSD Socket API.
Definition: socket.h:66
Introspection did not find any typical Config paths.
Definition: icmpv6-header.h:37
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:353
std::list< struct Data > m_data
Packet waiting to be processed.
Hold an unsigned integer type.
Definition: uinteger.h:44
virtual int GetSockName(Address &address) const
Get socket address.
An Inet6 address class.
void Icmpv6FilterSetBlock(uint8_t type)
Set the filter to block one ICMPv6 type.
virtual int Bind6()
Allocate a local IPv6 endpoint for this socket.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
virtual Ptr< Node > GetNode() const
Return the node this socket is associated with.
IPv6 raw data and additional information.
Struct to hold the ICMPv6 filter.
virtual int Listen()
Listen for incoming connections.
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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static uint16_t GetStaticProtocolNumber()
Get ICMPv6 protocol number.
bool Icmpv6FilterWillBlock(uint8_t type)
Ask the filter about the status of one ICMPv6 type.
void Icmpv6FilterSetPass(uint8_t type)
Set the filter to pass one ICMPv6 type.
icmpv6Filter m_icmpFilter
ICMPv6 filter.
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 bool SetAllowBroadcast(bool allowBroadcast)
Configure whether broadcast datagram transmissions are allowed.
virtual void DoDispose(void)
Destructor implementation.
Definition: socket.cc:314
Introspection did not find any typical Config paths.
virtual uint32_t GetRxAvailable() const
Return number of bytes which can be returned from one or multiple calls to Recv.
Describes an IPv6 address.
Definition: ipv6-address.h:47
virtual uint32_t GetTxAvailable() const
Returns the number of bytes which can be sent in a single call to Send.
enum Socket::SocketErrno m_err
Last error number.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)
Send data to a specified peer.
virtual int Close()
Close a socket.
Ipv6Address m_dst
Destination address.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:843
Ipv6Address GetSourceAddress(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:101
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
bool Icmpv6FilterWillPass(uint8_t type)
Ask the filter about the status of one ICMPv6 type.
void Icmpv6FilterSetPassAll()
Clean the ICMPv6 filter structure.
static bool IsMatchingType(const Address &addr)
If the address match.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:368
This class implements a tag that carries socket ancillary data to the socket interface.
tuple address
Definition: first.py:37
Definition: first.py:1
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
virtual bool GetAllowBroadcast() const
Query whether broadcast datagram transmissions are allowed.
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:51
void SetDestinationAddress(Ipv6Address dst)
Set the "Destination address" field.
Definition: ipv6-header.cc:106
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void SetRecvIf(uint32_t ifindex)
Set the tag's receiving interface.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
Ipv6Address GetDestinationAddress(void) const
Get the "Destination address" field.
Definition: ipv6-header.cc:111
uint16_t m_protocol
Protocol.
void Icmpv6FilterSetBlockAll()
Set the filter to block all the ICMPv6 types.