Parse Dig output
| 78 | RR,RD,RDMAP,QR,RCODE,CLASS,QTYPE) |
| 79 | |
| 80 | class DigParser: |
| 81 | |
| 82 | """ |
| 83 | Parse Dig output |
| 84 | """ |
| 85 | |
| 86 | def __init__(self,dig,debug=False): |
| 87 | self.debug = debug |
| 88 | self.l = WordLexer(dig) |
| 89 | self.l.commentchars = ';' |
| 90 | self.l.nltok = ('NL',None) |
| 91 | self.i = iter(self.l) |
| 92 | |
| 93 | def parseHeader(self,l1,l2): |
| 94 | _,_,_,opcode,_,status,_,_id = l1.split() |
| 95 | _,flags,_ = l2.split(';') |
| 96 | header = DNSHeader(id=int(_id),bitmap=0) |
| 97 | header.opcode = getattr(QR,opcode.rstrip(',')) |
| 98 | header.rcode = getattr(RCODE,status.rstrip(',')) |
| 99 | for f in ('qr','aa','tc','rd','ra'): |
| 100 | if f in flags: |
| 101 | setattr(header,f,1) |
| 102 | return header |
| 103 | |
| 104 | def expect(self,expect): |
| 105 | t,val = next(self.i) |
| 106 | if t != expect: |
| 107 | raise ValueError("Invalid Token: %s (expecting: %s)" % (t,expect)) |
| 108 | return val |
| 109 | |
| 110 | def parseQuestions(self,q,dns): |
| 111 | for qname,qclass,qtype in q: |
| 112 | dns.add_question(DNSQuestion(qname, |
| 113 | getattr(QTYPE,qtype), |
| 114 | getattr(CLASS,qclass))) |
| 115 | |
| 116 | def parseAnswers(self,a,auth,ar,dns): |
| 117 | sect_map = {'a':'add_answer','auth':'add_auth','ar':'add_ar'} |
| 118 | for sect in 'a','auth','ar': |
| 119 | f = getattr(dns,sect_map[sect]) |
| 120 | for rr in locals()[sect]: |
| 121 | rname,ttl,rclass,rtype = rr[:4] |
| 122 | rdata = rr[4:] |
| 123 | rd = RDMAP.get(rtype,RD) |
| 124 | try: |
| 125 | if rd == RD and \ |
| 126 | any([ x not in string.hexdigits for x in rdata[-1]]): |
| 127 | # Only support hex encoded data for fallback RD |
| 128 | pass |
| 129 | else: |
| 130 | f(RR(rname=rname, |
| 131 | ttl=int(ttl), |
| 132 | rtype=getattr(QTYPE,rtype), |
| 133 | rclass=getattr(CLASS,rclass), |
| 134 | rdata=rd.fromZone(rdata))) |
| 135 | except DNSError as e: |
| 136 | if self.debug: |
| 137 | print("DNSError:",e,rr) |
no outgoing calls