design help/questions
Amritansh Raghav
amritansh at mobilian.com
Wed Jan 17 16:58:11 EST 2001
Thanks
The reason I didnt want to do it in C was to allow the parser to be extended
easily. I did look at the pack and unpack and they seem to do what I want. I
just need to build the dictionary now
"Donn Cave" <donn at u.washington.edu> wrote in message
news:9452jt$s8e$1 at nntp6.u.washington.edu...
> Quoth "Amritansh Raghav" <amritansh at mobilian.com>:
>
> | So here is what I need to do. I'm trying to write a scripting tool for
our
> | test team. They wish to send arbitrary packets over the network. What
I'd
> | like to do is allow the testers to specify any packet in a text file -
say
> | something like this:
> | <IP>
> | "VersionLength" 0 1 # Name of field, Starting
offset,
> | Length in bytes
> | "TOS" 1 1
> | "Length" 2 2
> | "Id" 4 2
> | "Offset" 6 2
> | "TTL" 8 1
> | "ProtoId" 9 1
> | CheckSum" 10 2
> | "Source" 12 4
> | "Dest" 16 4
> | </IP>
> |
> | Once they have specified this, the tester should be able to create a
packet
> | an assign values to the field, or receive a packet and parse out its
> | contents.
> | so a script should be able to say
> | p = read()
> | if p["ProtoId"] == 17:
> | # do something here for UDP packets
> |
> | Solution
> | I have written a DLL which can open/close/read/write to an ethernet
> | device. I've got it interfacing to Python. It can accept a buffer and
return
> | a buffer. I've looked at socketmodule.c to get most of that figured out.
I
> | assume what I need to do is build a dictionary whose keys are the field
> | names of a packet and then write a parser to fill in the values into the
> | dictionary. I also need something to parse the text file description.
> |
> | If someone has done something like this before, or can point me to
helpful
> | scripts and source, I would be grateful.
>
> If I understand this, I think at the lowest level the options are few.
> I think since you're already writing C extensions, you might find it
> simpler to make the buffer into a tuple there, but if you want to do
> it in Python, you can use struct.unpack. See your documentation, or
> print struct.__doc__.
>
> Then it's up to you how to map this tuple to the field names. I think
> one candidate would be a class ...
>
> class Packet:
> def __init__(self, buffer = None):
> if buffer is None:
> self.VersionLength = 1
> ... # default values
> else:
> self.unpack(buffer)
> def unpack(self, buffer):
> self.VersionLength, ... = struct.unpack('B...', buffer)
> def pack(self):
> return struct.pack('B...', self.VersionLength, ...)
>
> packet = Packet(buffer)
> packet.ProtoId = 17
> buffer = packet.pack()
>
> Donn Cave, donn at u.washington.edu
More information about the Python-list
mailing list