liuweireign / dpkt

Automatically exported from code.google.com/p/dpkt
Other
0 stars 0 forks source link

pcap writer should use native endianness when writing pcaps #48

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. use pcap.Writer to create a pcap
2. od -X <> | head shows big-endian pcap file.

What is the expected output? What do you see instead?
Expected a little endian pcap on x86 platforms.

What version of the product are you using? On what operating system?
dpkt 1.7, Linux (Ubuntu 10.04 x86), Mac OS X (Snow Leopard 10.6.4 x86)

Please provide any additional information below.

--- pcap.py 2009-11-06 22:28:26.000000000 +0000
+++ pcap-new.py 2010-09-09 13:07:09.000000000 +0100
@@ -70,7 +70,10 @@
     """Simple pcap dumpfile writer."""
     def __init__(self, fileobj, snaplen=1500, linktype=DLT_EN10MB):
         self.__f = fileobj
-        fh = FileHdr(snaplen=snaplen, linktype=linktype)
+        if sys.byteorder == 'little':
+            fh = LEFileHdr(snaplen=snaplen, linktype=linktype)
+        else:
+            fh = FileHdr(snaplen=snaplen, linktype=linktype)
         self.__f.write(str(fh))

     def writepkt(self, pkt, ts=None):
@@ -78,7 +81,12 @@
             ts = time.time()
         s = str(pkt)
         n = len(s)
-        ph = PktHdr(tv_sec=int(ts),
+        if sys.byteorder == 'little':
+            ph = LEPktHdr(tv_sec=int(ts),
+                    tv_usec=int((float(ts) - int(ts)) * 1000000.0),
+                    caplen=n, len=n)
+        else:
+            ph = PktHdr(tv_sec=int(ts),
                     tv_usec=int((float(ts) - int(ts)) * 1000000.0),
                     caplen=n, len=n)
         self.__f.write(str(ph))

Original issue reported on code.google.com by pete.sha...@gmail.com on 9 Sep 2010 at 12:10

GoogleCodeExporter commented 9 years ago
Thanks - committed in r77.

Original comment by dugsong on 6 Jan 2011 at 4:00