Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuesongbj/9ba554648ef90cb73499d8fc44b38b0a to your computer and use it in GitHub Desktop.
Save xuesongbj/9ba554648ef90cb73499d8fc44b38b0a to your computer and use it in GitHub Desktop.
How to compare an IPv6 is greater or less than in C?
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *ip6str = "2a03:6300:1:103:211:5bff:fe31:13e1";
const char *first = "2a03:6300:1:103:218:5bff:fe31:13e1";
const char *last = "2a03:6300:1:103:220:5bff:fe31:13e1";
struct in6_addr result, resfirst, reslast;
inet_pton(AF_INET6, first, &resfirst);
inet_pton(AF_INET6, last, &reslast);
inet_pton(AF_INET6, ip6str, &result);
if (memcmp(&result, &resfirst, sizeof(result)) > 0 && memcmp(&result, &reslast, sizeof(result)) < 0 ) {
printf("OOOK\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment