A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv4-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) 2005 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ipv4-end-point-demux.h"
22 #include "ipv4-end-point.h"
23 #include "ns3/log.h"
24 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("Ipv4EndPointDemux")
28  ;
29 
31  : m_ephemeral (49152), m_portLast (65535), m_portFirst (49152)
32 {
33  NS_LOG_FUNCTION (this);
34 }
35 
37 {
38  NS_LOG_FUNCTION (this);
39  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
40  {
41  Ipv4EndPoint *endPoint = *i;
42  delete endPoint;
43  }
44  m_endPoints.clear ();
45 }
46 
47 bool
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
63 {
64  NS_LOG_FUNCTION (this << addr << port);
65  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
66  {
67  if ((*i)->GetLocalPort () == port &&
68  (*i)->GetLocalAddress () == addr)
69  {
70  return true;
71  }
72  }
73  return false;
74 }
75 
78 {
79  NS_LOG_FUNCTION (this);
80  uint16_t port = AllocateEphemeralPort ();
81  if (port == 0)
82  {
83  NS_LOG_WARN ("Ephemeral port allocation failed.");
84  return 0;
85  }
86  Ipv4EndPoint *endPoint = new Ipv4EndPoint (Ipv4Address::GetAny (), port);
87  m_endPoints.push_back (endPoint);
88  NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
89  return endPoint;
90 }
91 
94 {
95  NS_LOG_FUNCTION (this << address);
96  uint16_t port = AllocateEphemeralPort ();
97  if (port == 0)
98  {
99  NS_LOG_WARN ("Ephemeral port allocation failed.");
100  return 0;
101  }
102  Ipv4EndPoint *endPoint = new Ipv4EndPoint (address, port);
103  m_endPoints.push_back (endPoint);
104  NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
105  return endPoint;
106 }
107 
108 Ipv4EndPoint *
110 {
111  NS_LOG_FUNCTION (this << port);
112 
113  return Allocate (Ipv4Address::GetAny (), port);
114 }
115 
116 Ipv4EndPoint *
118 {
119  NS_LOG_FUNCTION (this << address << port);
120  if (LookupLocal (address, port))
121  {
122  NS_LOG_WARN ("Duplicate address/port; failing.");
123  return 0;
124  }
125  Ipv4EndPoint *endPoint = new Ipv4EndPoint (address, port);
126  m_endPoints.push_back (endPoint);
127  NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
128  return endPoint;
129 }
130 
131 Ipv4EndPoint *
132 Ipv4EndPointDemux::Allocate (Ipv4Address localAddress, uint16_t localPort,
133  Ipv4Address peerAddress, uint16_t peerPort)
134 {
135  NS_LOG_FUNCTION (this << localAddress << localPort << peerAddress << peerPort);
136  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
137  {
138  if ((*i)->GetLocalPort () == localPort &&
139  (*i)->GetLocalAddress () == localAddress &&
140  (*i)->GetPeerPort () == peerPort &&
141  (*i)->GetPeerAddress () == peerAddress)
142  {
143  NS_LOG_WARN ("No way we can allocate this end-point.");
144  /* no way we can allocate this end-point. */
145  return 0;
146  }
147  }
148  Ipv4EndPoint *endPoint = new Ipv4EndPoint (localAddress, localPort);
149  endPoint->SetPeer (peerAddress, peerPort);
150  m_endPoints.push_back (endPoint);
151 
152  NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
153 
154  return endPoint;
155 }
156 
157 void
159 {
160  NS_LOG_FUNCTION (this << endPoint);
161  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
162  {
163  if (*i == endPoint)
164  {
165  delete endPoint;
166  m_endPoints.erase (i);
167  break;
168  }
169  }
170 }
171 
172 /*
173  * return list of all available Endpoints
174  */
177 {
178  NS_LOG_FUNCTION (this);
179  EndPoints ret;
180 
181  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
182  {
183  Ipv4EndPoint* endP = *i;
184  ret.push_back (endP);
185  }
186  return ret;
187 }
188 
189 
190 /*
191  * If we have an exact match, we return it.
192  * Otherwise, if we find a generic match, we return it.
193  * Otherwise, we return 0.
194  */
196 Ipv4EndPointDemux::Lookup (Ipv4Address daddr, uint16_t dport,
197  Ipv4Address saddr, uint16_t sport,
198  Ptr<Ipv4Interface> incomingInterface)
199 {
200  NS_LOG_FUNCTION (this << daddr << dport << saddr << sport << incomingInterface);
201 
202  EndPoints retval1; // Matches exact on local port, wildcards on others
203  EndPoints retval2; // Matches exact on local port/adder, wildcards on others
204  EndPoints retval3; // Matches all but local address
205  EndPoints retval4; // Exact match on all 4
206 
207  NS_LOG_DEBUG ("Looking up endpoint for destination address " << daddr);
208  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
209  {
210  Ipv4EndPoint* endP = *i;
211  NS_LOG_DEBUG ("Looking at endpoint dport=" << endP->GetLocalPort ()
212  << " daddr=" << endP->GetLocalAddress ()
213  << " sport=" << endP->GetPeerPort ()
214  << " saddr=" << endP->GetPeerAddress ());
215  if (endP->GetLocalPort () != dport)
216  {
217  NS_LOG_LOGIC ("Skipping endpoint " << &endP
218  << " because endpoint dport "
219  << endP->GetLocalPort ()
220  << " does not match packet dport " << dport);
221  continue;
222  }
223  if (endP->GetBoundNetDevice ())
224  {
225  if (endP->GetBoundNetDevice () != incomingInterface->GetDevice ())
226  {
227  NS_LOG_LOGIC ("Skipping endpoint " << &endP
228  << " because endpoint is bound to specific device and"
229  << endP->GetBoundNetDevice ()
230  << " does not match packet device " << incomingInterface->GetDevice ());
231  continue;
232  }
233  }
234  bool subnetDirected = false;
235  Ipv4Address incomingInterfaceAddr = daddr; // may be a broadcast
236  for (uint32_t i = 0; i < incomingInterface->GetNAddresses (); i++)
237  {
238  Ipv4InterfaceAddress addr = incomingInterface->GetAddress (i);
239  if (addr.GetLocal ().CombineMask (addr.GetMask ()) == daddr.CombineMask (addr.GetMask ()) &&
240  daddr.IsSubnetDirectedBroadcast (addr.GetMask ()))
241  {
242  subnetDirected = true;
243  incomingInterfaceAddr = addr.GetLocal ();
244  }
245  }
246  bool isBroadcast = (daddr.IsBroadcast () || subnetDirected == true);
247  NS_LOG_DEBUG ("dest addr " << daddr << " broadcast? " << isBroadcast);
248  bool localAddressMatchesWildCard =
249  endP->GetLocalAddress () == Ipv4Address::GetAny ();
250  bool localAddressMatchesExact = endP->GetLocalAddress () == daddr;
251 
252  if (isBroadcast)
253  {
254  NS_LOG_DEBUG ("Found bcast, localaddr " << endP->GetLocalAddress ());
255  }
256 
257  if (isBroadcast && (endP->GetLocalAddress () != Ipv4Address::GetAny ()))
258  {
259  localAddressMatchesExact = (endP->GetLocalAddress () ==
260  incomingInterfaceAddr);
261  }
262  // if no match here, keep looking
263  if (!(localAddressMatchesExact || localAddressMatchesWildCard))
264  continue;
265  bool remotePeerMatchesExact = endP->GetPeerPort () == sport;
266  bool remotePeerMatchesWildCard = endP->GetPeerPort () == 0;
267  bool remoteAddressMatchesExact = endP->GetPeerAddress () == saddr;
268  bool remoteAddressMatchesWildCard = endP->GetPeerAddress () ==
270  // If remote does not match either with exact or wildcard,
271  // skip this one
272  if (!(remotePeerMatchesExact || remotePeerMatchesWildCard))
273  continue;
274  if (!(remoteAddressMatchesExact || remoteAddressMatchesWildCard))
275  continue;
276 
277  // Now figure out which return list to add this one to
278  if (localAddressMatchesWildCard &&
279  remotePeerMatchesWildCard &&
280  remoteAddressMatchesWildCard)
281  { // Only local port matches exactly
282  retval1.push_back (endP);
283  }
284  if ((localAddressMatchesExact || (isBroadcast && localAddressMatchesWildCard))&&
285  remotePeerMatchesWildCard &&
286  remoteAddressMatchesWildCard)
287  { // Only local port and local address matches exactly
288  retval2.push_back (endP);
289  }
290  if (localAddressMatchesWildCard &&
291  remotePeerMatchesExact &&
292  remoteAddressMatchesExact)
293  { // All but local address
294  retval3.push_back (endP);
295  }
296  if (localAddressMatchesExact &&
297  remotePeerMatchesExact &&
298  remoteAddressMatchesExact)
299  { // All 4 match
300  retval4.push_back (endP);
301  }
302  }
303 
304  // Here we find the most exact match
305  if (!retval4.empty ()) return retval4;
306  if (!retval3.empty ()) return retval3;
307  if (!retval2.empty ()) return retval2;
308  return retval1; // might be empty if no matches
309 }
310 
311 Ipv4EndPoint *
313  uint16_t dport,
314  Ipv4Address saddr,
315  uint16_t sport)
316 {
317  NS_LOG_FUNCTION (this << daddr << dport << saddr << sport);
318 
319  // this code is a copy/paste version of an old BSD ip stack lookup
320  // function.
321  uint32_t genericity = 3;
322  Ipv4EndPoint *generic = 0;
323  for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
324  {
325  if ((*i)->GetLocalPort () != dport)
326  {
327  continue;
328  }
329  if ((*i)->GetLocalAddress () == daddr &&
330  (*i)->GetPeerPort () == sport &&
331  (*i)->GetPeerAddress () == saddr)
332  {
333  /* this is an exact match. */
334  return *i;
335  }
336  uint32_t tmp = 0;
337  if ((*i)->GetLocalAddress () == Ipv4Address::GetAny ())
338  {
339  tmp++;
340  }
341  if ((*i)->GetPeerAddress () == Ipv4Address::GetAny ())
342  {
343  tmp++;
344  }
345  if (tmp < genericity)
346  {
347  generic = (*i);
348  genericity = tmp;
349  }
350  }
351  return generic;
352 }
353 uint16_t
355 {
356  // Similar to counting up logic in netinet/in_pcb.c
357  NS_LOG_FUNCTION (this);
358  uint16_t port = m_ephemeral;
359  int count = m_portLast - m_portFirst;
360  do
361  {
362  if (count-- < 0)
363  {
364  return 0;
365  }
366  ++port;
367  if (port < m_portFirst || port > m_portLast)
368  {
369  port = m_portFirst;
370  }
371  } while (LookupPortLocal (port));
372  m_ephemeral = port;
373  return port;
374 }
375 
376 } // namespace ns3
377 
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Ipv4Mask GetMask(void) const
Get the network mask.
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
Ipv4Address GetLocal(void) const
Get the local address.
EndPoints GetAllEndPoints(void)
Get the entire list of end points registered.
uint16_t m_ephemeral
The ephemeral port.
bool LookupPortLocal(uint16_t port)
Lookup for port local.
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
uint16_t m_portFirst
The first ephemeral port.
uint16_t port
Definition: dsdv-manet.cc:44
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
Ipv4Address GetLocalAddress(void)
Get the local address.
bool IsBroadcast(void) const
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
uint16_t m_portLast
The last ephemeral port.
Ipv4InterfaceAddress GetAddress(uint32_t index) const
Ptr< NetDevice > GetBoundNetDevice(void)
Returns socket's bound netdevice, if any.
Ipv4EndPoint * SimpleLookup(Ipv4Address daddr, uint16_t dport, Ipv4Address saddr, uint16_t sport)
simple lookup for a match with all the parameters.
bool LookupLocal(Ipv4Address addr, uint16_t port)
Lookup for address and port.
void SetPeer(Ipv4Address address, uint16_t port)
Set the peer informations (address and port).
EndPoints Lookup(Ipv4Address daddr, uint16_t dport, Ipv4Address saddr, uint16_t sport, Ptr< Ipv4Interface > incomingInterface)
lookup for a match with all the parameters.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
std::list< Ipv4EndPoint * > EndPoints
Container of the IPv4 endpoints.
a class to store IPv4 address information on an interface
uint16_t GetLocalPort(void)
Get the local port.
#define NS_LOG_WARN(msg)
Definition: log.h:280
Ptr< NetDevice > GetDevice(void) const
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
Ipv4Address GetPeerAddress(void)
Get the peer address.
uint16_t GetPeerPort(void)
Get the peer port.
std::list< Ipv4EndPoint * >::iterator EndPointsI
Iterator to the container of the IPv4 endpoints.
tuple address
Definition: first.py:37
void DeAllocate(Ipv4EndPoint *endPoint)
Remove a end point.
EndPoints m_endPoints
A list of IPv4 end points.
uint32_t GetNAddresses(void) const
uint16_t AllocateEphemeralPort(void)
Allocate an ephemeral port.
A representation of an internet endpoint/connection.
Ipv4EndPoint * Allocate(void)
Allocate a Ipv4EndPoint.