Forgot to force little-endian pcap headers on big-endian machines
authorCraig Dowell <craigdo@ee.washington.edu>
Wed, 24 Feb 2010 20:41:11 -0800
changeset 6062 f62b76f5c92a
parent 6061 ee5c91bd781a
child 6063 d9163d8a18c5
child 6068 a2127017ecb4
child 6075 375ef5337b41
Forgot to force little-endian pcap headers on big-endian machines
src/common/pcap-file.cc
--- a/src/common/pcap-file.cc	Wed Feb 24 17:07:51 2010 -0800
+++ b/src/common/pcap-file.cc	Wed Feb 24 20:41:11 2010 -0800
@@ -398,7 +398,33 @@
   m_fileHeader.m_type = dataLinkType;
 
   m_haveFileHeader = true;
-  m_swapMode = swapMode;
+
+  //
+  // We use pcap files for regression testing.  We do byte-for-byte comparisons
+  // in those tests to determine pass or fail.  If we allow big endian systems
+  // to write big endian headers, they will end up byte-swapped and the
+  // regression tests will fail.  Until we get rid of the regression tests, we
+  // have to pick an endianness and stick with it.  The precedent is little
+  // endian, so we set swap mode if required to pick little endian.
+  //
+  // We do want to allow a user or test suite to enable swapmode irrespective
+  // of what we decide here, so we allow setting swapmode from formal parameter
+  // as well.
+  //
+  // So, determine the endianness of the running system.
+  //
+  union {
+    uint32_t a;
+    uint8_t  b[4];
+  } u;
+
+  u.a = 1;
+  bool bigEndian = u.b[3];
+
+  //
+  // And set swap mode if requested or we are on a big-endian system.
+  //
+  m_swapMode = swapMode | bigEndian;
 
   return WriteFileHeader ();
 }