How to create an IP packet with variable byte order (binary data) in Python -


i've been more day trying solve one. need create ip packet in python, length of fields different. every time have field bigger 1 byte need change byte order big endian. did pack each variable separately , save in file. i'm saving in file because need checksum after packing , pack on again checksum (if has better idea more welcome).

i'm having 2 problems:

1) packet generate bigger original 1 parsed information (the ip packet generate change in 1 field ttl, should remain same size)

2) when parse packet (stream of data in file), information has change (i didn't change it).

this original packet information:

ver: 4 hl: 12 tos: 0 len: 50 id= 456 off: 16384 ttl: 5 prot: 6 chsum: 30512 src: 16885952 dst: 167880896 rem_head: ['q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q'] data: ['a', 'a'] 

this result parsing packet. verify check sum , decrease ttl 1 , reassemble packet this:

pk_cksum=0x0000 arch= open('packet.out', 'w') arch.write( struct.pack('b',byte) ) arch.write( struct.pack('>b', pk_tos) ) arch.write( struct.pack('>h', pk_len) ) arch.write( struct.pack('>h', pk_id) ) arch.write( struct.pack('>h',pk_off) ) arch.write( struct.pack('b', pk_ttl) ) arch.write( struct.pack('b', pk_p) ) arch.write( struct.pack('>h', pk_cksum) ) arch.write( struct.pack('<i', pk_src) ) arch.write( struct.pack('<i', pk_dst) ) if (pk_hl>5):     in range(len(pk_head)):         arch.write(struct.pack('c', pk_head[i])[0]) if (pk_len>(pk_hl*4)):     j in range(len(pk_data)):         arch.write(struct.pack('c', pk_data[j])[0]) arch.close() 

to verify if packing successful use same code parse last packet , information:

ver: 4 hl: 12 tos: 0 len: 50 id= 456 off: 16384 ttl: 4 prot: 6 chsum: 0 src: 16885952 dst: 218212544  1101 0000 0001 1010 1000 1100 0000 rem_head: ['\n', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q'] data: ['q', 'a'] 

as see, destination address changed , have '\n' in head, original variable doesn't have , data have q not suppose have.

can tell me i'm doing wrong?

thanks


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -