[ofw] question resolving IP addresses
Tzachi Dar
tzachid at mellanox.co.il
Wed Dec 10 16:47:06 PST 2008
Here is a part of a code that I once wrote, I hope this can help.
Thanks
Tzachi
HRESULT hr = S_OK;
WspConnectIn ConnectIn;
WspConnectOut ConnectOut;
DWORD BytesReturned = 0;
MIB_IPFORWARDROW BestRoute;
char temp[10000];
ULONG size = sizeof temp;
PMIB_IPADDRTABLE pIp = (PMIB_IPADDRTABLE) temp;
unsigned int i;
struct sockaddr_in *addr = (struct sockaddr_in *)name;
// Verify input paramters
if( namelen != sizeof(struct sockaddr_in) ) {
hr = E_INVALIDARG;
*lpErrno = WSAEFAULT;
goto Cleanup;
}
if( addr->sin_family != AF_INET ) {
hr = E_INVALIDARG;
*lpErrno = WSAEFAULT;
goto Cleanup;
}
if( addr->sin_addr.S_un.S_addr == INADDR_ANY ) {
hr = E_INVALIDARG;
*lpErrno = WSAEFAULT;
goto Cleanup;
}
// Parameters were verified, lets pass the request down.
// We translate this params to be in host format
ConnectIn.DestIP = ntohl(addr->sin_addr.S_un.S_addr);
ConnectIn.DestPort = ntohs(addr->sin_port);
ConnectIn.pSocket = KernelHandle;
ConnectIn.ForceMTU1024 = m_ForceMTU1024;
// Let's get the source IP (Based on GetBestRoute)
DWORD dwRet = GetBestRoute(
addr->sin_addr.S_un.S_addr,
0,
&BestRoute);
if (dwRet != NO_ERROR) {
hr = HRESULT_FROM_WIN32(dwRet);
*lpErrno = WSAENETUNREACH;
goto Cleanup;
}
dwRet = GetIpAddrTable((PMIB_IPADDRTABLE) temp, &size, FALSE);
if (dwRet != NO_ERROR) {
SDPDLL_PRINT(TRACE_LEVEL_ERROR ,SDPDLL_SDPLIB ,("GetIpAddrTable
failed error = 0x%x", dwRet));
hr = HRESULT_FROM_WIN32(dwRet);
*lpErrno = WSAENETUNREACH;
goto Cleanup;
}
ConnectIn.SrcIP = 0;
for ( i=0;i<pIp->dwNumEntries ; i++) {
if (pIp->table[i].dwIndex == BestRoute.dwForwardIfIndex) {
// We have found the interface that we want
ConnectIn.SrcIP = ntohl(pIp->table[i].dwAddr);
}
}
if (ConnectIn.SrcIP == 0) {
// We couldn't find the local addresses
hr = E_INVALIDARG;
*lpErrno = WSAENETUNREACH;
goto Cleanup;
}
// Now do the arp of the local addresses
ULONG pMacAddr[2], PhyAddrLen ;
PhyAddrLen = sizeof(pMacAddr);
const int MaxArp = 13;
for (int i = 0 ; i < MaxArp; i++) {
PhyAddrLen = sizeof(pMacAddr);
dwRet = SendARP(addr->sin_addr.S_un.S_addr ,0 ,pMacAddr,
&PhyAddrLen );
if (dwRet == ERROR_GEN_FAILURE) {
// This seems to be a BUG in windows when some threads call
ARP togeater
int sevirity;
if (i < MaxArp-1) {
sevirity = TRACE_LEVEL_INFORMATION;
} else {
sevirity = TRACE_LEVEL_ERROR;
}
Sleep(rand() * 10*i*i / RAND_MAX + 10 * i);
} else {
// Everyting is fine
break;
}
}
if (dwRet != NO_ERROR) {
hr = HRESULT_FROM_WIN32(dwRet);
*lpErrno = WSAENETUNREACH;
goto Cleanup;
}
memcpy(ConnectIn.DestMac, pMacAddr, MAC_ADDR_SIZE);
> -----Original Message-----
> From: ofw-bounces at lists.openfabrics.org
> [mailto:ofw-bounces at lists.openfabrics.org] On Behalf Of Sean Hefty
> Sent: Thursday, December 11, 2008 12:38 AM
> To: ofw at lists.openfabrics.org
> Subject: [ofw] question resolving IP addresses
>
> Does anyone know of sample code or what APIs to invoke to
> find what source IP address to use to reach a given
> destination IP address? I need APIs that work on 2003 and 2008
>
> _______________________________________________
> ofw mailing list
> ofw at lists.openfabrics.org
> http://lists.openfabrics.org/cgi-bin/mailman/listinfo/ofw
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openfabrics.org/pipermail/ofw/attachments/20081211/03af1321/attachment.html>
More information about the ofw
mailing list