|
| 1 | +from models_p import AS, LinkedAS, Link, ROLES |
| 2 | + |
| 3 | +links_2 = [ |
| 4 | + { |
| 5 | + "first": {"as_number": "0", "role": ROLES.PROVIDER}, |
| 6 | + "second": {"as_number": "1", "role": ROLES.COSTUMER} |
| 7 | + }, |
| 8 | + { |
| 9 | + "first": {"as_number": "1", "role": ROLES.PEER}, |
| 10 | + "second": {"as_number": "2", "role": ROLES.PEER} |
| 11 | + }, |
| 12 | + { |
| 13 | + "first": {"as_number": "1", "role": ROLES.PEER}, |
| 14 | + "second": {"as_number": "3", "role": ROLES.PEER} |
| 15 | + }, |
| 16 | + { |
| 17 | + "first": {"as_number": "2", "role": ROLES.PEER}, |
| 18 | + "second": {"as_number": "3", "role": ROLES.PEER} |
| 19 | + }, |
| 20 | + { |
| 21 | + "first": {"as_number": "2", "role": ROLES.COSTUMER}, |
| 22 | + "second": {"as_number": "4", "role": ROLES.PROVIDER} |
| 23 | + }, |
| 24 | + { |
| 25 | + "first": {"as_number": "2", "role": ROLES.COSTUMER}, |
| 26 | + "second": {"as_number": "5", "role": ROLES.PROVIDER} |
| 27 | + }, |
| 28 | + { |
| 29 | + "first": {"as_number": "3", "role": ROLES.COSTUMER}, |
| 30 | + "second": {"as_number": "4", "role": ROLES.PROVIDER} |
| 31 | + }, |
| 32 | + { |
| 33 | + "first": {"as_number": "4", "role": ROLES.COSTUMER}, |
| 34 | + "second": {"as_number": "5", "role": ROLES.PROVIDER} |
| 35 | + }, |
| 36 | +] |
| 37 | +AS_MAP_2 = { |
| 38 | + "0": { |
| 39 | + "ips": [ |
| 40 | + "6.0.0.0/8", |
| 41 | + ] |
| 42 | + }, |
| 43 | + "1": { |
| 44 | + "ips": [ |
| 45 | + "1.0.0.0/8", |
| 46 | + ] |
| 47 | + }, |
| 48 | + "2": { |
| 49 | + "ips": [ |
| 50 | + "2.0.0.0/8", |
| 51 | + "22.0.0.0/8", |
| 52 | + "222.0.0.0/8", |
| 53 | + ] |
| 54 | + }, |
| 55 | + "3": { |
| 56 | + "ips": [ |
| 57 | + "3.0.0.0/8", |
| 58 | + "33.0.0.0/8", |
| 59 | + ] |
| 60 | + }, |
| 61 | + "4": { |
| 62 | + "ips": [ |
| 63 | + "4.0.0.0/8", |
| 64 | + ] |
| 65 | + }, |
| 66 | + "5": { |
| 67 | + "ips": [ |
| 68 | + "5.0.0.0/8", |
| 69 | + ] |
| 70 | + }, |
| 71 | +} |
| 72 | + |
| 73 | +commands_2_1 = [ |
| 74 | + "AS 0:auto advertise on", |
| 75 | + "AS 1:auto advertise on", |
| 76 | + "AS 2:auto advertise on", |
| 77 | + "AS 3:auto advertise on", |
| 78 | + "AS 4:auto advertise on", |
| 79 | + "AS 5:auto advertise on", |
| 80 | + "AS 4:advertise self", |
| 81 | + "AS 0:advertise self", |
| 82 | + "AS 1:advertise self", |
| 83 | + "AS 2:advertise self", |
| 84 | + "AS 3:advertise self", |
| 85 | + "AS 5:advertise self", |
| 86 | + "AS 0:get route 33.0.0.0/8", |
| 87 | + "AS 0:hijack 5.0.0.0/8", |
| 88 | + "AS 3:withdrawn 33.0.0.0/8", |
| 89 | + "AS 0:get route 33.0.0.0/8", |
| 90 | +] |
| 91 | + |
| 92 | +commands_2_2 = [ |
| 93 | + "AS 0:auto advertise on", |
| 94 | + "AS 1:auto advertise on", |
| 95 | + "AS 2:auto advertise on", |
| 96 | + "AS 3:auto advertise on", |
| 97 | + "AS 4:auto advertise on", |
| 98 | + "AS 5:auto advertise on", |
| 99 | + "AS 4:advertise self", |
| 100 | + "AS 0:advertise self", |
| 101 | + "AS 1:advertise self", |
| 102 | + "AS 2:advertise self", |
| 103 | + "AS 3:advertise self", |
| 104 | + "AS 5:advertise self", |
| 105 | + "AS 0:get route 5.0.0.0/8", |
| 106 | + "AS 2:get route 5.0.0.0/8", |
| 107 | + "link delete 2 5", |
| 108 | + "AS 0:get route 5.0.0.0/8", |
| 109 | + "AS 2:get route 5.0.0.0/8", |
| 110 | + "link create 2 5 COSTUMER PROVIDER", |
| 111 | + "AS 0:get route 5.0.0.0/8", |
| 112 | + "AS 2:get route 5.0.0.0/8", |
| 113 | +] |
| 114 | + |
| 115 | +commands_2_3 = [ |
| 116 | + "AS 0:auto advertise on", |
| 117 | + "AS 1:auto advertise on", |
| 118 | + "AS 3:auto advertise on", |
| 119 | + "AS 4:auto advertise on", |
| 120 | + "AS 5:auto advertise on", |
| 121 | + "AS 4:advertise self", |
| 122 | + "AS 0:advertise self", |
| 123 | + "AS 1:advertise self", |
| 124 | + "AS 2:advertise self", |
| 125 | + "AS 3:advertise self", |
| 126 | + "AS 5:advertise self", |
| 127 | + "AS 0:get route 5.0.0.0/8", |
| 128 | + "AS 2:get route 5.0.0.0/8", |
| 129 | + "AS 2:advertise all", |
| 130 | + "AS 0:get route 5.0.0.0/8", |
| 131 | + "AS 2:get route 5.0.0.0/8", |
| 132 | +] |
| 133 | + |
| 134 | +commands_2_4 = [ |
| 135 | + "AS 0:auto advertise on", |
| 136 | + "AS 1:auto advertise on", |
| 137 | + "AS 2:auto advertise on", |
| 138 | + "AS 3:auto advertise on", |
| 139 | + "AS 4:auto advertise on", |
| 140 | + "AS 5:auto advertise on", |
| 141 | + "AS 0:advertise self", |
| 142 | + "AS 1:advertise self", |
| 143 | + "AS 2:advertise self", |
| 144 | + "AS 3:advertise self", |
| 145 | + "AS 4:advertise self", |
| 146 | + "AS 5:advertise self", |
| 147 | + "AS 3:get route 1.0.0.0/8", |
| 148 | + "link delete 1 3", |
| 149 | + "AS 3:get route 1.0.0.0/8", |
| 150 | + |
| 151 | + "AS 4:get route 1.0.0.0/8", |
| 152 | + "link delete 2 4", |
| 153 | + "AS 4:get route 1.0.0.0/8", |
| 154 | + |
| 155 | +] |
| 156 | + |
| 157 | +commands_2_5 = [ |
| 158 | + "AS 0:auto advertise on", |
| 159 | + "AS 1:auto advertise on", |
| 160 | + "AS 2:auto advertise on", |
| 161 | + "AS 3:auto advertise on", |
| 162 | + "AS 4:auto advertise on", |
| 163 | + "AS 5:auto advertise on", |
| 164 | + "AS 0:advertise self", |
| 165 | + "AS 1:advertise self", |
| 166 | + "AS 2:advertise self", |
| 167 | + "AS 3:advertise self", |
| 168 | + "AS 4:advertise self", |
| 169 | + "AS 5:advertise self", |
| 170 | + "AS 3:get route 1.23.0.0/16", |
| 171 | +] |
| 172 | +commands_2_6 = [ |
| 173 | + "AS 0:auto advertise on", |
| 174 | + "AS 1:auto advertise on", |
| 175 | + "AS 2:auto advertise on", |
| 176 | + "AS 3:auto advertise on", |
| 177 | + "AS 4:auto advertise on", |
| 178 | + "AS 5:auto advertise on", |
| 179 | + "AS 0:advertise self", |
| 180 | + "AS 1:advertise self", |
| 181 | + "AS 2:advertise self", |
| 182 | + "AS 3:advertise self", |
| 183 | + "AS 4:advertise self", |
| 184 | + "AS 5:advertise self", |
| 185 | + |
| 186 | + "AS 3:get route 2.0.0.0/8", |
| 187 | + "link delete 2 3", |
| 188 | + "AS 3:get route 2.0.0.0/8", |
| 189 | +] |
| 190 | + |
| 191 | +commands_2_7 = [ |
| 192 | + "AS 0:auto advertise on", |
| 193 | + "AS 1:auto advertise on", |
| 194 | + "AS 2:auto advertise on", |
| 195 | + "AS 3:auto advertise on", |
| 196 | + "AS 4:auto advertise on", |
| 197 | + "AS 5:auto advertise on", |
| 198 | + "AS 0:advertise self", |
| 199 | + "AS 1:advertise self", |
| 200 | + "AS 2:advertise self", |
| 201 | + "AS 3:advertise self", |
| 202 | + "AS 4:advertise self", |
| 203 | + "AS 5:advertise self", |
| 204 | + |
| 205 | + "link delete 1 3", |
| 206 | + "AS 2:hijack 3.0.0.0/8", |
| 207 | + "AS 1:get route 3.0.0.0/8", |
| 208 | + "AS 0:get route 3.0.0.0/8", |
| 209 | + "AS 4:get route 3.0.0.0/8", |
| 210 | +] |
| 211 | + |
| 212 | +commands_2_8 = [ |
| 213 | + "AS 0:auto advertise on", |
| 214 | + "AS 1:auto advertise on", |
| 215 | + "AS 2:auto advertise on", |
| 216 | + "AS 3:auto advertise on", |
| 217 | + "AS 4:auto advertise on", |
| 218 | + "AS 5:auto advertise on", |
| 219 | + "AS 1:advertise self", |
| 220 | + "AS 2:advertise self", |
| 221 | + "AS 3:advertise self", |
| 222 | + "AS 4:advertise self", |
| 223 | + "AS 5:advertise self", |
| 224 | + |
| 225 | + "AS 5:get route 6.0.0.0/8", |
| 226 | + "AS 0:advertise self", |
| 227 | + "AS 5:get route 6.0.0.0/8", |
| 228 | + |
| 229 | +] |
| 230 | + |
| 231 | + |
| 232 | +def run(AS_MAP, links, commands): |
| 233 | + AS_dictionary = dict() |
| 234 | + for as_number in AS_MAP.keys(): |
| 235 | + AS_dictionary[int(as_number)] = AS(int(as_number), owned_ips=AS_MAP.get(as_number).get("ips").copy()) |
| 236 | + |
| 237 | + for link in links: |
| 238 | + first_as_number = int(link.get("first").get("as_number")) |
| 239 | + first_role = link.get("first").get("role") |
| 240 | + second_as_number = int(link.get("second").get("as_number")) |
| 241 | + second_role = link.get("second").get("role") |
| 242 | + create_link(AS_dictionary, first_as_number, first_role, second_as_number, second_role) |
| 243 | + print("Initializing finished.") |
| 244 | + |
| 245 | + for command in commands: |
| 246 | + if command.startswith("link delete"): |
| 247 | + first_as_number, second_as_number = command.split()[2:] |
| 248 | + AS_dictionary[int(first_as_number)].command_handler("link delete " + second_as_number) |
| 249 | + AS_dictionary[int(second_as_number)].command_handler("link delete " + first_as_number) |
| 250 | + elif command.startswith("link create"): |
| 251 | + first_as_number, second_as_number, first_role, second_role = command.split()[2:] |
| 252 | + create_link(AS_dictionary, int(first_as_number), ROLES[first_role], int(second_as_number), |
| 253 | + ROLES[second_role]) |
| 254 | + AS_dictionary[int(first_as_number)].command_handler("link create " + second_as_number) |
| 255 | + AS_dictionary[int(second_as_number)].command_handler("link create " + first_as_number) |
| 256 | + pass |
| 257 | + else: |
| 258 | + as_number = int(command.split(":")[0].split()[1]) |
| 259 | + ASa = AS_dictionary[as_number] |
| 260 | + ASa.command_handler(command.split(":")[1]) |
| 261 | + |
| 262 | + |
| 263 | +def create_link(AS_dictionary, first_as_number, first_role, second_as_number, second_role): |
| 264 | + first_as = AS_dictionary[first_as_number] |
| 265 | + second_as = AS_dictionary[second_as_number] |
| 266 | + l = Link(first_as, second_as) |
| 267 | + first_as.add_link(LinkedAS(second_as_number, first_as_number, l, first_role)) |
| 268 | + second_as.add_link(LinkedAS(first_as_number, second_as_number, l, second_role)) |
| 269 | + |
| 270 | + |
| 271 | +print("test 1 : hijack - get route - withdrawn ip ") |
| 272 | +run(AS_MAP_2, links_2, commands_2_1) |
| 273 | +print("\ntest 2 : link delete/create") |
| 274 | +run(AS_MAP_2, links_2, commands_2_2) |
| 275 | +print("\ntest 3 : advertise all") |
| 276 | +run(AS_MAP_2, links_2, commands_2_3) |
| 277 | +print("\ntest 4 : another link delete + bgp rule") |
| 278 | +run(AS_MAP_2, links_2, commands_2_4) |
| 279 | +print("\ntest 5 : subnet ") |
| 280 | +run(AS_MAP_2, links_2, commands_2_5) |
| 281 | +print("\ntest 6 : another delete link + bgp rule ") |
| 282 | +run(AS_MAP_2, links_2, commands_2_6) |
| 283 | +print("\ntest 7 : test successful hijack") |
| 284 | +run(AS_MAP_2, links_2, commands_2_7) |
| 285 | +print("\ntest 8 : advertise self") |
| 286 | +run(AS_MAP_2, links_2, commands_2_8) |
0 commit comments