From f317c6504219a427117ccd94e8a11063e0b376ad Mon Sep 17 00:00:00 2001
From: gpotter2 <gabriel@potter.fr>
Date: Sat, 6 May 2017 02:15:22 +0200
Subject: [PATCH] Fix src and dst usages in inet6

---
 scapy/base_classes.py |  2 +-
 scapy/layers/inet6.py | 71 ++++++++++++++++++++++++-------------------
 2 files changed, 41 insertions(+), 32 deletions(-)

diff --git a/scapy/base_classes.py b/scapy/base_classes.py
index 2b7eb4e5..a726b94d 100644
--- a/scapy/base_classes.py
+++ b/scapy/base_classes.py
@@ -81,7 +81,7 @@ class Net(Gen):
         self.parsed,self.netmask = self._parse_net(net)
 
     def __str__(self):
-        return self.repr
+        return next(self.__iter__())
                                                                                                
     def __iter__(self):
         for d in range(*self.parsed[3]):
diff --git a/scapy/layers/inet6.py b/scapy/layers/inet6.py
index 6e4f154d..0cf46ad6 100644
--- a/scapy/layers/inet6.py
+++ b/scapy/layers/inet6.py
@@ -198,6 +198,9 @@ class Net6(Gen): # syntax ex. fec0::/126
 
         return iter(rec(0, ['']))
 
+    def __str__(self):
+        return next(self.__iter__())
+
     def __repr__(self):
         return "Net6(%r)" % self.repr
 
@@ -405,7 +408,7 @@ class IPv6(_IPv6GuessPayload, Packet, IPTools):
         return conf.route6.route(dst)
 
     def mysummary(self):
-        return "%s > %s (%i)" % (self.src,self.dst, self.nh)
+        return "%s > %s (%i)" % (str(self.src),str(self.dst), self.nh)
 
     def post_build(self, p, pay):
         p += pay
@@ -429,8 +432,10 @@ class IPv6(_IPv6GuessPayload, Packet, IPTools):
             return self.payload.hashret()
 
         nh = self.nh
-        sd = self.dst
-        ss = self.src
+        _sd = str(self.dst)
+        _ss = str(self.src)
+        sd = str(_sd)
+        ss=str(_ss)
         if self.nh == 43 and isinstance(self.payload, IPv6ExtHdrRouting):
             # With routing header, the destination is the last
             # address of the IPv6 list if segleft > 0
@@ -454,7 +459,7 @@ class IPv6(_IPv6GuessPayload, Packet, IPTools):
             try:
                 sd = self.addresses[0]
             except IndexError:
-                sd = self.dst
+                sd = str(_sd)
 
         if self.nh == 44 and isinstance(self.payload, IPv6ExtHdrFragment):
             nh = self.payload.nh
@@ -473,7 +478,7 @@ class IPv6(_IPv6GuessPayload, Packet, IPTools):
 
         if conf.checkIPsrc and conf.checkIPaddr and not in6_ismaddr(sd):
             sd = inet_pton(socket.AF_INET6, sd)
-            ss = inet_pton(socket.AF_INET6, self.src)
+            ss = inet_pton(socket.AF_INET6, str(_ss))
             return strxor(sd, ss) + struct.pack("B", nh) + self.payload.hashret()
         else:
             return struct.pack("B", nh)+self.payload.hashret()
@@ -488,19 +493,23 @@ class IPv6(_IPv6GuessPayload, Packet, IPTools):
                 return self.answers(other.payload)
         if not isinstance(other, IPv6): # self is reply, other is request
             return False
+        _sd = str(self.dst)
+        _ss = str(self.src)
+        _od = str(other.dst)
+        _os = str(other.src)
         if conf.checkIPaddr:
-            ss = inet_pton(socket.AF_INET6, self.src)
-            sd = inet_pton(socket.AF_INET6, self.dst)
-            os = inet_pton(socket.AF_INET6, other.src)
-            od = inet_pton(socket.AF_INET6, other.dst)
+            ss = inet_pton(socket.AF_INET6, _ss)
+            sd = inet_pton(socket.AF_INET6, _sd)
+            os = inet_pton(socket.AF_INET6, _os)
+            od = inet_pton(socket.AF_INET6, _od)
             # request was sent to a multicast address (other.dst)
             # Check reply destination addr matches request source addr (i.e
             # sd == os) except when reply is multicasted too
             # XXX test mcast scope matching ?
-            if in6_ismaddr(other.dst):
-                if in6_ismaddr(self.dst):
+            if in6_ismaddr(_od):
+                if in6_ismaddr(_sd):
                     if ((od == sd) or
-                        (in6_isaddrllallnodes(self.dst) and in6_isaddrllallservers(other.dst))):
+                        (in6_isaddrllallnodes(_od) and in6_isaddrllallservers(_od))):
                          return self.payload.answers(other.payload)
                     return False
                 if (os == sd):
@@ -559,10 +568,10 @@ class IPerror6(IPv6):
     def answers(self, other):
         if not isinstance(other, IPv6):
             return False
-        sd = inet_pton(socket.AF_INET6, self.dst)
-        ss = inet_pton(socket.AF_INET6, self.src)
-        od = inet_pton(socket.AF_INET6, other.dst)
-        os = inet_pton(socket.AF_INET6, other.src)
+        sd = inet_pton(socket.AF_INET6, str(self.dst))
+        ss = inet_pton(socket.AF_INET6, str(self.src))
+        od = inet_pton(socket.AF_INET6, str(other.dst))
+        os = inet_pton(socket.AF_INET6, str(other.src))
 
         # Make sure that the ICMPv6 error is related to the packet scapy sent
         if isinstance(self.underlayer, _ICMPv6) and self.underlayer.type < 128:
@@ -3153,7 +3162,7 @@ class TracerouteResult6(TracerouteResult):
         for s,r in self.res:
             if IPv6 not in s:
                 continue
-            d = s[IPv6].dst
+            d = str(s[IPv6].dst)
             if d not in trace:
                 trace[d] = {}
 
@@ -3162,7 +3171,7 @@ class TracerouteResult6(TracerouteResult):
                      ICMPv6PacketTooBig in r or
                      ICMPv6ParamProblem in r)
 
-            trace[d][s[IPv6].hlim] = r[IPv6].src, t
+            trace[d][s[IPv6].hlim] = str(r[IPv6].src), t
 
         for k in six.itervalues(trace):
             try:
@@ -3249,13 +3258,13 @@ class _IPv6inIP(SuperSocket):
       return p
     elif isinstance(p, IP):
       # TODO: verify checksum
-      if p.src == self.dst and p.proto == socket.IPPROTO_IPV6:
+      if str(p.src) == str(self.dst) and p.proto == socket.IPPROTO_IPV6:
         if isinstance(p.payload, IPv6):
           return p.payload
     return p
 
   def send(self, x):
-    return self.worker.send(IP(dst=self.dst, src=self.src, proto=socket.IPPROTO_IPV6)/x)
+    return self.worker.send(IP(dst=str(self.dst), src=str(self.src), proto=socket.IPPROTO_IPV6)/x)
 
 
 #############################################################################
@@ -3286,7 +3295,7 @@ def _NDP_Attack_DAD_DoS(reply_callback, iface=None, mac_src_filter=None,
             return 0
 
         # Source must be the unspecified address
-        if req[IPv6].src != "::":
+        if str(req[IPv6].src) != "::":
             return 0
 
         # Check destination is the link-local solicited-node multicast
@@ -3294,7 +3303,7 @@ def _NDP_Attack_DAD_DoS(reply_callback, iface=None, mac_src_filter=None,
         tgt = socket.inet_pton(socket.AF_INET6, req[ICMPv6ND_NS].tgt)
         if tgt_filter and tgt != tgt_filter:
             return 0
-        received_snma = socket.inet_pton(socket.AF_INET6, req[IPv6].dst)
+        received_snma = socket.inet_pton(socket.AF_INET6, str(req[IPv6].dst))
         expected_snma = in6_getnsma(tgt)
         if received_snma != expected_snma:
             return 0
@@ -3361,7 +3370,7 @@ def NDP_Attack_DAD_DoS_via_NS(iface=None, mac_src_filter=None, tgt_filter=None,
 
         # Let's build a reply and send it
         mac = req[Ether].src
-        dst = req[IPv6].dst
+        dst = str(req[IPv6].dst)
         tgt = req[ICMPv6ND_NS].tgt
         rep = Ether(src=reply_mac)/IPv6(src="::", dst=dst)/ICMPv6ND_NS(tgt=tgt)
         sendp(rep, iface=iface, verbose=0)
@@ -3420,7 +3429,7 @@ def NDP_Attack_DAD_DoS_via_NA(iface=None, mac_src_filter=None, tgt_filter=None,
 
         # Let's build a reply and send it
         mac = req[Ether].src
-        dst = req[IPv6].dst
+        dst = str(req[IPv6].dst)
         tgt = req[ICMPv6ND_NS].tgt
         rep = Ether(src=reply_mac)/IPv6(src=tgt, dst=dst)
         rep /= ICMPv6ND_NA(tgt=tgt, S=0, R=0, O=1)
@@ -3507,14 +3516,14 @@ def NDP_Attack_NA_Spoofing(iface=None, mac_src_filter=None, tgt_filter=None,
             return 0
 
         # Source must NOT be the unspecified address
-        if req[IPv6].src == "::":
+        if str(req[IPv6].src) == "::":
             return 0
 
         tgt = socket.inet_pton(socket.AF_INET6, req[ICMPv6ND_NS].tgt)
         if tgt_filter and tgt != tgt_filter:
             return 0
 
-        dst = req[IPv6].dst
+        dst = str(req[IPv6].dst)
         if in6_isllsnmaddr(dst): # Address is Link Layer Solicited Node mcast.
 
             # If this is a real address resolution NS, then the destination
@@ -3540,7 +3549,7 @@ def NDP_Attack_NA_Spoofing(iface=None, mac_src_filter=None, tgt_filter=None,
         # send it back.
         mac = req[Ether].src
         pkt = req[IPv6]
-        src = pkt.src
+        src = str(pkt.src)
         tgt = req[ICMPv6ND_NS].tgt
         rep = Ether(src=reply_mac, dst=mac)/IPv6(src=tgt, dst=src)
         rep /= ICMPv6ND_NA(tgt=tgt, S=1, R=router, O=1) # target from the NS
@@ -3734,7 +3743,7 @@ def NDP_Attack_Kill_Default_Router(iface=None, mac_src_filter=None,
         if mac_src_filter and mac_src != mac_src_filter:
             return 0
 
-        ip_src = req[IPv6].src
+        ip_src = str(req[IPv6].src)
         if ip_src_filter and ip_src != ip_src_filter:
             return 0
 
@@ -3752,7 +3761,7 @@ def NDP_Attack_Kill_Default_Router(iface=None, mac_src_filter=None,
 
         # Let's build a reply and send it
 
-        src = req[IPv6].src
+        src = str(req[IPv6].src)
 
         # Prepare packets parameters
         ether_params = {}
@@ -3855,7 +3864,7 @@ def NDP_Attack_Fake_Router(ra, iface=None, mac_src_filter=None,
         if mac_src_filter and mac_src != mac_src_filter:
             return 0
 
-        ip_src = req[IPv6].src
+        ip_src = str(req[IPv6].src)
         if ip_src_filter and ip_src != ip_src_filter:
             return 0
 
@@ -3866,7 +3875,7 @@ def NDP_Attack_Fake_Router(ra, iface=None, mac_src_filter=None,
         Callback that sends an RA in reply to an RS
         """
 
-        src = req[IPv6].src
+        src = str(req[IPv6].src)
         sendp(ra, iface=iface, verbose=0)
         print("Fake RA sent in response to RS from %s" % src)
 
-- 
GitLab