21 #include "ipv4-end-point-demux.h"
22 #include "ipv4-end-point.h"
29 Ipv4EndPointDemux::Ipv4EndPointDemux ()
30 : m_ephemeral (49152), m_portLast (65535), m_portFirst (49152)
35 Ipv4EndPointDemux::~Ipv4EndPointDemux ()
38 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
40 Ipv4EndPoint *endPoint = *i;
47 Ipv4EndPointDemux::LookupPortLocal (uint16_t port)
50 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
52 if ((*i)->GetLocalPort () == port)
61 Ipv4EndPointDemux::LookupLocal (Ipv4Address addr, uint16_t port)
64 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
66 if ((*i)->GetLocalPort () == port &&
67 (*i)->GetLocalAddress () == addr)
76 Ipv4EndPointDemux::Allocate (
void)
79 uint16_t port = AllocateEphemeralPort ();
86 m_endPoints.push_back (endPoint);
87 NS_LOG_DEBUG (
"Now have >>" << m_endPoints.size () <<
"<< endpoints.");
92 Ipv4EndPointDemux::Allocate (Ipv4Address address)
95 uint16_t port = AllocateEphemeralPort ();
101 Ipv4EndPoint *endPoint =
new Ipv4EndPoint (address, port);
102 m_endPoints.push_back (endPoint);
103 NS_LOG_DEBUG (
"Now have >>" << m_endPoints.size () <<
"<< endpoints.");
108 Ipv4EndPointDemux::Allocate (uint16_t port)
116 Ipv4EndPointDemux::Allocate (Ipv4Address address, uint16_t port)
119 if (LookupLocal (address, port))
124 Ipv4EndPoint *endPoint =
new Ipv4EndPoint (address, port);
125 m_endPoints.push_back (endPoint);
126 NS_LOG_DEBUG (
"Now have >>" << m_endPoints.size () <<
"<< endpoints.");
131 Ipv4EndPointDemux::Allocate (Ipv4Address localAddress, uint16_t localPort,
132 Ipv4Address peerAddress, uint16_t peerPort)
134 NS_LOG_FUNCTION (
this << localAddress << localPort << peerAddress << peerPort);
135 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
137 if ((*i)->GetLocalPort () == localPort &&
138 (*i)->GetLocalAddress () == localAddress &&
139 (*i)->GetPeerPort () == peerPort &&
140 (*i)->GetPeerAddress () == peerAddress)
142 NS_LOG_WARN (
"No way we can allocate this end-point.");
147 Ipv4EndPoint *endPoint =
new Ipv4EndPoint (localAddress, localPort);
148 endPoint->SetPeer (peerAddress, peerPort);
149 m_endPoints.push_back (endPoint);
151 NS_LOG_DEBUG (
"Now have >>" << m_endPoints.size () <<
"<< endpoints.");
157 Ipv4EndPointDemux::DeAllocate (Ipv4EndPoint *endPoint)
160 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
165 m_endPoints.erase (i);
174 Ipv4EndPointDemux::EndPoints
175 Ipv4EndPointDemux::GetAllEndPoints (
void)
180 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
182 Ipv4EndPoint* endP = *i;
183 ret.push_back (endP);
194 Ipv4EndPointDemux::EndPoints
195 Ipv4EndPointDemux::Lookup (Ipv4Address daddr, uint16_t dport,
196 Ipv4Address saddr, uint16_t sport,
197 Ptr<Ipv4Interface> incomingInterface)
199 NS_LOG_FUNCTION (
this << daddr << dport << saddr << sport << incomingInterface);
206 NS_LOG_DEBUG (
"Looking up endpoint for destination address " << daddr);
207 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
209 Ipv4EndPoint* endP = *i;
210 NS_LOG_DEBUG (
"Looking at endpoint dport=" << endP->GetLocalPort ()
211 <<
" daddr=" << endP->GetLocalAddress ()
212 <<
" sport=" << endP->GetPeerPort ()
213 <<
" saddr=" << endP->GetPeerAddress ());
214 if (endP->GetLocalPort () != dport)
217 <<
" because endpoint dport "
218 << endP->GetLocalPort ()
219 <<
" does not match packet dport " << dport);
222 if (endP->GetBoundNetDevice ())
224 if (endP->GetBoundNetDevice () != incomingInterface->GetDevice ())
227 <<
" because endpoint is bound to specific device and"
228 << endP->GetBoundNetDevice ()
229 <<
" does not match packet device " << incomingInterface->GetDevice ());
233 bool subnetDirected =
false;
234 Ipv4Address incomingInterfaceAddr = daddr;
235 for (uint32_t i = 0; i < incomingInterface->GetNAddresses (); i++)
237 Ipv4InterfaceAddress addr = incomingInterface->GetAddress (i);
238 if (addr.GetLocal ().CombineMask (addr.GetMask ()) == daddr.CombineMask (addr.GetMask ()) &&
239 daddr.IsSubnetDirectedBroadcast (addr.GetMask ()))
241 subnetDirected =
true;
242 incomingInterfaceAddr = addr.GetLocal ();
245 bool isBroadcast = (daddr.IsBroadcast () || subnetDirected ==
true);
246 NS_LOG_DEBUG (
"dest addr " << daddr <<
" broadcast? " << isBroadcast);
247 bool localAddressMatchesWildCard =
249 bool localAddressMatchesExact = endP->GetLocalAddress () == daddr;
253 NS_LOG_DEBUG (
"Found bcast, localaddr " << endP->GetLocalAddress ());
258 localAddressMatchesExact = (endP->GetLocalAddress () ==
259 incomingInterfaceAddr);
262 if (!(localAddressMatchesExact || localAddressMatchesWildCard))
264 bool remotePeerMatchesExact = endP->GetPeerPort () == sport;
265 bool remotePeerMatchesWildCard = endP->GetPeerPort () == 0;
266 bool remoteAddressMatchesExact = endP->GetPeerAddress () == saddr;
267 bool remoteAddressMatchesWildCard = endP->GetPeerAddress () ==
271 if (!(remotePeerMatchesExact || remotePeerMatchesWildCard))
273 if (!(remoteAddressMatchesExact || remoteAddressMatchesWildCard))
277 if (localAddressMatchesWildCard &&
278 remotePeerMatchesWildCard &&
279 remoteAddressMatchesWildCard)
281 retval1.push_back (endP);
283 if ((localAddressMatchesExact || (isBroadcast && localAddressMatchesWildCard))&&
284 remotePeerMatchesWildCard &&
285 remoteAddressMatchesWildCard)
287 retval2.push_back (endP);
289 if (localAddressMatchesWildCard &&
290 remotePeerMatchesExact &&
291 remoteAddressMatchesExact)
293 retval3.push_back (endP);
295 if (localAddressMatchesExact &&
296 remotePeerMatchesExact &&
297 remoteAddressMatchesExact)
299 retval4.push_back (endP);
304 if (!retval4.empty ())
return retval4;
305 if (!retval3.empty ())
return retval3;
306 if (!retval2.empty ())
return retval2;
311 Ipv4EndPointDemux::SimpleLookup (Ipv4Address daddr,
320 uint32_t genericity = 3;
321 Ipv4EndPoint *
generic = 0;
322 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
324 if ((*i)->GetLocalPort () != dport)
328 if ((*i)->GetLocalAddress () == daddr &&
329 (*i)->GetPeerPort () == sport &&
330 (*i)->GetPeerAddress () == saddr)
344 if (tmp < genericity)
353 Ipv4EndPointDemux::AllocateEphemeralPort (
void)
357 uint16_t port = m_ephemeral;
358 int count = m_portLast - m_portFirst;
366 if (port < m_portFirst || port > m_portLast)
370 }
while (LookupPortLocal (port));
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
#define NS_LOG_COMPONENT_DEFINE(name)
#define NS_LOG_LOGIC(msg)
#define NS_LOG_DEBUG(msg)