isinetaddr -> isinetaddr4, iscidraddr -> iscidraddr4
This commit is contained in:
parent
c4fbb4b67f
commit
eb0b1d3ace
10 changed files with 28 additions and 29 deletions
6
Makefile
6
Makefile
|
@ -1,5 +1,5 @@
|
|||
SRCDIR = src
|
||||
SRCFILES = $(SRCDIR)/isinetaddr.c $(SRCDIR)/iscidraddr.c $(SRCDIR)/isinetaddr6.c
|
||||
SRCFILES = $(SRCDIR)/isinetaddr4.c $(SRCDIR)/iscidraddr4.c $(SRCDIR)/isinetaddr6.c
|
||||
INCDIR = include
|
||||
TESTDIR = test
|
||||
|
||||
|
@ -11,10 +11,10 @@ test:
|
|||
@make test6
|
||||
|
||||
test4:
|
||||
@$(CC) $(CFLAGS) $(SRCFILES) $(TESTDIR)/isinetaddr_test.c -o $(TESTDIR)/isinetaddr
|
||||
@$(CC) $(CFLAGS) $(SRCFILES) $(TESTDIR)/isinetaddr4_test.c -o $(TESTDIR)/isinetaddr
|
||||
@echo -n test/isinetaddr: ''
|
||||
@$(TESTDIR)/isinetaddr
|
||||
@$(CC) $(CFLAGS) $(SRCFILES) $(TESTDIR)/iscidraddr_test.c -o $(TESTDIR)/iscidraddr
|
||||
@$(CC) $(CFLAGS) $(SRCFILES) $(TESTDIR)/iscidraddr4_test.c -o $(TESTDIR)/iscidraddr
|
||||
@echo -n test/iscidraddr: ''
|
||||
@$(TESTDIR)/iscidraddr
|
||||
|
||||
|
|
25
README.md
25
README.md
|
@ -1,18 +1,17 @@
|
|||
## About
|
||||
|
||||
isinetaddr is a simple C library that provides an interface that can
|
||||
be used to validate one or more IPv(<b>4</b>|<b>6</b>) addresses
|
||||
(with optional support for CIDR notation as well). The library is
|
||||
guided by easy to extend [testcases](test/) that help verify safety
|
||||
and correctness.
|
||||
be used to validate an IPv(<b>4</b>|<b>6</b>) address (with optional
|
||||
support for CIDR notation as well). The library is guided by easy to
|
||||
extend [testcases](test/) that help verify safety and correctness.
|
||||
|
||||
## Examples
|
||||
|
||||
### IPv4
|
||||
|
||||
The following example demonstrates the `isinetaddr` function with
|
||||
both valid and invalid inputs. The `isinetaddr` function returns 1
|
||||
when the input given is valid, and otherwise returns 0.
|
||||
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.
|
||||
|
||||
```C
|
||||
#include <isinetaddr.h>
|
||||
|
@ -37,7 +36,7 @@ main(void)
|
|||
const int i = sizeof(strings) / sizeof(strings[0]);
|
||||
for (int j = 0; j < i; j++) {
|
||||
str = strings[j];
|
||||
if (isinetaddr(str)) {
|
||||
if (isinetaddr4(str)) {
|
||||
printf("%s is a valid IPv4 address\n", str);
|
||||
} else {
|
||||
printf("%s is an invalid IPv4 address\n", str);
|
||||
|
@ -51,7 +50,7 @@ When the above source code is compiled and run the output is
|
|||
expected to be as follows:
|
||||
|
||||
```
|
||||
$ cc -Iinclude src/*.c share/isinetaddr/examples/isinetaddr.c -o example
|
||||
$ cc -Iinclude src/*.c share/isinetaddr/examples/isinetaddr4.c -o example
|
||||
$ ./example
|
||||
127.0.0.1 is a valid IPv4 address
|
||||
1.1.1.1 is a valid IPv4 address
|
||||
|
@ -62,7 +61,7 @@ foobar is an invalid IPv4 address
|
|||
|
||||
### CIDR notation (IPv4)
|
||||
|
||||
The `iscidraddr` function supports the same feature set as `isinetaddr`, and
|
||||
The `iscidraddr4` function supports the same feature set as `isinetaddr4`, and
|
||||
in addition supports
|
||||
[CIDR notation](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation).
|
||||
The following example builds on the previous example:
|
||||
|
@ -95,7 +94,7 @@ main(void)
|
|||
const int i = sizeof(strings) / sizeof(strings[0]);
|
||||
for (int j = 0; j < i; j++) {
|
||||
str = strings[j];
|
||||
if (iscidraddr(str)) {
|
||||
if (iscidraddr4(str)) {
|
||||
printf("%s is a valid IPv4 address\n", str);
|
||||
} else {
|
||||
printf("%s is an invalid IPv4 address\n", str);
|
||||
|
@ -109,7 +108,7 @@ When the above source code is compiled and run the output is
|
|||
expected to be as follows:
|
||||
|
||||
```
|
||||
$ cc -Iinclude src/*.c share/isinetaddr/examples/iscidraddr.c -o example
|
||||
$ cc -Iinclude src/*.c share/isinetaddr/examples/iscidraddr4.c -o example
|
||||
$ ./example
|
||||
127.0.0.1 is a valid IPv4 address
|
||||
1.1.1.1 is a valid IPv4 address
|
||||
|
@ -127,7 +126,7 @@ foobar is an invalid IPv4 address
|
|||
|
||||
The following example demonstrates the `isinetaddr6` function with
|
||||
both valid and invalid inputs. The `isinetaddr6` function returns 1
|
||||
when the input given is valid, and otherwise returns 0.
|
||||
when the input given is a valid IPv6 address, and otherwise returns 0.
|
||||
|
||||
```C
|
||||
#include <isinetaddr.h>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#pragma once
|
||||
int isinetaddr(const char *str);
|
||||
int iscidraddr(const char *str);
|
||||
int isinetaddr4(const char *str);
|
||||
int iscidraddr4(const char *str);
|
||||
int isinetaddr6(const char *str);
|
||||
|
|
|
@ -24,7 +24,7 @@ main(void)
|
|||
const int i = sizeof(strings) / sizeof(strings[0]);
|
||||
for (int j = 0; j < i; j++) {
|
||||
str = strings[j];
|
||||
if (iscidraddr(str)) {
|
||||
if (iscidraddr4(str)) {
|
||||
printf("%s is a valid IPv4 address\n", str);
|
||||
} else {
|
||||
printf("%s is an invalid IPv4 address\n", str);
|
|
@ -20,7 +20,7 @@ main(void)
|
|||
const int i = sizeof(strings) / sizeof(strings[0]);
|
||||
for (int j = 0; j < i; j++) {
|
||||
str = strings[j];
|
||||
if (isinetaddr(str)) {
|
||||
if (isinetaddr4(str)) {
|
||||
printf("%s is a valid IPv4 address\n", str);
|
||||
} else {
|
||||
printf("%s is an invalid IPv4 address\n", str);
|
|
@ -6,7 +6,7 @@
|
|||
static int in_range(char buf[4], int min, int max);
|
||||
|
||||
int
|
||||
iscidraddr(const char *str)
|
||||
iscidraddr4(const char *str)
|
||||
{
|
||||
size_t offset = 0;
|
||||
size_t len = (str == NULL ? 0 : strnlen(str, 16));
|
||||
|
@ -18,14 +18,14 @@ iscidraddr(const char *str)
|
|||
}
|
||||
}
|
||||
if (offset == 0) {
|
||||
return isinetaddr(str);
|
||||
return isinetaddr4(str);
|
||||
} else {
|
||||
char addr[offset], cidr[3];
|
||||
char *c = (char*)&str[offset + 1];
|
||||
memcpy(addr, str, offset);
|
||||
memcpy(cidr, c, 3);
|
||||
addr[offset] = '\0';
|
||||
return isinetaddr(addr) && in_range(cidr, 0, 32);
|
||||
return isinetaddr4(addr) && in_range(cidr, 0, 32);
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ static void register_octet(int *octets, char *buf, int *buflen);
|
|||
static void register_digit(char digit, int *digits, char *buf, int *buflen);
|
||||
|
||||
int
|
||||
isinetaddr(const char *str)
|
||||
isinetaddr4(const char *str)
|
||||
{
|
||||
char buf[4];
|
||||
int digits = 0, octets = 1, buflen = 0;
|
|
@ -21,7 +21,7 @@ isinetaddr6(const char *str)
|
|||
if (len == 0) {
|
||||
return 0;
|
||||
} else if (strncasecmp(str, "::ffff", 6) == 0) {
|
||||
return isinetaddr(&str[7]);
|
||||
return isinetaddr4(&str[7]);
|
||||
}
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (has_consecutive_chars(&str[i], SEP, 3)) {
|
||||
|
|
|
@ -39,7 +39,7 @@ main(void) {
|
|||
/* IPv4: valid */
|
||||
len = sizeof(valid) / sizeof(valid[0]);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (iscidraddr(valid[i]) != 1) {
|
||||
if (iscidraddr4(valid[i]) != 1) {
|
||||
fprintf(stderr, "assertion failed: '%s' should be valid\n", valid[i]);
|
||||
abort();
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ main(void) {
|
|||
/* IPv4: invalid */
|
||||
len = sizeof(invalid) / sizeof(invalid[0]);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (iscidraddr(invalid[i]) != 0) {
|
||||
if (iscidraddr4(invalid[i]) != 0) {
|
||||
fprintf(stderr, "assertion failed: '%s' should NOT be valid\n", invalid[i]);
|
||||
abort();
|
||||
}
|
|
@ -30,7 +30,7 @@ main(void) {
|
|||
/* IPv4: valid */
|
||||
len = sizeof(valid) / sizeof(valid[0]);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (isinetaddr(valid[i]) != 1) {
|
||||
if (isinetaddr4(valid[i]) != 1) {
|
||||
fprintf(stderr, "assertion failed: '%s' should be valid\n", valid[i]);
|
||||
abort();
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ main(void) {
|
|||
/* IPv4: invalid */
|
||||
len = sizeof(invalid) / sizeof(invalid[0]);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (isinetaddr(invalid[i]) != 0) {
|
||||
if (isinetaddr4(invalid[i]) != 0) {
|
||||
fprintf(stderr, "assertion failed: '%s' should NOT be valid\n", invalid[i]);
|
||||
abort();
|
||||
}
|
Loading…
Reference in a new issue