In my application I use the code below to get the default ip address of host ( needed for port forwarding etc )
On my host machine (XP Pro service pack 3) the Virtual Box ip address is 192.168.0.222 and the real network adapter
ip address is 192.168.0.101
The problem is this. The code below gets the VirtualBox Host-Only Network ip instead of the default ip address I need.
How can I detect which ip is the host ip and which is the virtual box ip.
Keep in mind that my application runs on both windows and linux so windows only solution is not desireable but acceptable
Thanks in advance
Code: Select all
char HostName[ MAX_PATH ];
rval = gethostname(HostName, sizeof(HostName));
if (rval == SOCKET_ERROR)
{
#ifdef WIN_PLATFORM
RCODE rc = WSAGetLastError();
#else
RCODE rc = VxGetLastError()?rc:-1;
#endif // WIN_PLATFORM
log_msg( 0, "VxGetDefaultLocalIp::Init Error %d gethostname\n", rc );
ErrMsgBox( "VxGetDefaultLocalIp::Init Error %d gethostname\n", rc );
return 0;
}
else if( strcmp(HostName, "") )
{
//Look up this host info via supplied name
he = gethostbyname(HostName);
if( he )
{
int i=0;
if( he->h_name)
{
log_msg( 0, "Host Official name: %s\n", he->h_name );
}
char ** pAlias = he->h_aliases;
if( *pAlias != 0 )
{
log_msg( 0, "Host Alternate name #%d: %s\n", *pAlias);
}
while (he->h_addr_list[i])
{
memcpy(&u32Ip, he->h_addr_list[i++], 4);
// get
//convert to host endian
u32Ip = ntohl( u32Ip );
if( u32Ip )
{
log_msg( 0, "Local Ip %s\n", VxIpToString( u32Ip ) );
return u32Ip;
}
i++;
}
}