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;
} 15 20748
"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
"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
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>
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/
"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 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!
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
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
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
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/ 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
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
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
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
>> 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 .
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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 =...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |