A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipv6-end-point-demux.cc
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 "ipv6-end-point-demux.h"
22 #include "ipv6-end-point.h"
23 #include "ns3/log.h"
24 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("Ipv6EndPointDemux");
28 
30  : m_ephemeral (49152),
31  m_portFirst (49152),
32  m_portLast (65535)
33 {
35 }
36 
38 {
40  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
41  {
42  Ipv6EndPoint *endPoint = *i;
43  delete endPoint;
44  }
45  m_endPoints.clear ();
46 }
47 
49 {
50  NS_LOG_FUNCTION (this << port);
51  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
52  {
53  if ((*i)->GetLocalPort () == port)
54  {
55  return true;
56  }
57  }
58  return false;
59 }
60 
61 bool Ipv6EndPointDemux::LookupLocal (Ipv6Address addr, uint16_t port)
62 {
63  NS_LOG_FUNCTION (this << addr << port);
64  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
65  {
66  if ((*i)->GetLocalPort () == port
67  && (*i)->GetLocalAddress () == addr)
68  {
69  return true;
70  }
71  }
72  return false;
73 }
74 
76 {
78  uint16_t port = AllocateEphemeralPort ();
79  if (port == 0)
80  {
81  NS_LOG_WARN ("Ephemeral port allocation failed.");
82  return 0;
83  }
84  Ipv6EndPoint *endPoint = new Ipv6EndPoint (Ipv6Address::GetAny (), port);
85  m_endPoints.push_back (endPoint);
86  NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
87  return endPoint;
88 }
89 
91 {
92  NS_LOG_FUNCTION (this << address);
93  uint16_t port = AllocateEphemeralPort ();
94  if (port == 0)
95  {
96  NS_LOG_WARN ("Ephemeral port allocation failed.");
97  return 0;
98  }
99  Ipv6EndPoint *endPoint = new Ipv6EndPoint (address, port);
100  m_endPoints.push_back (endPoint);
101  NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
102  return endPoint;
103 }
104 
106 {
107  NS_LOG_FUNCTION (this << port);
108 
109  return Allocate (Ipv6Address::GetAny (), port);
110 }
111 
113 {
114  NS_LOG_FUNCTION (this << address << port);
115  if (LookupLocal (address, port))
116  {
117  NS_LOG_WARN ("Duplicate address/port; failing.");
118  return 0;
119  }
120  Ipv6EndPoint *endPoint = new Ipv6EndPoint (address, port);
121  m_endPoints.push_back (endPoint);
122  NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
123  return endPoint;
124 }
125 
126 Ipv6EndPoint* Ipv6EndPointDemux::Allocate (Ipv6Address localAddress, uint16_t localPort,
127  Ipv6Address peerAddress, uint16_t peerPort)
128 {
129  NS_LOG_FUNCTION (this << localAddress << localPort << peerAddress << peerPort);
130  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
131  {
132  if ((*i)->GetLocalPort () == localPort
133  && (*i)->GetLocalAddress () == localAddress
134  && (*i)->GetPeerPort () == peerPort
135  && (*i)->GetPeerAddress () == peerAddress)
136  {
137  NS_LOG_WARN ("No way we can allocate this end-point.");
138  /* no way we can allocate this end-point. */
139  return 0;
140  }
141  }
142  Ipv6EndPoint *endPoint = new Ipv6EndPoint (localAddress, localPort);
143  endPoint->SetPeer (peerAddress, peerPort);
144  m_endPoints.push_back (endPoint);
145 
146  NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
147 
148  return endPoint;
149 }
150 
152 {
154  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
155  {
156  if (*i == endPoint)
157  {
158  delete endPoint;
159  m_endPoints.erase (i);
160  break;
161  }
162  }
163 }
164 
165 /*
166  * If we have an exact match, we return it.
167  * Otherwise, if we find a generic match, we return it.
168  * Otherwise, we return 0.
169  */
170 Ipv6EndPointDemux::EndPoints Ipv6EndPointDemux::Lookup (Ipv6Address daddr, uint16_t dport,
171  Ipv6Address saddr, uint16_t sport,
172  Ptr<Ipv6Interface> incomingInterface)
173 {
174  NS_LOG_FUNCTION (this << daddr << dport << saddr << sport << incomingInterface);
175 
176  EndPoints retval1; /* Matches exact on local port, wildcards on others */
177  EndPoints retval2; /* Matches exact on local port/adder, wildcards on others */
178  EndPoints retval3; /* Matches all but local address */
179  EndPoints retval4; /* Exact match on all 4 */
180 
181  NS_LOG_DEBUG ("Looking up endpoint for destination address " << daddr);
182  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
183  {
184  Ipv6EndPoint* endP = *i;
185  NS_LOG_DEBUG ("Looking at endpoint dport=" << endP->GetLocalPort ()
186  << " daddr=" << endP->GetLocalAddress ()
187  << " sport=" << endP->GetPeerPort ()
188  << " saddr=" << endP->GetPeerAddress ());
189  if (endP->GetLocalPort () != dport)
190  {
191  NS_LOG_LOGIC ("Skipping endpoint " << &endP
192  << " because endpoint dport "
193  << endP->GetLocalPort ()
194  << " does not match packet dport " << dport);
195  continue;
196  }
197 
198  if (endP->GetBoundNetDevice ())
199  {
200  if (endP->GetBoundNetDevice () != incomingInterface->GetDevice ())
201  {
202  NS_LOG_LOGIC ("Skipping endpoint " << &endP
203  << " because endpoint is bound to specific device and"
204  << endP->GetBoundNetDevice ()
205  << " does not match packet device " << incomingInterface->GetDevice ());
206  continue;
207  }
208  }
209 
210  /* Ipv6Address incomingInterfaceAddr = incomingInterface->GetAddress (); */
211  NS_LOG_DEBUG ("dest addr " << daddr);
212 
213  bool localAddressMatchesWildCard = endP->GetLocalAddress () == Ipv6Address::GetAny ();
214  bool localAddressMatchesExact = endP->GetLocalAddress () == daddr;
215  bool localAddressMatchesAllRouters = endP->GetLocalAddress () == Ipv6Address::GetAllRoutersMulticast ();
216 
217  /* if no match here, keep looking */
218  if (!(localAddressMatchesExact || localAddressMatchesWildCard))
219  {
220  continue;
221  }
222  bool remotePeerMatchesExact = endP->GetPeerPort () == sport;
223  bool remotePeerMatchesWildCard = endP->GetPeerPort () == 0;
224  bool remoteAddressMatchesExact = endP->GetPeerAddress () == saddr;
225  bool remoteAddressMatchesWildCard = endP->GetPeerAddress () == Ipv6Address::GetAny ();
226 
227  /* If remote does not match either with exact or wildcard,i
228  skip this one */
229  if (!(remotePeerMatchesExact || remotePeerMatchesWildCard))
230  {
231  continue;
232  }
233  if (!(remoteAddressMatchesExact || remoteAddressMatchesWildCard))
234  {
235  continue;
236  }
237 
238  /* Now figure out which return list to add this one to */
239  if (localAddressMatchesWildCard
240  && remotePeerMatchesWildCard
241  && remoteAddressMatchesWildCard)
242  { /* Only local port matches exactly */
243  retval1.push_back (endP);
244  }
245  if ((localAddressMatchesExact || (localAddressMatchesAllRouters))
246  && remotePeerMatchesWildCard
247  && remoteAddressMatchesWildCard)
248  { /* Only local port and local address matches exactly */
249  retval2.push_back (endP);
250  }
251  if (localAddressMatchesWildCard
252  && remotePeerMatchesExact
253  && remoteAddressMatchesExact)
254  { /* All but local address */
255  retval3.push_back (endP);
256  }
257  if (localAddressMatchesExact
258  && remotePeerMatchesExact
259  && remoteAddressMatchesExact)
260  { /* All 4 match */
261  retval4.push_back (endP);
262  }
263  }
264 
265  /* Here we find the most exact match */
266  if (!retval4.empty ())
267  {
268  return retval4;
269  }
270  if (!retval3.empty ())
271  {
272  return retval3;
273  }
274  if (!retval2.empty ())
275  {
276  return retval2;
277  }
278  return retval1; /* might be empty if no matches */
279 }
280 
281 Ipv6EndPoint* Ipv6EndPointDemux::SimpleLookup (Ipv6Address dst, uint16_t dport, Ipv6Address src, uint16_t sport)
282 {
283  uint32_t genericity = 3;
284  Ipv6EndPoint *generic = 0;
285 
286  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
287  {
288  uint32_t tmp = 0;
289 
290  if ((*i)->GetLocalPort () != dport)
291  {
292  continue;
293  }
294 
295  if ((*i)->GetLocalAddress () == dst && (*i)->GetPeerPort () == sport
296  && (*i)->GetPeerAddress () == src)
297  {
298  /* this is an exact match. */
299  return *i;
300  }
301 
302  if ((*i)->GetLocalAddress () == Ipv6Address::GetAny ())
303  {
304  tmp++;
305  }
306 
307  if ((*i)->GetPeerAddress () == Ipv6Address::GetAny ())
308  {
309  tmp++;
310  }
311 
312  if (tmp < genericity)
313  {
314  generic = (*i);
315  genericity = tmp;
316  }
317  }
318  return generic;
319 }
320 
322 {
324  uint16_t port = m_ephemeral;
325  int count = m_portLast - m_portFirst;
326  do
327  {
328  if (count-- < 0)
329  {
330  return 0;
331  }
332  ++port;
333  if (port < m_portFirst || port > m_portLast)
334  {
335  port = m_portFirst;
336  }
337  }
338  while (LookupPortLocal (port));
339  m_ephemeral = port;
340  return port;
341 }
342 
343 Ipv6EndPointDemux::EndPoints Ipv6EndPointDemux::GetEndPoints () const
344 {
345  return m_endPoints;
346 }
347 
348 } /* namespace ns3 */
349 
Ipv6Address GetLocalAddress()
Get the local address.
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
EndPoints Lookup(Ipv6Address dst, uint16_t dport, Ipv6Address src, uint16_t sport, Ptr< Ipv6Interface > incomingInterface)
lookup for a match with all the parameters.
uint16_t m_ephemeral
The ephemeral port.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
EndPoints m_endPoints
A list of IPv6 end points.
bool LookupLocal(Ipv6Address addr, uint16_t port)
Lookup for address and port.
uint16_t GetPeerPort()
Get the peer port.
uint16_t GetLocalPort()
Get the local port.
Ipv6EndPoint * Allocate(void)
Allocate a Ipv6EndPoint.
virtual Ptr< NetDevice > GetDevice() const
Get the NetDevice.
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
uint16_t m_portLast
The last ephemeral port.
void DeAllocate(Ipv6EndPoint *endPoint)
Remove a end point.
bool LookupPortLocal(uint16_t port)
Lookup for port local.
Ipv6EndPoint * SimpleLookup(Ipv6Address dst, uint16_t dport, Ipv6Address src, uint16_t sport)
Simple lookup for a four-tuple match.
uint16_t m_portFirst
The first ephemeral port.
Describes an IPv6 address.
Definition: ipv6-address.h:44
#define NS_LOG_WARN(msg)
Definition: log.h:246
Ipv6Address GetPeerAddress()
Get the peer address.
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
An IPv6 end point, four tuples identification.
uint16_t AllocateEphemeralPort()
Allocate a ephemeral port.
Ptr< NetDevice > GetBoundNetDevice(void)
Returns socket's bound netdevice, if any.
EndPoints GetEndPoints() const
Get the entire list of end points registered.
void SetPeer(Ipv6Address addr, uint16_t port)
Set the peer informations (address and port).
static Ipv6Address GetAllRoutersMulticast()
Get the "all routers multicast" address.