2023-08-25 20:57:09 +02:00
|
|
|
## About
|
|
|
|
|
|
|
|
isinetaddr is a simple C library that provides an interface that can
|
2023-09-12 20:38:43 +02:00
|
|
|
be used to validate an IPv(<b>4</b>|<b>6</b>) address (with optional
|
2023-09-15 04:55:27 +02:00
|
|
|
support for CIDR notation included). The library is guided by easy to
|
2023-09-12 20:38:43 +02:00
|
|
|
extend [testcases](test/) that help verify safety and correctness.
|
2023-08-25 20:57:09 +02:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
### IPv4
|
|
|
|
|
2023-09-12 20:38:43 +02:00
|
|
|
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.
|
2023-08-25 20:57:09 +02:00
|
|
|
|
|
|
|
```C
|
|
|
|
#include <isinetaddr.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-09-15 04:55:27 +02:00
|
|
|
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 };
|
2023-08-26 23:35:27 +02:00
|
|
|
|
2023-09-15 04:55:27 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 20:57:09 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2023-09-15 04:55:27 +02:00
|
|
|
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;
|
2023-08-25 20:57:09 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-09-15 04:55:27 +02:00
|
|
|
Expected output:
|
2023-08-25 20:57:09 +02:00
|
|
|
|
|
|
|
```
|
2023-09-12 20:38:43 +02:00
|
|
|
$ cc -Iinclude src/*.c share/isinetaddr/examples/isinetaddr4.c -o example
|
2023-08-25 20:57:09 +02:00
|
|
|
$ ./example
|
2023-09-15 04:55:27 +02:00
|
|
|
// 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.
|
2023-08-25 20:57:09 +02:00
|
|
|
```
|
|
|
|
|
2023-08-26 21:25:52 +02:00
|
|
|
### CIDR notation (IPv4)
|
|
|
|
|
2023-09-12 20:38:43 +02:00
|
|
|
The `iscidraddr4` function supports the same feature set as `isinetaddr4`, and
|
2023-08-26 21:25:52 +02:00
|
|
|
in addition supports
|
|
|
|
[CIDR notation](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation).
|
|
|
|
The following example builds on the previous example:
|
|
|
|
|
|
|
|
```C
|
|
|
|
#include <isinetaddr.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-09-15 04:55:27 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (iscidraddr4(str)) {
|
|
|
|
printf("%s is a valid IPv4 address.\n", str);
|
|
|
|
} else {
|
|
|
|
printf("%s is an invalid IPv4 address.\n", str);
|
|
|
|
}
|
|
|
|
}
|
2023-08-26 21:25:52 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2023-09-15 04:55:27 +02:00
|
|
|
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;
|
2023-08-26 21:25:52 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-09-15 04:55:27 +02:00
|
|
|
Expected output:
|
2023-08-26 21:25:52 +02:00
|
|
|
|
|
|
|
```
|
2023-09-12 20:38:43 +02:00
|
|
|
$ cc -Iinclude src/*.c share/isinetaddr/examples/iscidraddr4.c -o example
|
2023-08-26 21:25:52 +02:00
|
|
|
$ ./example
|
2023-09-15 04:55:27 +02:00
|
|
|
// 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.
|
2023-08-26 21:25:52 +02:00
|
|
|
```
|
|
|
|
|
2023-09-12 03:37:25 +02:00
|
|
|
### IPv6
|
|
|
|
|
|
|
|
The following example demonstrates the `isinetaddr6` function with
|
|
|
|
both valid and invalid inputs. The `isinetaddr6` function returns 1
|
2023-09-12 20:38:43 +02:00
|
|
|
when the input given is a valid IPv6 address, and otherwise returns 0.
|
2023-09-12 03:37:25 +02:00
|
|
|
|
|
|
|
```C
|
|
|
|
#include <isinetaddr.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-09-15 04:55:27 +02:00
|
|
|
const char *valid[] = { "::", "::1", "0000:0000:0000:0000:0000:0000:0000:0000" };
|
|
|
|
const char *invalid[] = { "foobar", "00:::0", NULL };
|
2023-09-12 03:37:25 +02:00
|
|
|
|
2023-09-15 04:55:27 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 03:37:25 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2023-09-15 04:55:27 +02:00
|
|
|
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;
|
2023-09-12 03:37:25 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-09-15 04:55:27 +02:00
|
|
|
Expected output:
|
2023-09-12 03:37:25 +02:00
|
|
|
|
|
|
|
```
|
|
|
|
$ cc -Iinclude src/*.c share/isinetaddr/examples/isinetaddr6.c -o example
|
|
|
|
$ ./example
|
2023-09-15 04:55:27 +02:00
|
|
|
// 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.
|
2023-09-12 03:37:25 +02:00
|
|
|
```
|
|
|
|
|
2023-08-25 20:57:09 +02:00
|
|
|
## Sources
|
|
|
|
|
|
|
|
* [Source code (GitHub)](https://github.com/0x1eef/isinetaddr#readme)
|
|
|
|
* [Source code (GitLab)](https://gitlab.com/0x1eef/isinetaddr#about)
|
|
|
|
|
|
|
|
## <a id="license"> License </a>
|
|
|
|
|
|
|
|
[BSD Zero Clause](https://choosealicense.com/licenses/0bsd/).
|
|
|
|
<br>
|
|
|
|
See [LICENSE](./LICENSE).
|