Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9180 > unrolled thread

parsing packets

Started by"Littlefield, Tyler" <tyler@tysdomain.com>
First post2011-07-10 14:59 -0600
Last post2011-07-11 14:05 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  parsing packets "Littlefield, Tyler" <tyler@tysdomain.com> - 2011-07-10 14:59 -0600
    Re: parsing packets Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-07-11 14:05 +0200

#9180 — parsing packets

From"Littlefield, Tyler" <tyler@tysdomain.com>
Date2011-07-10 14:59 -0600
Subjectparsing packets
Message-ID<mailman.848.1310331905.1164.python-list@python.org>
Hello all:
I'm working on a server that will need to parse packets sent from a 
client, and construct it's own packets.
The setup is something like this: the first two bytes is the type of the 
packet.
So, lets say we have a packet set to connect. There are two types of 
connect packet: a auth packet and a connect packet.
The connect packet then has two bytes with the type, another byte that 
notes that it is a connect packet, and 4 bytes that contains the version 
of the client.
The auth packet has the two bytes that tells what packet it is, one byte 
denoting that it is an auth packet, then the username, a NULL character, 
and a password and a NULL character.

With all of this said, I'm kind of curious how I would 1) parse out 
something like this (I am using twisted, so it'll just be passed to my 
Receive function), and how I get the length of the packet with multiple 
NULL values. I'm also looking to build a packet and send it back out, is 
there something that will allow me to designate two bytes, set 
individual bits, then put it altogether in a packet to be sent out?

-- 

Take care,
Ty
my website:
http://tds-solutions.net
my blog:
http://tds-solutions.net/blog
skype: st8amnd127
My programs don't have bugs; they're randomly added features!

[toc] | [next] | [standalone]


#9234

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2011-07-11 14:05 +0200
Message-ID<iveoue$9v5$1@r03.glglgl.eu>
In reply to#9180
Am 10.07.2011 22:59 schrieb Littlefield, Tyler:
> Hello all:
> I'm working on a server that will need to parse packets sent from a
> client, and construct it's own packets.

Are these packets sent as separate UDP packets or embedded in a TCP 
stream? In the first case, you already have packets and only have to 
parse them. In a stream, you first have to split them up.

In the following, I will talk about UDP datagrams. For TCP, further work 
is needed.


> The setup is something like this: the first two bytes is the type of the
> packet.

Then you have

type = struct.unpack(">H", packet),
payload1 = packet[2:]

> So, lets say we have a packet set to connect. There are two types of
> connect packet: a auth packet and a connect packet.
> The connect packet then has two bytes with the type, another byte that
> notes that it is a connect packet, and 4 bytes that contains the version
> of the client.

if type == CONNECT:
     subtype = struct.unpack("B", payload1)
     payload2 = payload1[1:]
     if subtype == CONNECT:
         upx = payload2.split("\0")
         assert len(upx) == 3 and upx[-1] == ''
         username, password = upx[:2]
     else:
         assert len(payload2) == 4
         version = struct.unpack(">L", payload2)


> The auth packet has the two bytes that tells what packet it is, one byte
> denoting that it is an auth packet, then the username, a NULL character,
> and a password and a NULL character.


> With all of this said, I'm kind of curious how I would 1) parse out
> something like this (I am using twisted, so it'll just be passed to my
> Receive function),

I. e., you already have your packets distinct? That's fine.

 > and how I get the length of the packet with multiple NULL values.

With len(), how else?


 > I'm also looking to build a packet and send it back out, is
> there something that will allow me to designate two bytes, set
> individual bits, then put it altogether in a packet to be sent out?

The same: with struct.pack().


Thomas

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web