Add function to validate ip address

This commit is contained in:
Lakindu Akash 2021-12-27 01:15:32 +05:30
parent 31f9b44aec
commit 99e4f06374
No known key found for this signature in database
GPG Key ID: 6FB0085A614E0AC2

View File

@ -120,3 +120,34 @@ else {
}
}
int isValidIPaddress(const char * ip){
/* Compile regular expression */
reti = regcomp(&regex,
"^([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))."
"([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))."
"([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))."
"([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$", REG_EXTENDED);
if (reti) {
//printf( "Could not compile regex\n");
return -1;
}
/* Execute regular expression */
reti = regexec(&regex, ip, 0, NULL, 0);
if (!reti) {
return 0;
}
else if (reti == REG_NOMATCH) {
//puts("Invalid ip addresses");
return -1;
}
else {
regerror(reti, &regex, msgbuf, sizeof(msgbuf));
//printf("Regex match failed: %s\n", msgbuf);
return -1;
}
}