From df4e97b5736bde0c6cd601001db1ad11bdf2e762 Mon Sep 17 00:00:00 2001
From: Pierre LALET <pierre.lalet@cea.fr>
Date: Sat, 3 Jan 2015 17:41:35 +0100
Subject: [PATCH] Use `Packet().raw_packet_cache` attribute when iterating

When iterating over `Packet` instances, `.raw_packet_cache` attribute
was not used, causing bugs similar to the one detailed in issue #913
to appear when packets sniffed or built from strings representations
were used with `wrpcap()`, `send`(`p`?), `sr`(`p`?), etc.

This commit essentially copies the `.raw_packet_cache` attribute in
`.clone_with()` method.

--HG--
branch : issue-5105
---
 scapy/packet.py | 4 ++--
 scapy/utils.py  | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/scapy/packet.py b/scapy/packet.py
index 6c2094f2..01dac646 100644
--- a/scapy/packet.py
+++ b/scapy/packet.py
@@ -634,10 +634,10 @@ Creates an EPS file describing a packet. If filename is not provided a temporary
         pkt.underlayer = self.underlayer
         pkt.overload_fields = self.overload_fields.copy()
         pkt.post_transforms = self.post_transforms
+        pkt.raw_packet_cache = self.raw_packet_cache
         if payload is not None:
             pkt.add_payload(payload)
         return pkt
-        
 
     def __iter__(self):
         def loop(todo, done, self=self):
@@ -666,7 +666,7 @@ Creates an EPS file describing a packet. If filename is not provided a temporary
                     pkt = self.clone_with(payload=payl, **done2)
                     yield pkt
 
-        if self.explicit:
+        if self.explicit or self.raw_packet_cache is not None:
             todo = []
             done = self.fields
         else:
diff --git a/scapy/utils.py b/scapy/utils.py
index c5ac2520..07b00ab4 100644
--- a/scapy/utils.py
+++ b/scapy/utils.py
@@ -646,8 +646,9 @@ class RawPcapWriter:
     
 
     def write(self, pkt):
-        """accepts a either a single packet or a list of packets
-        to be written to the dumpfile
+        """accepts either a single packet or a list of packets to be
+        written to the dumpfile
+
         """
         if not self.header_present:
             self._write_header(pkt)
@@ -700,7 +701,7 @@ class PcapWriter(RawPcapWriter):
                 self.linktype = 1
         RawPcapWriter._write_header(self, pkt)
 
-    def _write_packet(self, packet):        
+    def _write_packet(self, packet):
         sec = int(packet.time)
         usec = int(round((packet.time-sec)*1000000))
         s = str(packet)
-- 
GitLab