-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathcidr.inc
45 lines (35 loc) · 963 Bytes
/
cidr.inc
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
stock cidr_ip2long(const ip[]) {
new parts[4];
#if defined sscanf
if (sscanf(ip, "p<.>a<i>[4]", parts)) {
return 0;
}
#else
new pos;
parts[0] = strval(ip);
if ((pos = strfind(ip, ".", false, 0)) == -1) {
return 0;
}
parts[1] = strval(ip[pos + 1]);
if ((pos = strfind(ip, ".", false, pos)) == -1) {
return 0;
}
parts[2] = strval(ip[pos + 1]);
if ((pos = strfind(ip, ".", false, pos)) == -1) {
return 0;
}
parts[3] = strval(ip[pos + 1]);
#endif
return ((parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]);
}
stock cidr_match(const ip[], const range[]) {
new pos, rangeip[18], bits, mask;
if ((pos = strfind(range, "/", true, 0)) == -1) {
bits = 26; // default bits
} else {
bits = strval(range[pos + 1]);
}
strmid(rangeip, range, 0, pos, sizeof(rangeip));
mask = -1 << (32 - bits);
return ((cidr_ip2long(ip) & mask) == (cidr_ip2long(rangeip) & mask));
}