00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ipv4-end-point-demux.h"
00022 #include "ipv4-end-point.h"
00023 #include "ns3/log.h"
00024
00025 namespace ns3{
00026
00027 NS_LOG_COMPONENT_DEFINE ("Ipv4EndPointDemux");
00028
00029 Ipv4EndPointDemux::Ipv4EndPointDemux ()
00030 : m_ephemeral (49152)
00031 {
00032 NS_LOG_FUNCTION_NOARGS ();
00033 }
00034
00035 Ipv4EndPointDemux::~Ipv4EndPointDemux ()
00036 {
00037 NS_LOG_FUNCTION_NOARGS ();
00038 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
00039 {
00040 Ipv4EndPoint *endPoint = *i;
00041 delete endPoint;
00042 }
00043 m_endPoints.clear ();
00044 }
00045
00046 bool
00047 Ipv4EndPointDemux::LookupPortLocal (uint16_t port)
00048 {
00049 NS_LOG_FUNCTION_NOARGS ();
00050 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
00051 {
00052 if ((*i)->GetLocalPort () == port)
00053 {
00054 return true;
00055 }
00056 }
00057 return false;
00058 }
00059
00060 bool
00061 Ipv4EndPointDemux::LookupLocal (Ipv4Address addr, uint16_t port)
00062 {
00063 NS_LOG_FUNCTION_NOARGS ();
00064 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
00065 {
00066 if ((*i)->GetLocalPort () == port &&
00067 (*i)->GetLocalAddress () == addr)
00068 {
00069 return true;
00070 }
00071 }
00072 return false;
00073 }
00074
00075 Ipv4EndPoint *
00076 Ipv4EndPointDemux::Allocate (void)
00077 {
00078 NS_LOG_FUNCTION_NOARGS ();
00079 uint16_t port = AllocateEphemeralPort ();
00080 if (port == 0)
00081 {
00082 NS_LOG_WARN ("Ephemeral port allocation failed.");
00083 return 0;
00084 }
00085 Ipv4EndPoint *endPoint = new Ipv4EndPoint (Ipv4Address::GetAny (), port);
00086 m_endPoints.push_back (endPoint);
00087 NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
00088 return endPoint;
00089 }
00090
00091 Ipv4EndPoint *
00092 Ipv4EndPointDemux::Allocate (Ipv4Address address)
00093 {
00094 NS_LOG_FUNCTION (this << address);
00095 uint16_t port = AllocateEphemeralPort ();
00096 if (port == 0)
00097 {
00098 NS_LOG_WARN ("Ephemeral port allocation failed.");
00099 return 0;
00100 }
00101 Ipv4EndPoint *endPoint = new Ipv4EndPoint (address, port);
00102 m_endPoints.push_back (endPoint);
00103 NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
00104 return endPoint;
00105 }
00106
00107 Ipv4EndPoint *
00108 Ipv4EndPointDemux::Allocate (uint16_t port)
00109 {
00110 NS_LOG_FUNCTION (this << port);
00111
00112 return Allocate (Ipv4Address::GetAny (), port);
00113 }
00114
00115 Ipv4EndPoint *
00116 Ipv4EndPointDemux::Allocate (Ipv4Address address, uint16_t port)
00117 {
00118 NS_LOG_FUNCTION (this << address << port);
00119 if (LookupLocal (address, port))
00120 {
00121 NS_LOG_WARN ("Duplicate address/port; failing.");
00122 return 0;
00123 }
00124 Ipv4EndPoint *endPoint = new Ipv4EndPoint (address, port);
00125 m_endPoints.push_back (endPoint);
00126 NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
00127 return endPoint;
00128 }
00129
00130 Ipv4EndPoint *
00131 Ipv4EndPointDemux::Allocate (Ipv4Address localAddress, uint16_t localPort,
00132 Ipv4Address peerAddress, uint16_t peerPort)
00133 {
00134 NS_LOG_FUNCTION (this << localAddress << localPort << peerAddress << peerPort);
00135 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
00136 {
00137 if ((*i)->GetLocalPort () == localPort &&
00138 (*i)->GetLocalAddress () == localAddress &&
00139 (*i)->GetPeerPort () == peerPort &&
00140 (*i)->GetPeerAddress () == peerAddress)
00141 {
00142 NS_LOG_WARN ("No way we can allocate this end-point.");
00143
00144 return 0;
00145 }
00146 }
00147 Ipv4EndPoint *endPoint = new Ipv4EndPoint (localAddress, localPort);
00148 endPoint->SetPeer (peerAddress, peerPort);
00149 m_endPoints.push_back (endPoint);
00150
00151 NS_LOG_DEBUG ("Now have >>" << m_endPoints.size () << "<< endpoints.");
00152
00153 return endPoint;
00154 }
00155
00156 void
00157 Ipv4EndPointDemux::DeAllocate (Ipv4EndPoint *endPoint)
00158 {
00159 NS_LOG_FUNCTION_NOARGS ();
00160 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
00161 {
00162 if (*i == endPoint)
00163 {
00164 delete endPoint;
00165 m_endPoints.erase (i);
00166 break;
00167 }
00168 }
00169 }
00170
00171
00172
00173
00174 Ipv4EndPointDemux::EndPoints
00175 Ipv4EndPointDemux::GetAllEndPoints (void)
00176 {
00177 NS_LOG_FUNCTION_NOARGS ();
00178 EndPoints ret;
00179
00180 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
00181 {
00182 Ipv4EndPoint* endP = *i;
00183 ret.push_back(endP);
00184 }
00185 return ret;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 Ipv4EndPointDemux::EndPoints
00195 Ipv4EndPointDemux::Lookup (Ipv4Address daddr, uint16_t dport,
00196 Ipv4Address saddr, uint16_t sport,
00197 Ptr<Ipv4Interface> incomingInterface)
00198 {
00199 NS_LOG_FUNCTION_NOARGS ();
00200 EndPoints retval1;
00201 EndPoints retval2;
00202 EndPoints retval3;
00203 EndPoints retval4;
00204
00205 NS_LOG_FUNCTION (this << daddr << dport << saddr << sport << incomingInterface);
00206 NS_LOG_DEBUG ("Looking up endpoint for destination address " << daddr);
00207 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
00208 {
00209 Ipv4EndPoint* endP = *i;
00210 NS_LOG_DEBUG ("Looking at endpoint dport=" << endP->GetLocalPort ()
00211 << " daddr=" << endP->GetLocalAddress ()
00212 << " sport=" << endP->GetPeerPort ()
00213 << " saddr=" << endP->GetPeerAddress ());
00214 if (endP->GetLocalPort () != dport)
00215 {
00216 NS_LOG_LOGIC ("Skipping endpoint " << &endP
00217 << " because endpoint dport "
00218 << endP->GetLocalPort ()
00219 << " does not match packet dport " << dport);
00220 continue;
00221 }
00222 bool isBroadcast = (daddr.IsBroadcast () ||
00223 daddr.IsSubnetDirectedBroadcast (
00224 incomingInterface->GetNetworkMask ()));
00225 Ipv4Address incomingInterfaceAddr = incomingInterface->GetAddress ();
00226 NS_LOG_DEBUG ("dest addr " << daddr << " broadcast? " << isBroadcast);
00227 bool localAddressMatchesWildCard =
00228 endP->GetLocalAddress() == Ipv4Address::GetAny();
00229 bool localAddressMatchesExact = endP->GetLocalAddress () == daddr;
00230
00231 if (isBroadcast)
00232 {
00233 NS_LOG_DEBUG("Found bcast, localaddr " << endP->GetLocalAddress());
00234 }
00235
00236 if (isBroadcast && (endP->GetLocalAddress() != Ipv4Address::GetAny()))
00237 {
00238 localAddressMatchesExact = (endP->GetLocalAddress () ==
00239 incomingInterfaceAddr);
00240 }
00241
00242 if (!(localAddressMatchesExact || localAddressMatchesWildCard))
00243 continue;
00244 bool remotePeerMatchesExact = endP->GetPeerPort () == sport;
00245 bool remotePeerMatchesWildCard = endP->GetPeerPort() == 0;
00246 bool remoteAddressMatchesExact = endP->GetPeerAddress () == saddr;
00247 bool remoteAddressMatchesWildCard = endP->GetPeerAddress () ==
00248 Ipv4Address::GetAny();
00249
00250
00251 if (!(remotePeerMatchesExact || remotePeerMatchesWildCard))
00252 continue;
00253 if (!(remoteAddressMatchesExact || remoteAddressMatchesWildCard))
00254 continue;
00255
00256
00257 if (localAddressMatchesWildCard &&
00258 remotePeerMatchesWildCard &&
00259 remoteAddressMatchesWildCard)
00260 {
00261 retval1.push_back(endP);
00262 }
00263 if ((localAddressMatchesExact || (isBroadcast && localAddressMatchesWildCard))&&
00264 remotePeerMatchesWildCard &&
00265 remoteAddressMatchesWildCard)
00266 {
00267 retval2.push_back(endP);
00268 }
00269 if (localAddressMatchesWildCard &&
00270 remotePeerMatchesExact &&
00271 remoteAddressMatchesExact)
00272 {
00273 retval3.push_back(endP);
00274 }
00275 if (localAddressMatchesExact &&
00276 remotePeerMatchesExact &&
00277 remoteAddressMatchesExact)
00278 {
00279 retval4.push_back(endP);
00280 }
00281 }
00282
00283
00284 if (!retval4.empty()) return retval4;
00285 if (!retval3.empty()) return retval3;
00286 if (!retval2.empty()) return retval2;
00287 return retval1;
00288 }
00289
00290 Ipv4EndPoint *
00291 Ipv4EndPointDemux::SimpleLookup (Ipv4Address daddr,
00292 uint16_t dport,
00293 Ipv4Address saddr,
00294 uint16_t sport)
00295 {
00296
00297
00298 uint32_t genericity = 3;
00299 Ipv4EndPoint *generic = 0;
00300 for (EndPointsI i = m_endPoints.begin (); i != m_endPoints.end (); i++)
00301 {
00302 if ((*i)->GetLocalPort () != dport)
00303 {
00304 continue;
00305 }
00306 if ((*i)->GetLocalAddress () == daddr &&
00307 (*i)->GetPeerPort () == sport &&
00308 (*i)->GetPeerAddress () == saddr)
00309 {
00310
00311 return *i;
00312 }
00313 uint32_t tmp = 0;
00314 if ((*i)->GetLocalAddress () == Ipv4Address::GetAny ())
00315 {
00316 tmp ++;
00317 }
00318 if ((*i)->GetPeerAddress () == Ipv4Address::GetAny ())
00319 {
00320 tmp ++;
00321 }
00322 if (tmp < genericity)
00323 {
00324 generic = (*i);
00325 genericity = tmp;
00326 }
00327 }
00328 return generic;
00329 }
00330
00331 uint16_t
00332 Ipv4EndPointDemux::AllocateEphemeralPort (void)
00333 {
00334 NS_LOG_FUNCTION_NOARGS ();
00335 uint16_t port = m_ephemeral;
00336 do
00337 {
00338 port++;
00339 if (port == 65535)
00340 {
00341 port = 49152;
00342 }
00343 if (!LookupPortLocal (port))
00344 {
00345 return port;
00346 }
00347 } while (port != m_ephemeral);
00348 return 0;
00349 }
00350
00351 }
00352