Return an interface that works
()
| 375 | |
| 376 | |
| 377 | def get_working_if(): |
| 378 | # type: () -> Optional[NetworkInterface] |
| 379 | """Return an interface that works""" |
| 380 | # return the interface associated with the route with smallest |
| 381 | # mask (route by default if it exists) |
| 382 | routes = conf.route.routes[:] |
| 383 | routes.sort(key=lambda x: x[1]) |
| 384 | ifaces = (x[3] for x in routes) |
| 385 | # First check the routing ifaces from best to worse, |
| 386 | # then check all the available ifaces as backup. |
| 387 | for ifname in itertools.chain(ifaces, conf.ifaces.values()): |
| 388 | try: |
| 389 | iface = conf.ifaces.dev_from_networkname(ifname) # type: ignore |
| 390 | if iface.is_valid(): |
| 391 | return iface |
| 392 | except ValueError: |
| 393 | pass |
| 394 | # There is no hope left |
| 395 | try: |
| 396 | return conf.ifaces.dev_from_networkname(conf.loopback_name) |
| 397 | except ValueError: |
| 398 | return None |
| 399 | |
| 400 | |
| 401 | def get_working_ifaces(): |
no test coverage detected