How to compare IPv6 more / less than in C

Converting IPv4 with inet_ptonfor comparison, if it is within the IPv4 range is quite simple. However, I am not sure how to use inet_ptonand in6_addrsee if it is smaller / larger than the other IP. Here is what I thought:

#include <arpa/inet.h>

  ...

const char *ip6str = "0:0:0:0:0:ffff:c0a8:3";
const char *first = "0:0:0:0:0:ffff:c0a8:1";
const char *last = "0:0:0:0:0:ffff:c0a8:5";

struct in6_addr result, resfirst, restlast;
uint8_t ipv6[16]; // perhaps to hold the result?

inet_pton(AF_INET6, first, &resfirst);
inet_pton(AF_INET6, last, &reslast);
inet_pton(AF_INET6, ip6str, &result);

//assuming inet_pton succeed
if(result.s6_addr >= resfirst.s6_addr && result.s6_addr <= reslast.s6_addr)
    //within range
0
source share
2 answers

You can use memcmpfor this, since they are stored in network byte order (aka big endian).

if (memcmp(&result, &resfirst, sizeof(result)) > 0 && memcmp(&result, &reslast, sizeof(result)) < 0)

I think you could keep in mind >=though, perhaps <=.

In fact, you will have to do the same for IPv4, at least on small end machines.

+3
source

is_in_network_v6() http://grothoff.org/christian/rmv608.pdf.

#include <arpa/inet.h>
#include <stdio.h>

int is_in_network_v6(const struct in6_addr *network,
                       const struct in6_addr *mask,
                       const struct in6_addr *ip)
{
    unsigned int i;
    for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) {
        if ( ((((int *) ip )[i] & ((int *) mask)[i])) != (((int *) network)[i]
            & ((int *) mask)[i]))
            return 0;
    }
    return 1;
}

int main(int argc, char *argv[]) 
{
    char *ipStr = "2001:db8:8714:3a90::12";
    char *netmaskStr = "ffff:ffff:ffff:ffff::";
    char *networkStr = "2001:db8:8714:3a90::";

    struct sockaddr_in6 ip, netmask, network;
    inet_pton(AF_INET6, ipStr, &(ip.sin6_addr));
    inet_pton(AF_INET6, netmaskStr, &(netmask.sin6_addr));
    inet_pton(AF_INET6, networkStr, &(network.sin6_addr));

    printf("ip: '%s', netmask: '%s', network: '%s': %d\n",
           ipStr, netmaskStr, networkStr,
           is_in_network_v6(&(network.sin6_addr), &(netmask.sin6_addr),
                            &(ip.sin6_addr)));

    return 0;
}
0

All Articles