A set of C functions for IPv(4|6) validation
Find a file
2023-09-12 15:38:43 -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 isinetaddr -> isinetaddr4, iscidraddr -> iscidraddr4 2023-09-12 15:38:43 -03:00
src isinetaddr -> isinetaddr4, iscidraddr -> iscidraddr4 2023-09-12 15:38:43 -03:00
test isinetaddr -> isinetaddr4, iscidraddr -> iscidraddr4 2023-09-12 15:38:43 -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 isinetaddr -> isinetaddr4, iscidraddr -> iscidraddr4 2023-09-12 15:38:43 -03:00
README.md isinetaddr -> isinetaddr4, iscidraddr -> iscidraddr4 2023-09-12 15:38:43 -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 as well). 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 *strings[] = {
  /* valid */
  "127.0.0.1",
  "1.1.1.1",
  "0.0.0.0",

  /* invalid */
  "foobar",
  "0.0.0.0.0"
};

int
main(void)
{
    const char *str;
    const int i = sizeof(strings) / sizeof(strings[0]);
    for (int j = 0; j < i; j++) {
        str = strings[j];
        if (isinetaddr4(str)) {
            printf("%s is a valid IPv4 address\n", str);
        } else {
            printf("%s is an invalid IPv4 address\n", str);
        }
    }
    return EXIT_SUCCESS;
}

When the above source code is compiled and run the output is expected to be as follows:

$ 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
0.0.0.0 is a valid IPv4 address
foobar is an invalid IPv4 address
0.0.0.0.0 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 *strings[] = {
  /* valid */
  "127.0.0.1",
  "1.1.1.1",
  "0.0.0.0",
  "127.0.0.1/8",
  "127.0.0.1/16",
  "127.0.0.1/32",

  /* invalid */
  "foobar",
  "0.0.0.0.0",
  "127.0.0.1/33",
  "127.0.0.1/64"
};

int
main(void)
{
    const char *str;
    const int i = sizeof(strings) / sizeof(strings[0]);
    for (int j = 0; j < i; j++) {
        str = strings[j];
        if (iscidraddr4(str)) {
            printf("%s is a valid IPv4 address\n", str);
        } else {
            printf("%s is an invalid IPv4 address\n", str);
        }
    }
    return EXIT_SUCCESS;
}

When the above source code is compiled and run the output is expected to be as follows:

$ 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
0.0.0.0 is a valid IPv4 address
127.0.0.1/8 is a valid IPv4 address
127.0.0.1/16 is a valid IPv4 address
127.0.0.1/32 is a valid IPv4 address
foobar is an invalid IPv4 address
0.0.0.0.0 is an invalid IPv4 address
127.0.0.1/33 is an invalid IPv4 address
127.0.0.1/64 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 *strings[] = {
  /* valid */
  "::",
  "::1",
  "0000:0000:0000:0000:0000:0000:0000:0000",

  /* invalid */
  "foobar",
  NULL,
  "00:::0",
};

int
main(void)
{
    const char *str;
    const int i = sizeof(strings) / sizeof(strings[0]);
    for (int j = 0; j < i; j++) {
        str = strings[j];
        if (isinetaddr6(str)) {
            printf("%s is a valid IPv6 address\n", str);
        } else {
            printf("%s is an invalid IPv6 address\n", str);
        }
    }
    return EXIT_SUCCESS;
}

When the above source code is compiled and run the output is expected to be as follows:

$ cc -Iinclude src/*.c share/isinetaddr/examples/isinetaddr6.c -o example
$ ./example
0x1eef [isinetaddr] % ./example
:: is a valid IPv6 address
::1 is a valid IPv6 address
0000:0000:0000:0000:0000:0000:0000:0000 is a valid IPv6 address
foobar is an invalid IPv6 address
(null) is an invalid IPv6 address
00:::0 is an invalid IPv6 address

Sources

License

BSD Zero Clause.
See LICENSE.