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.
精彩评论