467,858 Members | 1,271 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,858 developers. It's quick & easy.

code for validating IPv4 address

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;
}
Nov 13 '05 #1
  • viewed: 20113
Share:
15 Replies

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
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
Nov 13 '05 #2
MG

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
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;
}


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

MG
Nov 13 '05 #3
In article <db**************************@posting.google.com >,
qazmlp <qa********@rediffmail.com> wrote:
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;

That's not good. Surely you meant:

if (b1 > 255 || b2 > 255 || b3 > 255 || b4 > 255 ) return INVALID;
--
Rouben Rostamian <ro*******@umbc.edu>
Nov 13 '05 #4
In 'comp.lang.c', "Burne C" <no****@notexist.com> wrote:
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 ?


Yes, 'unsigned' a short name for 'unsigned int', like 'long' is a shortname
for 'long int' etc.

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #5

"Rouben Rostamian" <ro****@pc18.math.umbc.edu> wrote in message
news:bg**********@pc18.math.umbc.edu...
In article <db**************************@posting.google.com >,
qazmlp <qa********@rediffmail.com> wrote:
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;

That's not good. Surely you meant:

if (b1 > 255 || b2 > 255 || b3 > 255 || b4 > 255 ) return INVALID;


I think the statement (b1 | b2 | b3 | b4) > 255 is very effective, the
result of bitwise OR (b1 | b2 | b3 | b4) is bigger than 255 if any of them
is bigger than 255.


--
Rouben Rostamian <ro*******@umbc.edu>


--
Cael

Nov 13 '05 #6
qa********@rediffmail.com (qazmlp) wrote in message news:<db**************************@posting.google. com>...
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;
}


I am expecting your suggestions for validating the 'address:port' format.

Thanks!
Nov 13 '05 #7
In <db**************************@posting.google.com > qa********@rediffmail.com (qazmlp) writes:
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.

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


This looks exactly like code I've posted a couple of years ago. The more
paranoid check at the end was added in response to some criticism from
Tak-Shing :-)

Extending it for IPAddressv4:port shouldn't be that difficult to anyone
who has understood how the current version works (which, of course,
excludes qazmlp).

#define PORTMAX 65535 /* or whatever appropriate */

int validateIP(char *ipadd)
{
unsigned b1, b2, b3, b4, port = 0;
unsigned char c;
int rc;

rc = sscanf(ipadd, "%3u.%3u.%3u.%3u:%u%c",
&b1, &b2, &b3, &b4, &port, &c);
if (rc != 4 && rc != 5) return INVALID;
if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
if (strspn(ipadd, "0123456789.:") < strlen(ipadd)) return INVALID;
return VALID;
}

Trivia quiz: what is the purpose of the last check?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8
In <bg*********@imsp212.netvigator.com> "Burne C" <no****@notexist.com> writes:

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
?


Here's the complete list of C99 type "aliases":

- short, signed short, short int, or signed short int
- unsigned short, or unsigned short int
- int, signed, or signed int
- unsigned, or unsigned int
- long, signed long, long int, or signed long int
- unsigned long, or unsigned long int
- long long, signed long long, long long int, or signed long long int
- unsigned long long, or unsigned long long int

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #9
In <3f***************@news21.on.aibn.com> Le*********@td.com (Lew Pitcher) writes:
On 28 Jul 2003 13:49:08 GMT, Da*****@cern.ch (Dan Pop) wrote:
[snip]

#define PORTMAX 65535 /* or whatever appropriate */

int validateIP(char *ipadd)
{
unsigned b1, b2, b3, b4, port = 0;
unsigned char sep, c;
int rc;

rc = sscanf(ipadd, "%3u.%3u.%3u.%3u:%u%c",
&b1, &b2, &b3, &b4, &port, &c);
if (rc != 4 && rc != 5) return INVALID;
if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
if (strspn(ipadd, "0123456789.:") < strlen(ipadd)) return INVALID;
return VALID;
}

Trivia quiz: what is the purpose of the last check?


Well, I won't profess to know the purpose of the last check, but it /will/
reject an IP address string of "1.2.3.4=", where the prior tests would not.


The original version, as posted by the OP, would catch "1.2.3.4=" because
scanf would return 5 instead of 4. Yet, the test is still there.

This points out a bug in my version above: "1.2.3.4.5" will not be
identified as an invalid IP address. So, back to the drawing board:

rc = sscanf(ipadd, "%3u.%3u.%3u.%3u%c%u%c",
&b1, &b2, &b3, &b4, &sep, &port, &c);
if (rc != 4 && rc != 6) return INVALID;
if (rc == 6 && sep != ':') return INVALID;
if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
if (strspn(ipadd, "0123456789.:") < strlen(ipadd)) return INVALID;
return VALID;

Why is the last check still there?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10
On Mon, 28 Jul 2003 18:13:41 +0000, Dan Pop wrote:
The original version, as posted by the OP, would catch "1.2.3.4=" because
scanf would return 5 instead of 4. Yet, the test is still there.

This points out a bug in my version above: "1.2.3.4.5" will not be
identified as an invalid IP address. So, back to the drawing board:

rc = sscanf(ipadd, "%3u.%3u.%3u.%3u%c%u%c",
&b1, &b2, &b3, &b4, &sep, &port, &c);
if (rc != 4 && rc != 6) return INVALID;
if (rc == 6 && sep != ':') return INVALID;
if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
if (strspn(ipadd, "0123456789.:") < strlen(ipadd)) return INVALID;
return VALID;

Why is the last check still there?


I'll assume because strtoul() doens't do any checking on input, which
makes "-0" valid as an unsidnged number.

You could also argue that because of locale's the above will catch...

0.0.0.0:1,000

....but it won't if the thousands seperator is '.' (which it is in germany
for instance) ... so it doesn't catch that.

The truley strict versions I've seen/written don't use scanf().

--
James Antill -- ja***@and.org
Need an efficent and powerful string library for C?
http://www.and.org/vstr/

Nov 13 '05 #11
Da*****@cern.ch (Dan Pop) wrote in message news:<bg**********@sunnews.cern.ch>...
In <db**************************@posting.google.com > qa********@rediffmail.com (qazmlp) writes:
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.

#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;
}
This looks exactly like code I've posted a couple of years ago. The more
paranoid check at the end was added in response to some criticism from
Tak-Shing :-)

Extending it for IPAddressv4:port shouldn't be that difficult to anyone
who has understood how the current version works (which, of course,
excludes qazmlp).

#define PORTMAX 65535 /* or whatever appropriate */

int validateIP(char *ipadd)
{
unsigned b1, b2, b3, b4, port = 0;
unsigned char c;
int rc;

rc = sscanf(ipadd, "%3u.%3u.%3u.%3u:%u%c",
&b1, &b2, &b3, &b4, &port, &c);
if (rc != 4 && rc != 5) return INVALID;
if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
if (strspn(ipadd, "0123456789.:") < strlen(ipadd)) return INVALID;
return VALID;
}

As I mentioned in my original post, the same function should handle
the input which can either be in 'ipaddress' or 'ipaddress:port'. Is
this possible with a single sscanf? Or, should I do like this:
call strchr() to find ':'
If found, call the 2-nd version of validateIP() which handles
ipaddress:port' format.
else, call the 1st version of validateIP() which handles 'ipaddress'
format.

Trivia quiz: what is the purpose of the last check?

Dan

Nov 13 '05 #12
In <db**************************@posting.google.com > qa********@rediffmail.com (qazmlp) writes:
Da*****@cern.ch (Dan Pop) wrote in message news:<bg**********@sunnews.cern.ch>...

Extending it for IPAddressv4:port shouldn't be that difficult to anyone
who has understood how the current version works (which, of course,
excludes qazmlp).

#define PORTMAX 65535 /* or whatever appropriate */

int validateIP(char *ipadd)
{
unsigned b1, b2, b3, b4, port = 0;
unsigned char c;
int rc;

rc = sscanf(ipadd, "%3u.%3u.%3u.%3u:%u%c",
&b1, &b2, &b3, &b4, &port, &c);
if (rc != 4 && rc != 5) return INVALID;
if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
if (strspn(ipadd, "0123456789.:") < strlen(ipadd)) return INVALID;
return VALID;
}

As I mentioned in my original post, the same function should handle
the input which can either be in 'ipaddress' or 'ipaddress:port'. Is
this possible with a single sscanf?


If you were not the idiot you actually are, you'd have noticed that this
is exactly what the code I've posted is supposed to do.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #13
In <pa****************************@and.org> "James Antill" <ja***********@and.org> writes:
On Mon, 28 Jul 2003 18:13:41 +0000, Dan Pop wrote:
The original version, as posted by the OP, would catch "1.2.3.4=" because
scanf would return 5 instead of 4. Yet, the test is still there.

This points out a bug in my version above: "1.2.3.4.5" will not be
identified as an invalid IP address. So, back to the drawing board:

rc = sscanf(ipadd, "%3u.%3u.%3u.%3u%c%u%c",
&b1, &b2, &b3, &b4, &sep, &port, &c);
if (rc != 4 && rc != 6) return INVALID;
if (rc == 6 && sep != ':') return INVALID;
if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
if (strspn(ipadd, "0123456789.:") < strlen(ipadd)) return INVALID;
return VALID;

Why is the last check still there?
I'll assume because strtoul() doens't do any checking on input, which
makes "-0" valid as an unsidnged number.


Even simpler, +12 is a valid unsigned number. The other issue is embedded
white space: "+12. 34. 56. 78" would pass all the other tests, yet it
hardly looks like a valid IP address.
You could also argue that because of locale's the above will catch...


The code was meant to be used in the C locale...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #14


Dan Pop wrote:
If you were not the idiot you actually are, you'd have noticed that this
is exactly what the code I've posted is supposed to do.

I still can't figure out if this guy (quasilump or whatever) is an
elaborate troll or the world's slowest-learning programmer. At least in
this thread he actually interacted a bit with the respondents. Usually
he declines to respond to any comments or questions.


Brian Rodenborn
Nov 13 '05 #15
>> if (strspn(ipadd, "0123456789.") < strlen(ipadd)) return INVALID;

I believe the last check is for making sure that the IP Address does not contain any character other than 0-9 and .
Feb 24 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Addam | last post: by
3 posts views Thread by xxyyzz | last post: by
2 posts views Thread by Valerie Hough | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.