Created
May 15, 2019 01:51
-
-
Save xuesongbj/9ba554648ef90cb73499d8fc44b38b0a to your computer and use it in GitHub Desktop.
How to compare an IPv6 is greater or less than in C?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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