使用inet_pton
转换IPv4以进行比较是否在IPv4范围内相当简单。但是,我不确定如何使用inet_pton
和in6_addr
,看看它是否比其他IP更少/更大。这就是我的想法:
#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 :(得分:3)
您可以使用memcmp
,因为它们以网络字节顺序存储(也称为大端)。
if (memcmp(&result, &resfirst, sizeof(result)) > 0 && memcmp(&result, &reslast, sizeof(result)) < 0)
我认为您可能意味着>=
,也可能<=
。
事实上,你必须以这种方式为IPv4做这件事,至少在小端机器上。
答案 1 :(得分:0)
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;
}