473,498 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading binary using a struct - behavour not as expected?

Hi,
I am writing code to deal with PCAP files. I have a PCAP dump and I am
looking at the timestamps in the PCAP packet headers to see if they are in
the correct order in the file. To do this I have a class called
PCAPPacketHdr as follows

import struct

class PCAPPacketHdr:
FormatString = "LLLL"
TSSec = None
TSUSec = None
InclLen = None
OrigLen = None

def Pack(self):
return struct.pack( self.FormatString, self.TSSec, self.TSUSec,
self.InclLen, self.OrigLen )

def Unpack(self, buffer):
self.TSSec, self.TSUSec, self.InclLen, self.OrigLen =
struct.unpack( self.FormatString, buffer )

def Size(self):
return struct.calcsize (self.FormatString)
I then have code which opens up the file (skipping the PCAP file magic
number and PCAP file header), and reads in each packet header as follows:

while not eof:
#read in PCAPPacketHdr
buf = curFile.read(packetHdr.Size())
if len(buf) == packetHdr.Size():
packetHdr.Unpack(buf)
if lastPacketHdr != None:
if lastPacketHdr.TSSec > packetHdr.TSSec:
outputFile.write("ERROR: Packet TSSec earlier than last
one: \n")
outputFile.write(" Last Packet
"+repr(lastPacketHdr.TSSec)+"."+repr(lastPacketHdr .TSUSec)+"\n")
elif lastPacketHdr.TSSec == packetHdr.TSSec:
if lastPacketHdr.TSUSec > packetHdr.TSUSec:
outputFile.write("ERROR: Packet TSUSec earlier than
last one\n")
outputFile.write(" Last Packet
"+repr(lastPacketHdr.TSSec)+"."+repr(lastPacketHdr .TSUSec)+"\n")
outputFile.write(" Packet
"+repr(packetHdr.TSSec)+"."+repr(packetHdr.TSUSec) +"\n")
lastPacketHdr = copy.deepcopy(packetHdr)
#skip packet payload
packetPayload = curFile.read(packetHdr.InclLen)
else:
eof = True
This code appears to work fine for extracting the timestamps from the file,
the repr( ) calls on the timestamps allow me to write them to the output
file correctly, it's just the comparison operators don't appear to be
working as I would expect. It appears than when the TSUSec timestamp is the
same as the previous one in the data I input, it reports "ERROR: Packet
TSUSec earlier than last one".

This makes me think that the comparison operators aren't acting on the data
as longs as I expected.

Can anyone shed some light on what I'm doing wrong, I'm still very new to
Python.

Thanks in advance,

Rich
Jul 18 '05 #1
1 2183
Rich,

Ok, your problem is this:

When you define attributes immediately after
the class definition they are SHARED among
all instances of the class. Basically they are
global (actually this can come in handy if you
want to keep counters across class instances).
You were copying into lastcopy, but everytime
you unpacked, you overwrote your shared attributes.

Try this instead:

class PCAPPacketHdr:
#
# Attributes defined here are global across
# instances of this class.
#
FormatString = "LLLL"

def __init__(self):
#
# Attributes defined here are local to
# the class instance.
#
self.TSSec = None
self.TSUSec = None
self.InclclLen = None
self.OrigLen = None
return

This will share FormatString (it doesn't change),
but have individual attributes for the other
values you wish to be unique between two different
instances of the class.

Regards,
Larry Bates
Syscon, Inc.

"richardd" <ri******@hmgcc.gov.uk> wrote in message
news:40********@mail.hmgcc.gov.uk...
Hi,
I am writing code to deal with PCAP files. I have a PCAP dump and I am
looking at the timestamps in the PCAP packet headers to see if they are in
the correct order in the file. To do this I have a class called
PCAPPacketHdr as follows

import struct

class PCAPPacketHdr:
FormatString = "LLLL"
TSSec = None
TSUSec = None
InclLen = None
OrigLen = None

def Pack(self):
return struct.pack( self.FormatString, self.TSSec, self.TSUSec,
self.InclLen, self.OrigLen )

def Unpack(self, buffer):
self.TSSec, self.TSUSec, self.InclLen, self.OrigLen =
struct.unpack( self.FormatString, buffer )

def Size(self):
return struct.calcsize (self.FormatString)
I then have code which opens up the file (skipping the PCAP file magic
number and PCAP file header), and reads in each packet header as follows:

while not eof:
#read in PCAPPacketHdr
buf = curFile.read(packetHdr.Size())
if len(buf) == packetHdr.Size():
packetHdr.Unpack(buf)
if lastPacketHdr != None:
if lastPacketHdr.TSSec > packetHdr.TSSec:
outputFile.write("ERROR: Packet TSSec earlier than last
one: \n")
outputFile.write(" Last Packet
"+repr(lastPacketHdr.TSSec)+"."+repr(lastPacketHdr .TSUSec)+"\n")
elif lastPacketHdr.TSSec == packetHdr.TSSec:
if lastPacketHdr.TSUSec > packetHdr.TSUSec:
outputFile.write("ERROR: Packet TSUSec earlier than
last one\n")
outputFile.write(" Last Packet
"+repr(lastPacketHdr.TSSec)+"."+repr(lastPacketHdr .TSUSec)+"\n")
outputFile.write(" Packet
"+repr(packetHdr.TSSec)+"."+repr(packetHdr.TSUSec) +"\n")
lastPacketHdr = copy.deepcopy(packetHdr)
#skip packet payload
packetPayload = curFile.read(packetHdr.InclLen)
else:
eof = True
This code appears to work fine for extracting the timestamps from the file, the repr( ) calls on the timestamps allow me to write them to the output
file correctly, it's just the comparison operators don't appear to be
working as I would expect. It appears than when the TSUSec timestamp is the same as the previous one in the data I input, it reports "ERROR: Packet
TSUSec earlier than last one".

This makes me think that the comparison operators aren't acting on the data as longs as I expected.

Can anyone shed some light on what I'm doing wrong, I'm still very new to
Python.

Thanks in advance,

Rich

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1714
by: Fredrik Normann | last post by:
Hello, I'm trying to read the binary files under /var/spool/rwho/ so I'm wondering if anyone has done that before or could give me some clues on how to read those files. I've tried to use the...
3
3374
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
20
3011
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
3742
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
0
356
by: Derrick | last post by:
I am trying to read a binary file into a struct, but am having trouble getting all the data. the struct and a snip of the code follows at the end of the message. Expected results: 'Sai,,LW,'...
3
3399
by: Zeke Zinzul | last post by:
Hi Guys & Geeks, What's the most elegant way of dealing with binary data and structures? Say I have this (which I actually do, a woo-hoo): struct Struct_IconHeader { byte width; byte...
13
3657
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
3510
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
6
4211
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.