<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.6000.16587" name=GENERATOR></HEAD>
<BODY><!-- Converted from text/plain format -->
<P><FONT size=2>Here is a part of a code that I once wrote, I hope this can
help.<BR><BR>Thanks<BR>Tzachi<BR><BR> HRESULT hr =
S_OK;<BR> WspConnectIn ConnectIn;<BR>
WspConnectOut ConnectOut;<BR> DWORD BytesReturned =
0;<BR> MIB_IPFORWARDROW BestRoute;<BR> char
temp[10000];<BR> ULONG size = sizeof
temp;<BR> PMIB_IPADDRTABLE pIp = (PMIB_IPADDRTABLE)
temp; <BR> unsigned int
i;<BR><BR> struct sockaddr_in *addr = (struct sockaddr_in
*)name;<BR> // Verify input paramters<BR>
if( namelen != sizeof(struct sockaddr_in) )
{<BR> hr =
E_INVALIDARG;<BR> *lpErrno =
WSAEFAULT;<BR> goto
Cleanup;<BR> }<BR> <BR> if(
addr->sin_family != AF_INET ) {<BR>
hr = E_INVALIDARG;<BR> *lpErrno =
WSAEFAULT;<BR> goto
Cleanup;<BR> }<BR><BR> if(
addr->sin_addr.S_un.S_addr == INADDR_ANY )
{<BR> hr =
E_INVALIDARG;<BR> *lpErrno =
WSAEFAULT;<BR> goto
Cleanup;<BR> }<BR><BR> // Parameters were
verified, lets pass the request down.<BR> // We translate this
params to be in host
format<BR> <BR>
ConnectIn.DestIP = ntohl(addr->sin_addr.S_un.S_addr);<BR>
ConnectIn.DestPort = ntohs(addr->sin_port);<BR>
ConnectIn.pSocket = KernelHandle;<BR> ConnectIn.ForceMTU1024 =
m_ForceMTU1024;<BR><BR> // Let's get the source IP (Based on
GetBestRoute)<BR> DWORD dwRet = <FONT
color=#ff0000>GetBestRoute</FONT>(<BR>
addr->sin_addr.S_un.S_addr,<BR>
0,<BR>
&BestRoute);<BR><BR> if (dwRet != NO_ERROR)
{<BR> hr =
HRESULT_FROM_WIN32(dwRet);<BR>
*lpErrno = WSAENETUNREACH;<BR> goto
Cleanup;<BR> }<BR> dwRet = <FONT
color=#ff0000>GetIpAddrTable</FONT>((PMIB_IPADDRTABLE) temp, &size,
FALSE);<BR> if (dwRet != NO_ERROR)
{<BR> SDPDLL_PRINT(TRACE_LEVEL_ERROR
,SDPDLL_SDPLIB ,("GetIpAddrTable failed error = 0x%x",
dwRet));<BR> hr =
HRESULT_FROM_WIN32(dwRet);<BR>
*lpErrno = WSAENETUNREACH;<BR> goto
Cleanup;<BR> }<BR><BR> ConnectIn.SrcIP =
0;<BR> for ( i=0;i<pIp->dwNumEntries ; i++)
{<BR> if (pIp->table[i].dwIndex ==
BestRoute.dwForwardIfIndex)
{<BR> // We
have found the interface that we
want<BR>
ConnectIn.SrcIP =
ntohl(pIp->table[i].dwAddr);<BR>
}<BR> }<BR> if (ConnectIn.SrcIP == 0)
{<BR> // We couldn't find the local
addresses<BR> hr =
E_INVALIDARG;<BR> *lpErrno =
WSAENETUNREACH;<BR> goto
Cleanup; <BR>
}<BR><BR> // Now do the arp of the local
addresses<BR> ULONG pMacAddr[2], PhyAddrLen
;<BR> PhyAddrLen = sizeof(pMacAddr);<BR>
const int MaxArp = 13;<BR><BR> for (int i = 0 ; i < MaxArp;
i++) {<BR> PhyAddrLen =
sizeof(pMacAddr);<BR> dwRet = <FONT
color=#ff0000>SendARP</FONT>(addr->sin_addr.S_un.S_addr ,0 ,pMacAddr,
&PhyAddrLen );<BR> if (dwRet ==
ERROR_GEN_FAILURE)
{<BR> // This
seems to be a BUG in windows when some threads call ARP
togeater<BR>
int
sevirity;<BR>
if (i < MaxArp-1)
{<BR>
sevirity =
TRACE_LEVEL_INFORMATION;<BR>
} else
{<BR>
sevirity =
TRACE_LEVEL_ERROR;<BR>
}<BR>
Sleep(rand() * 10*i*i / RAND_MAX + 10 *
i);<BR> <BR>
} else {<BR>
// Everyting is
fine<BR>
break;<BR> }<BR>
}<BR> if (dwRet != NO_ERROR)
{<BR> <BR>
hr =
HRESULT_FROM_WIN32(dwRet);<BR>
*lpErrno =
WSAENETUNREACH;<BR>
goto
Cleanup;<BR> <BR>
}<BR> memcpy(ConnectIn.DestMac, pMacAddr,
MAC_ADDR_SIZE);<BR><BR>> -----Original Message-----<BR>> From:
ofw-bounces@lists.openfabrics.org<BR>> [<A
href="mailto:ofw-bounces@lists.openfabrics.org">mailto:ofw-bounces@lists.openfabrics.org</A>]
On Behalf Of Sean Hefty<BR>> Sent: Thursday, December 11, 2008 12:38
AM<BR>> To: ofw@lists.openfabrics.org<BR>> Subject: [ofw] question
resolving IP addresses<BR>><BR>> Does anyone know of sample code or what
APIs to invoke to<BR>> find what source IP address to use to reach a
given<BR>> destination IP address? I need APIs that work on 2003 and
2008<BR>><BR>> _______________________________________________<BR>> ofw
mailing list<BR>> ofw@lists.openfabrics.org<BR>> <A
href="http://lists.openfabrics.org/cgi-bin/mailman/listinfo/ofw">http://lists.openfabrics.org/cgi-bin/mailman/listinfo/ofw</A><BR>>
</FONT></P></BODY></HTML>