diff --git a/scapy/fields.py b/scapy/fields.py index ffd0209d579eea2233c3fe9bbe5ed48888260d48..cbe155342da01b05ad5d889a3fc8d2ce27b60381 100644 --- a/scapy/fields.py +++ b/scapy/fields.py @@ -257,26 +257,25 @@ class SourceIPField(IPField): def __init__(self, name, dstname): IPField.__init__(self, name, None) self.dstname = dstname + def __findaddr(self, pkt): + if conf.route is None: + # unused import, only to initialize conf.route + import scapy.route + dst = ("0.0.0.0" if self.dstname is None + else getattr(pkt, self.dstname)) + if isinstance(dst, (Gen, list)): + r = {conf.route.route(daddr) for daddr in dst} + if len(r) > 1: + warning("More than one possible route for %r" % (dst,)) + return min(r)[1] + return conf.route.route(dst)[1] def i2m(self, pkt, x): if x is None: - iff,x,gw = pkt.route() - if x is None: - x = "0.0.0.0" + x = self.__findaddr(pkt) return IPField.i2m(self, pkt, x) def i2h(self, pkt, x): if x is None: - if conf.route is None: - # unused import, only to initialize conf.route - import scapy.route - dst = ("0.0.0.0" if self.dstname is None else - getattr(pkt, self.dstname)) - if isinstance(dst, (Gen, list)): - r = {conf.route.route(daddr) for daddr in dst} - if len(r) > 1: - warning("More than one possible route for %r" % (dst,)) - x = min(r)[1] - else: - x = conf.route.route(dst)[1] + x = self.__findaddr(pkt) return IPField.i2h(self, pkt, x) diff --git a/scapy/layers/hsrp.py b/scapy/layers/hsrp.py index 3558bbf9a31ff69788821ca2e479ed5457693ea0..9fa09c1db3aa32e196a59f706af6e6593537278e 100644 --- a/scapy/layers/hsrp.py +++ b/scapy/layers/hsrp.py @@ -67,7 +67,7 @@ class HSRPmd5(Packet): ByteEnumField("algo", 0, {1: "MD5"}), ByteField("padding", 0x00), XShortField("flags", 0x00), - IPField("sourceip", "127.0.0.1"), + SourceIPField("sourceip", None), XIntField("keyid", 0x00), StrFixedLenField("authdigest", "\00" * 16, 16)] diff --git a/test/regression.uts b/test/regression.uts index 25c7ec5fec0250beca11e6aae6ca6086dc0b7a1f..a2cd5a5bf442397c7a81eef40ad9ffe5ca2f2be7 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -279,6 +279,20 @@ assert( _ == ("ABCD","1.2.3.4") ) i.addfield(None, "FOO", "1.2.3.4") assert( _ == "FOO\x01\x02\x03\x04" ) += SourceIPField +~ core field +defaddr = conf.route.route('0.0.0.0')[1] +class Test(Packet): fields_desc = [SourceIPField("sourceip", None)] + +assert Test().sourceip == defaddr +assert Test(str(Test())).sourceip == defaddr + +assert IP(dst="0.0.0.0").src == defaddr +assert IP(str(IP(dst="0.0.0.0"))).src == defaddr +assert IP(dst="0.0.0.0/31").src == defaddr +assert IP(str(IP(dst="0.0.0.0/31"))).src == defaddr + + #= ByteField #~ core field #b = ByteField("foo", None) @@ -7171,13 +7185,12 @@ L2TP in p and p[L2TP].len == 14 and p.tunnel_id == 0 and p[UDP].chksum == 0xf465 + HSRP tests -= HSRP - build -s = str(IP(src="127.0.0.1")/UDP(dport=1985, sport=1985)/HSRP()/HSRPmd5()) -s == 'E\x00\x00N\x00\x01\x00\x00@\x11\x1b\x9b\x7f\x00\x00\x01\xe0\x00\x00\x02\x07\xc1\x07\xc1\x00:\xeb\x00\x00\x00\x10\x03\nx\x01\x00cisco\x00\x00\x00\xc0\xa8\x01\x01\x04\x00\x00\x00\x00\x00\x7f\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - -= HSRP - dissection -p = IP(s) -p[IP].dst == "224.0.0.2" and HSRPmd5 in p and p[HSRPmd5].sourceip == "127.0.0.1" += HSRP - build & dissection +defaddr = conf.route.route('0.0.0.0')[1] +pkt = IP(str(IP()/UDP(dport=1985, sport=1985)/HSRP()/HSRPmd5())) +assert pkt[IP].dst == "224.0.0.2" and pkt[UDP].sport == pkt[UDP].dport == 1985 +assert pkt[HSRP].opcode == 0 and pkt[HSRP].state == 16 +assert pkt[HSRPmd5].type == 4 and pkt[HSRPmd5].sourceip == defaddr ############