An str that also holds extra attributes. Examples:: >>> ScopedIP("224.0.0.1%eth0") # interface 'eth0' >>> ScopedIP("224.0.0.1%1") # interface index 1 >>> ScopedIP("224.0.0.1", scope=conf.iface)
(net: str, scope: Optional[Any] = None)
| 126 | |
| 127 | |
| 128 | def ScopedIP(net: str, scope: Optional[Any] = None) -> _ScopedIP: |
| 129 | """ |
| 130 | An str that also holds extra attributes. |
| 131 | |
| 132 | Examples:: |
| 133 | |
| 134 | >>> ScopedIP("224.0.0.1%eth0") # interface 'eth0' |
| 135 | >>> ScopedIP("224.0.0.1%1") # interface index 1 |
| 136 | >>> ScopedIP("224.0.0.1", scope=conf.iface) |
| 137 | """ |
| 138 | if "%" in net: |
| 139 | try: |
| 140 | net, scope = net.split("%", 1) |
| 141 | except ValueError: |
| 142 | raise Scapy_Exception("Scope identifier can only be present once !") |
| 143 | if scope is not None: |
| 144 | from scapy.interfaces import resolve_iface, network_name, dev_from_index |
| 145 | try: |
| 146 | iface = dev_from_index(int(scope)) |
| 147 | except (ValueError, TypeError): |
| 148 | iface = resolve_iface(scope) |
| 149 | if not iface.is_valid(): |
| 150 | raise Scapy_Exception( |
| 151 | "RFC6874 scope identifier '%s' could not be resolved to a " |
| 152 | "valid interface !" % scope |
| 153 | ) |
| 154 | scope = network_name(iface) |
| 155 | x = _ScopedIP(net) |
| 156 | x.scope = scope |
| 157 | return x |
| 158 | |
| 159 | |
| 160 | class Net(Gen[str]): |
no test coverage detected