Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing packet got error: unpack requires a string argument of length
    primarykey
    data
    text
    <p>I'm parsing a packet written in a binary file. I have to retrieve each field in different variables because I have to validate them. I have an error after using struct over some fields: </p> <pre><code>(pk_dst,)=struct.unpack('!I', line[16:20])#ip_dst struct.error: unpack requires a string argument of length 4 </code></pre> <p>I've search about this error but it seems is a unique problem in each case. I don't understand why the struct.unpack works perfectly with the IP address source (src), but not for the destination (dst) were I have the error.</p> <p>My code is:</p> <pre><code>with open(filename, 'rb') as f: for line in f.readlines(): mask1=0b1110000 mask2=0b0001111 ba = bytearray(line) byte=ba[0] frstb=byte &amp; mask1 pk_ver=frstb &gt;&gt; 4 #ip_vr print 'Ver:',pk_ver pk_hl=byte &amp; mask2 #ip_hl print 'HL:',pk_hl #packet= struct.unpack('BBHHHBBHLL140s', line) (pk_tos,)= struct.unpack('!B', line[1]) #ip_tos print 'TOS:',pk_tos (pk_len,)=struct.unpack('!H', line[2:4])#ip_len print 'LEN:',pk_len (pk_id,)= struct.unpack('!H', line[4:6])#ip_id print 'ID=',pk_id (pk_off,)= struct.unpack('!H', line[6:8])#ip_off print 'OFF:',pk_off (pk_ttl,)=struct.unpack('@B', line[8])#ip_ttl print 'TTL:',pk_ttl (pk_p,)=struct.unpack('@B', line[9])#ip_p print'PROT:',pk_p (pk_chsum,)=struct.unpack('!H', line[10:12])#ip_sum print'CHSUM:',pk_chsum (pk_src,)=struct.unpack('!I', line[12:16])#ip_src print'SRC:',pk_src (pk_dst,)=struct.unpack('!I', line[16:20])#ip_dst </code></pre> <p>The structure of the packet is in C language in a different code:</p> <pre><code>struct cip { uint8_t ip_hl:4, /* both fields are 4 bits */ ip_v:4; uint8_t ip_tos; uint16_t ip_len; uint16_t ip_id; uint16_t ip_off; uint8_t ip_ttl; uint8_t ip_p; uint16_t ip_sum; struct in_addr ip_src; struct in_addr ip_dst; char head[100]; }; </code></pre> <p>Hope someone can tell me what is wrong with my code.</p> <p>This is the data that is in the packet (extract from the program that generated the packed), so you will see that the data is no bigger or smaller than 4 bytes. </p> <pre><code>HL:10 Ver:4 TOS:0 Len:15360 id:14082 offset:64 ttl:5 Prot:6 SUM:62141 src:167880896 dst:184658112 header:QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ </code></pre> <hr>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload