求IP地址校验函数,该如何解决

求IP地址校验函数我有一个字符串,用.分开的IP地址,如192.168.1.100。谁有判断一个字符串是合法IP地址的函数

求IP地址校验函数
我有一个字符串,用.分开的IP地址,如192.168.1.100。谁有判断一个字符串是合法IP地址的函数。

[解决办法]
先用atoi把每个段转换为整数,再移位相加获得一个32位数,然后用这个宏

C/C++ code
#define IS_GOODIP(ip)                       \    (!(((ip) == 0 \            || (((ip) >> 24 & 0xFF) == 0x00) || ((ip) & 0xFF) == 0x00 \            || (((ip) >> 24) & 0xFF) == 0x7F || (((ip) >> 24) & 0xFF) >= 0xE0 \            || ((((ip) >> 24) & 0xFF) == 0xC0 && (((ip) >> 16) & 0xFF) == 0xA8) \            || ((((ip) >> 24) & 0xFF) == 0xAC && (((ip) >> 16) & 0xFF) <= 0x1F) \            || ((((ip) >> 24) & 0xFF) == 0xAC && (((ip) >> 16) & 0xFF) >= 0x10) \            || ((((ip) >> 24) & 0xFF) == 0x0A && (((ip) >> 16) & 0xFF) == 0x00))))
[解决办法]
直接用socket函数就行了

inet_addr
The inet_addr function converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the IN_ADDR structure.

unsigned long inet_addr(
const char* cp
);

Parameters
cp 
[in] Null-terminated character string representing a number expressed in the Internet standard ".'' (dotted) notation. 
Return Values
If no error occurs, inet_addr returns an unsigned long value containing a suitable binary representation of the Internet address given. If the string in the cp parameter does not contain a legitimate Internet address, for example if a portion of an "a.b.c.d" address exceeds 255, then inet_addr returns the value INADDR_NONE.
Remarks
The inet_addr function interprets the character string specified by the cp parameter. This string represents a numeric Internet address expressed in the Internet standard ".'' notation. The value returned is a number suitable for use as an Internet address. All Internet addresses are returned in IP's network order (bytes ordered from left to right). If you pass in " " (a space) to the inet_addr function, inet_addr returns zero.


[解决办法]
正则表达式!
Pattern = “\d+\.\d+\.\d+\.\d+”
[解决办法]
仅供参考
C/C++ code
#include <stdio.h>int main() {    int IP[4];    int i;    char c;    printf("请输入一个ip地址:");    while (1) {        fflush(stdin);        if (5==scanf("%d.%d.%d.%d%c",&IP[0],&IP[1],&IP[2],&IP[3],&c)) {            if (0<=IP[0] && IP[0]<=255             && 0<=IP[1] && IP[1]<=255             && 0<=IP[2] && IP[2]<=255             && 0<=IP[3] && IP[3]<=255             && '\n'==c) {                break;            } else printf("输入的ip地址格式不对!\n请重新输入:\n");        } else printf("输入的ip地址格式不对!\n请重新输入:\n");    }    for (i=0;i<4;i++) {        printf("IP[%d]=%d\n",i,IP[i]);    }    return 0;}
[解决办法]
C/C++ code
if(sscanf(ip,"%u.%u.%u.%u",&a,&b,&c,&d)==4 && 0<=a && a<=255 && 0<=b.......){  }
[解决办法]
不好意思,这个IP校验函数包含了去除局域网IP,从我自己代码里随手摘出来的,你可以根据自己需要改改,自定义自己需要过滤的IP。
探讨

谢谢楼上的,但是我有个疑问,192.168.1.1有什么问题吗?C0 A8

[解决办法]
探讨
我有一个字符串“192.165.054.56”,大家认为是合法的不?结果设置的IP地址是192.165.44.56.貌似中间不能有0?大家又遇到过这种情况吗?