用于验证IPv4地址的代码 [英] code for validating IPv4 address

查看:59
本文介绍了用于验证IPv4地址的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好的&验证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;
}

推荐答案



" qazmlp" < QA ******** @ rediffmail.com>在留言中写道

news:db ************************** @ posting.google.c om ...

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
什么是最好的&验证IPv4地址的最快方法是什么?
基本上,输入可以是IPAddressv4或IPAddressv4:port
格式。

目前我有以下代码来验证第一种格式。
有人对此发表评论吗?另外,请建议一种机制来验证第二种格式(地址:端口)。

谢谢!

---------- -------------------
#define INVALID -1
#define VALID 0
int validateIP(char * ipadd )
{
无符号b1,b2,b3,b4;


之前我没有看到这种声明,你只是使用了unsigned。在这里没有类型

说明符。但是,我编译了你的代码而没有错误。使用无符号是否合法?单独



unsigned char c;

if(sscanf(ipadd,"%3u。%3u。%3u。%3u% c",& b1,& b2,& b3,& b4,& c)!= 4)
返回INVALID;

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;
I didn''t see this kind of declaration before, you just used "unsigned" in here and no type
specifier. However, I compiled your code with no error in my. Is it legal for using "unsigned" alone
?
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




" ; qazmlp" < QA ******** @ rediffmail.com>在留言中写道

news:db ************************** @ posting.google.c om ...

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
什么是最好的&验证IPv4地址的最快方法是什么?
基本上,输入可以是IPAddressv4或IPAddressv4:port
格式。

目前我有以下代码来验证第一种格式。
有人对此发表评论吗?另外,请建议一种机制来验证第二种格式(地址:端口)。

谢谢!

---------- -------------------
#define INVALID -1
#define VALID 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)
返回INVALID;

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;
}




只是一个补充......

视情况而定,你可能需要加一些检查

保留IP地址......


MG



just an add on..
depending upon the situation, you might require some add on checks of the
reserved IP address...

MG


文章< db ***** *********************@posting.google.com>,

qazmlp< qa ******** @ rediffmail.com>写道:
In article <db**************************@posting.google.com >,
qazmlp <qa********@rediffmail.com> wrote:
什么是最好的&验证IPv4地址的最快方法是什么?
基本上,输入可以是IPAddressv4或IPAddressv4:port
格式。

目前我有以下代码来验证第一种格式。
有人对此发表评论吗?另外,请建议一种机制来验证第二种格式(地址:端口)。


[代码剪辑]

if((b1 | b2 | b3 | b4)> 255)返回INVALID;
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.
[code snipped]
if ((b1 | b2 | b3 | b4) > 255) return INVALID;



这不好。当然你的意思是:


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地址的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆