| 937 | |
| 938 | |
| 939 | class IP6Field(Field[Optional[Union[str, Net6]], bytes]): |
| 940 | def __init__(self, name, default): |
| 941 | # type: (str, Optional[str]) -> None |
| 942 | Field.__init__(self, name, default, "16s") |
| 943 | |
| 944 | def h2i(self, pkt, x): |
| 945 | # type: (Optional[Packet], Any) -> str |
| 946 | if isinstance(x, bytes): |
| 947 | x = plain_str(x) |
| 948 | if isinstance(x, _ScopedIP): |
| 949 | return x |
| 950 | elif isinstance(x, str): |
| 951 | x = ScopedIP(x) |
| 952 | try: |
| 953 | x = ScopedIP(in6_ptop(x), scope=x.scope) |
| 954 | except socket.error: |
| 955 | return Net6(x) # type: ignore |
| 956 | elif isinstance(x, tuple): |
| 957 | if len(x) != 2: |
| 958 | raise ValueError("Invalid IPv6 format") |
| 959 | return Net6(*x) # type: ignore |
| 960 | elif isinstance(x, list): |
| 961 | x = [self.h2i(pkt, n) for n in x] |
| 962 | return x # type: ignore |
| 963 | |
| 964 | def i2h(self, pkt, x): |
| 965 | # type: (Optional[Packet], Optional[Union[str, Net6]]) -> str |
| 966 | return cast(str, x) |
| 967 | |
| 968 | def i2m(self, pkt, x): |
| 969 | # type: (Optional[Packet], Optional[Union[str, Net6]]) -> bytes |
| 970 | if x is None: |
| 971 | x = "::" |
| 972 | return inet_pton(socket.AF_INET6, plain_str(x)) |
| 973 | |
| 974 | def m2i(self, pkt, x): |
| 975 | # type: (Optional[Packet], bytes) -> str |
| 976 | return inet_ntop(socket.AF_INET6, x) |
| 977 | |
| 978 | def any2i(self, pkt, x): |
| 979 | # type: (Optional[Packet], Optional[str]) -> str |
| 980 | return self.h2i(pkt, x) |
| 981 | |
| 982 | def i2repr(self, pkt, x): |
| 983 | # type: (Optional[Packet], Optional[Union[str, Net6]]) -> str |
| 984 | if x is None: |
| 985 | return self.i2h(pkt, x) |
| 986 | elif not isinstance(x, Net6) and not isinstance(x, list): |
| 987 | if in6_isaddrTeredo(x): # print Teredo info |
| 988 | server, _, maddr, mport = teredoAddrExtractInfo(x) |
| 989 | return "%s [Teredo srv: %s cli: %s:%s]" % (self.i2h(pkt, x), server, maddr, mport) # noqa: E501 |
| 990 | elif in6_isaddr6to4(x): # print encapsulated address |
| 991 | vaddr = in6_6to4ExtractAddr(x) |
| 992 | return "%s [6to4 GW: %s]" % (self.i2h(pkt, x), vaddr) |
| 993 | elif isinstance(x, _ScopedIP) and x.scope: |
| 994 | return repr(x) |
| 995 | r = self.i2h(pkt, x) # No specific information to return |
| 996 | return r if isinstance(r, str) else repr(r) |
no outgoing calls
no test coverage detected