def create_ixp_radix( basedata ): ixp_radix = Radix() for ixp_name,ixp_entry in basedata['ixps'].iteritems(): for prefix in ixp_entry['peeringlans']: node = ixp_radix.add( prefix ) node.data['name'] = ixp_name return ixp_radix
def has_announced_bigger(asn_stable_pfx, pfx): rad_tree = Radix() rad_tree.add(pfx) for p in asn_stable_pfx: if rad_tree.search_covered(p): return True return False
def create_ixp_radix(conf): ixp_radix = Radix() for ixp in conf['ixps']: for prefix in ixp['peeringlans']: node = ixp_radix.add(prefix) node.data['name'] = ixp['name'] return ixp_radix
def radix_tree(self) -> Radix: """Return a radix tree (from the py-radix library) for fast IP-IX lookups.""" rtree = Radix() for obj in self.objects: for prefix in obj.prefixes: rnode = rtree.add(prefix.prefix) rnode.data["ix"] = obj.ix return rtree
def load_bgp_file(ymd): bgp_fn = c.bgp_fn(ymd) # Find suitable BGP file print("bgp_fn = %s" % bgp_fn) bfa = bgp_fn.split('.') print("bfa = %s" % bfa) bfa_files = glob.glob(bfa[0] + '*') print("bfa_files %s" % bfa_files) f_hours = 0 for fn in bfa_files: print(" %s" % fn) if fn.index(bfa[0]) == 0: print(" date matches") fna = fn.split('.') print("fna %s" % fna) if bfa[1] == fna[1]: print(" exact match - use it") break else: print(" Different number of hours, OK") bgp_fn = fn print(" using file %s <<<" % bgp_fn) rq_date_s = c.start_ymd n = 0 rtree = Radix() # Load the BGP file into rtree with gzip.open(bgp_fn, 'r') as zif: tb_n = 0 tb = None for ln in zif: n += 1 line = ln.decode("utf-8", "replace") la = line.strip().split() if len(la) != 2: # BGP record had no Origin AS ! print("line len != 2 >%s<" % la) else: pfx = str(la[0]) origin = str(la[1]) try: rnode = rtree.search_exact(pfx) if rnode: rnode.data['origin'].add(origin) else: rnode = rtree.add(pfx) rnode.data['origin'] = set([origin]) except: print("search_exact failed for pfx = %s" % pfx) if n % 20000 == 0: print(".", end='') sys.stdout.flush() sys.stderr.write("finished loading BGP data\n") sys.stderr.write("Loaded %d BGP lines" % n) return rtree
def init_result(): ''' initialises the structure that hold data for collector peers to be compared ''' r = {} for who in (1, 2): r[who] = {} r[who]['radix'] = Radix() # holds the radix trees for both r[who]['path_len_cnt'] = Counter() # path length counter r[who]['path_asn_cnt'] = Counter( ) # number of ASNs counter (different from path length because of prepending r[who][ 'asn_xpending'] = 0 # covers inpending, prepending (ie. where path_len != asn_count return r
def __init__(me, fpath_geoip, fpath_known_networks): # load known networks into radix tree me.rt = Radix() with open(fpath_known_networks, 'rb') as known_net_file: known_networks = json.load(known_net_file) for p in known_networks: # Only keep prefixes that we know their country if 'country' in p: n = me.rt.add(p['net']) n.data['cc'] = p['country'] # load geoIP data me.gi = GeoIP.open(fpath_geoip, GeoIP.GEOIP_STANDARD)
def __init__(self): """Create a BGP protocol. """ self.fsm = None self.peer_id = None self.disconnected = False self._receive_buffer = b'' self.fourbytesas = False self.add_path_ipv4_receive = False self.add_path_ipv4_send = False self.adj_rib_in = {k: {} for k in CONF.bgp.afi_safi} self.adj_rib_out = {k: {} for k in CONF.bgp.afi_safi} self.adj_rib_in_ipv4_tree = Radix() # statistic self.msg_sent_stat = { 'Opens': 0, 'Notifications': 0, 'Updates': 0, 'Keepalives': 0, 'RouteRefresh': 0 } self.msg_recv_stat = { 'Opens': 0, 'Notifications': 0, 'Updates': 0, 'Keepalives': 0, 'RouteRefresh': 0 } self.send_version = { 'ipv4': 0, 'flowspec': 0, 'sr_policy': 0, 'mpls_vpn': 0 } self.receive_version = { 'ipv4': 0, 'flowspec': 0, 'sr_policy': 0, 'mpls_vpn': 0 } self.flowspec_send_dict = {} self.flowspec_receive_dict = {} self.sr_send_dict = {} self.sr_receive_dict = {} self.mpls_vpn_send_dict = {} self.mpls_vpn_receive_dict = {}
def __init__(self): # Patricia trie for reserved prefixes ipv4 self.__reserved_tree_ipv4 = Radix() self.__reserved_tree_ipv4.add("0.0.0.0/8") self.__reserved_tree_ipv4.add("1.1.1.0/24") self.__reserved_tree_ipv4.add("10.0.0.0/8") self.__reserved_tree_ipv4.add("100.64.0.0/10") self.__reserved_tree_ipv4.add("127.0.0.0/8") self.__reserved_tree_ipv4.add("169.254.0.0/16") self.__reserved_tree_ipv4.add("172.16.0.0/12") self.__reserved_tree_ipv4.add("192.0.0.0/24") self.__reserved_tree_ipv4.add("192.0.2.0/24") self.__reserved_tree_ipv4.add("192.88.99.0/24") self.__reserved_tree_ipv4.add("192.168.0.0/16") self.__reserved_tree_ipv4.add("198.18.0.0/15") self.__reserved_tree_ipv4.add("198.51.100.0/24") self.__reserved_tree_ipv4.add("203.0.113.0/24") self.__reserved_tree_ipv4.add("224.0.0.0/4") self.__reserved_tree_ipv4.add("240.0.0.0/4") self.__reserved_tree_ipv4.add("255.255.255.255/32") # routable address space self.__routable_tree_ipv4 = Radix()
def test_fill_ro_struct(): file = os.path.join(PATH, "conflict_annotation", "inputs", "ro_file") rad_tree = Radix() fill_ro_struct(file, rad_tree) res = list() for node in rad_tree: res.append((node.prefix, node.data)) assert res == [('60.136.0.0/16', { 17676: {'jpnic'} }), ('60.145.0.0/16', { 17676: {'jpnic'} }), ('60.145.0.0/24', { 17676: {'jpnic'} }), ('192.0.0.0/16', { 17: {'jpnic'} })]
def test_fill_roa_struct(): file = os.path.join(PATH, "conflict_annotation", "inputs", "roa_file") rad_tree = Radix() fill_roa_struct(file, rad_tree) res = list() for node in rad_tree: res.append((node.prefix, node.data)) assert res == [('86.63.224.0/19', { 35238: 19 }), ('91.91.0.0/16', { 35238: 16 }), ('192.113.0.0/16', { 16074: 16 }), ('194.209.159.0/24', { 16079: 32 }), ('212.234.194.0/24', { 16071: 28, 16072: 24 })]
def test_annotate_if_valid_both(): file = os.path.join(PATH, "conflict_annotation", "inputs", "ro_file") ro_rad_tree = Radix() fill_ro_struct(file, ro_rad_tree) input_dict = { "timestamp": 1445817600.0, "collector": "rrc01", "peer_as": 13030, "peer_ip": "195.66.224.175", "type": "F", "announce": { "prefix": "60.145.0.0/28", "asn": 17676, "as_path": "13030 3491 4651 9737 23969" }, "conflict_with": { "prefix": "192.0.0.0", "asn": 17 }, "asn": 17676 } expected = { "timestamp": 1445817600.0, "collector": "rrc01", "peer_as": 13030, "peer_ip": "195.66.224.175", "type": "F", "announce": { "prefix": "60.145.0.0/28", "asn": 17676, "valid": ['jpnic'], "as_path": "13030 3491 4651 9737 23969" }, "conflict_with": { "prefix": "192.0.0.0", "asn": 17, "valid": ['jpnic'] }, "asn": 17676 } annotate_if_route_objects(ro_rad_tree, input_dict) assert input_dict == expected
def test_annotate_if_valid_rpki_ok1(): file = os.path.join(PATH, "conflict_annotation", "inputs", "roa_file") rpki_rad_tree = Radix() fill_roa_struct(file, rpki_rad_tree) input_dict = { "timestamp": 1445817600.0, "collector": "rrc01", "peer_as": 13030, "peer_ip": "195.66.224.175", "type": "F", "announce": { "prefix": "212.234.194.0/24", "asn": 16071, "as_path": "13030 3491 4651 9737 23969" }, "conflict_with": { "prefix": "212.234.194.0/24", "asn": 16072 }, "asn": 16072 } expected = { "timestamp": 1445817600.0, "collector": "rrc01", "peer_as": 13030, "peer_ip": "195.66.224.175", "type": "F", "announce": { "prefix": "212.234.194.0/24", "asn": 16071, "valid": ["roa"], "as_path": "13030 3491 4651 9737 23969" }, "conflict_with": { "prefix": "212.234.194.0/24", "asn": 16072, "valid": ["roa"] }, "asn": 16072 } annotate_if_roa(rpki_rad_tree, input_dict) assert input_dict == expected
def __init__(self): self.radixRib = Radix() self.pathselection = PathSelection()
from collections import defaultdict, Counter import re import GeoIP import itertools from networkx.readwrite import json_graph import random import os import datetime from radix import Radix import math probe_addr = dict() g = GeoIP.open("data/GeoIPASNum.dat", GeoIP.GEOIP_STANDARD) remapped_addr = {} rt = Radix() net_idx = {} known_as = {} def dpath(fname): return os.path.join(args.datadir, fname) def name_hop(node, probe_id, seq): if node == "unknown_hop": return "Hop %s-%s" % (probe_id, seq) try: if ipaddr.IPv4Address(node).is_private: return "Private %s-%s" % (probe_id, seq)
def __init__(self): # initialize a radix tree self.rtree = Radix() # initialize an asn dict self.asn = {}
def __init__(self): self.radix = Radix() self.peers = dict()
print_naked_characteristics(r, missing1_naked, missing2_naked) print_path_len_stats(r, len(pfxset1), len(pfxset2)) print_up_path_similarities(r, overlap) CMD_BGPDUMP = "/Users/emile/bin/bgpdump" MAX_REPORTED_PATH_LEN = 6 if __name__ == '__main__': main() sys.exit(0) ### OLD peers = Counter() tree = {1: Radix(), 2: Radix()} meta = {} for who in (1, 2): meta[who] = {} meta[who]['age'] = Counter() meta[1]['asn'] = ASN1 meta[2]['asn'] = ASN2 def aap(): last_change_ts = int(fields[1]) ts_5m = (last_change_ts / 300) * 300 pfx = fields[5] asn = fields[6] meta[who]['age'][ts_5m] += 1 asns = asn.split(" ")
print("Insertion: \t{}s".format(obj.time_taken)) arr_copy = arr.copy() from merge import MergeSort obj = MergeSort(arr_copy) obj.sort() print("Merge Sort: \t{}s".format(obj.time_taken)) arr_copy = arr.copy() from heap import HeapSort obj = HeapSort(arr_copy) obj.sort() print("Heap Sort: \t{}s".format(obj.time_taken)) arr_copy = arr.copy() from bst import Bst obj = Bst(arr_copy) obj.sort() print("BST Sort: \t{}s".format(obj.time_taken)) arr_copy = arr.copy() from avl import AVL obj = AVL(arr_copy) obj.sort() print("AVL Sort: \t{}s".format(obj.time_taken)) arr_copy = arr.copy() from radix import Radix obj = Radix(arr_copy) obj.sort() print("Radix Sort: \t{}s".format(obj.time_taken))
def radix_tree(self): rtree = Radix() for prefix, origins in self._data: rnode = rtree.add(prefix) rnode.data["origins"] = origins return rtree
def __init__(self): self.radixRib = Radix()
def __init__(self): self.radixRib = Radix() self.policy = PolicyHandler()
.format(size, hex(version_ihl), identifier, hex(protocol), socket.inet_ntoa(src), socket.inet_ntoa(dst), match)) if protocol is 0x01: # ICMP protocol icmp_parse(packet[20:]) os.write(fd, packet) elif event & select.EPOLLOUT: print("EPOLLOUT") packet = os.read(fineno, 1024) print(len(packet), packet) if __name__ == '__main__': rtree = Radix() if os.geteuid() != 0: print("Need root privileges.") exit(0) ftun, mtu = tun_alloc(TUN_IFNAME.encode()) with open(IP_FILE) as f: route_prepare(rtree, f.readlines()) try: monitor_prepare(TUN_IFNAME) loop(ftun, mtu, rtree) except Exception: print(traceback.format_exc()) finally: clean_prepare(TUN_IFNAME)