A set of C functions for IPv(4|6) validation
Find a file
0x1eef 3290d6aa14
Some checks failed
isinetaddr / tests (macos-latest) (push) Has been cancelled
isinetaddr / tests (ubuntu-latest) (push) Has been cancelled
Apply editorconfig
2024-09-12 23:56:46 -03:00
.github/workflows Add GitHub action 2023-08-25 17:38:12 -03:00
include Utilize stdbool.h 2024-03-04 23:50:46 -03:00
share/isinetaddr Apply editorconfig 2024-09-12 23:56:46 -03:00
src Add editorconfig 2024-09-12 23:52:24 -03:00
test Add support for 2001:DB8::8:800:200C:417A 2024-03-04 23:55:42 -03:00
.editorconfig Add editorconfig 2024-09-12 23:52:24 -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 Apply editorconfig 2024-09-12 23:56:46 -03:00

About

isinetaddr is a clang library that provides a set of functions for IP address validation. The library is guided by testcases that help verify safety and correctness.

Examples

isinetaddr4

The isinetaddr4 function returns true when given a valid IPv4 address:

#include <isinetaddr.h>
#include <stdio.h>
#include <stdlib.h>

const char *ipv4[] = {
    /* valid */
    "127.0.0.1", "1.1.1.1", "0.0.0.0",
    /* invalid */
    "0.0.0.0.0", "foobar"
};

int
main(void)
{
    for (int i = 0; i < 5; i++) {
        if (isinetaddr4(ipv4[i])) {
            printf("%s is a valid IPv4 address.\n", ipv4[i]);
        } else {
            printf("%s is an invalid IPv4 address.\n", ipv4[i]);
        }
    }
    return (EXIT_SUCCESS);
}

iscidraddr4

The iscidraddr4 function supports the same features as isinetaddr4 plus CIDR notation. The following example builds on the previous example:

#include <isinetaddr.h>
#include <stdio.h>
#include <stdlib.h>

const char *ipv4[] = {
    /* valid */
    "127.0.0.1", "192.168.2.1/32", "192.168.2.1/0",
    /* invalid */
    "192.168.2.1/33", "foobar"
};

int
main(void)
{
    for (int i = 0; i < 5; i++) {
        if (iscidraddr4(ipv4[i])) {
            printf("%s is a valid IPv4 address.\n", ipv4[i]);
        } else {
            printf("%s is an invalid IPv4 address.\n", ipv4[i]);
        }
    }
    return (EXIT_SUCCESS);
}

isinetaddr6

The isinetaddr6 function returns true when given a valid IPv6 address:

#include <isinetaddr.h>
#include <stdio.h>
#include <stdlib.h>

const char *ipv6[] = {
    /* valid */
    "::", "::1", "0000:0000:0000:0000:0000:0000:0000:0000",
    /* invalid */
    "foobar", "00:::0"
};


int
main(void)
{
    for (int i = 0; i < 5; i++) {
        if (isinetaddr6(ipv6[i])) {
            printf("%s is a valid IPv4 address.\n", ipv6[i]);
        } else {
            printf("%s is an invalid IPv4 address.\n", ipv6[i]);
        }
    }
    return (EXIT_SUCCESS);
}

Sources

License

BSD Zero Clause
See LICENSE