-
Notifications
You must be signed in to change notification settings - Fork 4
/
manrs_core.py
345 lines (199 loc) · 5.97 KB
/
manrs_core.py
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import requests
from collections import defaultdict
import bz2, gzip
# ## Download MANRS ISP list
# In[2]:
manrs = []
manrs_org = {}
manrs_org2as = defaultdict(list)
manrsisp_count = 0
with open('/project/manic-geo/irr/manrs/manrsisps.csv', 'rt') as f: #replace with new file
for line in f:
data = line.strip().split(',')
if len(data) < 7:
continue
manrsisp_count += 1
asns = data[2].split(';')
for i in asns:
try:
manrs.append(int(i))
manrs_org[int(i)] = data[0]
manrs_org2as[data[0]].append(int(i))
except:
pass
# In[3]:
manrscdn = []
manrscdn_count = 0
with open('/project/manic-geo/irr/manrs/manrscdns.csv', 'rt') as f: #replace with new file
for line in f:
data = line.strip().split(',')
if len(data) < 7:
continue
manrscdn_count += 1
asns = data[1].split(';')
for i in asns:
if not i.isdigit():
continue
try:
manrscdn.append(int(i))
manrs_org[int(i)] = data[0]
manrs_org2as[data[0]].append(int(i))
except:
pass
# In[4]:
print("Numbers of orgs in the MANRS ISP Program: ", manrsisp_count)
# In[5]:
print("Numbers of orgs in the MANRS CDN Program: ", manrscdn_count)
# In[6]:
manrsas = manrs + manrscdn
# In[7]:
manrs = manrsas
# In[8]:
print('number of MANRS ASes', len(manrsas))
# In[9]:
print('number of MANRS ASes', len(manrs))
# ## Parse AS-Relationship
#
# Download from CAIDA https://www.caida.org/catalog/datasets/as-relationships/
# In[10]:
path = '/project/manic-geo/irr/manrs/20221201.as-rel.txt.bz2'
as_rel = defaultdict(lambda: defaultdict(set))
topAS = set()
with bz2.open(path, 'rt') as file:
for line in file:
if line.startswith('# input clique'):
data = line.split(' ')
for i in data[3:]:
topAS.add(int(i))
if line.startswith('#'):
continue
data = line.strip().split('|')
AS1 = data[0]
AS2 = data[1]
rel = data[2]
if rel == '0':
as_rel[AS1]['peer'].add(AS2)
as_rel[AS2]['peer'].add(AS1)
elif rel == '1':
as_rel[AS1]['provider'].add(AS2)
as_rel[AS2]['customer'].add(AS1)
elif rel == '-1':
as_rel[AS1]['customer'].add(AS2)
as_rel[AS2]['provider'].add(AS1)
# In[11]:
##peering clique at the top of AS hierarchy
# ## find the manrs core
# ### find the peering clique at the top of AS hierarchy that are MANRS ASes
# In[12]:
manrs_root = set()
for i in topAS:
if i in manrs:
manrs_root.add(i)
# In[13]:
# ### find the customers of the manrs root
#
# recursively look for customers
# #### strict mode: for multi-homed customers, all providers must be in MANRS
# In[14]:
def check_multihome(asn):
for i in as_rel[asn]['provider']:
if int(i) not in manrs:
return False
return True
# In[15]:
def recur_manrs_customers_multihome(tc,asn):
for i in as_rel[str(asn)]['customer']:
if i in tc or int(i) not in manrs:
continue
elif check_multihome(str(i)):
tc.add(i)
recur_manrs_customers_multihome(tc,i)
# In[16]:
manrs_core_strict = set()
for i in manrs_root:
recur_manrs_customers_multihome(manrs_core_strict, i)
manrs_core_strict.add(i)
# In[17]:
print('# of MANRS core ASes where all providers of any AS are in MANRS: ', len(manrs_core_strict))
# In[18]:
manrs_core_strict_org = set()
for i in manrs_core_strict:
manrs_core_strict_org.add(manrs_org[int(i)])
# In[19]:
print('# of MANRS core_strict organizations: ', len(manrs_core_strict_org))
# #### relaxed mode: all customers
# In[20]:
def recur_manrs_customers(tc,asn):
for i in as_rel[str(asn)]['customer']:
if i in tc or int(i) not in manrs:
continue
else:
tc.add(i)
recur_manrs_customers(tc,i)
# In[21]:
manrs_core = set()
for i in manrs_root:
recur_manrs_customers(manrs_core, i)
manrs_core.add(i)
# In[22]:
print('# of MANRS core ASes: ', len(manrs_core))
# In[23]:
manrs_core_org = set()
for i in manrs_core:
manrs_core_org.add(manrs_org[int(i)])
# In[24]:
print('# of MANRS core organizations: ', len(manrs_core_org))
# ## all other ASes that connect to the MANRS core (strict)
# In[25]:
def recur_customers(tc,asn):
for i in as_rel[str(asn)]['customer']:
if i in tc:
continue
else:
tc.add(i)
# In[39]:
connected_manrs_core = set()
for i in manrs_core:
recur_customers(connected_manrs_core, i)
# ### Download as2org dataset from CAIDA
#
# https://www.caida.org/catalog/datasets/as-organizations/
# In[33]:
path = '/project/manic-geo/irr/manrs/20221001.as-org2info.txt.gz'
mapping = {}
companyname = {}
with gzip.open(path, 'rt') as as2org:
for line in as2org:
l = line.strip('\n').split('|')
asn = None
if l[0].isdigit():
asn = l[0]
mapping[asn] = l[3]
elif len(l) == 5:
companyname[l[0]] = (l[2], l[3])
for i in mapping:
orgname = companyname[mapping[i]]
mapping[i] = orgname
# ### Count US based ASes; Map ASes to Organizations
# In[40]:
connected_manrs_core_org = set()
US_based = set()
no_org_data = set()
for i in connected_manrs_core:
if i not in mapping:
no_org_data.add(i)
continue
connected_manrs_core_org.add(mapping[i][0])
if mapping[i][1] == 'US':
US_based.add(i)
# In[41]:
print('# of ASes connected to the MANRS core: ', len(connected_manrs_core))
# In[42]:
print('# of organizations connected to the MANRS core: ', len(connected_manrs_core_org))
# In[37]:
print('# of US based ASes connected to the MANRS core: ', len(US_based))
# In[38]:
# In[ ]: