任何人都可以建议需要进行哪些更改,以便它也与 IPV6 兼容。 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该函数似乎采用 IPv4 地址的文本表示形式(例如“127.0.0.1”)并将其转换为字节数组。
你真的不应该手动做这种事情,在我看来,你最好使用 getaddrinfo() 这可能在您的平台上可用。
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.