~ubuntu-branches/ubuntu/quantal/ns3/quantal

« back to all changes in this revision

Viewing changes to ns-3.13/src/internet/model/ipv6-raw-socket-impl.cc

  • Committer: Package Import Robot
  • Author(s): YunQiang Su, Aron Xu, YunQiang Su, Upstream
  • Date: 2012-01-06 00:35:42 UTC
  • mfrom: (10.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120106003542-vcn5g03mhapm991h
Tags: 3.13+dfsg-1
[ Aron Xu ]:
        add tag binary and binary-indep, 
  for not build doc when --binary-arch (Closes: #654493).
[ YunQiang Su ]
        add waf 1.5/1.6 source to debian directory, 
  and build waf from there (Closes: #642217).
[ Upstream ]
  Successfully link with --as-needed option (Closes: #642225).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
NS_OBJECT_ENSURE_REGISTERED (Ipv6RawSocketImpl);
 
45
 
 
46
TypeId Ipv6RawSocketImpl::GetTypeId ()
 
47
{
 
48
  static TypeId tid = TypeId ("ns3::Ipv6RawSocketImpl")
 
49
    .SetParent<Socket> ()
 
50
    .AddAttribute ("Protocol", "Protocol number to match.", 
 
51
                   UintegerValue (0),
 
52
                   MakeUintegerAccessor (&Ipv6RawSocketImpl::m_protocol),
 
53
                   MakeUintegerChecker<uint16_t> ())
 
54
    .AddAttribute ("IcmpFilter", "Any ICMPv6 header whose type field matches a bit in this filter is dropped.",
 
55
                   UintegerValue (0),
 
56
                   MakeUintegerAccessor (&Ipv6RawSocketImpl::m_icmpFilter),
 
57
                   MakeUintegerChecker<uint32_t> ())
 
58
  ;
 
59
  return tid;
 
60
}
 
61
 
 
62
Ipv6RawSocketImpl::Ipv6RawSocketImpl ()
 
63
{
 
64
  NS_LOG_FUNCTION_NOARGS ();
 
65
  m_err = Socket::ERROR_NOTERROR;
 
66
  m_node = 0;
 
67
  m_src = Ipv6Address::GetAny ();
 
68
  m_dst = Ipv6Address::GetAny ();
 
69
  m_protocol = 0;
 
70
  m_shutdownSend = false;
 
71
  m_shutdownRecv = false;
 
72
}
 
73
 
 
74
Ipv6RawSocketImpl::~Ipv6RawSocketImpl ()
 
75
{
 
76
}
 
77
 
 
78
void Ipv6RawSocketImpl::DoDispose ()
 
79
{
 
80
  NS_LOG_FUNCTION_NOARGS ();
 
81
  m_node = 0;
 
82
  Socket::DoDispose ();
 
83
}
 
84
 
 
85
void Ipv6RawSocketImpl::SetNode (Ptr<Node> node)
 
86
{
 
87
  NS_LOG_FUNCTION (this << node);
 
88
  m_node = node;
 
89
}
 
90
 
 
91
Ptr<Node> Ipv6RawSocketImpl::GetNode () const
 
92
{
 
93
  return m_node;
 
94
}
 
95
 
 
96
enum Socket::SocketErrno Ipv6RawSocketImpl::GetErrno () const
 
97
{
 
98
  NS_LOG_FUNCTION_NOARGS ();
 
99
  return m_err;
 
100
}
 
101
 
 
102
enum Socket::SocketType Ipv6RawSocketImpl::GetSocketType () const
 
103
{
 
104
  return NS3_SOCK_RAW;
 
105
}
 
106
 
 
107
int Ipv6RawSocketImpl::Bind (const Address& address)
 
108
{
 
109
  NS_LOG_FUNCTION (this << address);
 
110
 
 
111
  if (!Inet6SocketAddress::IsMatchingType (address))
 
112
    {
 
113
      m_err = Socket::ERROR_INVAL;
 
114
      return -1;
 
115
    }
 
116
  Inet6SocketAddress ad = Inet6SocketAddress::ConvertFrom (address);
 
117
  m_src = ad.GetIpv6 ();
 
118
  return 0;
 
119
}
 
120
 
 
121
int Ipv6RawSocketImpl::Bind ()
 
122
{
 
123
  NS_LOG_FUNCTION_NOARGS ();
 
124
  m_src = Ipv6Address::GetAny ();
 
125
  return 0;
 
126
}
 
127
 
 
128
int Ipv6RawSocketImpl::GetSockName (Address& address) const
 
129
{
 
130
  NS_LOG_FUNCTION_NOARGS ();
 
131
  address = Inet6SocketAddress (m_src, 0);
 
132
  return 0;
 
133
}
 
134
 
 
135
int Ipv6RawSocketImpl::Close ()
 
136
{
 
137
  NS_LOG_FUNCTION_NOARGS ();
 
138
  Ptr<Ipv6L3Protocol> ipv6 = m_node->GetObject<Ipv6L3Protocol> ();
 
139
 
 
140
  if (ipv6)
 
141
    {
 
142
      ipv6->DeleteRawSocket (this);
 
143
    }
 
144
  return 0;
 
145
}
 
146
 
 
147
int Ipv6RawSocketImpl::ShutdownSend ()
 
148
{
 
149
  NS_LOG_FUNCTION_NOARGS ();
 
150
  m_shutdownSend = true;
 
151
  return 0;
 
152
}
 
153
 
 
154
int Ipv6RawSocketImpl::ShutdownRecv ()
 
155
{
 
156
  NS_LOG_FUNCTION_NOARGS ();
 
157
  m_shutdownRecv = true;
 
158
  return 0;
 
159
}
 
160
 
 
161
int Ipv6RawSocketImpl::Connect (const Address& address)
 
162
{
 
163
  NS_LOG_FUNCTION (this << address);
 
164
 
 
165
  if (!Inet6SocketAddress::IsMatchingType (address))
 
166
    {
 
167
      m_err = Socket::ERROR_INVAL;
 
168
      return -1;
 
169
    }
 
170
 
 
171
  Inet6SocketAddress ad = Inet6SocketAddress::ConvertFrom (address);
 
172
  m_dst = ad.GetIpv6 ();
 
173
  return 0;
 
174
}
 
175
 
 
176
int Ipv6RawSocketImpl::Listen ()
 
177
{
 
178
  NS_LOG_FUNCTION_NOARGS ();
 
179
  m_err = Socket::ERROR_OPNOTSUPP;
 
180
  return -1;
 
181
}
 
182
 
 
183
int Ipv6RawSocketImpl::Send (Ptr<Packet> p, uint32_t flags)
 
184
{
 
185
  NS_LOG_FUNCTION (this << p << flags);
 
186
  Inet6SocketAddress to = Inet6SocketAddress (m_dst, m_protocol);
 
187
  return SendTo (p, flags, to);
 
188
}
 
189
 
 
190
int Ipv6RawSocketImpl::SendTo (Ptr<Packet> p, uint32_t flags, const Address& toAddress)
 
191
{
 
192
  NS_LOG_FUNCTION (this << p << flags << toAddress);
 
193
 
 
194
  if (!Inet6SocketAddress::IsMatchingType (toAddress))
 
195
    {
 
196
      m_err = Socket::ERROR_INVAL;
 
197
      return -1;
 
198
    }
 
199
 
 
200
  if (m_shutdownSend)
 
201
    {
 
202
      return 0;
 
203
    }
 
204
 
 
205
  Inet6SocketAddress ad = Inet6SocketAddress::ConvertFrom (toAddress);
 
206
  Ptr<Ipv6L3Protocol> ipv6 = m_node->GetObject<Ipv6L3Protocol> ();
 
207
  Ipv6Address dst = ad.GetIpv6 ();
 
208
 
 
209
  if (ipv6->GetRoutingProtocol ())
 
210
    {
 
211
      Ipv6Header hdr;
 
212
      hdr.SetDestinationAddress (dst);
 
213
      SocketErrno err = ERROR_NOTERROR;
 
214
      Ptr<Ipv6Route> route = 0;
 
215
      Ptr<NetDevice> oif (0); /*specify non-zero if bound to a source address */
 
216
 
 
217
      if (!m_src.IsAny ())
 
218
        {
 
219
          int32_t index = ipv6->GetInterfaceForAddress (m_src);
 
220
          NS_ASSERT (index >= 0);
 
221
          oif = ipv6->GetNetDevice (index);
 
222
        }
 
223
 
 
224
      route = ipv6->GetRoutingProtocol ()->RouteOutput (p, hdr, oif, err);
 
225
 
 
226
      if (route)
 
227
        {
 
228
          NS_LOG_LOGIC ("Route exists");
 
229
          if (m_protocol == Icmpv6L4Protocol::GetStaticProtocolNumber ())
 
230
            {
 
231
              /* calculate checksum here for ICMPv6 echo request (sent by ping6) 
 
232
               * as we cannot determine source IPv6 address at application level 
 
233
               */
 
234
              uint8_t type;
 
235
              p->CopyData (&type, sizeof(type));
 
236
              if (type == Icmpv6Header::ICMPV6_ECHO_REQUEST)
 
237
                {
 
238
                  Icmpv6Echo hdr (1);
 
239
                  p->RemoveHeader (hdr);
 
240
                  hdr.CalculatePseudoHeaderChecksum (route->GetSource (), dst, p->GetSize () + hdr.GetSerializedSize (), Icmpv6L4Protocol::GetStaticProtocolNumber ());
 
241
                  p->AddHeader (hdr);
 
242
                }
 
243
            }
 
244
 
 
245
          ipv6->Send (p, route->GetSource (), dst, m_protocol, route);
 
246
        }
 
247
      else
 
248
        {
 
249
          NS_LOG_DEBUG ("No route, dropped!");
 
250
        }
 
251
    }
 
252
  return 0;
 
253
}
 
254
 
 
255
Ptr<Packet> Ipv6RawSocketImpl::Recv (uint32_t maxSize, uint32_t flags)
 
256
{
 
257
  NS_LOG_FUNCTION (this << maxSize << flags);
 
258
  Address tmp;
 
259
  return RecvFrom (maxSize, flags, tmp);
 
260
}
 
261
 
 
262
Ptr<Packet> Ipv6RawSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags, Address& fromAddress)
 
263
{
 
264
  NS_LOG_FUNCTION (this << maxSize << flags << fromAddress);
 
265
 
 
266
  if (m_data.empty ())
 
267
    {
 
268
      return 0;
 
269
    }
 
270
 
 
271
  /* get packet */
 
272
  struct Data data = m_data.front ();
 
273
  m_data.pop_front ();
 
274
 
 
275
  if (data.packet->GetSize () > maxSize)
 
276
    {
 
277
      Ptr<Packet> first = data.packet->CreateFragment (0, maxSize);
 
278
      if (!(flags & MSG_PEEK))
 
279
        {
 
280
          data.packet->RemoveAtStart (maxSize);
 
281
        }
 
282
      m_data.push_front (data);
 
283
      return first;
 
284
    }
 
285
 
 
286
  fromAddress = Inet6SocketAddress (data.fromIp, data.fromProtocol);
 
287
  return data.packet;
 
288
}
 
289
 
 
290
uint32_t Ipv6RawSocketImpl::GetTxAvailable () const
 
291
{
 
292
  NS_LOG_FUNCTION_NOARGS ();
 
293
  return 0xffffffff;
 
294
}
 
295
 
 
296
uint32_t Ipv6RawSocketImpl::GetRxAvailable () const
 
297
{
 
298
  NS_LOG_FUNCTION_NOARGS ();
 
299
  uint32_t rx = 0;
 
300
 
 
301
  for (std::list<Data>::const_iterator it = m_data.begin (); it != m_data.end (); ++it)
 
302
    {
 
303
      rx+= (it->packet)->GetSize ();
 
304
    }
 
305
 
 
306
  return rx;
 
307
}
 
308
 
 
309
bool Ipv6RawSocketImpl::ForwardUp (Ptr<const Packet> p, Ipv6Header hdr, Ptr<NetDevice> device)
 
310
{
 
311
  NS_LOG_FUNCTION (this << *p << hdr << device);
 
312
 
 
313
  if (m_shutdownRecv)
 
314
    {
 
315
      return false;
 
316
    }
 
317
 
 
318
  if ((m_src == Ipv6Address::GetAny () || hdr.GetDestinationAddress () == m_src) && 
 
319
      (m_dst == Ipv6Address::GetAny () || hdr.GetSourceAddress () == m_dst) &&
 
320
      hdr.GetNextHeader () == m_protocol)
 
321
    {
 
322
      Ptr<Packet> copy = p->Copy ();
 
323
 
 
324
      if (m_protocol == Icmpv6L4Protocol::GetStaticProtocolNumber ())
 
325
        {
 
326
          /* filter */
 
327
          Icmpv6Header icmpHeader;
 
328
          copy->PeekHeader (icmpHeader);
 
329
          uint8_t type = icmpHeader.GetType ();
 
330
 
 
331
          if ((1 << type) & m_icmpFilter)
 
332
            {
 
333
              /* packet filtered */
 
334
              return false;
 
335
            }
 
336
        }
 
337
 
 
338
      // Should check via getsockopt ()..
 
339
      if (this->m_recvpktinfo)
 
340
        {
 
341
          Ipv6PacketInfoTag tag;
 
342
          copy->RemovePacketTag (tag);
 
343
          tag.SetRecvIf (device->GetIfIndex ());
 
344
          copy->AddPacketTag (tag);
 
345
        }
 
346
 
 
347
      copy->AddHeader (hdr);
 
348
      struct Data data;
 
349
      data.packet = copy;
 
350
      data.fromIp = hdr.GetSourceAddress ();
 
351
      data.fromProtocol = hdr.GetNextHeader ();
 
352
      m_data.push_back (data);
 
353
      NotifyDataRecv ();
 
354
      return true;
 
355
    }
 
356
  return false;
 
357
}
 
358
 
 
359
bool
 
360
Ipv6RawSocketImpl::SetAllowBroadcast (bool allowBroadcast)
 
361
{
 
362
  if (!allowBroadcast)
 
363
    {
 
364
      return false;
 
365
    }
 
366
  return true;
 
367
}
 
368
 
 
369
bool
 
370
Ipv6RawSocketImpl::GetAllowBroadcast () const
 
371
{
 
372
  return true;
 
373
}
 
374
 
 
375
} /* namespace ns3 */
 
376