Esempio n. 1
0
void MatchMaker_RequestConnection( char *_targetIp, int _targetPort, Directory *_myDetails )
{
#ifdef WAN_PLAY_ENABLED
    AppDebugOut( "Requesting connection to %s:%d via matchmaker\n", _targetIp, _targetPort );

    Directory request( _myDetails );
    request.SetName     ( NET_MATCHMAKER_MESSAGE );
    request.CreateData  ( NET_METASERVER_COMMAND, NET_MATCHMAKER_REQUEST_CONNECT );
    request.CreateData  ( NET_MATCHMAKER_TARGETIP, _targetIp );
    request.CreateData  ( NET_MATCHMAKER_TARGETPORT, _targetPort );

    AppAssert( s_matchMakerIp );

    NetSocket socket;   
    NetRetCode result = socket.Connect( s_matchMakerIp, s_matchMakerPort );
    AppAssert( result == NetOk );

    MetaServer_SendDirectory( &request, &socket );
#endif
}
Esempio n. 2
0
void GetLocalHostIP( char *str, int len )
{
	static bool retrievedAddress = false;
	static char localHostIP[16];

	if (!retrievedAddress) 
	{
		// Try to find the IP address of the default route (without using name lookup)
		NetSocket s;
	
		// It doesn't actually matter whether the host is reachable
		// Since UDP is a connectionless protocol, the only effect 
		// of connect is to see if there is a route to the specified
		// host.
		
		if (s.Connect( "80.175.29.66", 5000 ) == NetOk) 
		{
			NetIpAddress localAddress = s.GetLocalAddress();

			// On some systems (Win98) this can return 0.0.0.0 
			// which is not a valid IP to connect to.
			if (* (unsigned long *) (&localAddress.sin_addr) != 0) {
				IpAddressToStr( localAddress, localHostIP );
				retrievedAddress = true;
			}

			// It seems that on some systems this can return 0.0.0.0, strange, strange!
			if (strcmp( localHostIP, "0.0.0.0" ) != 0)
				retrievedAddress = true;
		}

		if (!retrievedAddress) {
			// Not connected to the internet (or 0.0.0.0 returned), revert to 
			// host name lookup method

			// Default fallback
			strcpy( localHostIP, "127.0.0.1" );

			// Get our host name
			char hostname[256];
			int result = gethostname( hostname, sizeof(hostname) );
			if (result >= 0) {
				struct hostent *lpHostEnt = gethostbyname( hostname );

				if (lpHostEnt) {
					NetIpAddress addr;
					memcpy( &addr.sin_addr, lpHostEnt->h_addr_list[0], sizeof(addr.sin_addr) );
					addr.sin_port = 0;

					IpAddressToStr( addr, localHostIP );
				}
			}
			
		}

		AppDebugOut("Local IP address is: %s\n", localHostIP );
		retrievedAddress = true;
	}

	strncpy( str, localHostIP, len );
	str[ len - 1 ] = '\0';
}