开发者

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

开发者 https://www.devze.com 2023-03-03 16:24 出处:网络
int lookup_numeric( const char * hostname, char * ip_address ) { int index = 0; int value = 0; for( const char * cursor = hostname; ; ++cursor )
int lookup_numeric( const char * hostname, char * ip_address )
{
    int index = 0;
    int value = 0;

    for( const char * cursor = hostname; ; ++cursor )
    {
    if( ( '0' <= *cursor ) &&am开发者_如何学Cp; ( *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;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消