2023-08-25 20:57:09 +02:00
|
|
|
#include <isinetaddr.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-08-25 23:20:49 +02:00
|
|
|
const char *valid[] = {
|
|
|
|
"192.168.1.1",
|
|
|
|
"0.0.0.0",
|
|
|
|
"255.255.255.255",
|
2023-08-26 01:05:43 +02:00
|
|
|
"123.45.67.89",
|
|
|
|
"123.45.67.255",
|
2023-08-25 23:20:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *invalid[] = {
|
2023-08-26 01:05:43 +02:00
|
|
|
"123.45.67.891",
|
2023-08-25 23:20:49 +02:00
|
|
|
"192.168.500.1",
|
|
|
|
".192.168.1.1",
|
|
|
|
"192..168.1.1",
|
|
|
|
"192.168.1.1.",
|
|
|
|
"192.168.1.1..",
|
|
|
|
"192.168.1.1a",
|
2023-08-25 23:37:53 +02:00
|
|
|
"555555555555555555555555555555555555",
|
2023-08-25 23:25:20 +02:00
|
|
|
"",
|
2023-08-26 01:08:22 +02:00
|
|
|
".",
|
|
|
|
".......",
|
2023-08-26 02:01:34 +02:00
|
|
|
"192.2.2.",
|
|
|
|
"...4",
|
2023-08-25 23:25:20 +02:00
|
|
|
NULL
|
2023-08-25 23:20:49 +02:00
|
|
|
};
|
|
|
|
|
2023-08-25 20:57:09 +02:00
|
|
|
int
|
|
|
|
main(void) {
|
2023-08-25 23:20:49 +02:00
|
|
|
size_t len;
|
|
|
|
/* IPv4: valid */
|
|
|
|
len = sizeof(valid) / sizeof(valid[0]);
|
2023-08-25 23:58:59 +02:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2023-08-25 23:20:49 +02:00
|
|
|
if (isinetaddr(valid[i]) != 1) {
|
|
|
|
fprintf(stderr, "assertion failed: '%s' should be valid\n", valid[i]);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* IPv4: invalid */
|
|
|
|
len = sizeof(invalid) / sizeof(invalid[0]);
|
2023-08-25 23:58:59 +02:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2023-08-25 23:20:49 +02:00
|
|
|
if (isinetaddr(invalid[i]) != 0) {
|
|
|
|
fprintf(stderr, "assertion failed: '%s' should NOT be valid\n", invalid[i]);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Done */
|
2023-08-25 20:57:09 +02:00
|
|
|
printf("OK\n");
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|