问题描述
什么是最好的&验证IPv4地址的最快方法是什么?
基本上,输入可以是IPAddressv4或IPAddressv4:端口
格式。
目前我有以下代码来验证第一种格式。
有人对此发表评论吗?另外,请建议一个机制,以便
验证第二种格式(地址:端口)。
谢谢!
-----------------------------
#define INVALID -1
#define有效0
int validateIP(char * ipadd)
{
无符号b1,b2,b3,b4 ;
unsigned char c;
if(sscanf(ipadd,"%3u。%3u。%3u。%3u%c",& b1,& b2,& b3,& b4,& c)!= 4)
返回无效;
if((b1 | b2 | b3 | b4)> 255)返回INVALID;
if(strspn(ipadd," 0123456789。")< strlen(ipadd))返回INVALID;
返回VALID;
}
What is the best & fastest way of validating an IPv4 address?
Basically, the input can be either in IPAddressv4 or IPAddressv4:port
format.
Currently I have the following code to validate the first format. Does
anybody have any comment on this? Also, please suggest a mechanism to
validate the second format(address:port) also.
Thanks!
-----------------------------
#define INVALID -1
#define VALID 0
int validateIP(char *ipadd)
{
unsigned b1, b2, b3, b4;
unsigned char c;
if (sscanf(ipadd, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c) != 4)
return INVALID;
if ((b1 | b2 | b3 | b4) > 255) return INVALID;
if (strspn(ipadd, "0123456789.") < strlen(ipadd)) return INVALID;
return VALID;
}
推荐答案
-
BC
--
BC
只是一个补充......
视情况而定,你可能需要加一些检查
保留IP地址......
MG
just an add on..
depending upon the situation, you might require some add on checks of the
reserved IP address...
MG
这不好。当然你的意思是:
if(b1> 255 || b2> 255 || b3> 255 || b4> 255)返回INVALID;
-
Rouben Rostamian< ro ******* @ umbc.edu>
That''s not good. Surely you meant:
if (b1 > 255 || b2 > 255 || b3 > 255 || b4 > 255 ) return INVALID;
--
Rouben Rostamian <ro*******@umbc.edu>
这篇关于用于验证IPv4地址的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!