473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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
15 20786

"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
jitrc
1
>> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: K0 | last post by:
Hi all! I have a small app that opens a port on one of its IP Addresses. The address comes from DB Table. The problem is, if the address is incorrect, when i pass it to a TcpListener a...
1
by: Eli | last post by:
Hello all, I am now building a section for members only in my site and I want to validate the email given on the form. I don't want a simple validation but I want to send a email message to the...
1
by: Henrik Nyberg | last post by:
Here's a small method for validating email in C#. It may save you some time.. public static bool IsValidEmailAddress(string sEmail) { if (sEmail == null) { return false; } int nFirstAT =...
0
by: Addam | last post by:
Anyone, I am writting a server that accepts a list of IPv4 and IPv6 address. The IPv4 address are padded with leading zeros. I belive this allows me to just point to the beging and run both IPv4...
3
by: xxyyzz | last post by:
Please help me out 1. What is meant by HexBinary Encoding,how is this done? 2. How must i convert a IPv4 Address to HexBinary Format ??? Thnx Fernando
2
by: Valerie Hough | last post by:
My app has so far only encountered IPv4 addresses and I use: Dns.GetHostByName( "someOtherComputer", portNumber ).AddressList. Can someone please point me to an example of how to turn this into...
3
by: thalamus | last post by:
Hello: I'm working with an URL with this query string at the end: /page/?item=01x-001/ I want to check to make sure the parameter "item" exists in the address, and that it is displayed...
1
by: prudhvinath | last post by:
Could you solve this problem. Write a simple utility program in the perl language which will read in a list of IPv4 addresses, one per line, from STDIN and then either insert or update records in an...
0
by: dinesh1985singh | last post by:
Hi all, I want to get the zip code from an ip address of the client machine.I am using http://ipinfodb.com api to get my task done. I am getting almost every thing except zip code,Gmt offset,and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.