| 29 | ############################## |
| 30 | |
| 31 | class Route: |
| 32 | def __init__(self): |
| 33 | # type: () -> None |
| 34 | self.routes = [] # type: List[Tuple[int, int, str, str, str, int]] |
| 35 | self.invalidate_cache() |
| 36 | if conf.route_autoload: |
| 37 | self.resync() |
| 38 | |
| 39 | def invalidate_cache(self): |
| 40 | # type: () -> None |
| 41 | self.cache = {} # type: Dict[Tuple[str, Optional[str]], Tuple[str, str, str]] |
| 42 | |
| 43 | def resync(self): |
| 44 | # type: () -> None |
| 45 | from scapy.arch import read_routes |
| 46 | self.invalidate_cache() |
| 47 | self.routes = read_routes() |
| 48 | |
| 49 | def __repr__(self): |
| 50 | # type: () -> str |
| 51 | rtlst = [] # type: List[Tuple[Union[str, List[str]], ...]] |
| 52 | for net, msk, gw, iface, addr, metric in self.routes: |
| 53 | if_repr = resolve_iface(iface).description |
| 54 | rtlst.append((ltoa(net), |
| 55 | ltoa(msk), |
| 56 | gw, |
| 57 | if_repr, |
| 58 | addr, |
| 59 | str(metric))) |
| 60 | |
| 61 | return pretty_list(rtlst, |
| 62 | [("Network", "Netmask", "Gateway", "Iface", "Output IP", "Metric")]) # noqa: E501 |
| 63 | |
| 64 | def make_route(self, |
| 65 | host=None, # type: Optional[str] |
| 66 | net=None, # type: Optional[str] |
| 67 | gw=None, # type: Optional[str] |
| 68 | dev=None, # type: Optional[str] |
| 69 | metric=1, # type: int |
| 70 | ): |
| 71 | # type: (...) -> Tuple[int, int, str, str, str, int] |
| 72 | if host is not None: |
| 73 | thenet, msk = host, 32 |
| 74 | elif net is not None: |
| 75 | thenet, msk_b = net.split("/") |
| 76 | msk = int(msk_b) |
| 77 | else: |
| 78 | raise Scapy_Exception("make_route: Incorrect parameters. You should specify a host or a net") # noqa: E501 |
| 79 | if gw is None: |
| 80 | gw = "0.0.0.0" |
| 81 | if dev is None: |
| 82 | if gw: |
| 83 | nhop = gw |
| 84 | else: |
| 85 | nhop = thenet |
| 86 | dev, ifaddr, _ = self.route(nhop) |
| 87 | else: |
| 88 | ifaddr = "0.0.0.0" # acts as a 'via' in `ip addr add` |
no outgoing calls
no test coverage detected
searching dependent graphs…