-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathb2f-grep.c
508 lines (410 loc) · 13.2 KB
/
b2f-grep.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <assert.h>
#include <arpa/inet.h>
#include <byteswap.h>
#include <getopt.h>
#include <gmp.h>
#include "ez_libc.h"
#include "ptrvec.h"
#include "util.h"
/*==================================================================*/
/*=================== Support structs ==============================*/
/*==================================================================*/
/****************************************************************
* signed 129 bit integer IP address or IP address difference.
* This covers IPv4 and IPv6 addresses.
*/
typedef struct _MpAddr {
enum {
MPADDR_INIT_FLG= 1<<0
} flags;
// AF_INET or AF_INET6
int domain;
// GNU multi-precision integer
mpz_t addr;
} MpAddr;
/****************************************************************
* Entry class, one for each address passed on command line.
*/
/* Useful class for this app */
typedef struct _Entry {
// 129 bit integer address.
MpAddr mp_addr;
// Line from stdin which has was closest to ours.
unsigned lineno;
// Invalid if 0 == lineno
MpAddr mp_closest_addr, // Address which was closest to ours.
mp_closest_prox; // Proximity to ours.
} Entry;
/*==================================================================*/
/*================= Forward declarations ===========================*/
/*==================================================================*/
static void MpAddr_sinit(MpAddr *self);
static MpAddr* MpAddr_constructor(MpAddr *self);
static void* MpAddr_destructor(MpAddr *self);
static int MpAddr_inetAssign(MpAddr *self, const char *addrStr);
static void MpAddr_assign(MpAddr *self, const MpAddr *src);
static void MpAdd_absDiff(MpAddr *self, const MpAddr *opl, const MpAddr *opr);
static int MpAdd_cmp(const MpAddr *self, const MpAddr *other);
static int MpAddr_isSameFamily(const MpAddr *self, const MpAddr *other);
static int MpAddr_lt(const MpAddr *self, const MpAddr *other);
#define Entry_create(p, addr) \
((p)=(Entry_constructor((p)=malloc(sizeof(Entry)), addr) ? (p) : ( p ? realloc(Entry_destructor(p),0) : 0 )))
static Entry* Entry_constructor(Entry *self, const char *addr);
static void* Entry_destructor(Entry *self);
static int Entry_is_closer(const Entry *self, const MpAddr *mp_addr);
static int Entry_lineno_cmp(const void *const*pp1, const void *const* pp2);
static void Entry_print(const Entry *self);
static void Entry_register(Entry *self, unsigned lineno, const MpAddr *mp_addr);
/*==================================================================*/
/*========================= static data ============================*/
/*==================================================================*/
static struct {
PTRVEC entry_vec;
unsigned max_off;
struct {
int major,
minor,
patch;
} version;
} S= {
.max_off= 50,
.version= {
.major= 0,
.minor= 0,
.patch= 2
}
};
/*==================================================================*/
/*======================== main() stuff ============================*/
/*==================================================================*/
/* Enums for long options */
enum {
VERSION_OPT_ENUM=128, /* Larger than any printable character */
HELP_OPT_ENUM
};
/*==================================================================*/
/*======================== main() ==================================*/
/*==================================================================*/
int
main(int argc, char **argv)
/***************************************************
* Program execution begins here ...
*/
{
int rtn= EXIT_FAILURE;
extern char *optarg;
extern int optind, optopt;
PTRVEC_constructor(&S.entry_vec, 10);
/* Parse command line arguments */
{ /*==================================================================*/
int c, errflg= 0;
for(;;) {
static const struct option long_options[]= {
{"help", no_argument, 0, HELP_OPT_ENUM},
{"version", no_argument, 0, VERSION_OPT_ENUM},
{}
};
int c, option_ndx= 0;
c= getopt_long(argc, argv, ":m:", long_options, &option_ndx);
if(-1 == c) break;
switch(c) {
/* print usage help */
case HELP_OPT_ENUM:
++errflg;
break;
case VERSION_OPT_ENUM:
ez_fprintf(stdout, "b2f-grep v%d.%d.%d\n", S.version.major, S.version.minor, S.version.patch);
return 0;
case 'm':
{
int rc= sscanf(optarg, "%u", &S.max_off);
if(1 != rc)
++errflg;
} break;
case '?':
ez_fprintf(stderr, "Unrecognized option: -%c\n", optopt);
++errflg;
break;
}
}
if(errflg) {
ez_fprintf(stderr,
"b2f-grep v%d.%d.%d Usage:\n"
"%s [options] addr1 [...] <ban2fail.cfg_file\n"
" -m max\tlargest value to consider in MAX_OFFENSES block\n"
" --help\tprint this usage message.\n"
" --version\tprint the version number and exit.\n"
, S.version.major, S.version.minor, S.version.patch
, argv[0]
);
goto abort;
}
/* Make sure we have at least one address to process */
if(optind == argc) {
eprintf("ERROR: no addresses supplied on command line.");
goto abort;
}
/* Pick up addresses on command line */
for(; optind < argc; ++optind) {
const char *addr= argv[optind];
/* Create an entry, and place it in our vector */
Entry *e;
if(!Entry_create(e, addr))
continue;
PTRVEC_addTail(&S.entry_vec, e);
}
} // command options
{ /*====== Line by line processing ============*/
static char lbuf[1024],
ipbuf[40];
static MpAddr mp_addr,
mp_prox;
MpAddr_sinit(&mp_addr);
MpAddr_sinit(&mp_prox);
char *str;
unsigned lineno, max_off= 0;
for(lineno= 1, str= ez_fgets(lbuf, sizeof(lbuf)-1, stdin);
str;
str= ez_fgets(lbuf, sizeof(lbuf)-1, stdin), ++lineno)
{
str= trim(str);
/* See if this is a MAX_OFFENSES block declaration */
int rc= sscanf(str, "MAX_OFFENSES %u", &max_off);
if(1 == rc || max_off > S.max_off)
continue; // We have what we need for this line
/* Nope, maybe an IP address ... */
rc= sscanf(str, "IP = %39[^ \t]", ipbuf);
if(1 != rc)
continue; // Ignore line
if(MpAddr_inetAssign(&mp_addr, ipbuf)) {
eprintf("ERROR: on line %u", lineno);
continue; // Don't recognize address string
}
{ /* Loop through all entries, assign line numbers if necessary */
unsigned i;
Entry *e;
PTRVEC_loopFwd(&S.entry_vec, i, e) {
Entry_register(e, lineno, &mp_addr);
}
}
}
} // reading stdin
{ /*========= Print results ============*/
unsigned i;
Entry *e;
PTRVEC_sort(&S.entry_vec, Entry_lineno_cmp);
PTRVEC_loopFwd(&S.entry_vec, i, e) {
if(i)
ez_fputc('\t', stdout);
Entry_print(e);
}
ez_fputc('\n', stdout);
}
rtn= EXIT_SUCCESS;
abort:
return rtn;
}
/*==================================================================*/
/*============== Supporting functions ==============================*/
/*==================================================================*/
static void
MpAddr_sinit(MpAddr *self)
/**************************************************************
* Construct if necessary.
*/
{
if(!(self->flags & MPADDR_INIT_FLG))
MpAddr_constructor(self);
}
static MpAddr*
MpAddr_constructor(MpAddr *self)
/**************************************************************
* Initialize for use.
*/
{
mpz_init2(self->addr, 129);
self->flags= MPADDR_INIT_FLG;
return self;
}
static void*
MpAddr_destructor(MpAddr *self)
/**************************************************************
* Free resources.
*/
{
if(self->flags & MPADDR_INIT_FLG)
mpz_clear(self->addr);
return self;
}
static int
MpAddr_inetAssign(MpAddr *self, const char *addrStr)
/**************************************************************
* Assign address from string representation of numeric address
*/
{
int rtn= -1;
static char hex_buf[33];
unsigned nBytes;
unsigned char buf[sizeof(struct in6_addr)];
/* Determine if this is ipv6 or ipv6 */
self->domain= strchr(addrStr, ':') ? AF_INET6 : AF_INET;
/* Convert to a big-endian integer of 4 or 16 bytes */
int rc= inet_pton(self->domain, addrStr, buf);
switch(rc) {
case -1:
sys_eprintf("ERROR: inet_pton(\"%s\")", addrStr);
goto abort;
case 0:
eprintf("WARNING: \"%s\" not recognized as an address", addrStr);
goto abort;
}
/* At this point we have the address as an integer in big-endian order in buf */
switch(self->domain) {
case AF_INET:
nBytes= 4;
break;
case AF_INET6:
nBytes= 16;
break;
default:
assert(0);
}
/* Convert big-endian integer we have in buf[] to hexidecimal string */
for(unsigned i= 0; i < nBytes; ++i)
snprintf(hex_buf+2*i, sizeof(hex_buf) - 2*i, "%02hhx", buf[i]);
/* Set multi-precision integer to hex string value */
rc= mpz_set_str(self->addr, hex_buf, 16);
if(-1 == rc) {
eprintf("ERROR: \"%s\" not recognized as hexidecimal integer.", hex_buf);
goto abort;
}
rtn= 0;
abort:
return rtn;
}
static void
MpAdd_absDiff(MpAddr *self, const MpAddr *opl, const MpAddr *opr)
/***************************************************************
* Compute the absolute different between opl and opr
*/
{
MpAddr diff;
MpAddr_sinit(&diff);
mpz_sub(diff.addr, opl->addr, opr->addr);
mpz_abs(self->addr, diff.addr);
}
static int
MpAdd_cmp(const MpAddr *self, const MpAddr *other)
/***************************************************************
* strcmp() style comparision return.
*/
{
return mpz_cmp(self->addr, other->addr);
}
static int
MpAddr_isSameFamily(const MpAddr *self, const MpAddr *other)
/***************************************************************
* Returns non-zero if self and other are in the same address
* family.
*/
{
return self->domain == other->domain;
}
static int
MpAddr_cmp(const MpAddr *self, const MpAddr *other)
/***************************************************************
* strcmp() style return value for comparison of self & other.
*/
{
return mpz_cmp(self->addr, other->addr);
}
static void
MpAddr_assign(MpAddr *self, const MpAddr *src)
/***************************************************************
* Assign the value of src to ourself.
*/
{
mpz_set(self->addr, src->addr);
}
static Entry*
Entry_constructor(Entry *self, const char *addr)
/***************************************************************
* Create an entry object for the supplied IP address string.
*/
{
memset(self, 0, sizeof(*self));
Entry *rtn= NULL;
MpAddr_constructor(&self->mp_addr);
MpAddr_constructor(&self->mp_closest_addr);
MpAddr_constructor(&self->mp_closest_prox);
if(MpAddr_inetAssign(&self->mp_addr, addr))
goto abort;
rtn= self;
abort:
return rtn;
}
static void*
Entry_destructor(Entry *self)
/***************************************************************
* Destructor.
*/
{
MpAddr_destructor(&self->mp_addr);
MpAddr_destructor(&self->mp_closest_addr);
MpAddr_destructor(&self->mp_closest_prox);
return self;
}
static void
Entry_print(const Entry *self)
/***************************************************************
* Print result to stdout.
*/
{
unsigned lineno= MAX(self->lineno, 1);
switch(MpAddr_cmp(&self->mp_addr, &self->mp_closest_addr)) {
case -1: --lineno;
}
ez_fprintf(stdout, "%u", lineno);
}
static int
Entry_lineno_cmp(const void *const*pp1, const void *const* pp2)
/***************************************************************
* Comparison function we can use with PTRVEC_sort().
*/
{
const Entry *e1= *(const Entry *const*)pp1,
*e2= *(const Entry *const*)pp2;
if(e1->lineno < e2->lineno)
return -1;
if(e1->lineno > e2->lineno)
return 1;
return 0;
return -1;
}
static void
Entry_register(Entry *self, unsigned lineno, const MpAddr *mp_addr)
/***************************************************************
* Registers the address found on lineno.
*/
{
/* Ignore address of different family */
if(!MpAddr_isSameFamily(&self->mp_addr, mp_addr))
return;
static MpAddr prox;
MpAddr_sinit(&prox);
MpAdd_absDiff(&prox, &self->mp_addr, mp_addr);
if(!self->lineno || 0 < MpAddr_cmp(&self->mp_closest_prox, &prox)) {
// if(!self->lineno || 0 > MpAddr_cmp(&prox, &self->mp_closest_prox)) {
self->lineno= lineno;
MpAddr_assign(&self->mp_closest_addr, mp_addr);
MpAddr_assign(&self->mp_closest_prox, &prox);
}
}
static int
Entry_isSameFamily(const Entry *self, const MpAddr *mp_addr)
/***************************************************************
* Frontend for MpAddr_isSameFamily()
*/
{
return MpAddr_isSameFamily(&self->mp_addr, mp_addr);
}