C语言:
/*************************************************************
函数名称:inet_aton
函数功能: 判断字符串cp 的是否符合IPv4 地址格式
参数说明:cp : 待判断的字符串;
ap : struct in_addr结构体变量;
成功:返回
***************************************************************/
int inet_aton(const char *cp, struct in_addr *ap)
{
int dots = 0;
register u_long acc = 0, addr = 0;
do {
register char cc = *cp;
switch (cc) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
acc = acc * 10 + (cc - '0');
break;
case '.':
if (++dots > 3) {
return 0;
}
/* Fall through */
case '\0':
if (acc > 255) {
return 0;
}
addr = addr << 8 | acc;
acc = 0;
break;
default:
return 0;
}
} while (*cp++) ;
/* Normalize the address */
if (dots < 3) {
addr <<= 8 * (3 - dots) ;
}
/* Store it if requested */
if (ap) {
ap->s_addr = htonl(addr);
}
return 1;
}