58193daba1
Add support for `2001:DB8::8:800:200C:417A` |
||
---|---|---|
.github/workflows | ||
include | ||
share/isinetaddr/examples | ||
src | ||
test | ||
.gitignore | ||
.projectile | ||
LICENSE | ||
Makefile | ||
README.md | ||
VERSION |
About
isinetaddr is a simple C library that provides an interface that can be used to validate an IPv(4|6) address (with optional support for CIDR notation included). The library is guided by easy to extend testcases that help verify safety and correctness.
Examples
IPv4
The following example demonstrates the isinetaddr4
function with
both valid and invalid inputs. The isinetaddr4
function returns 1
when the input given is a valid IPv4 address, and otherwise returns 0.
#include <isinetaddr.h>
#include <stdio.h>
#include <stdlib.h>
const char *valid[] = { "127.0.0.1", "1.1.1.1", "0.0.0.0" };
const char *invalid[] = { "foobar", "0.0.0.0.0", NULL };
void validate(const char *str);
int
main(void)
{
printf("// valid\n");
for (int i = 0; i < 3; i++) {
validate(valid[i]);
}
printf("// invalid\n");
for (int i = 0; i < 3; i++) {
validate(invalid[i]);
}
return EXIT_SUCCESS;
}
void
validate(const char *str)
{
if (isinetaddr4(str)) {
printf("%s is a valid IPv4 address.\n", str);
} else {
printf("%s is an invalid IPv4 address.\n", str);
}
}
Expected output:
$ cc -Iinclude src/*.c share/isinetaddr/examples/isinetaddr4.c -o example
$ ./example
// valid
127.0.0.1 is a valid IPv4 address.
1.1.1.1 is a valid IPv4 address.
0.0.0.0 is a valid IPv4 address.
// invalid
foobar is an invalid IPv4 address.
0.0.0.0.0 is an invalid IPv4 address.
(null) is an invalid IPv4 address.
CIDR notation (IPv4)
The iscidraddr4
function supports the same feature set as isinetaddr4
, and
in addition supports
CIDR notation.
The following example builds on the previous example:
#include <isinetaddr.h>
#include <stdio.h>
#include <stdlib.h>
const char *valid[] = { "127.0.0.1", "192.168.2.1/32", "192.168.2.1/0" };
const char *invalid[] = { "foobar", "0.0.0.0.0", "192.168.2.1/33" };
void validate(const char *str);
int
main(void)
{
printf("// valid\n");
for (int i = 0; i < 3; i++) {
validate(valid[i]);
}
printf("// invalid\n");
for (int i = 0; i < 3; i++) {
validate(invalid[i]);
}
return EXIT_SUCCESS;
}
void
validate(const char *str)
{
if (iscidraddr4(str)) {
printf("%s is a valid IPv4 address.\n", str);
} else {
printf("%s is an invalid IPv4 address.\n", str);
}
}
Expected output:
$ cc -Iinclude src/*.c share/isinetaddr/examples/iscidraddr4.c -o example
$ ./example
// valid
127.0.0.1 is a valid IPv4 address.
192.168.2.1/32 is a valid IPv4 address.
192.168.2.1/0 is a valid IPv4 address.
// invalid
foobar is an invalid IPv4 address.
0.0.0.0.0 is an invalid IPv4 address.
192.168.2.1/33 is an invalid IPv4 address.
IPv6
The following example demonstrates the isinetaddr6
function with
both valid and invalid inputs. The isinetaddr6
function returns 1
when the input given is a valid IPv6 address, and otherwise returns 0.
#include <isinetaddr.h>
#include <stdio.h>
#include <stdlib.h>
const char *valid[] = { "::", "::1", "0000:0000:0000:0000:0000:0000:0000:0000" };
const char *invalid[] = { "foobar", "00:::0", NULL };
void validate(const char *str);
int
main(void)
{
printf("// valid\n");
for (int i = 0; i < 3; i++) {
validate(valid[i]);
}
printf("// invalid\n");
for (int i = 0; i < 3; i++) {
validate(invalid[i]);
}
return EXIT_SUCCESS;
}
void
validate(const char *str)
{
if (isinetaddr6(str)) {
printf("%s is a valid IPv6 address.\n", str);
} else {
printf("%s is an invalid IPv6 address.\n", str);
}
}
Expected output:
$ cc -Iinclude src/*.c share/isinetaddr/examples/isinetaddr6.c -o example
$ ./example
// valid
:: is a valid IPv6 address.
::1 is a valid IPv6 address.
0000:0000:0000:0000:0000:0000:0000:0000 is a valid IPv6 address.
// invalid
foobar is an invalid IPv6 address.
00:::0 is an invalid IPv6 address.
(null) is an invalid IPv6 address.
Sources
License
BSD Zero Clause.
See LICENSE.