Kundan Kumar
Kundan Kumar

Reputation: 2002

Can any one please suggest what's needed to change so that it becomes compatible with IPV6 also. Its working fine for IPV4

int lookup_numeric( const char * hostname, char * ip_address )
{
    int index = 0;
    int value = 0;

    for( const char * cursor = hostname; ; ++cursor )
    {
    if( ( '0' <= *cursor ) && ( *cursor <= '9' ) )
    {
        value *= 10;
        value += *cursor - '0';
        if( value > 255 )
        break;
    }
    else if( *cursor == '.' )
    {
        if( index >= IpAddressSize ) //IpAddressSize is 16 for IPV6 and 4 for IPV4.
        break;
        ip_address[ index++ ] = (char)value;
        value = 0;
    }
    else if( *cursor == '\0' )
    {
        if( index != IpAddressSize - 1 )
        break;
        ip_address[ index ] = (char)value;
        return 1;
    }
    else
        break;
    }
    return 0;
}

Upvotes: 0

Views: 70

Answers (1)

Len Holgate
Len Holgate

Reputation: 21644

The function appears to take a text representation of an IPv4 address (e.g. "127.0.0.1") and converts it into an array of bytes.

You really shouldn't be doing this kind of thing by hand, in my opinion, you'd be better off using getaddrinfo() which is probably available on your platform.

Upvotes: 3

Related Questions