| 912 | |
| 913 | |
| 914 | class SourceIPField(IPField): |
| 915 | def __init__(self, name): |
| 916 | # type: (str) -> None |
| 917 | IPField.__init__(self, name, None) |
| 918 | |
| 919 | def __findaddr(self, pkt): |
| 920 | # type: (Packet) -> Optional[str] |
| 921 | if conf.route is None: |
| 922 | # unused import, only to initialize conf.route |
| 923 | import scapy.route # noqa: F401 |
| 924 | return pkt.route()[1] or conf.route.route()[1] |
| 925 | |
| 926 | def i2m(self, pkt, x): |
| 927 | # type: (Optional[Packet], Optional[Union[str, Net]]) -> bytes |
| 928 | if x is None and pkt is not None: |
| 929 | x = self.__findaddr(pkt) |
| 930 | return super(SourceIPField, self).i2m(pkt, x) |
| 931 | |
| 932 | def i2h(self, pkt, x): |
| 933 | # type: (Optional[Packet], Optional[Union[str, Net]]) -> str |
| 934 | if x is None and pkt is not None: |
| 935 | x = self.__findaddr(pkt) |
| 936 | return super(SourceIPField, self).i2h(pkt, x) |
| 937 | |
| 938 | |
| 939 | class IP6Field(Field[Optional[Union[str, Net6]], bytes]): |