473,791 Members | 3,097 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.FormatStri ng, self.TSSec, self.TSUSec,
self.InclLen, self.OrigLen )

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

def Size(self):
return struct.calcsize (self.FormatStr ing)
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(pa cketHdr.Size())
if len(buf) == packetHdr.Size( ):
packetHdr.Unpac k(buf)
if lastPacketHdr != None:
if lastPacketHdr.T SSec > packetHdr.TSSec :
outputFile.writ e("ERROR: Packet TSSec earlier than last
one: \n")
outputFile.writ e(" Last Packet
"+repr(lastPack etHdr.TSSec)+". "+repr(lastPack etHdr.TSUSec)+" \n")
elif lastPacketHdr.T SSec == packetHdr.TSSec :
if lastPacketHdr.T SUSec > packetHdr.TSUSe c:
outputFile.writ e("ERROR: Packet TSUSec earlier than
last one\n")
outputFile.writ e(" Last Packet
"+repr(lastPack etHdr.TSSec)+". "+repr(lastPack etHdr.TSUSec)+" \n")
outputFile.writ e(" Packet
"+repr(packetHd r.TSSec)+"."+re pr(packetHdr.TS USec)+"\n")
lastPacketHdr = copy.deepcopy(p acketHdr)
#skip packet payload
packetPayload = curFile.read(pa cketHdr.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 2199
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.FormatStri ng, self.TSSec, self.TSUSec,
self.InclLen, self.OrigLen )

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

def Size(self):
return struct.calcsize (self.FormatStr ing)
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(pa cketHdr.Size())
if len(buf) == packetHdr.Size( ):
packetHdr.Unpac k(buf)
if lastPacketHdr != None:
if lastPacketHdr.T SSec > packetHdr.TSSec :
outputFile.writ e("ERROR: Packet TSSec earlier than last
one: \n")
outputFile.writ e(" Last Packet
"+repr(lastPack etHdr.TSSec)+". "+repr(lastPack etHdr.TSUSec)+" \n")
elif lastPacketHdr.T SSec == packetHdr.TSSec :
if lastPacketHdr.T SUSec > packetHdr.TSUSe c:
outputFile.writ e("ERROR: Packet TSUSec earlier than
last one\n")
outputFile.writ e(" Last Packet
"+repr(lastPack etHdr.TSSec)+". "+repr(lastPack etHdr.TSUSec)+" \n")
outputFile.writ e(" Packet
"+repr(packetHd r.TSSec)+"."+re pr(packetHdr.TS USec)+"\n")
lastPacketHdr = copy.deepcopy(p acketHdr)
#skip packet payload
packetPayload = curFile.read(pa cketHdr.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
1731
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 binascii module without any luck. Best regards, -fredrik-normann-
3
3402
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 created when the program executes. But the compile keeps reading that it can't find the text file. Karl you wrote the original program from which this one is but a poor copy, can you or anyone else enlighten me as to why yours worked and mine...
20
3079
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: --cut here-- typedef struct pkg_ { short int info; char* data;
6
3796
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 trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple coding convention for reading in a flatfile - one record at a time?? SCANF does not work because of...
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,' Actual results: 'i,,L,' It appears that only the last letter of the sTmCode is assigned in the struct. It appears that only the first letter of the sPos is assisgned in the
3
3439
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 height;
13
3713
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 consists of structures of type---- struct record { int acountnum; char name; float value;
6
3531
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
6
4232
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 function. Here a few lines: char *sAnv; .... sprintf(file_a, "%s/anverso.jpg", strDir);
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9995
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4110
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 we have to send another system
3
2916
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.