A set of C functions for IPv(4|6) validation
Find a file
0x1eef 58193daba1
Merge pull request #12 from 0x1eef/expandbug
Add support for `2001:DB8::8:800:200C:417A`
2023-09-22 17:20:24 -03:00
.github/workflows Add GitHub action 2023-08-25 17:38:12 -03:00
include isinetaddr -> isinetaddr4, iscidraddr -> iscidraddr4 2023-09-12 15:38:43 -03:00
share/isinetaddr/examples Tweak examples 2023-09-15 00:20:57 -03:00
src Move strncasecmp into for loop 2023-09-22 17:08:26 -03:00
test Add support for 2001:DB8::8:800:200C:417A 2023-09-22 16:42:20 -03:00
.gitignore Add isinetaddr6 2023-09-11 22:51:35 -03:00
.projectile Hide test/(iscidraddr|isinetaddr) from projectile 2023-08-27 22:56:30 -03:00
LICENSE First commit 2023-08-25 15:57:09 -03:00
Makefile Add make example 2023-09-15 01:46:34 -03:00
README.md Tweak examples 2023-09-15 00:20:57 -03:00
VERSION v0.3.1 2023-08-28 00:12:52 -03:00

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.