A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
44 
45 NS_OBJECT_ENSURE_REGISTERED (Ipv6RawSocketImpl)
46  ;
47 
49 {
50  static TypeId tid = TypeId ("ns3::Ipv6RawSocketImpl")
51  .SetParent<Socket> ()
52  .AddAttribute ("Protocol", "Protocol number to match.",
53  UintegerValue (0),
54  MakeUintegerAccessor (&Ipv6RawSocketImpl::m_protocol),
55  MakeUintegerChecker<uint16_t> ())
56  ;
57  return tid;
58 }
59 
61 {
64  m_node = 0;
67  m_protocol = 0;
68  m_shutdownSend = false;
69  m_shutdownRecv = false;
71 }
72 
74 {
75 }
76 
78 {
80  m_node = 0;
82 }
83 
85 {
86  NS_LOG_FUNCTION (this << node);
87  m_node = node;
88 }
89 
91 {
92  return m_node;
93 }
94 
96 {
98  return m_err;
99 }
100 
102 {
103  return NS3_SOCK_RAW;
104 }
105 
107 {
108  NS_LOG_FUNCTION (this << address);
109 
110  if (!Inet6SocketAddress::IsMatchingType (address))
111  {
113  return -1;
114  }
116  m_src = ad.GetIpv6 ();
117  return 0;
118 }
119 
121 {
124  return 0;
125 }
126 
128 {
129  return(Bind());
130 }
131 
133 {
135  address = Inet6SocketAddress (m_src, 0);
136  return 0;
137 }
138 
140 {
143 
144  if (ipv6)
145  {
146  ipv6->DeleteRawSocket (this);
147  }
148  return 0;
149 }
150 
152 {
154  m_shutdownSend = true;
155  return 0;
156 }
157 
159 {
161  m_shutdownRecv = true;
162  return 0;
163 }
164 
166 {
167  NS_LOG_FUNCTION (this << address);
168 
169  if (!Inet6SocketAddress::IsMatchingType (address))
170  {
172  return -1;
173  }
174 
176  m_dst = ad.GetIpv6 ();
177  return 0;
178 }
179 
181 {
184  return -1;
185 }
186 
187 int Ipv6RawSocketImpl::Send (Ptr<Packet> p, uint32_t flags)
188 {
189  NS_LOG_FUNCTION (this << p << flags);
191  return SendTo (p, flags, to);
192 }
193 
194 int Ipv6RawSocketImpl::SendTo (Ptr<Packet> p, uint32_t flags, const Address& toAddress)
195 {
196  NS_LOG_FUNCTION (this << p << flags << toAddress);
197 
198  if (!Inet6SocketAddress::IsMatchingType (toAddress))
199  {
201  return -1;
202  }
203 
204  if (m_shutdownSend)
205  {
206  return 0;
207  }
208 
211  Ipv6Address dst = ad.GetIpv6 ();
212 
213  if (ipv6->GetRoutingProtocol ())
214  {
215  Ipv6Header hdr;
216  hdr.SetDestinationAddress (dst);
218  Ptr<Ipv6Route> route = 0;
219  Ptr<NetDevice> oif (0); /*specify non-zero if bound to a source address */
220 
221  if (!m_src.IsAny ())
222  {
223  int32_t index = ipv6->GetInterfaceForAddress (m_src);
224  NS_ASSERT (index >= 0);
225  oif = ipv6->GetNetDevice (index);
226  }
227 
228  route = ipv6->GetRoutingProtocol ()->RouteOutput (p, hdr, oif, err);
229 
230  if (route)
231  {
232  NS_LOG_LOGIC ("Route exists");
234  {
235  /* calculate checksum here for ICMPv6 echo request (sent by ping6)
236  * as we cannot determine source IPv6 address at application level
237  */
238  uint8_t type;
239  p->CopyData (&type, sizeof(type));
241  {
242  Icmpv6Echo hdr (1);
243  p->RemoveHeader (hdr);
245  p->AddHeader (hdr);
246  }
247  }
248 
249  if (m_src.IsAny ())
250  {
251  ipv6->Send (p, route->GetSource (), dst, m_protocol, route);
252  }
253  else
254  {
255  ipv6->Send (p, m_src, dst, m_protocol, route);
256  }
257  // Return only payload size (as Linux does).
258  return p->GetSize () - hdr.GetSerializedSize ();
259  }
260  else
261  {
262  NS_LOG_DEBUG ("No route, dropped!");
263  }
264  }
265  return 0;
266 }
267 
268 Ptr<Packet> Ipv6RawSocketImpl::Recv (uint32_t maxSize, uint32_t flags)
269 {
270  NS_LOG_FUNCTION (this << maxSize << flags);
271  Address tmp;
272  return RecvFrom (maxSize, flags, tmp);
273 }
274 
275 Ptr<Packet> Ipv6RawSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags, Address& fromAddress)
276 {
277  NS_LOG_FUNCTION (this << maxSize << flags << fromAddress);
278 
279  if (m_data.empty ())
280  {
281  return 0;
282  }
283 
284  /* get packet */
285  struct Data data = m_data.front ();
286  m_data.pop_front ();
287  fromAddress = Inet6SocketAddress (data.fromIp, data.fromProtocol);
288  if (data.packet->GetSize () > maxSize)
289  {
290  Ptr<Packet> first = data.packet->CreateFragment (0, maxSize);
291  if (!(flags & MSG_PEEK))
292  {
293  data.packet->RemoveAtStart (maxSize);
294  }
295  m_data.push_front (data);
296  return first;
297  }
298 
299  return data.packet;
300 }
301 
303 {
305  return 0xffffffff;
306 }
307 
309 {
311  uint32_t rx = 0;
312 
313  for (std::list<Data>::const_iterator it = m_data.begin (); it != m_data.end (); ++it)
314  {
315  rx+= (it->packet)->GetSize ();
316  }
317 
318  return rx;
319 }
320 
322 {
323  NS_LOG_FUNCTION (this << *p << hdr << device);
324 
325  if (m_shutdownRecv)
326  {
327  return false;
328  }
329 
330  Ptr<NetDevice> boundNetDevice = Socket::GetBoundNetDevice();
331  if (boundNetDevice)
332  {
333  if (boundNetDevice != device)
334  {
335  return false;
336  }
337  }
338 
339  if ((m_src == Ipv6Address::GetAny () || hdr.GetDestinationAddress () == m_src) &&
340  (m_dst == Ipv6Address::GetAny () || hdr.GetSourceAddress () == m_dst) &&
341  hdr.GetNextHeader () == m_protocol)
342  {
343  Ptr<Packet> copy = p->Copy ();
344 
346  {
347  /* filter */
348  Icmpv6Header icmpHeader;
349  copy->PeekHeader (icmpHeader);
350  uint8_t type = icmpHeader.GetType ();
351 
352  if (Icmpv6FilterWillBlock(type))
353  {
354  /* packet filtered */
355  return false;
356  }
357  }
358 
359  // Should check via getsockopt ()..
360  if (IsRecvPktInfo ())
361  {
362  Ipv6PacketInfoTag tag;
363  copy->RemovePacketTag (tag);
364  tag.SetRecvIf (device->GetIfIndex ());
365  copy->AddPacketTag (tag);
366  }
367 
368  copy->AddHeader (hdr);
369  struct Data data;
370  data.packet = copy;
371  data.fromIp = hdr.GetSourceAddress ();
372  data.fromProtocol = hdr.GetNextHeader ();
373  m_data.push_back (data);
374  NotifyDataRecv ();
375  return true;
376  }
377  return false;
378 }
379 
380 bool
382 {
383  if (!allowBroadcast)
384  {
385  return false;
386  }
387  return true;
388 }
389 
390 bool
392 {
393  return true;
394 }
395 
396 void
398 {
399  memset(&m_icmpFilter, 0xff, sizeof(icmpv6Filter));
400 }
401 
402 void
404 {
405  memset(&m_icmpFilter, 0x00, sizeof(icmpv6Filter));
406 }
407 
408 void
410 {
411  (m_icmpFilter.icmpv6Filt[(type) >> 5]) |= (uint32_t(1) << ((type) & 31));
412 }
413 
414 void
416 {
417  (m_icmpFilter.icmpv6Filt[(type) >> 5]) &= ~(uint32_t(1) << ((type) & 31));
418 }
419 
420 bool
422 {
423  return (((m_icmpFilter.icmpv6Filt[(type) >> 5]) & (uint32_t(1) << ((type) & 31))) != 0);
424 }
425 
426 bool
428 {
429  return (((m_icmpFilter.icmpv6Filt[(type) >> 5]) & (uint32_t(1) << ((type) & 31))) == 0);
430 }
431 
432 } /* namespace ns3 */
433 
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:144
uint8_t GetNextHeader(void) const
Get the next header.
Definition: ipv6-header.cc:82
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.
Doxygen introspection did not find any typical Config paths.
Definition: ipv6-header.h:33
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
virtual void DoDispose()
Dispose object.
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
void NotifyDataRecv(void)
Notify through the callback (if set) that some data have been received.
Definition: socket.cc:305
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:175
IPv6 layer implementation.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:841
bool m_shutdownSend
Flag to shutdown send capability.
uint8_t GetType() const
Get the type field.
#define NS_ASSERT(condition)
Definition: assert.h:64
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
uint32_t GetSize(void) const
Definition: packet.h:650
uint16_t fromProtocol
Protocol used.
Ptr< Packet > packet
Packet data.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
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)
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
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:364
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:86
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: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
A low-level Socket API based loosely on the BSD Socket API.
Definition: socket.h:66
Doxygen 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:46
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)
Definition: log.h:368
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
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 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)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: socket.cc:315
Doxygen 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:46
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:848
Ipv6Address GetSourceAddress(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:102
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
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:381
This class implements a tag that carries socket ancillary data to the socket interface.
tuple address
Definition: first.py:37
Ptr< T > GetObject(void) const
Definition: object.h:361
virtual bool GetAllowBroadcast() const
Query whether broadcast datagram transmissions are allowed.
SocketType
Enumeration of the possible socket types.
Definition: socket.h:104
a unique identifier for an interface.
Definition: type-id.h:49
void SetDestinationAddress(Ipv6Address dst)
Set the "Destination address" field.
Definition: ipv6-header.cc:107
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
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:112
uint16_t m_protocol
Protocol.
void Icmpv6FilterSetBlockAll()
Set the filter to block all the ICMPv6 types.