def _parse_header(cls, buf, offset): fmt = "!HHHHHH" reqlen = struct.calcsize(fmt) strng = buf[offset:offset + reqlen] res = struct.unpack(fmt, strng) hdr = {"ID": res[0]} qr = 0x8000 opcode_mask = 0x7800 opcode_shift = 11 aa = 0x0400 tc = 0x0200 rd = 0x0100 ra = 0x0080 z_mask = 0x0070 z_shift = 4 rcode_mask = 0x000F rcode_shift = 0 hdr['QR'] = not not (res[1] & qr) hdr['OpCode'] = opcode_to_text((res[1] & opcode_mask) >> opcode_shift) hdr['AA'] = not not (res[1] & aa) hdr['TC'] = not not (res[1] & tc) hdr['RD'] = not not (res[1] & rd) hdr['RA'] = not not (res[1] & ra) hdr['Z'] = (res[1] & z_mask) >> z_shift hdr['ReturnCode'] = rcode_to_text((res[1] & rcode_mask) >> rcode_shift) hdr['QDCOUNT'] = res[2] hdr['ANCOUNT'] = res[3] hdr['NSCOUNT'] = res[4] hdr['ARCOUNT'] = res[5] return offset + reqlen, hdr
def do_header(buf, offset): fmt = "!HHHHHH" reqlen = struct.calcsize(fmt) #print(reqlen) str = buf[offset:offset + reqlen] res = struct.unpack(fmt, str) hdr = {} hdr['ID'] = res[0] QR = 0x8000 Opcode_mask = 0x7800 Opcode_shift = 11 AA = 0x0400 TC = 0x0200 RD = 0x0100 RA = 0x0080 Z = 0x0040 AD = 0x0020 CD = 0x0010 RCODE_mask = 0x000F RCODE_shift = 0 hdr['QR'] = not not (res[1] & QR) hdr['OpCode'] = opcode_to_text((res[1] & Opcode_mask) >> Opcode_shift) hdr['AA'] = not not (res[1] & AA) hdr['TC'] = not not (res[1] & TC) hdr['RD'] = not not (res[1] & RD) hdr['RA'] = not not (res[1] & RA) hdr['Z'] = not not (res[1] & Z) hdr['AD'] = not not (res[1] & AD) hdr['CD'] = not not (res[1] & CD) hdr['ReturnCode'] = rcode_to_text((res[1] & RCODE_mask) >> RCODE_shift) hdr['QDCOUNT'] = res[2] hdr['ANCOUNT'] = res[3] hdr['NSCOUNT'] = res[4] hdr['ARCOUNT'] = res[5] #print ('Header Ok') return (offset + reqlen, hdr)
def do_header(buf, offset): fmt= "!HHHHHH" reqlen= struct.calcsize(fmt) str= buf[offset:offset+reqlen] res= struct.unpack(fmt, str) #print res hdr={} hdr['ID']= res[0] QR= 0x8000 Opcode_mask= 0x7800 Opcode_shift= 11 AA= 0x0400 TC= 0x0200 RD= 0x0100 RA= 0x0080 Z= 0x0040 AD= 0x0020 CD= 0x0010 RCODE_mask= 0x000F RCODE_shift= 0 hdr['QR']= not not(res[1] & QR) hdr['OpCode']= opcode_to_text((res[1] & Opcode_mask) >> Opcode_shift) hdr['AA']= not not(res[1] & AA) hdr['TC']= not not(res[1] & TC) hdr['RD']= not not(res[1] & RD) hdr['RA']= not not(res[1] & RA) hdr['Z']= not not(res[1] & Z) hdr['AD']= not not(res[1] & AD) hdr['CD']= not not(res[1] & CD) hdr['ReturnCode']= rcode_to_text((res[1] & RCODE_mask) >> RCODE_shift) hdr['QDCOUNT']= res[2] hdr['ANCOUNT']= res[3] hdr['NSCOUNT']= res[4] hdr['ARCOUNT']= res[5] return (offset+reqlen, hdr)
def _parse_header(cls, buf, offset): fmt = "!HHHHHH" reqlen = struct.calcsize(fmt) strng = buf[offset:offset + reqlen] res = struct.unpack(fmt, strng) hdr = { "ID": res[0] } qr = 0x8000 opcode_mask = 0x7800 opcode_shift = 11 aa = 0x0400 tc = 0x0200 rd = 0x0100 ra = 0x0080 z_mask = 0x0070 z_shift = 4 rcode_mask = 0x000F rcode_shift = 0 hdr['QR'] = not not(res[1] & qr) hdr['OpCode'] = opcode_to_text((res[1] & opcode_mask) >> opcode_shift) hdr['AA'] = not not(res[1] & aa) hdr['TC'] = not not(res[1] & tc) hdr['RD'] = not not(res[1] & rd) hdr['RA'] = not not(res[1] & ra) hdr['Z'] = (res[1] & z_mask) >> z_shift hdr['ReturnCode'] = rcode_to_text((res[1] & rcode_mask) >> rcode_shift) hdr['QDCOUNT'] = res[2] hdr['ANCOUNT'] = res[3] hdr['NSCOUNT'] = res[4] hdr['ARCOUNT'] = res[5] return offset + reqlen, hdr