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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
|
# $Id$
#
# Copyright (C) 2013--2014 Dragon Research Labs ("DRL")
# Portions copyright (C) 2009--2012 Internet Systems Consortium ("ISC")
# Portions copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN")
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND DRL, ISC, AND ARIN DISCLAIM ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DRL,
# ISC, OR ARIN BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
Classes dealing with sets of resources.
The basic mechanics of a resource set are the same for any of the
resources we handle (ASNs, IPv4 addresses, or IPv6 addresses), so we
can provide the same operations on any of them, even though the
underlying details vary.
We also provide some basic set operations (union, intersection, etc).
"""
import re
import math
import rpki.exceptions
import rpki.POW
## @var inherit_token
# Token used to indicate inheritance in read and print syntax.
inherit_token = "<inherit>"
re_asn_range = re.compile("^([0-9]+)-([0-9]+)$")
re_address_range = re.compile("^([0-9:.a-fA-F]+)-([0-9:.a-fA-F]+)$")
re_prefix_with_maxlen = re.compile("^([0-9:.a-fA-F]+)/([0-9]+)-([0-9]+)$")
re_prefix = re.compile("^([0-9:.a-fA-F]+)/([0-9]+)$")
class resource_range(object):
"""
Generic resource range type. Assumes underlying type is some kind
of integer.
This is a virtual class. You probably don't want to use this type
directly.
"""
# Give pylint a little help here
datum_type = int
parse_str = int
def __init__(self, range_min, range_max):
assert range_min.__class__ is range_max.__class__, \
"Type mismatch, %r doesn't match %r" % (range_min.__class__, range_max.__class__)
assert range_min <= range_max, "Mis-ordered range: %s before %s" % (range_min, range_max)
self.min = range_min
self.max = range_max
def __cmp__(self, other):
assert self.__class__ is other.__class__, \
"Type mismatch, comparing %r with %r" % (self.__class__, other.__class__)
return cmp(self.min, other.min) or cmp(self.max, other.max)
class resource_range_as(resource_range):
"""
Range of Autonomous System Numbers.
Denotes a single ASN by a range whose min and max values are
identical.
"""
## @var datum_type
# Type of underlying data (min and max).
datum_type = long
def __init__(self, range_min, range_max):
resource_range.__init__(self,
long(range_min) if isinstance(range_min, int) else range_min,
long(range_max) if isinstance(range_max, int) else range_max)
def __str__(self):
"""
Convert a resource_range_as to string format.
"""
if self.min == self.max:
return str(self.min)
else:
return str(self.min) + "-" + str(self.max)
@classmethod
def parse_str(cls, x):
"""
Parse ASN resource range from text (eg, XML attributes).
"""
r = re_asn_range.match(x)
if r:
return cls(long(r.group(1)), long(r.group(2)))
else:
return cls(long(x), long(x))
@classmethod
def from_strings(cls, a, b = None):
"""
Construct ASN range from strings.
"""
if b is None:
b = a
return cls(long(a), long(b))
class resource_range_ip(resource_range):
"""
Range of (generic) IP addresses.
Prefixes are converted to ranges on input, and ranges that can be
represented as prefixes are written as prefixes on output.
This is a virtual class. You probably don't want to use it
directly.
"""
## @var datum_type
# Type of underlying data (min and max).
datum_type = rpki.POW.IPAddress
# Give pylint a little help here
version = None
def prefixlen(self):
"""
Determine whether a resource_range_ip can be expressed as a
prefix. Returns prefix length if it can, otherwise raises
MustBePrefix exception.
"""
mask = self.min ^ self.max
if self.min & mask != 0:
raise rpki.exceptions.MustBePrefix
prefixlen = self.min.bits
while mask & 1:
prefixlen -= 1
mask >>= 1
if mask:
raise rpki.exceptions.MustBePrefix
return prefixlen
@property
def can_be_prefix(self):
"""
Boolean property indicating whether this range can be expressed as
a prefix.
This just calls .prefixlen() to do the work, so that we can keep
the logic in one place. This property is useful primarily in
context where catching an exception isn't practical.
"""
try:
self.prefixlen()
return True
except rpki.exceptions.MustBePrefix:
return False
def __str__(self):
"""
Convert a resource_range_ip to string format.
"""
try:
return str(self.min) + "/" + str(self.prefixlen())
except rpki.exceptions.MustBePrefix:
return str(self.min) + "-" + str(self.max)
@classmethod
def parse_str(cls, x):
"""
Parse IP address range or prefix from text (eg, XML attributes).
"""
r = re_address_range.match(x)
if r:
return cls.from_strings(r.group(1), r.group(2))
r = re_prefix.match(x)
if r:
a = rpki.POW.IPAddress(r.group(1))
if cls is resource_range_ip and a.version == 4:
cls = resource_range_ipv4
if cls is resource_range_ip and a.version == 6:
cls = resource_range_ipv6
return cls.make_prefix(a, int(r.group(2)))
raise rpki.exceptions.BadIPResource('Bad IP resource "%s"' % x)
@classmethod
def make_prefix(cls, prefix, prefixlen):
"""
Construct a resource range corresponding to a prefix.
"""
assert isinstance(prefix, rpki.POW.IPAddress) and isinstance(prefixlen, (int, long))
assert prefixlen >= 0 and prefixlen <= prefix.bits, "Nonsensical prefix length: %s" % prefixlen
mask = (1 << (prefix.bits - prefixlen)) - 1
assert (prefix & mask) == 0, "Resource not in canonical form: %s/%s" % (prefix, prefixlen)
return cls(prefix, rpki.POW.IPAddress(prefix | mask))
def chop_into_prefixes(self, result):
"""
Chop up a resource_range_ip into ranges that can be represented as
prefixes.
"""
try:
self.prefixlen()
result.append(self)
except rpki.exceptions.MustBePrefix:
range_min = self.min
range_max = self.max
while range_max >= range_min:
bits = int(math.log(long(range_max - range_min + 1), 2))
while True:
mask = ~(~0 << bits)
assert range_min + mask <= range_max
if range_min & mask == 0:
break
assert bits > 0
bits -= 1
result.append(self.make_prefix(range_min, range_min.bits - bits))
range_min = range_min + mask + 1
@classmethod
def from_strings(cls, a, b = None):
"""
Construct IP address range from strings.
"""
if b is None:
b = a
a = rpki.POW.IPAddress(a)
b = rpki.POW.IPAddress(b)
if a.version != b.version:
raise TypeError
if cls is resource_range_ip:
if a.version == 4:
return resource_range_ipv4(a, b)
if a.version == 6:
return resource_range_ipv6(a, b)
elif a.version == cls.version:
return cls(a, b)
else:
raise TypeError
class resource_range_ipv4(resource_range_ip):
"""
Range of IPv4 addresses.
"""
version = 4
class resource_range_ipv6(resource_range_ip):
"""
Range of IPv6 addresses.
"""
version = 6
def _rsplit(rset, that):
"""
Utility function to split a resource range into two resource ranges.
"""
this = rset.pop(0)
assert type(this) is type(that), "type(this) [%r] is not type(that) [%r]" % (type(this), type(that))
assert type(this.min) is type(that.min), "type(this.min) [%r] is not type(that.min) [%r]" % (type(this.min), type(that.min))
assert type(this.min) is type(this.max), "type(this.min) [%r] is not type(this.max) [%r]" % (type(this.min), type(this.max))
assert type(that.min) is type(that.max), "type(that.min) [%r] is not type(that.max) [%r]" % (type(that.min), type(that.max))
if this.min < that.min:
rset.insert(0, type(this)(this.min, type(that.min)(that.min - 1)))
rset.insert(1, type(this)(that.min, this.max))
else:
assert this.max > that.max
rset.insert(0, type(this)(this.min, that.max))
rset.insert(1, type(this)(type(that.max)(that.max + 1), this.max))
class resource_set(list):
"""
Generic resource set, a list subclass containing resource ranges.
This is a virtual class. You probably don't want to use it
directly.
"""
## @var inherit
# Boolean indicating whether this resource_set uses RFC 3779 inheritance.
inherit = False
## @var canonical
# Whether this resource_set is currently in canonical form.
canonical = False
# Give pylint a little help here
range_type = resource_range
def __init__(self, ini = None, allow_overlap = False):
"""
Initialize a resource_set.
"""
list.__init__(self)
if isinstance(ini, (int, long)):
ini = str(ini)
if ini is inherit_token:
self.inherit = True
elif isinstance(ini, (str, unicode)) and len(ini):
self.extend(self.parse_str(s) for s in ini.split(","))
elif isinstance(ini, list):
self.extend(ini)
elif ini is not None and ini != "":
raise ValueError("Unexpected initializer: %s" % str(ini))
self.canonize(allow_overlap)
def canonize(self, allow_overlap = False):
"""
Whack this resource_set into canonical form.
"""
assert not self.inherit or len(self) == 0
if not self.canonical:
self.sort()
i = 0
while i + 1 < len(self):
if allow_overlap and self[i].max + 1 >= self[i+1].min:
self[i] = type(self[i])(self[i].min, max(self[i].max, self[i+1].max))
del self[i+1]
elif self[i].max + 1 == self[i+1].min:
self[i] = type(self[i])(self[i].min, self[i+1].max)
del self[i+1]
else:
i += 1
for i in xrange(0, len(self) - 1):
if self[i].max >= self[i+1].min:
raise rpki.exceptions.ResourceOverlap("Resource overlap: %s %s" % (self[i], self[i+1]))
self.canonical = True
def append(self, item):
"""
Wrapper around list.append() (q.v.) to reset canonical flag.
"""
list.append(self, item)
self.canonical = False
def extend(self, item):
"""
Wrapper around list.extend() (q.v.) to reset canonical flag.
"""
list.extend(self, item)
self.canonical = False
def __str__(self):
"""
Convert a resource_set to string format.
"""
if self.inherit:
return inherit_token
else:
return ",".join(str(x) for x in self)
def _comm(self, other):
"""
Like comm(1), sort of.
Returns a tuple of three resource sets: resources only in self,
resources only in other, and resources in both. Used (not very
efficiently) as the basis for most set operations on resource
sets.
"""
assert not self.inherit
assert type(self) is type(other), "Type mismatch %r %r" % (type(self), type(other))
set1 = type(self)(self) # clone and whack into canonical form
set2 = type(other)(other) # ditto
only1, only2, both = [], [], []
while set1 or set2:
if set1 and (not set2 or set1[0].max < set2[0].min):
only1.append(set1.pop(0))
elif set2 and (not set1 or set2[0].max < set1[0].min):
only2.append(set2.pop(0))
elif set1[0].min < set2[0].min:
_rsplit(set1, set2[0])
elif set2[0].min < set1[0].min:
_rsplit(set2, set1[0])
elif set1[0].max < set2[0].max:
_rsplit(set2, set1[0])
elif set2[0].max < set1[0].max:
_rsplit(set1, set2[0])
else:
assert set1[0].min == set2[0].min and set1[0].max == set2[0].max
both.append(set1.pop(0))
set2.pop(0)
return type(self)(only1), type(self)(only2), type(self)(both)
def union(self, other):
"""
Set union for resource sets.
"""
assert not self.inherit
assert type(self) is type(other), "Type mismatch: %r %r" % (type(self), type(other))
set1 = type(self)(self) # clone and whack into canonical form
set2 = type(other)(other) # ditto
result = []
while set1 or set2:
if set1 and (not set2 or set1[0].max < set2[0].min):
result.append(set1.pop(0))
elif set2 and (not set1 or set2[0].max < set1[0].min):
result.append(set2.pop(0))
else:
this = set1.pop(0)
that = set2.pop(0)
assert type(this) is type(that)
range_min = min(this.min, that.min)
range_max = max(this.max, that.max)
result.append(type(this)(range_min, range_max))
while set1 and set1[0].max <= range_max:
assert set1[0].min >= range_min
del set1[0]
while set2 and set2[0].max <= range_max:
assert set2[0].min >= range_min
del set2[0]
return type(self)(result)
__or__ = union
def intersection(self, other):
"""
Set intersection for resource sets.
"""
return self._comm(other)[2]
__and__ = intersection
def difference(self, other):
"""
Set difference for resource sets.
"""
return self._comm(other)[0]
__sub__ = difference
def symmetric_difference(self, other):
"""
Set symmetric difference (XOR) for resource sets.
"""
com = self._comm(other)
return com[0] | com[1]
__xor__ = symmetric_difference
def contains(self, item):
"""
Set membership test for resource sets.
"""
assert not self.inherit
self.canonize()
if not self:
return False
if type(item) is type(self[0]):
range_min = item.min
range_max = item.max
else:
range_min = item
range_max = item
lo = 0
hi = len(self)
while lo < hi:
mid = (lo + hi) / 2
if self[mid].max < range_max:
lo = mid + 1
else:
hi = mid
return lo < len(self) and self[lo].min <= range_min and self[lo].max >= range_max
__contains__ = contains
def issubset(self, other):
"""
Test whether self is a subset (possibly improper) of other.
"""
for i in self:
if not other.contains(i):
return False
return True
__le__ = issubset
def issuperset(self, other):
"""
Test whether self is a superset (possibly improper) of other.
"""
return other.issubset(self)
__ge__ = issuperset
def __lt__(self, other):
return not self.issuperset(other)
def __gt__(self, other):
return not self.issubset(other)
def __ne__(self, other):
"""
A set with the inherit bit set is always unequal to any other set, because
we can't know the answer here. This is also consistent with __nonzero__
which returns True for inherit sets, and False for empty sets.
"""
return self.inherit or other.inherit or list.__ne__(self, other)
def __eq__(self, other):
return not self.__ne__(other)
def __nonzero__(self):
"""
Tests whether or not this set is empty. Note that sets with the inherit
bit set are considered non-empty, despite having zero length.
"""
return self.inherit or len(self)
@classmethod
def from_django(cls, iterable):
"""
Create resource set from a Django query.
iterable is something which returns (min, max) pairs.
"""
return cls(ini = [cls.range_type(cls.range_type.datum_type(b),
cls.range_type.datum_type(e))
for (b, e) in iterable])
@classmethod
def parse_str(cls, s):
"""
Parse resource set from text string (eg, XML attributes). This is
a backwards compatability wrapper, real functionality is now part
of the range classes.
"""
return cls.range_type.parse_str(s)
class resource_set_as(resource_set):
"""
Autonomous System Number resource set.
"""
## @var range_type
# Type of range underlying this type of resource_set.
range_type = resource_range_as
class resource_set_ip(resource_set):
"""
(Generic) IP address resource set.
This is a virtual class. You probably don't want to use it
directly.
"""
def to_roa_prefix_set(self):
"""
Convert from a resource set to a ROA prefix set.
"""
# pylint: disable=E1101
prefix_ranges = []
for r in self:
r.chop_into_prefixes(prefix_ranges)
return self.roa_prefix_set_type([
self.roa_prefix_set_type.prefix_type(r.min, r.prefixlen())
for r in prefix_ranges])
class resource_set_ipv4(resource_set_ip):
"""
IPv4 address resource set.
"""
## @var range_type
# Type of range underlying this type of resource_set.
range_type = resource_range_ipv4
class resource_set_ipv6(resource_set_ip):
"""
IPv6 address resource set.
"""
## @var range_type
# Type of range underlying this type of resource_set.
range_type = resource_range_ipv6
class resource_bag(object):
"""
Container to simplify passing around the usual triple of ASN, IPv4,
and IPv6 resource sets.
"""
## @var asn
# Set of Autonomous System Number resources.
## @var v4
# Set of IPv4 resources.
## @var v6
# Set of IPv6 resources.
## @var valid_until
# Expiration date of resources, for setting certificate notAfter field.
def __init__(self, asn = None, v4 = None, v6 = None, valid_until = None):
self.asn = asn or resource_set_as()
self.v4 = v4 or resource_set_ipv4()
self.v6 = v6 or resource_set_ipv6()
self.valid_until = valid_until
def oversized(self, other):
"""
True iff self is oversized with respect to other.
"""
return not self.asn.issubset(other.asn) or \
not self.v4.issubset(other.v4) or \
not self.v6.issubset(other.v6)
def undersized(self, other):
"""
True iff self is undersized with respect to other.
"""
return not other.asn.issubset(self.asn) or \
not other.v4.issubset(self.v4) or \
not other.v6.issubset(self.v6)
@classmethod
def from_inheritance(cls):
"""
Build a resource bag that just inherits everything from its
parent.
"""
self = cls()
self.asn = resource_set_as()
self.v4 = resource_set_ipv4()
self.v6 = resource_set_ipv6()
self.asn.inherit = True
self.v4.inherit = True
self.v6.inherit = True
return self
@classmethod
def from_str(cls, text, allow_overlap = False):
"""
Parse a comma-separated text string into a resource_bag. Not
particularly efficient, fix that if and when it becomes an issue.
"""
asns = []
v4s = []
v6s = []
for word in text.split(","):
if "." in word:
v4s.append(word)
elif ":" in word:
v6s.append(word)
else:
asns.append(word)
return cls(asn = resource_set_as(",".join(asns), allow_overlap) if asns else None,
v4 = resource_set_ipv4(",".join(v4s), allow_overlap) if v4s else None,
v6 = resource_set_ipv6(",".join(v6s), allow_overlap) if v6s else None)
@classmethod
def from_POW_rfc3779(cls, resources):
"""
Build a resource_bag from data returned by
rpki.POW.X509.getRFC3779().
The conversion to long for v4 and v6 is (intended to be)
temporary: in the long run, we should be using rpki.POW.IPAddress
rather than long here.
"""
asn = inherit_token if resources[0] == "inherit" else [resource_range_as( r[0], r[1]) for r in resources[0] or ()]
v4 = inherit_token if resources[1] == "inherit" else [resource_range_ipv4(r[0], r[1]) for r in resources[1] or ()]
v6 = inherit_token if resources[2] == "inherit" else [resource_range_ipv6(r[0], r[1]) for r in resources[2] or ()]
return cls(resource_set_as(asn) if asn else None,
resource_set_ipv4(v4) if v4 else None,
resource_set_ipv6(v6) if v6 else None)
def empty(self):
"""
True iff all resource sets in this bag are empty.
"""
return not self.asn and not self.v4 and not self.v6
def __nonzero__(self):
return not self.empty()
def __eq__(self, other):
return self.asn == other.asn and \
self.v4 == other.v4 and \
self.v6 == other.v6 and \
self.valid_until == other.valid_until
def __ne__(self, other):
return not (self == other) # pylint: disable=C0325
def intersection(self, other):
"""
Compute intersection with another resource_bag. valid_until
attribute (if any) inherits from self.
"""
return self.__class__(self.asn & other.asn,
self.v4 & other.v4,
self.v6 & other.v6,
self.valid_until)
__and__ = intersection
def union(self, other):
"""
Compute union with another resource_bag. valid_until attribute
(if any) inherits from self.
"""
return self.__class__(self.asn | other.asn,
self.v4 | other.v4,
self.v6 | other.v6,
self.valid_until)
__or__ = union
def difference(self, other):
"""
Compute difference against another resource_bag. valid_until
attribute (if any) inherits from self
"""
return self.__class__(self.asn - other.asn,
self.v4 - other.v4,
self.v6 - other.v6,
self.valid_until)
__sub__ = difference
def symmetric_difference(self, other):
"""
Compute symmetric difference against another resource_bag.
valid_until attribute (if any) inherits from self
"""
return self.__class__(self.asn ^ other.asn,
self.v4 ^ other.v4,
self.v6 ^ other.v6,
self.valid_until)
__xor__ = symmetric_difference
def __str__(self):
s = ""
if self.asn:
s += "ASN: %s" % self.asn
if self.v4:
if s:
s += ", "
s += "V4: %s" % self.v4
if self.v6:
if s:
s += ", "
s += "V6: %s" % self.v6
return s
def __iter__(self):
for r in self.asn:
yield r
for r in self.v4:
yield r
for r in self.v6:
yield r
# Sadly, there are enough differences between RFC 3779 and the data
# structures in the latest proposed ROA format that we can't just use
# the RFC 3779 code for ROAs. So we need a separate set of classes
# that are similar in concept but different in detail, with conversion
# functions. Such is life. I suppose it might be possible to do this
# with multiple inheritance, but that's probably more bother than it's
# worth.
class roa_prefix(object):
"""
ROA prefix. This is similar to the resource_range_ip class, but
differs in that it only represents prefixes, never ranges, and
includes the maximum prefix length as an additional value.
This is a virtual class, you probably don't want to use it directly.
"""
## @var prefix
# The prefix itself, an IP address with bits beyond the prefix
# length zeroed.
## @var prefixlen
# (Minimum) prefix length.
## @var max_prefixlen
# Maxmimum prefix length.
# Give pylint a little help
range_type = resource_range_ip
def __init__(self, prefix, prefixlen, max_prefixlen = None):
"""
Initialize a ROA prefix. max_prefixlen is optional and defaults
to prefixlen. max_prefixlen must not be smaller than prefixlen.
"""
if max_prefixlen is None:
max_prefixlen = prefixlen
assert max_prefixlen >= prefixlen, "Bad max_prefixlen: %d must not be shorter than %d" % (max_prefixlen, prefixlen)
self.prefix = prefix
self.prefixlen = prefixlen
self.max_prefixlen = max_prefixlen
def __cmp__(self, other):
"""
Compare two ROA prefix objects. Comparision is based on prefix,
prefixlen, and max_prefixlen, in that order.
"""
assert self.__class__ is other.__class__
return (cmp(self.prefix, other.prefix) or
cmp(self.prefixlen, other.prefixlen) or
cmp(self.max_prefixlen, other.max_prefixlen))
def __str__(self):
"""
Convert a ROA prefix to string format.
"""
if self.prefixlen == self.max_prefixlen:
return str(self.prefix) + "/" + str(self.prefixlen)
else:
return str(self.prefix) + "/" + str(self.prefixlen) + "-" + str(self.max_prefixlen)
def to_resource_range(self):
"""
Convert this ROA prefix to the equivilent resource_range_ip
object. This is an irreversable transformation because it loses
the max_prefixlen attribute, nothing we can do about that.
"""
return self.range_type.make_prefix(self.prefix, self.prefixlen)
def min(self):
"""
Return lowest address covered by prefix.
"""
return self.prefix
def max(self):
"""
Return highest address covered by prefix.
"""
return self.prefix | ((1 << (self.prefix.bits - self.prefixlen)) - 1)
def to_POW_roa_tuple(self):
"""
Convert a resource_range_ip to rpki.POW.ROA.setPrefixes() format.
"""
return self.prefix, self.prefixlen, self.max_prefixlen
@classmethod
def parse_str(cls, x):
"""
Parse ROA prefix from text (eg, an XML attribute).
"""
r = re_prefix_with_maxlen.match(x)
if r:
return cls(rpki.POW.IPAddress(r.group(1)), int(r.group(2)), int(r.group(3)))
r = re_prefix.match(x)
if r:
return cls(rpki.POW.IPAddress(r.group(1)), int(r.group(2)))
raise rpki.exceptions.BadROAPrefix('Bad ROA prefix "%s"' % x)
class roa_prefix_ipv4(roa_prefix):
"""
IPv4 ROA prefix.
"""
## @var range_type
# Type of corresponding resource_range_ip.
range_type = resource_range_ipv4
class roa_prefix_ipv6(roa_prefix):
"""
IPv6 ROA prefix.
"""
## @var range_type
# Type of corresponding resource_range_ip.
range_type = resource_range_ipv6
class roa_prefix_set(list):
"""
Set of ROA prefixes, analogous to the resource_set_ip class.
"""
# Give pylint a little help
prefix_type = roa_prefix
resource_set_type = resource_set_ip
def __init__(self, ini = None):
"""
Initialize a ROA prefix set.
"""
list.__init__(self)
if isinstance(ini, (str, unicode)) and len(ini):
self.extend(self.parse_str(s) for s in ini.split(","))
elif isinstance(ini, (list, tuple)):
self.extend(ini)
else:
assert ini is None or ini == "", "Unexpected initializer: %s" % str(ini)
self.sort()
def __str__(self):
"""
Convert a ROA prefix set to string format.
"""
return ",".join(str(x) for x in self)
@classmethod
def parse_str(cls, s):
"""
Parse ROA prefix from text (eg, an XML attribute).
This method is a backwards compatability shim.
"""
return cls.prefix_type.parse_str(s)
def to_resource_set(self):
"""
Convert a ROA prefix set to a resource set. This is an
irreversable transformation. We have to compute a union here
because ROA prefix sets can include overlaps, while RFC 3779
resource sets cannot. This is ugly, and there is almost certainly
a more efficient way to do this, but start by getting the output
right before worrying about making it fast or pretty.
"""
r = self.resource_set_type()
s = self.resource_set_type()
s.append(None)
for p in self:
s[0] = p.to_resource_range()
r |= s
return r
@classmethod
def from_sql(cls, sql, query, args = None):
"""
Create ROA prefix set from an SQL query.
sql is an object that supports execute() and fetchall() methods
like a DB API 2.0 cursor object.
query is an SQL query that returns a sequence of (prefix,
prefixlen, max_prefixlen) triples.
"""
sql.execute(query, args)
return cls([cls.prefix_type(rpki.POW.IPAddress(x), int(y), int(z))
for (x, y, z) in sql.fetchall()])
@classmethod
def from_django(cls, iterable):
"""
Create ROA prefix set from a Django query.
iterable is something which returns (prefix, prefixlen,
max_prefixlen) triples.
"""
return cls([cls.prefix_type(rpki.POW.IPAddress(x), int(y), int(z))
for (x, y, z) in iterable])
def to_POW_roa_tuple(self):
"""
Convert ROA prefix set to form used by rpki.POW.ROA.setPrefixes().
"""
if self:
return tuple(a.to_POW_roa_tuple() for a in self)
else:
return None
class roa_prefix_set_ipv4(roa_prefix_set):
"""
Set of IPv4 ROA prefixes.
"""
## @var prefix_type
# Type of underlying roa_prefix.
prefix_type = roa_prefix_ipv4
## @var resource_set_type
# Type of corresponding resource_set_ip class.
resource_set_type = resource_set_ipv4
# Fix back link from resource_set to roa_prefix
resource_set_ipv4.roa_prefix_set_type = roa_prefix_set_ipv4
class roa_prefix_set_ipv6(roa_prefix_set):
"""
Set of IPv6 ROA prefixes.
"""
## @var prefix_type
# Type of underlying roa_prefix.
prefix_type = roa_prefix_ipv6
## @var resource_set_type
# Type of corresponding resource_set_ip class.
resource_set_type = resource_set_ipv6
# Fix back link from resource_set to roa_prefix
resource_set_ipv6.roa_prefix_set_type = roa_prefix_set_ipv6
class roa_prefix_bag(object):
"""
Container to simplify passing around the combination of an IPv4 ROA
prefix set and an IPv6 ROA prefix set.
"""
## @var v4
# Set of IPv4 prefixes.
## @var v6
# Set of IPv6 prefixes.
def __init__(self, v4 = None, v6 = None):
self.v4 = v4 or roa_prefix_set_ipv4()
self.v6 = v6 or roa_prefix_set_ipv6()
def __eq__(self, other):
return self.v4 == other.v4 and self.v6 == other.v6
def __ne__(self, other):
return not (self == other) # pylint: disable=C0325
# Test suite for set operations.
if __name__ == "__main__":
def testprefix(v):
return " (%s)" % v.to_roa_prefix_set() if isinstance(v, resource_set_ip) else ""
def test1(t, s1, s2):
if isinstance(s1, (str, unicode)) and isinstance(s2, (str, unicode)):
print "x: ", s1
print "y: ", s2
r1 = t(s1)
r2 = t(s2)
print "x: ", r1, testprefix(r1)
print "y: ", r2, testprefix(r2)
v1 = r1._comm(r2) # pylint: disable=W0212
v2 = r2._comm(r1) # pylint: disable=W0212
assert v1[0] == v2[1] and v1[1] == v2[0] and v1[2] == v2[2]
assert all(i in r1 and i.min in r1 and i.max in r1 for i in r1)
assert all(i in r2 and i.min in r2 and i.max in r2 for i in r2)
assert all(i in r1 and i not in r2 for i in v1[0])
assert all(i not in r1 and i in r2 for i in v1[1])
assert all(i in r1 and i in r2 for i in v1[2])
v1 = r1 | r2
v2 = r2 | r1
assert v1 == v2
print "x|y:", v1, testprefix(v1)
v1 = r1 - r2
v2 = r2 - r1
print "x-y:", v1, testprefix(v1)
print "y-x:", v2, testprefix(v2)
v1 = r1 ^ r2
v2 = r2 ^ r1
assert v1 == v2
print "x^y:", v1, testprefix(v1)
v1 = r1 & r2
v2 = r2 & r1
assert v1 == v2
print "x&y:", v1, testprefix(v1)
def test2(t, s1, s2):
print "x: ", s1
print "y: ", s2
r1 = t(s1)
r2 = t(s2)
print "x: ", r1
print "y: ", r2
print "x>y:", (r1 > r2)
print "x<y:", (r1 < r2)
test1(t.resource_set_type,
r1.to_resource_set(),
r2.to_resource_set())
def test3(t, s1, s2):
test1(t, s1, s2)
r1 = t(s1).to_roa_prefix_set()
r2 = t(s2).to_roa_prefix_set()
print "x: ", r1
print "y: ", r2
print "x>y:", (r1 > r2)
print "x<y:", (r1 < r2)
test1(t.roa_prefix_set_type.resource_set_type,
r1.to_resource_set(),
r2.to_resource_set())
print
print "Testing set operations on resource sets"
print
test1(resource_set_as, "1,2,3,4,5,6,11,12,13,14,15", "1,2,3,4,5,6,111,121,131,141,151")
print
test1(resource_set_ipv4, "10.0.0.44/32,10.6.0.2/32", "10.3.0.0/24,10.0.0.77/32")
print
test1(resource_set_ipv4, "10.0.0.44/32,10.6.0.2/32", "10.0.0.0/24")
print
test1(resource_set_ipv4, "10.0.0.0/24", "10.3.0.0/24,10.0.0.77/32")
print
test1(resource_set_ipv4, "10.0.0.0/24", "10.0.0.0/32,10.0.0.2/32,10.0.0.4/32")
print
print "Testing set operations on ROA prefixes"
print
test2(roa_prefix_set_ipv4, "10.0.0.44/32,10.6.0.2/32", "10.3.0.0/24,10.0.0.77/32")
print
test2(roa_prefix_set_ipv4, "10.0.0.0/24-32,10.6.0.0/24-32", "10.3.0.0/24,10.0.0.0/16-32")
print
test2(roa_prefix_set_ipv4, "10.3.0.0/24-24,10.0.0.0/16-32", "10.3.0.0/24,10.0.0.0/16-32")
print
test2(roa_prefix_set_ipv6, "2002:0a00:002c::1/128", "2002:0a00:002c::2/128")
print
test2(roa_prefix_set_ipv6, "2002:0a00:002c::1/128", "2002:0a00:002c::7/128")
print
test2(roa_prefix_set_ipv6, "2002:0a00:002c::1/128", "2002:0a00:002c::/120")
print
test2(roa_prefix_set_ipv6, "2002:0a00:002c::1/128", "2002:0a00:002c::/120-128")
print
test3(resource_set_ipv4, "10.0.0.44/32,10.6.0.2/32", "10.3.0.0/24,10.0.0.77/32")
print
test3(resource_set_ipv6, "2002:0a00:002c::1/128", "2002:0a00:002c::2/128")
print
test3(resource_set_ipv6, "2002:0a00:002c::1/128", "2002:0a00:002c::/120")
|