[Opalvoip-svn] SF.net SVN: opalvoip: [18750] ptlib/trunk
Brought to you by:
csoutheren,
rjongbloed
From: <rjo...@us...> - 2007-10-23 05:10:05
|
Revision: 18750 http://opalvoip.svn.sourceforge.net/opalvoip/?rev=18750&view=rev Author: rjongbloed Date: 2007-10-22 22:10:09 -0700 (Mon, 22 Oct 2007) Log Message: ----------- Applied OpenH323 patch 1532400 Function to do interface selection based on remote address. Thanks Drazen Dimoti (jimbosimo) Modified Paths: -------------- ptlib/trunk/include/ptlib/ipsock.h ptlib/trunk/src/ptlib/msos/ethsock.cxx Modified: ptlib/trunk/include/ptlib/ipsock.h =================================================================== --- ptlib/trunk/include/ptlib/ipsock.h 2007-10-23 04:14:53 UTC (rev 18749) +++ ptlib/trunk/include/ptlib/ipsock.h 2007-10-23 05:10:09 UTC (rev 18750) @@ -776,7 +776,16 @@ */ static PString GetGatewayInterface(); - #ifdef _WIN32 + /** Get the interface address that will be used to reach the specified + remote address. Uses longest prefix match when multiple matching interfaces + are found. + + @return + Network interface address. + */ + static PIPSocket::Address GetRouteInterfaceAddress(PIPSocket::Address remoteAddress); + +#ifdef _WIN32 /** Get the IP address for the interface that is being used as the gateway, that is, the interface that packets on the default route will be sent. Modified: ptlib/trunk/src/ptlib/msos/ethsock.cxx =================================================================== --- ptlib/trunk/src/ptlib/msos/ethsock.cxx 2007-10-23 04:14:53 UTC (rev 18749) +++ ptlib/trunk/src/ptlib/msos/ethsock.cxx 2007-10-23 05:10:09 UTC (rev 18750) @@ -1994,6 +1994,57 @@ return snmp.GetInterfaceName(ifNum); } + +PIPSocket::Address PIPSocket::GetRouteInterfaceAddress(PIPSocket::Address remoteAddress) +{ + PIPSocket::InterfaceTable hostInterfaceTable; + PIPSocket::GetInterfaceTable(hostInterfaceTable); + + PIPSocket::RouteTable hostRouteTable; + PIPSocket::GetRouteTable(hostRouteTable); + + if (hostInterfaceTable.IsEmpty()) + return PIPSocket::GetDefaultIpAny(); + + for (PINDEX IfaceIdx = 0; IfaceIdx < hostInterfaceTable.GetSize(); IfaceIdx++) { + if (remoteAddress == hostInterfaceTable[IfaceIdx].GetAddress()) { + PTRACE(5, "PWLib\tRoute packet for " << remoteAddress + << " over interface " << hostInterfaceTable[IfaceIdx].GetName() + << "[" << hostInterfaceTable[IfaceIdx].GetAddress() << "]"); + return hostInterfaceTable[IfaceIdx].GetAddress(); + } + } + + PIPSocket::RouteEntry * route = NULL; + for (PINDEX routeIdx = 0; routeIdx < hostRouteTable.GetSize(); routeIdx++) { + PIPSocket::RouteEntry & routeEntry = hostRouteTable[routeIdx]; + + DWORD network = (DWORD) routeEntry.GetNetwork(); + DWORD mask = (DWORD) routeEntry.GetNetMask(); + + if (((DWORD)remoteAddress & mask) == network) { + if (route == NULL) + route = &routeEntry; + else if ((DWORD)routeEntry.GetNetMask() > (DWORD)route->GetNetMask()) + route = &routeEntry; + } + } + + if (route != NULL) { + for (PINDEX IfaceIdx = 0; IfaceIdx < hostInterfaceTable.GetSize(); IfaceIdx++) { + if (route->GetInterface() == hostInterfaceTable[IfaceIdx].GetName()) { + PTRACE(5, "PWLib\tRoute packet for " << remoteAddress + << " over interface " << hostInterfaceTable[IfaceIdx].GetName() + << "[" << hostInterfaceTable[IfaceIdx].GetAddress() << "]"); + return hostInterfaceTable[IfaceIdx].GetAddress(); + } + } + } + + return PIPSocket::GetDefaultIpAny(); +} + + PIPSocket::Address PIPSocket::GetGatewayInterfaceAddress() { PWaitAndSignal m(GetSNMPMutex()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |