This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 607
/
naxsi_net.c
70 lines (62 loc) · 1.88 KB
/
naxsi_net.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "naxsi_net.h"
int
parse_ipv6(const char* addr, ip_t* ip, char* ip_str)
{
struct in6_addr ipv6 = { .s6_addr = { 0 } };
if (inet_pton(AF_INET6, addr, &ipv6) != 1) {
return 0;
}
if (ip) {
// ipv6 hi
ip->v6[0] = ipv6.s6_addr[0];
ip->v6[0] = (ip->v6[0] << 8) | ipv6.s6_addr[1];
ip->v6[0] = (ip->v6[0] << 8) | ipv6.s6_addr[2];
ip->v6[0] = (ip->v6[0] << 8) | ipv6.s6_addr[3];
ip->v6[0] = (ip->v6[0] << 8) | ipv6.s6_addr[4];
ip->v6[0] = (ip->v6[0] << 8) | ipv6.s6_addr[5];
ip->v6[0] = (ip->v6[0] << 8) | ipv6.s6_addr[6];
ip->v6[0] = (ip->v6[0] << 8) | ipv6.s6_addr[7];
// ipv6 low
ip->v6[1] = ipv6.s6_addr[8];
ip->v6[1] = (ip->v6[1] << 8) | ipv6.s6_addr[9];
ip->v6[1] = (ip->v6[1] << 8) | ipv6.s6_addr[10];
ip->v6[1] = (ip->v6[1] << 8) | ipv6.s6_addr[11];
ip->v6[1] = (ip->v6[1] << 8) | ipv6.s6_addr[12];
ip->v6[1] = (ip->v6[1] << 8) | ipv6.s6_addr[13];
ip->v6[1] = (ip->v6[1] << 8) | ipv6.s6_addr[14];
ip->v6[1] = (ip->v6[1] << 8) | ipv6.s6_addr[15];
}
if (ip_str) {
inet_ntop(AF_INET6, &ipv6, ip_str, INET6_ADDRSTRLEN);
}
return 1;
}
int
parse_ipv4(const char* addr, ip_t* ip, char* ip_str)
{
struct in_addr ipv4 = { .s_addr = 0 };
if (inet_pton(AF_INET, addr, &ipv4) != 1) {
return 0;
}
if (ip) {
ip->v4 = htonl(ipv4.s_addr);
}
if (ip_str) {
inet_ntop(AF_INET, &ipv4, ip_str, INET_ADDRSTRLEN);
}
return 1;
}
int
is_in_subnet(const cidr_t* cidr, const ip_t* ip, int is_ipv6)
{
if ((cidr->version == IPv6 && !is_ipv6) || (cidr->version == IPv4 && is_ipv6)) {
return 0;
}
if (cidr->version == IPv4) {
return (ip->v4 & cidr->mask.v4) == (cidr->subnet.v4 & cidr->mask.v4);
} else {
return (ip->v6[0] & cidr->mask.v6[0]) == (cidr->subnet.v6[0] & cidr->mask.v6[0]) &&
(ip->v6[1] & cidr->mask.v6[1]) == (cidr->subnet.v6[1] & cidr->mask.v6[1]);
}
return 0;
}