A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv6-end-point-demux.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 "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 
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  */
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  /* Ipv6Address incomingInterfaceAddr = incomingInterface->GetAddress (); */
199  NS_LOG_DEBUG ("dest addr " << daddr);
200 
201  bool localAddressMatchesWildCard = endP->GetLocalAddress () == Ipv6Address::GetAny ();
202  bool localAddressMatchesExact = endP->GetLocalAddress () == daddr;
203  bool localAddressMatchesAllRouters = endP->GetLocalAddress () == Ipv6Address::GetAllRoutersMulticast ();
204 
205  /* if no match here, keep looking */
206  if (!(localAddressMatchesExact || localAddressMatchesWildCard))
207  {
208  continue;
209  }
210  bool remotePeerMatchesExact = endP->GetPeerPort () == sport;
211  bool remotePeerMatchesWildCard = endP->GetPeerPort () == 0;
212  bool remoteAddressMatchesExact = endP->GetPeerAddress () == saddr;
213  bool remoteAddressMatchesWildCard = endP->GetPeerAddress () == Ipv6Address::GetAny ();
214 
215  /* If remote does not match either with exact or wildcard,i
216  skip this one */
217  if (!(remotePeerMatchesExact || remotePeerMatchesWildCard))
218  {
219  continue;
220  }
221  if (!(remoteAddressMatchesExact || remoteAddressMatchesWildCard))
222  {
223  continue;
224  }
225 
226  /* Now figure out which return list to add this one to */
227  if (localAddressMatchesWildCard
228  && remotePeerMatchesWildCard
229  && remoteAddressMatchesWildCard)
230  { /* Only local port matches exactly */
231  retval1.push_back (endP);
232  }
233  if ((localAddressMatchesExact || (localAddressMatchesAllRouters))
234  && remotePeerMatchesWildCard
235  && remoteAddressMatchesWildCard)
236  { /* Only local port and local address matches exactly */
237  retval2.push_back (endP);
238  }
239  if (localAddressMatchesWildCard
240  && remotePeerMatchesExact
241  && remoteAddressMatchesExact)
242  { /* All but local address */
243  retval3.push_back (endP);
244  }
245  if (localAddressMatchesExact
246  && remotePeerMatchesExact
247  && remoteAddressMatchesExact)
248  { /* All 4 match */
249  retval4.push_back (endP);
250  }
251  }
252 
253  /* Here we find the most exact match */
254  if (!retval4.empty ())
255  {
256  return retval4;
257  }
258  if (!retval3.empty ())
259  {
260  return retval3;
261  }
262  if (!retval2.empty ())
263  {
264  return retval2;
265  }
266  return retval1; /* might be empty if no matches */
267 }
268 
269 Ipv6EndPoint* Ipv6EndPointDemux::SimpleLookup (Ipv6Address dst, uint16_t dport, Ipv6Address src, uint16_t sport)
270 {
271  uint32_t genericity = 3;
272  Ipv6EndPoint *generic = 0;
273 
274  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
275  {
276  uint32_t tmp = 0;
277 
278  if ((*i)->GetLocalPort () != dport)
279  {
280  continue;
281  }
282 
283  if ((*i)->GetLocalAddress () == dst && (*i)->GetPeerPort () == sport
284  && (*i)->GetPeerAddress () == src)
285  {
286  /* this is an exact match. */
287  return *i;
288  }
289 
290  if ((*i)->GetLocalAddress () == Ipv6Address::GetAny ())
291  {
292  tmp++;
293  }
294 
295  if ((*i)->GetPeerAddress () == Ipv6Address::GetAny ())
296  {
297  tmp++;
298  }
299 
300  if (tmp < genericity)
301  {
302  generic = (*i);
303  genericity = tmp;
304  }
305  }
306  return generic;
307 }
308 
310 {
312  uint16_t port = m_ephemeral;
313  int count = m_portLast - m_portFirst;
314  do
315  {
316  if (count-- < 0)
317  {
318  return 0;
319  }
320  ++port;
321  if (port < m_portFirst || port > m_portLast)
322  {
323  port = m_portFirst;
324  }
325  }
326  while (LookupPortLocal (port));
327  m_ephemeral = port;
328  return port;
329 }
330 
332 {
333  return m_endPoints;
334 }
335 
336 } /* namespace ns3 */
337