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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
|
/*
This file is part of GNUnet
Copyright (C) 2012, 2013, 2015 GNUnet e.V.
GNUnet is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
*/
/**
* @file src/tun/regex.c
* @brief functions to convert IP networks to regexes
* @author Maximilian Szengel
* @author Christian Grothoff
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_tun_lib.h"
/**
* 'wildcard', matches all possible values (for HEX encoding).
*/
#define DOT "(0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"
/**
* Create a regex in @a rxstr from the given @a ip and @a netmask.
*
* @param ip IPv4 representation.
* @param port destination port
* @param rxstr generated regex, must be at least #GNUNET_TUN_IPV4_REGEXLEN
* bytes long.
*/
void
GNUNET_TUN_ipv4toregexsearch (const struct in_addr *ip,
uint16_t port,
char *rxstr)
{
GNUNET_snprintf (rxstr,
GNUNET_TUN_IPV4_REGEXLEN,
"4-%04X-%08X",
(unsigned int) port,
ntohl (ip->s_addr));
}
/**
* Create a regex in @a rxstr from the given @a ipv6 and @a prefixlen.
*
* @param ipv6 IPv6 representation.
* @param port destination port
* @param rxstr generated regex, must be at least #GNUNET_TUN_IPV6_REGEXLEN
* bytes long.
*/
void
GNUNET_TUN_ipv6toregexsearch (const struct in6_addr *ipv6,
uint16_t port,
char *rxstr)
{
const uint32_t *addr;
addr = (const uint32_t *) ipv6;
GNUNET_snprintf (rxstr,
GNUNET_TUN_IPV6_REGEXLEN,
"6-%04X-%08X%08X%08X%08X",
(unsigned int) port,
ntohl (addr[0]),
ntohl (addr[1]),
ntohl (addr[2]),
ntohl (addr[3]));
}
/**
* Convert the given 4-bit (!) number to a regex.
*
* @param value the value, only the lowest 4 bits will be looked at
* @param mask which bits in value are wildcards (any value)?
*/
static char *
nibble_to_regex (uint8_t value,
uint8_t mask)
{
char *ret;
value &= mask;
switch (mask)
{
case 0:
return GNUNET_strdup (DOT);
case 8:
GNUNET_asprintf (&ret,
"(%X|%X|%X|%X|%X|%X|%X|%X)",
value,
value + 1,
value + 2,
value + 3,
value + 4,
value + 5,
value + 6,
value + 7);
return ret;
case 12:
GNUNET_asprintf (&ret,
"(%X|%X|%X|%X)",
value,
value + 1,
value + 2,
value + 3);
return ret;
case 14:
GNUNET_asprintf (&ret,
"(%X|%X)",
value,
value + 1);
return ret;
case 15:
GNUNET_asprintf (&ret,
"%X",
value);
return ret;
default:
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
_("Bad mask: %d\n"),
mask);
GNUNET_break (0);
return NULL;
}
}
/**
* Convert the given 16-bit number to a regex.
*
* @param value the value
* @param mask which bits in value are wildcards (any value)?
*/
static char *
num_to_regex (uint16_t value,
uint16_t mask)
{
const uint8_t *v = (const uint8_t *) &value;
const uint8_t *m = (const uint8_t *) &mask;
char *a;
char *b;
char *c;
char *d;
char *ret;
a = nibble_to_regex (v[0] >> 4, m[0] >> 4);
b = nibble_to_regex (v[0] & 15, m[0] & 15);
c = nibble_to_regex (v[1] >> 4, m[1] >> 4);
d = nibble_to_regex (v[1] & 15, m[1] & 15);
ret = NULL;
if ( (NULL != a) &&
(NULL != b) &&
(NULL != c) &&
(NULL != d) )
GNUNET_asprintf (&ret,
"%s%s%s%s",
a, b, c, d);
GNUNET_free_non_null (a);
GNUNET_free_non_null (b);
GNUNET_free_non_null (c);
GNUNET_free_non_null (d);
return ret;
}
/**
* Do we need to put parents around the given argument?
*
* @param arg part of a regular expression
* @return #GNUNET_YES if we should parens,
* #GNUNET_NO if not
*/
static int
needs_parens (const char *arg)
{
size_t off;
size_t len;
unsigned int op;
op = 0;
len = strlen (arg);
for (off=0;off<len;off++)
{
switch (arg[off])
{
case '(':
op++;
break;
case ')':
GNUNET_assert (op > 0);
op--;
break;
case '|':
if (0 == op)
return GNUNET_YES;
break;
default:
break;
}
}
return GNUNET_NO;
}
/**
* Compute port policy for the given range of
* port numbers.
*
* @param start starting offset
* @param end end offset
* @param step increment level (power of 16)
* @param pp port policy to convert
* @return corresponding regex
*/
static char *
compute_policy (unsigned int start,
unsigned int end,
unsigned int step,
const struct GNUNET_STRINGS_PortPolicy *pp)
{
unsigned int i;
char before[36]; /* 16 * 2 + 3 dots + 0-terminator */
char middlel[33]; /* 16 * 2 + 0-terminator */
char middleh[33]; /* 16 * 2 + 0-terminator */
char after[36]; /* 16 * 2 + 3 dots + 0-terminator */
char beforep[36+2]; /* 16 * 2 + 3 dots + 0-terminator + ()*/
char middlehp[33+2]; /* 16 * 2 + 0-terminator + () */
char middlelp[33+2]; /* 16 * 2 + 0-terminator + () */
char afterp[36+2]; /* 16 * 2 + 3 dots + 0-terminator + () */
char dots[5 * strlen (DOT)];
char buf[3];
char *middle;
char *ret;
unsigned int xstep;
char *recl;
char *rech;
char *reclp;
char *rechp;
unsigned int start_port;
unsigned int end_port;
GNUNET_assert (GNUNET_YES == pp->negate_portrange);
start_port = pp->start_port;
if (1 == start_port)
start_port = 0;
end_port = pp->end_port;
GNUNET_assert ((end - start) / step <= 0xF);
before[0] = '\0';
middlel[0] = '\0';
middleh[0] = '\0';
after[0] = '\0';
for (i=start;i<=end;i+=step)
{
GNUNET_snprintf (buf,
sizeof (buf),
"%X|",
(i - start) / step);
if (i / step < start_port / step)
strcat (before, buf);
else if (i / step > end_port / step)
strcat (after, buf);
else if (i / step == start_port / step)
strcat (middlel, buf);
else if (i / step == end_port / step)
strcat (middleh, buf);
}
if (strlen (before) > 0)
before[strlen (before)-1] = '\0';
if (strlen (middlel) > 0)
middlel[strlen (middlel)-1] = '\0';
if (strlen (middleh) > 0)
middleh[strlen (middleh)-1] = '\0';
if (strlen (after) > 0)
after[strlen (after)-1] = '\0';
if (needs_parens (before))
GNUNET_snprintf (beforep,
sizeof (beforep),
"(%s)",
before);
else
strcpy (beforep, before);
if (needs_parens (middlel))
GNUNET_snprintf (middlelp,
sizeof (middlelp),
"(%s)",
middlel);
else
strcpy (middlelp, middlel);
if (needs_parens (middleh))
GNUNET_snprintf (middlehp,
sizeof (middlehp),
"(%s)",
middleh);
else
strcpy (middlehp, middleh);
if (needs_parens (after))
GNUNET_snprintf (afterp,
sizeof (afterp),
"(%s)",
after);
else
strcpy (afterp, after);
dots[0] = '\0';
for (xstep=step/16;xstep>0;xstep/=16)
strcat (dots, DOT);
if (step >= 16)
{
if (strlen (middlel) > 0)
recl = compute_policy ((start_port / step) * step,
(start_port / step) * step + step - 1,
step / 16,
pp);
else
recl = GNUNET_strdup ("");
if (strlen (middleh) > 0)
rech = compute_policy ((end_port / step) * step,
(end_port / step) * step + step - 1,
step / 16,
pp);
else
rech = GNUNET_strdup ("");
}
else
{
recl = GNUNET_strdup ("");
rech = GNUNET_strdup ("");
middlel[0] = '\0';
middlelp[0] = '\0';
middleh[0] = '\0';
middlehp[0] = '\0';
}
if (needs_parens (recl))
GNUNET_asprintf (&reclp,
"(%s)",
recl);
else
reclp = GNUNET_strdup (recl);
if (needs_parens (rech))
GNUNET_asprintf (&rechp,
"(%s)",
rech);
else
rechp = GNUNET_strdup (rech);
if ( (strlen (middleh) > 0) &&
(strlen (rech) > 0) &&
(strlen (middlel) > 0) &&
(strlen (recl) > 0) )
{
GNUNET_asprintf (&middle,
"%s%s|%s%s",
middlel,
reclp,
middleh,
rechp);
}
else if ( (strlen (middleh) > 0) &&
(strlen (rech) > 0) )
{
GNUNET_asprintf (&middle,
"%s%s",
middleh,
rechp);
}
else if ( (strlen (middlel) > 0) &&
(strlen (recl) > 0) )
{
GNUNET_asprintf (&middle,
"%s%s",
middlel,
reclp);
}
else
{
middle = GNUNET_strdup ("");
}
if ( (strlen(before) > 0) &&
(strlen(after) > 0) )
{
if (strlen (dots) > 0)
{
if (strlen (middle) > 0)
GNUNET_asprintf (&ret,
"(%s%s|%s|%s%s)",
beforep, dots,
middle,
afterp, dots);
else
GNUNET_asprintf (&ret,
"(%s|%s)%s",
beforep,
afterp,
dots);
}
else
{
if (strlen (middle) > 0)
GNUNET_asprintf (&ret,
"(%s|%s|%s)",
before,
middle,
after);
else if (1 == step)
GNUNET_asprintf (&ret,
"%s|%s",
before,
after);
else
GNUNET_asprintf (&ret,
"(%s|%s)",
before,
after);
}
}
else if (strlen (before) > 0)
{
if (strlen (dots) > 0)
{
if (strlen (middle) > 0)
GNUNET_asprintf (&ret,
"(%s%s|%s)",
beforep, dots,
middle);
else
GNUNET_asprintf (&ret,
"%s%s",
beforep, dots);
}
else
{
if (strlen (middle) > 0)
GNUNET_asprintf (&ret,
"(%s|%s)",
before,
middle);
else
GNUNET_asprintf (&ret,
"%s",
before);
}
}
else if (strlen (after) > 0)
{
if (strlen (dots) > 0)
{
if (strlen (middle) > 0)
GNUNET_asprintf (&ret,
"(%s|%s%s)",
middle,
afterp, dots);
else
GNUNET_asprintf (&ret,
"%s%s",
afterp, dots);
}
else
{
if (strlen (middle) > 0)
GNUNET_asprintf (&ret,
"%s|%s",
middle,
after);
else
GNUNET_asprintf (&ret,
"%s",
after);
}
}
else if (strlen (middle) > 0)
{
GNUNET_asprintf (&ret,
"%s",
middle);
}
else
{
ret = GNUNET_strdup ("");
}
GNUNET_free (middle);
GNUNET_free (reclp);
GNUNET_free (rechp);
GNUNET_free (recl);
GNUNET_free (rech);
return ret;
}
/**
* Convert a port policy to a regular expression. Note: this is a
* very simplistic implementation, we might want to consider doing
* something more sophisiticated (resulting in smaller regular
* expressions) at a later time.
*
* @param pp port policy to convert
* @return NULL on error
*/
static char *
port_to_regex (const struct GNUNET_STRINGS_PortPolicy *pp)
{
char *reg;
char *ret;
char *pos;
unsigned int i;
unsigned int cnt;
if ( (0 == pp->start_port) ||
( (1 == pp->start_port) &&
(0xFFFF == pp->end_port) &&
(GNUNET_NO == pp->negate_portrange)) )
return GNUNET_strdup (DOT DOT DOT DOT);
if ( (pp->start_port == pp->end_port) &&
(GNUNET_NO == pp->negate_portrange))
{
GNUNET_asprintf (&ret,
"%04X",
pp->start_port);
return ret;
}
if (pp->end_port < pp->start_port)
return NULL;
if (GNUNET_YES == pp->negate_portrange)
{
ret = compute_policy (0, 0xFFFF, 0x1000, pp);
}
else
{
cnt = pp->end_port - pp->start_port + 1;
reg = GNUNET_malloc (cnt * 5 + 1);
pos = reg;
for (i=1;i<=0xFFFF;i++)
{
if ( (i >= pp->start_port) && (i <= pp->end_port) )
{
if (pos == reg)
{
GNUNET_snprintf (pos,
5,
"%04X",
i);
}
else
{
GNUNET_snprintf (pos,
6,
"|%04X",
i);
}
pos += strlen (pos);
}
}
GNUNET_asprintf (&ret,
"(%s)",
reg);
GNUNET_free (reg);
}
return ret;
}
/**
* Convert an address (IPv4 or IPv6) to a regex.
*
* @param addr address
* @param mask network mask
* @param len number of bytes in @a addr and @a mask
* @return NULL on error, otherwise regex for the address
*/
static char *
address_to_regex (const void *addr,
const void *mask,
size_t len)
{
const uint16_t *a = addr;
const uint16_t *m = mask;
char *ret;
char *tmp;
char *reg;
unsigned int i;
ret = NULL;
GNUNET_assert (1 != (len % 2));
for (i=0;i<len / 2;i++)
{
reg = num_to_regex (a[i], m[i]);
if (NULL == reg)
{
GNUNET_free_non_null (ret);
return NULL;
}
if (NULL == ret)
{
ret = reg;
}
else
{
GNUNET_asprintf (&tmp,
"%s%s",
ret, reg);
GNUNET_free (ret);
GNUNET_free (reg);
ret = tmp;
}
}
return ret;
}
/**
* Convert a single line of an IPv4 policy to a regular expression.
*
* @param v4 line to convert
* @return NULL on error
*/
static char *
ipv4_to_regex (const struct GNUNET_STRINGS_IPv4NetworkPolicy *v4)
{
char *reg;
char *pp;
char *ret;
reg = address_to_regex (&v4->network,
&v4->netmask,
sizeof (struct in_addr));
if (NULL == reg)
return NULL;
pp = port_to_regex (&v4->pp);
if (NULL == pp)
{
GNUNET_free (reg);
return NULL;
}
GNUNET_asprintf (&ret,
"4-%s-%s",
pp, reg);
GNUNET_free (pp);
GNUNET_free (reg);
return ret;
}
/**
* Convert a single line of an IPv4 policy to a regular expression.
*
* @param v6 line to convert
* @return NULL on error
*/
static char *
ipv6_to_regex (const struct GNUNET_STRINGS_IPv6NetworkPolicy *v6)
{
char *reg;
char *pp;
char *ret;
reg = address_to_regex (&v6->network,
&v6->netmask,
sizeof (struct in6_addr));
if (NULL == reg)
return NULL;
pp = port_to_regex (&v6->pp);
if (NULL == pp)
{
GNUNET_free (reg);
return NULL;
}
GNUNET_asprintf (&ret,
"6-%s-%s",
pp, reg);
GNUNET_free (pp);
GNUNET_free (reg);
return ret;
}
/**
* Convert an exit policy to a regular expression. The exit policy
* specifies a set of subnets this peer is willing to serve as an
* exit for; the resulting regular expression will match the
* IPv4 address strings as returned by #GNUNET_TUN_ipv4toregexsearch().
*
* @param policy exit policy specification
* @return regular expression, NULL on error
*/
char *
GNUNET_TUN_ipv4policy2regex (const char *policy)
{
struct GNUNET_STRINGS_IPv4NetworkPolicy *np;
char *reg;
char *tmp;
char *line;
unsigned int i;
np = GNUNET_STRINGS_parse_ipv4_policy (policy);
if (NULL == np)
return NULL;
reg = NULL;
for (i=0; (0 == i) || (0 != np[i].network.s_addr); i++)
{
line = ipv4_to_regex (&np[i]);
if (NULL == line)
{
GNUNET_free_non_null (reg);
GNUNET_free (np);
return NULL;
}
if (NULL == reg)
{
reg = line;
}
else
{
GNUNET_asprintf (&tmp,
"%s|(%s)",
reg, line);
GNUNET_free (reg);
GNUNET_free (line);
reg = tmp;
}
if (0 == np[i].network.s_addr)
break;
}
GNUNET_free (np);
return reg;
}
/**
* Convert an exit policy to a regular expression. The exit policy
* specifies a set of subnets this peer is willing to serve as an
* exit for; the resulting regular expression will match the
* IPv6 address strings as returned by #GNUNET_TUN_ipv6toregexsearch().
*
* @param policy exit policy specification
* @return regular expression, NULL on error
*/
char *
GNUNET_TUN_ipv6policy2regex (const char *policy)
{
struct in6_addr zero;
struct GNUNET_STRINGS_IPv6NetworkPolicy *np;
char *reg;
char *tmp;
char *line;
unsigned int i;
np = GNUNET_STRINGS_parse_ipv6_policy (policy);
if (NULL == np)
return NULL;
reg = NULL;
memset (&zero, 0, sizeof (struct in6_addr));
for (i=0; (0 == i) || (0 != memcmp (&zero, &np[i].network, sizeof (struct in6_addr))); i++)
{
line = ipv6_to_regex (&np[i]);
if (NULL == line)
{
GNUNET_free_non_null (reg);
GNUNET_free (np);
return NULL;
}
if (NULL == reg)
{
reg = line;
}
else
{
GNUNET_asprintf (&tmp,
"%s|(%s)",
reg, line);
GNUNET_free (reg);
GNUNET_free (line);
reg = tmp;
}
if (0 == memcmp (&zero, &np[i].network, sizeof (struct in6_addr)))
break;
}
GNUNET_free (np);
return reg;
}
/**
* Hash the service name of a hosted service to the
* hash code that is used to identify the service on
* the network.
*
* @param service_name a string
* @param hc corresponding hash
*/
void
GNUNET_TUN_service_name_to_hash (const char *service_name,
struct GNUNET_HashCode *hc)
{
GNUNET_CRYPTO_hash (service_name,
strlen (service_name),
hc);
}
/**
* Compute the CADET port given a service descriptor
* (returned from #GNUNET_TUN_service_name_to_hash) and
* a TCP/UDP port @a ip_port.
*
* @param desc service shared secret
* @param ip_port TCP/UDP port, use 0 for ICMP
* @param[out] cadet_port CADET port to use
*/
void
GNUNET_TUN_compute_service_cadet_port (const struct GNUNET_HashCode *desc,
uint16_t ip_port,
struct GNUNET_HashCode *cadet_port)
{
uint16_t be_port = htons (ip_port);
*cadet_port = *desc;
GNUNET_memcpy (cadet_port,
&be_port,
sizeof (uint16_t));
}
/* end of regex.c */
|