示例#1
0
static void NET_GetLocalAddress( void )
{
    char            hostname[ 256 ];
    struct addrinfo hint;

    struct addrinfo *res = NULL;

    numIP = 0;

    if ( gethostname( hostname, 256 ) == SOCKET_ERROR )
    {
        return;
    }

    Com_Printf(_( "Hostname: %s\n"), hostname );

    memset( &hint, 0, sizeof( hint ) );

    hint.ai_family = AF_UNSPEC;
    hint.ai_socktype = SOCK_DGRAM;

    if ( !getaddrinfo( hostname, NULL, &hint, &res ) )
    {
        struct sockaddr_in  mask4;

        struct sockaddr_in6 mask6;

        struct addrinfo     *search;

        /* On operating systems where it's more difficult to find out the configured interfaces, we'll just assume a
         * netmask with all bits set. */

        memset( &mask4, 0, sizeof( mask4 ) );
        memset( &mask6, 0, sizeof( mask6 ) );
        mask4.sin_family = AF_INET;
        memset( &mask4.sin_addr.s_addr, 0xFF, sizeof( mask4.sin_addr.s_addr ) );
        mask6.sin6_family = AF_INET6;
        memset( &mask6.sin6_addr, 0xFF, sizeof( mask6.sin6_addr ) );

        // add all IPs from returned list.
        for ( search = res; search; search = search->ai_next )
        {
            if ( search->ai_family == AF_INET )
            {
                NET_AddLocalAddress( "", search->ai_addr, ( struct sockaddr * ) &mask4 );
            }
            else if ( search->ai_family == AF_INET6 )
            {
                NET_AddLocalAddress( "", search->ai_addr, ( struct sockaddr * ) &mask6 );
            }
        }

        Sys_ShowIP();
    }

    if ( res )
    {
        freeaddrinfo( res );
    }
}
示例#2
0
static void NET_GetLocalAddress( void )
{
    struct ifaddrs *ifap, *search;

    numIP = 0;

    if ( getifaddrs( &ifap ) )
    {
        Com_Printf(_( "NET_GetLocalAddress: Unable to get list of network interfaces: %s\n"), NET_ErrorString() );
    }
    else
    {
        for ( search = ifap; search; search = search->ifa_next )
        {
            // Only add interfaces that are up.
            if ( ifap->ifa_flags & IFF_UP )
            {
                NET_AddLocalAddress( search->ifa_name, search->ifa_addr, search->ifa_netmask );
            }
        }

        freeifaddrs( ifap );

        Sys_ShowIP();
    }
}
示例#3
0
static void NET_GetLocalAddress(void)
{
	char               hostname[256];
	struct hostent     *hostInfo;
	char               *p;
	struct sockaddr_in sockaddr;
	struct sockaddr_in mask4;
	int                numIP;

	if (gethostname(hostname, 256) == -1)
	{
		return;
	}

	hostInfo = gethostbyname(hostname);
	if (!hostInfo)
	{
		return;
	}

	if (hostInfo->h_addrtype != AF_INET)
	{
		return;
	}

	memset(&mask4, 0, sizeof(mask4));

	mask4.sin_family = AF_INET;
	memset(&mask4.sin_addr.s_addr, 0xFF, sizeof(mask4.sin_addr.s_addr));

	numIP = 0;
	while ((p = hostInfo->h_addr_list[numIP++]) != NULL)
	{
		memcpy((char *)&(sockaddr.sin_addr.s_addr), (char *)p, (size_t)hostInfo->h_length);
		NET_AddLocalAddress("", (struct sockaddr *)&sockaddr, (struct sockaddr *) &mask4);
	}
}