Q
qazmlp
What is the best & fastest way of validating an IPv4 address?
Basically, the input can be either in IPAddressv4 or IPAddressv4
ort
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
ort) 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;
}
Basically, the input can be either in IPAddressv4 or IPAddressv4
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
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;
}