-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathranges.c
More file actions
365 lines (337 loc) · 14.3 KB
/
ranges.c
File metadata and controls
365 lines (337 loc) · 14.3 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "ranges.h"
/* ─── globals ─────────────────────────────────────────────────── */
ip_range_t *g_blacklist = NULL;
int g_blacklist_count = 0;
ip_range_t *g_whitelist = NULL;
int g_whitelist_count = 0;
/* ══════════════════════════════════════════════════════════════
* GLOBAL ROUTABLE RANGES (host byte order)
* Excludes: RFC1918, RFC5735, DoD, IANA special, multicast,
* reserved, documentation ranges.
* Source: IANA IPv4 address space registry + RIR allocations.
* ══════════════════════════════════════════════════════════════ */
#define MK(a,b,c,d) (((uint32_t)(a)<<24)|((uint32_t)(b)<<16)|((uint32_t)(c)<<8)|(d))
const ip_range_t GLOBAL_RANGES[] = {
/* ─── ARIN (North America) ───────────────────────────── */
{MK(4,0,0,0), MK(4,255,255,255)},
{MK(8,0,0,0), MK(8,255,255,255)},
{MK(9,0,0,0), MK(9,255,255,255)},
{MK(12,0,0,0), MK(12,255,255,255)},
{MK(13,0,0,0), MK(13,255,255,255)},
{MK(17,0,0,0), MK(17,255,255,255)},
{MK(18,0,0,0), MK(18,255,255,255)},
{MK(20,0,0,0), MK(20,255,255,255)},
{MK(23,0,0,0), MK(23,255,255,255)},
{MK(24,0,0,0), MK(24,255,255,255)},
{MK(32,0,0,0), MK(32,255,255,255)},
{MK(34,0,0,0), MK(34,255,255,255)},
{MK(35,0,0,0), MK(35,255,255,255)},
{MK(38,0,0,0), MK(38,255,255,255)},
{MK(40,0,0,0), MK(40,255,255,255)},
{MK(44,0,0,0), MK(44,255,255,255)},
{MK(45,0,0,0), MK(45,255,255,255)},
{MK(47,0,0,0), MK(47,255,255,255)},
{MK(50,0,0,0), MK(50,255,255,255)},
{MK(52,0,0,0), MK(52,255,255,255)},
{MK(54,0,0,0), MK(54,255,255,255)},
{MK(63,0,0,0), MK(63,255,255,255)},
{MK(64,0,0,0), MK(64,255,255,255)},
{MK(65,0,0,0), MK(65,255,255,255)},
{MK(66,0,0,0), MK(66,255,255,255)},
{MK(67,0,0,0), MK(67,255,255,255)},
{MK(68,0,0,0), MK(68,255,255,255)},
{MK(69,0,0,0), MK(69,255,255,255)},
{MK(70,0,0,0), MK(70,255,255,255)},
{MK(71,0,0,0), MK(71,255,255,255)},
{MK(72,0,0,0), MK(72,255,255,255)},
{MK(73,0,0,0), MK(73,255,255,255)},
{MK(74,0,0,0), MK(74,255,255,255)},
{MK(75,0,0,0), MK(75,255,255,255)},
{MK(76,0,0,0), MK(76,255,255,255)},
{MK(96,0,0,0), MK(96,255,255,255)},
{MK(97,0,0,0), MK(97,255,255,255)},
{MK(98,0,0,0), MK(98,255,255,255)},
{MK(99,0,0,0), MK(99,255,255,255)},
{MK(104,0,0,0), MK(104,255,255,255)},
{MK(107,0,0,0), MK(107,255,255,255)},
{MK(108,0,0,0), MK(108,255,255,255)},
/* ─── RIPE NCC (Europe / Middle East / Central Asia) ─── */
{MK(2,0,0,0), MK(2,255,255,255)},
{MK(5,0,0,0), MK(5,255,255,255)},
{MK(25,0,0,0), MK(25,255,255,255)},
{MK(31,0,0,0), MK(31,255,255,255)},
{MK(37,0,0,0), MK(37,255,255,255)},
{MK(46,0,0,0), MK(46,255,255,255)},
{MK(51,0,0,0), MK(51,255,255,255)},
{MK(57,0,0,0), MK(57,255,255,255)},
{MK(62,0,0,0), MK(62,255,255,255)},
{MK(77,0,0,0), MK(77,255,255,255)},
{MK(78,0,0,0), MK(78,255,255,255)},
{MK(79,0,0,0), MK(79,255,255,255)},
{MK(80,0,0,0), MK(80,255,255,255)},
{MK(81,0,0,0), MK(81,255,255,255)},
{MK(82,0,0,0), MK(82,255,255,255)},
{MK(83,0,0,0), MK(83,255,255,255)},
{MK(84,0,0,0), MK(84,255,255,255)},
{MK(85,0,0,0), MK(85,255,255,255)},
{MK(86,0,0,0), MK(86,255,255,255)},
{MK(87,0,0,0), MK(87,255,255,255)},
{MK(88,0,0,0), MK(88,255,255,255)},
{MK(89,0,0,0), MK(89,255,255,255)},
{MK(90,0,0,0), MK(90,255,255,255)},
{MK(91,0,0,0), MK(91,255,255,255)},
{MK(92,0,0,0), MK(92,255,255,255)},
{MK(93,0,0,0), MK(93,255,255,255)},
{MK(94,0,0,0), MK(94,255,255,255)},
{MK(95,0,0,0), MK(95,255,255,255)},
{MK(109,0,0,0), MK(109,255,255,255)},
{MK(176,0,0,0), MK(176,255,255,255)},
{MK(178,0,0,0), MK(178,255,255,255)},
{MK(185,0,0,0), MK(185,255,255,255)},
{MK(188,0,0,0), MK(188,255,255,255)},
{MK(193,0,0,0), MK(193,255,255,255)},
{MK(194,0,0,0), MK(194,255,255,255)},
{MK(195,0,0,0), MK(195,255,255,255)},
{MK(212,0,0,0), MK(212,255,255,255)},
{MK(213,0,0,0), MK(213,255,255,255)},
{MK(217,0,0,0), MK(217,255,255,255)},
/* ─── APNIC (Asia-Pacific) ───────────────────────────── */
{MK(1,0,0,0), MK(1,255,255,255)},
{MK(14,0,0,0), MK(14,255,255,255)},
{MK(27,0,0,0), MK(27,255,255,255)},
{MK(36,0,0,0), MK(36,255,255,255)},
{MK(39,0,0,0), MK(39,255,255,255)},
{MK(42,0,0,0), MK(42,255,255,255)},
{MK(43,0,0,0), MK(43,255,255,255)},
{MK(49,0,0,0), MK(49,255,255,255)},
{MK(58,0,0,0), MK(58,255,255,255)},
{MK(59,0,0,0), MK(59,255,255,255)},
{MK(60,0,0,0), MK(60,255,255,255)},
{MK(61,0,0,0), MK(61,255,255,255)},
{MK(101,0,0,0), MK(101,255,255,255)},
{MK(103,0,0,0), MK(103,255,255,255)},
{MK(106,0,0,0), MK(106,255,255,255)},
{MK(110,0,0,0), MK(110,255,255,255)},
{MK(111,0,0,0), MK(111,255,255,255)},
{MK(112,0,0,0), MK(112,255,255,255)},
{MK(113,0,0,0), MK(113,255,255,255)},
{MK(114,0,0,0), MK(114,255,255,255)},
{MK(115,0,0,0), MK(115,255,255,255)},
{MK(116,0,0,0), MK(116,255,255,255)},
{MK(117,0,0,0), MK(117,255,255,255)},
{MK(118,0,0,0), MK(118,255,255,255)},
{MK(119,0,0,0), MK(119,255,255,255)},
{MK(120,0,0,0), MK(120,255,255,255)},
{MK(121,0,0,0), MK(121,255,255,255)},
{MK(122,0,0,0), MK(122,255,255,255)},
{MK(123,0,0,0), MK(123,255,255,255)},
{MK(124,0,0,0), MK(124,255,255,255)},
{MK(125,0,0,0), MK(125,255,255,255)},
{MK(126,0,0,0), MK(126,255,255,255)},
{MK(150,0,0,0), MK(150,255,255,255)},
{MK(153,0,0,0), MK(153,255,255,255)},
{MK(163,0,0,0), MK(163,255,255,255)},
{MK(168,0,0,0), MK(168,255,255,255)},
{MK(171,0,0,0), MK(171,255,255,255)},
{MK(175,0,0,0), MK(175,255,255,255)},
{MK(180,0,0,0), MK(180,255,255,255)},
{MK(182,0,0,0), MK(182,255,255,255)},
{MK(183,0,0,0), MK(183,255,255,255)},
{MK(202,0,0,0), MK(202,255,255,255)},
{MK(203,0,0,0), MK(203,255,255,255)},
{MK(210,0,0,0), MK(210,255,255,255)},
{MK(211,0,0,0), MK(211,255,255,255)},
{MK(218,0,0,0), MK(218,255,255,255)},
{MK(219,0,0,0), MK(219,255,255,255)},
{MK(220,0,0,0), MK(220,255,255,255)},
{MK(221,0,0,0), MK(221,255,255,255)},
{MK(222,0,0,0), MK(222,255,255,255)},
{MK(223,0,0,0), MK(223,255,255,255)},
/* ─── LACNIC (Latin America / Caribbean) ─────────────── */
{MK(177,0,0,0), MK(177,255,255,255)},
{MK(179,0,0,0), MK(179,255,255,255)},
{MK(181,0,0,0), MK(181,255,255,255)},
{MK(186,0,0,0), MK(186,255,255,255)},
{MK(187,0,0,0), MK(187,255,255,255)},
{MK(189,0,0,0), MK(189,255,255,255)},
{MK(190,0,0,0), MK(190,255,255,255)},
{MK(191,0,0,0), MK(191,255,255,255)},
{MK(200,0,0,0), MK(200,255,255,255)},
{MK(201,0,0,0), MK(201,255,255,255)},
/* ─── AFRINIC (Africa) ───────────────────────────────── */
{MK(41,0,0,0), MK(41,255,255,255)},
{MK(102,0,0,0), MK(102,255,255,255)},
{MK(105,0,0,0), MK(105,255,255,255)},
{MK(129,0,0,0), MK(129,255,255,255)},
{MK(154,0,0,0), MK(154,255,255,255)},
{MK(156,0,0,0), MK(156,255,255,255)},
{MK(160,0,0,0), MK(160,255,255,255)},
{MK(196,0,0,0), MK(196,255,255,255)},
{MK(197,0,0,0), MK(197,255,255,255)},
};
const int GLOBAL_RANGES_COUNT = (int)(sizeof(GLOBAL_RANGES)/sizeof(GLOBAL_RANGES[0]));
/* ══════════════════════════════════════════════════════════════
* BRASIL RANGES (sub-set do LACNIC + ARIN allocations to BR ASNs)
* ══════════════════════════════════════════════════════════════ */
const ip_range_t BR_RANGES[] = {
/* Claro/NET, Embratel, Oi, Vivo, TIM, etc. */
{MK(177,0,0,0), MK(177,255,255,255)},
{MK(179,0,0,0), MK(179,255,255,255)},
{MK(181,0,0,0), MK(181,255,255,255)},
{MK(186,0,0,0), MK(186,255,255,255)},
{MK(187,0,0,0), MK(187,255,255,255)},
{MK(189,0,0,0), MK(189,255,255,255)},
{MK(191,0,0,0), MK(191,255,255,255)},
{MK(200,128,0,0), MK(200,191,255,255)}, /* 200.128/9 */
{MK(200,192,0,0), MK(200,207,255,255)}, /* 200.192/11 */
{MK(201,0,0,0), MK(201,63,255,255)},
{MK(143,106,0,0), MK(143,107,255,255)},
{MK(143,208,0,0), MK(143,215,255,255)},
{MK(152,250,0,0), MK(152,255,255,255)},
{MK(189,112,0,0), MK(189,127,255,255)},
{MK(200,17,0,0), MK(200,17,255,255)},
{MK(200,18,0,0), MK(200,19,255,255)},
};
const int BR_RANGES_COUNT = (int)(sizeof(BR_RANGES)/sizeof(BR_RANGES[0]));
/* ══════════════════════════════════════════════════════════════
* Helpers
* ══════════════════════════════════════════════════════════════ */
uint32_t ip_to_int(const char *s) {
struct in_addr a;
if (inet_aton(s, &a) == 0) return 0;
return ntohl(a.s_addr);
}
void int_to_ip(uint32_t ip, char *buf) {
snprintf(buf, 16, "%u.%u.%u.%u",
(ip>>24)&0xff, (ip>>16)&0xff,
(ip>>8)&0xff, ip&0xff);
}
uint64_t parse_scaled_value(const char *s) {
char *end;
double v = strtod(s, &end);
if (!end || end == s) return 0;
switch (*end) {
case 'K': case 'k': return (uint64_t)(v * 1e3);
case 'M': case 'm': return (uint64_t)(v * 1e6);
case 'G': case 'g': return (uint64_t)(v * 1e9);
case 'T': case 't': return (uint64_t)(v * 1e12);
default: return (uint64_t)v;
}
}
/* ── CIDR string "a.b.c.d/n" or single IP → ip_range_t ───── */
int parse_ip_range(const char *cidr, ip_range_t **out) {
char buf[64];
strncpy(buf, cidr, sizeof(buf)-1);
buf[sizeof(buf)-1] = '\0';
char *slash = strchr(buf, '/');
int mask = 32;
if (slash) {
*slash = '\0';
mask = atoi(slash + 1);
if (mask < 0) mask = 0;
if (mask > 32) mask = 32;
}
uint32_t base = ip_to_int(buf);
if (base == 0 && strcmp(buf, "0.0.0.0") != 0) return -1;
uint32_t host_bits = 32 - mask;
uint64_t count = (uint64_t)1 << host_bits;
/* align base */
base &= ~((uint32_t)(count - 1));
*out = malloc(sizeof(ip_range_t));
if (!*out) return -1;
(*out)[0].start = base;
(*out)[0].end = (uint32_t)(base + count - 1);
return 1;
}
/* ── "80,443,8080-8090" → port_range_t array ─────────────── */
int parse_port_range(const char *spec, port_range_t **out) {
if (!spec || !*spec) {
/* default */
spec = DEFAULT_PORTS;
}
/* count commas */
int n = 1;
for (const char *p = spec; *p; p++) if (*p == ',') n++;
*out = malloc(sizeof(port_range_t) * n);
if (!*out) return -1;
char *copy = strdup(spec);
char *tok = strtok(copy, ",");
int idx = 0;
while (tok) {
char *dash = strchr(tok, '-');
if (dash) {
*dash = '\0';
(*out)[idx].start = (uint16_t)atoi(tok);
(*out)[idx].end = (uint16_t)atoi(dash + 1);
} else {
(*out)[idx].start = (uint16_t)atoi(tok);
(*out)[idx].end = (*out)[idx].start;
}
idx++;
tok = strtok(NULL, ",");
}
free(copy);
return idx;
}
/* ── load CIDR file (one entry per line, # comments) ─────── */
int load_range_file(const char *path, ip_range_t **out, int *cnt) {
FILE *f = fopen(path, "r");
if (!f) { perror(path); return 0; }
int cap = 256;
int used = 0;
*out = malloc(sizeof(ip_range_t) * cap);
char line[128];
while (fgets(line, sizeof(line), f)) {
char *nl = strchr(line, '\n'); if (nl) *nl = '\0';
char *cr = strchr(line, '\r'); if (cr) *cr = '\0';
char *p = line;
while (*p == ' ' || *p == '\t') p++;
if (*p == '#' || *p == '\0') continue;
ip_range_t *r = NULL;
int n = parse_ip_range(p, &r);
if (n <= 0 || !r) continue;
if (used + n > cap) {
cap *= 2;
*out = realloc(*out, sizeof(ip_range_t) * cap);
}
memcpy(*out + used, r, sizeof(ip_range_t) * n);
used += n;
free(r);
}
fclose(f);
*cnt = used;
return used;
}
/* ── total IPs across a range array ─────────────────────────── */
uint64_t calculate_total_ips(const ip_range_t *ranges, int n) {
uint64_t total = 0;
for (int i = 0; i < n; i++)
total += (uint64_t)(ranges[i].end - ranges[i].start) + 1;
return total;
}
/* ── get IP at position idx inside ranges (host byte order) ─ */
uint32_t get_ip_from_index(uint64_t idx,
const ip_range_t *ranges, int n) {
for (int i = 0; i < n; i++) {
uint64_t sz = (uint64_t)(ranges[i].end - ranges[i].start) + 1;
if (idx < sz)
return ranges[i].start + (uint32_t)idx;
idx -= sz;
}
return 0;
}
/* ── blacklist / whitelist checks (host byte order) ──────── */
static int range_contains(const ip_range_t *list, int cnt, uint32_t ip) {
/* linear — fine for <10k entries; upgrade to binary search if needed */
for (int i = 0; i < cnt; i++)
if (ip >= list[i].start && ip <= list[i].end)
return 1;
return 0;
}
int is_blacklisted(uint32_t ip) {
return range_contains(g_blacklist, g_blacklist_count, ip);
}
int is_whitelisted(uint32_t ip) {
if (g_whitelist_count == 0) return 1; /* no whitelist → allow all */
return range_contains(g_whitelist, g_whitelist_count, ip);
}