Page 1 of 1

Virtual Box changed my default ip address.. how to detect it

Posted: 3. Jul 2009, 18:31
by slytron
Virtual Box changed my default ip address
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++;
			}
		}


Re: Virtual Box changed my default ip address.. how to detect it

Posted: 3. Jul 2009, 18:53
by vbox4me2
Run ipconfig/ifconfig and redirect output to a share, then parse it with Perl?

Re: Virtual Box changed my default ip address.. how to detect it

Posted: 3. Jul 2009, 19:50
by slytron
vbox4me2 wrote:Run ipconfig/ifconfig and redirect output to a share, then parse it with Perl?
Well I found a windoz solution and most linux users are smart enough to do port forwading manually.. guess ill leave it at that.
here is my updated code

Code: Select all

//---------------------------------------------------------------------------
//! get default local ip address.. returns 0 if none
U32 VxGetDefaultLocalIp( void )
{
	//get default local ip
	int rval;
	struct hostent *he;
	U32 u32Ip = 0;
	#ifdef WIN_PLATFORM
		PIP_ADAPTER_INFO pinfo	= NULL;
		U32 u32AdaptInfoLen		= 0;
    #endif // WIN_PLATFORM

	//Get local host name and crudely validate
	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);
				//convert to host endian
				u32Ip = ntohl( u32Ip );
				std::string strHostIp = VxIpToString( u32Ip );
#ifdef WIN_PLATFORM
				// try to filter out virtual adapters
				U8		au8AdaptInfo[ 4096 ];
				u32AdaptInfoLen = 4096;
				pinfo = (PIP_ADAPTER_INFO)au8AdaptInfo;
				U32 u32Error;
				u32Error	=	GetAdaptersInfo( pinfo, &u32AdaptInfoLen );
				std::string strIp;
				BOOL bUseable = true;
				if( (0 == u32Error ) && pinfo )
				{
					while( pinfo )
					{
						BOOL bFoundIp = false;
						// see if this adapter has the ip we found
						PIP_ADDR_STRING pAddressList = &(pinfo->IpAddressList);
						strIp	= "";
						do 
						{
							strIp =	pAddressList->IpAddress.String;
							if( 0 == strcmp( strIp.c_str(), strHostIp.c_str() ) )
							{
								bFoundIp = true;
								break;
							}
							pAddressList = pAddressList->Next;
						}while( pAddressList != NULL );
						if( false == bFoundIp )
						{
							// this adapter is not the one for the ip address
							pinfo = pinfo->Next;
							continue;
						}
						break;
					}
					// check to see if adapter is useable
					std::string strGatway	=	pinfo->GatewayList.IpAddress.String;
					if( 0 == strGatway.size() )
					{
						bUseable = false; // no gateway
					}
					//std::string strDesc		=	pinfo->Description;
					//if( strstr( strDesc.c_str(), "Virtual" ) )
					//{
					//	// dont use anything with the word virtual
					//	bUseable = false;
					//}
					//m_type.Format("%d",pinfo->Type);
					//m_subnet.Format("%s",pinfo->IpAddressList.IpMask.String);
					if( false == bUseable )
					{
						// skip this ip
						continue;
					}
				}
#endif // WIN_PLATFORM

				if( u32Ip )
				{
					log_msg( 0, "Local Ip %s\n", VxIpToString( u32Ip ) );
					return u32Ip;
				}
			}
		}
	}
	log_msg( 0, "VxGetDefaultLocalIp::NO IP ADDRESS FOUND\n" );
	ErrMsgBox( "VxGetDefaultLocalIp::NO IP ADDRESS FOUND\n" );
	return 0;
}