1 # -*- encoding: utf-8 -*-
3 $Id: Lib.py,v 1.11.2.8 2011/03/16 20:06:39 customdesigned Exp $
5 This file is part of the pydns project.
6 Homepage: http://pydns.sourceforge.net
8 This code is covered by the standard Python License. See LICENSE for details.
10 Library code. Largely this is packers and unpackers for various types.
16 # ------------------------------------------------------------------------
17 # Network Working Group P. Mockapetris
18 # Request for Comments: 1035 ISI
20 # Obsoletes: RFCs 882, 883, 973
22 # DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION
23 # ------------------------------------------------------------------------
34 from Base import DNSError
37 LABEL_ENCODING = 'idna'
39 class UnpackError(DNSError): pass
40 class PackError(DNSError): pass
42 # Low-level 16 and 32 bit integer packing and unpacking
44 from struct import pack as struct_pack
45 from struct import unpack as struct_unpack
46 from socket import inet_ntoa, inet_aton
49 return struct_pack('!H', n)
52 return struct_pack('!L', n)
55 return struct_unpack('!H', s)[0]
58 return struct_unpack('!L', s)[0]
61 return struct_unpack('!l', inet_aton(addr))[0]
64 return inet_ntoa(struct_pack('!L', n))
69 " packer base class. supports basic byte/16bit/32bit/addr/string/name "
76 if len(c) != 1: raise TypeError, 'one character expected'
77 self.buf = self.buf + c
78 def addbytes(self, bytes):
79 self.buf = self.buf + bytes
80 def add16bit(self, n):
81 self.buf = self.buf + pack16bit(n)
82 def add32bit(self, n):
83 self.buf = self.buf + pack32bit(n)
84 def addaddr(self, addr):
86 self.buf = self.buf + pack32bit(n)
87 def addstring(self, s):
89 raise ValueError, "Can't encode string of length "+ \
91 self.addbyte(chr(len(s)))
93 def addname(self, name):
94 # Domain name packing (section 4.1.4)
95 # Add a domain name to the buffer, possibly using pointers.
96 # The case of the first occurrence of a name is preserved.
97 # Redundant dots are ignored.
99 for label in string.splitfields(name, '.'):
101 raise PackError, 'empty label'
104 for i in range(len(list)):
105 key = string.upper(string.joinfields(list[i:], '.'))
107 if self.index.has_key(key):
108 pointer = self.index[key]
113 # Do it into temporaries first so exceptions don't
114 # mess up self.index and self.buf
116 offset = len(self.buf)
121 enc = DNS.LABEL_ENCODING
125 label = label.encode(enc)
126 except UnicodeEncodeError:
127 if not DNS.LABEL_UTF8: raise
128 if not label.startswith('\ufeff'):
129 label = '\ufeff'+label
130 label = label.encode(enc)
133 raise PackError, 'label too long'
134 if offset + len(buf) < 0x3FFF:
135 index.append((keys[j], offset + len(buf)))
137 print 'DNS.Lib.Packer.addname:',
138 print 'warning: pointer too big'
139 buf = buf + (chr(n) + label)
141 buf = buf + pack16bit(pointer | 0xC000)
144 self.buf = self.buf + buf
145 for key, value in index:
146 self.index[key] = value
148 keys = self.index.keys()
152 print '%20s %3d' % (key, self.index[key])
155 for i in range(0, len(self.buf)+1, 2):
156 if self.buf[i:i+2] == '**':
162 for c in self.buf[i:i+2]:
166 print '%2d' % ord(c),
175 def __init__(self, buf):
179 if self.offset >= len(self.buf):
180 raise UnpackError, "Ran off end of data"
181 c = self.buf[self.offset]
182 self.offset = self.offset + 1
184 def getbytes(self, n):
185 s = self.buf[self.offset : self.offset + n]
186 if len(s) != n: raise UnpackError, 'not enough data left'
187 self.offset = self.offset + n
190 return unpack16bit(self.getbytes(2))
192 return unpack32bit(self.getbytes(4))
194 return bin2addr(self.get32bit())
196 return self.getbytes(ord(self.getbyte()))
198 # Domain name unpacking (section 4.1.4)
204 pointer = ((i<<8) | j) & ~0xC000
205 save_offset = self.offset
207 self.offset = pointer
208 domain = self.getname()
210 self.offset = save_offset
214 domain = self.getbytes(i)
215 remains = self.getname()
219 return domain + '.' + remains
222 # Test program for packin/unpacking (section 4.1.4)
228 # See section 4.1.4 of RFC 1035
232 p.addaddr('192.168.0.1')
234 p.addname('f.ISI.ARPA')
236 p.addname('Foo.F.isi.arpa')
242 print timing.milli(), "ms total for packing"
243 print round(timing.milli() / i, 4), 'ms per packing'
269 print timing.milli(), "ms total for unpacking"
270 print round(timing.milli() / i, 4), 'ms per unpacking'
271 #for item in res: print item
274 # Pack/unpack RR toplevel format (section 3.2.1)
276 class RRpacker(Packer):
278 Packer.__init__(self)
280 def addRRheader(self, name, type, klass, ttl, *rest):
286 if rest[1:]: raise TypeError, 'too many args'
290 self.add16bit(rdlength)
291 self.rdstart = len(self.buf)
292 def patchrdlength(self):
293 rdlength = unpack16bit(self.buf[self.rdstart-2:self.rdstart])
294 if rdlength == len(self.buf) - self.rdstart:
296 rdata = self.buf[self.rdstart:]
300 self.buf = self.buf[:self.rdstart-2]
301 self.add16bit(len(rdata))
302 self.buf = self.buf + rdata
305 if not ok: self.buf = save_buf
307 if self.rdstart is not None:
311 if self.rdstart is not None: self.patchrdlength()
312 return Packer.getbuf(self)
313 # Standard RRs (section 3.3)
314 def addCNAME(self, name, klass, ttl, cname):
315 self.addRRheader(name, Type.CNAME, klass, ttl)
318 def addHINFO(self, name, klass, ttl, cpu, os):
319 self.addRRheader(name, Type.HINFO, klass, ttl)
323 def addMX(self, name, klass, ttl, preference, exchange):
324 self.addRRheader(name, Type.MX, klass, ttl)
325 self.add16bit(preference)
326 self.addname(exchange)
328 def addNS(self, name, klass, ttl, nsdname):
329 self.addRRheader(name, Type.NS, klass, ttl)
330 self.addname(nsdname)
332 def addPTR(self, name, klass, ttl, ptrdname):
333 self.addRRheader(name, Type.PTR, klass, ttl)
334 self.addname(ptrdname)
336 def addSOA(self, name, klass, ttl,
337 mname, rname, serial, refresh, retry, expire, minimum):
338 self.addRRheader(name, Type.SOA, klass, ttl)
341 self.add32bit(serial)
342 self.add32bit(refresh)
344 self.add32bit(expire)
345 self.add32bit(minimum)
347 def addTXT(self, name, klass, ttl, list):
348 self.addRRheader(name, Type.TXT, klass, ttl)
349 if type(list) is types.StringType:
352 self.addstring(txtdata)
354 # Internet specific RRs (section 3.4) -- class = IN
355 def addA(self, name, klass, ttl, address):
356 self.addRRheader(name, Type.A, klass, ttl)
357 self.addaddr(address)
359 def addWKS(self, name, ttl, address, protocol, bitmap):
360 self.addRRheader(name, Type.WKS, Class.IN, ttl)
361 self.addaddr(address)
362 self.addbyte(chr(protocol))
363 self.addbytes(bitmap)
366 raise NotImplementedError
368 def prettyTime(seconds):
370 return seconds,"%d seconds"%(seconds)
372 return seconds,"%d minutes"%(seconds/60)
374 return seconds,"%d hours"%(seconds/3600)
376 return seconds,"%d days"%(seconds/86400)
378 return seconds,"%d weeks"%(seconds/604800)
381 class RRunpacker(Unpacker):
382 def __init__(self, buf):
383 Unpacker.__init__(self, buf)
385 def getRRheader(self):
386 name = self.getname()
387 rrtype = self.get16bit()
388 klass = self.get16bit()
389 ttl = self.get32bit()
390 rdlength = self.get16bit()
391 self.rdend = self.offset + rdlength
392 return (name, rrtype, klass, ttl, rdlength)
394 if self.offset != self.rdend:
395 raise UnpackError, 'end of RR not reached'
396 def getCNAMEdata(self):
397 return self.getname()
398 def getHINFOdata(self):
399 return self.getstring(), self.getstring()
401 return self.get16bit(), self.getname()
403 return self.getname()
404 def getPTRdata(self):
405 return self.getname()
406 def getSOAdata(self):
407 return self.getname(), \
409 ('serial',)+(self.get32bit(),), \
410 ('refresh ',)+prettyTime(self.get32bit()), \
411 ('retry',)+prettyTime(self.get32bit()), \
412 ('expire',)+prettyTime(self.get32bit()), \
413 ('minimum',)+prettyTime(self.get32bit())
414 def getTXTdata(self):
416 while self.offset != self.rdend:
417 list.append(self.getstring())
419 getSPFdata = getTXTdata
421 return self.getaddr()
422 def getWKSdata(self):
423 address = self.getaddr()
424 protocol = ord(self.getbyte())
425 bitmap = self.getbytes(self.rdend - self.offset)
426 return address, protocol, bitmap
427 def getSRVdata(self):
429 _Service._Proto.Name TTL Class SRV Priority Weight Port Target
431 priority = self.get16bit()
432 weight = self.get16bit()
433 port = self.get16bit()
434 target = self.getname()
435 #print '***priority, weight, port, target', priority, weight, port, target
436 return priority, weight, port, target
439 # Pack/unpack Message Header (section 4.1)
441 class Hpacker(Packer):
442 def addHeader(self, id, qr, opcode, aa, tc, rd, ra, z, rcode,
443 qdcount, ancount, nscount, arcount):
445 self.add16bit((qr&1)<<15 | (opcode&0xF)<<11 | (aa&1)<<10
446 | (tc&1)<<9 | (rd&1)<<8 | (ra&1)<<7
447 | (z&7)<<4 | (rcode&0xF))
448 self.add16bit(qdcount)
449 self.add16bit(ancount)
450 self.add16bit(nscount)
451 self.add16bit(arcount)
453 class Hunpacker(Unpacker):
456 flags = self.get16bit()
457 qr, opcode, aa, tc, rd, ra, z, rcode = (
466 qdcount = self.get16bit()
467 ancount = self.get16bit()
468 nscount = self.get16bit()
469 arcount = self.get16bit()
470 return (id, qr, opcode, aa, tc, rd, ra, z, rcode,
471 qdcount, ancount, nscount, arcount)
474 # Pack/unpack Question (section 4.1.2)
476 class Qpacker(Packer):
477 def addQuestion(self, qname, qtype, qclass):
480 self.add16bit(qclass)
482 class Qunpacker(Unpacker):
483 def getQuestion(self):
484 return self.getname(), self.get16bit(), self.get16bit()
487 # Pack/unpack Message(section 4)
488 # NB the order of the base classes is important for __init__()!
490 class Mpacker(RRpacker, Qpacker, Hpacker):
493 class Munpacker(RRunpacker, Qunpacker, Hunpacker):
497 # Routines to print an unpacker to stdout, for debugging.
498 # These affect the unpacker's current position!
502 (id, qr, opcode, aa, tc, rd, ra, z, rcode,
503 qdcount, ancount, nscount, arcount) = u.getHeader()
505 print 'qr=%d, opcode=%d, aa=%d, tc=%d, rd=%d, ra=%d, z=%d, rcode=%d,' \
506 % (qr, opcode, aa, tc, rd, ra, z, rcode)
507 if tc: print '*** response truncated! ***'
508 if rcode: print '*** nonzero error code! (%d) ***' % rcode
509 print ' qdcount=%d, ancount=%d, nscount=%d, arcount=%d' \
510 % (qdcount, ancount, nscount, arcount)
511 for i in range(qdcount):
512 print 'QUESTION %d:' % i,
514 for i in range(ancount):
515 print 'ANSWER %d:' % i,
517 for i in range(nscount):
518 print 'AUTHORITY RECORD %d:' % i,
520 for i in range(arcount):
521 print 'ADDITIONAL RECORD %d:' % i,
526 def __init__(self,u,args):
537 print '; <<>> PDG.py 1.0 <<>> %s %s'%(self.args['name'],
543 print ';; options: '+opt
544 print ';; got answer:'
545 print ';; ->>HEADER<<- opcode %s, status %s, id %d'%(
546 h['opcode'],h['status'],h['id'])
547 flags=filter(lambda x,h=h:h[x],('qr','aa','rd','ra','tc'))
548 print ';; flags: %s; Ques: %d, Ans: %d, Auth: %d, Addit: %d'%(
549 string.join(flags),h['qdcount'],h['ancount'],h['nscount'],
551 print ';; QUESTIONS:'
552 for q in self.questions:
553 print ';; %s, type = %s, class = %s'%(q['qname'],q['qtypestr'],
557 for a in self.answers:
558 print '%-20s %-6s %-6s %s'%(a['name'],`a['ttl']`,a['typename'],
561 print ';; AUTHORITY RECORDS:'
562 for a in self.authority:
563 print '%-20s %-6s %-6s %s'%(a['name'],`a['ttl']`,a['typename'],
566 print ';; ADDITIONAL RECORDS:'
567 for a in self.additional:
568 print '%-20s %-6s %-6s %s'%(a['name'],`a['ttl']`,a['typename'],
571 if self.args.has_key('elapsed'):
572 print ';; Total query time: %d msec'%self.args['elapsed']
573 print ';; To SERVER: %s'%(self.args['server'])
574 print ';; WHEN: %s'%time.ctime(time.time())
577 (self.header['id'], self.header['qr'], self.header['opcode'],
578 self.header['aa'], self.header['tc'], self.header['rd'],
579 self.header['ra'], self.header['z'], self.header['rcode'],
580 self.header['qdcount'], self.header['ancount'],
581 self.header['nscount'], self.header['arcount']) = u.getHeader()
582 self.header['opcodestr']=Opcode.opcodestr(self.header['opcode'])
583 self.header['status']=Status.statusstr(self.header['rcode'])
584 for i in range(self.header['qdcount']):
585 #print 'QUESTION %d:' % i,
586 self.questions.append(self.storeQ(u))
587 for i in range(self.header['ancount']):
588 #print 'ANSWER %d:' % i,
589 self.answers.append(self.storeRR(u))
590 for i in range(self.header['nscount']):
591 #print 'AUTHORITY RECORD %d:' % i,
592 self.authority.append(self.storeRR(u))
593 for i in range(self.header['arcount']):
594 #print 'ADDITIONAL RECORD %d:' % i,
595 self.additional.append(self.storeRR(u))
599 q['qname'], q['qtype'], q['qclass'] = u.getQuestion()
600 q['qtypestr']=Type.typestr(q['qtype'])
601 q['qclassstr']=Class.classstr(q['qclass'])
606 r['name'],r['type'],r['class'],r['ttl'],r['rdlength'] = u.getRRheader()
607 r['typename'] = Type.typestr(r['type'])
608 r['classstr'] = Class.classstr(r['class'])
609 #print 'name=%s, type=%d(%s), class=%d(%s), ttl=%d' \
612 # klass, Class.classstr(class),
614 mname = 'get%sdata' % r['typename']
615 if hasattr(u, mname):
616 r['data']=getattr(u, mname)()
618 r['data']=u.getbytes(r['rdlength'])
622 qname, qtype, qclass = u.getQuestion()
623 print 'qname=%s, qtype=%d(%s), qclass=%d(%s)' \
625 qtype, Type.typestr(qtype),
626 qclass, Class.classstr(qclass))
629 name, type, klass, ttl, rdlength = u.getRRheader()
630 typename = Type.typestr(type)
631 print 'name=%s, type=%d(%s), class=%d(%s), ttl=%d' \
634 klass, Class.classstr(klass),
636 mname = 'get%sdata' % typename
637 if hasattr(u, mname):
638 print ' formatted rdata:', getattr(u, mname)()
640 print ' binary rdata:', u.getbytes(rdlength)
642 if __name__ == "__main__":
646 # Revision 1.11.2.8 2011/03/16 20:06:39 customdesigned
647 # Refer to explicit LICENSE file.
649 # Revision 1.11.2.7 2009/06/09 18:39:06 customdesigned
650 # Built-in SPF support
652 # Revision 1.11.2.6 2008/10/15 22:34:06 customdesigned
653 # Default to idna encoding.
655 # Revision 1.11.2.5 2008/09/17 17:35:14 customdesigned
656 # Use 7-bit ascii encoding, because case folding needs to be disabled
657 # before utf8 is safe to use, even experimentally.
659 # Revision 1.11.2.4 2008/09/17 16:09:53 customdesigned
660 # Encode unicode labels as UTF-8
662 # Revision 1.11.2.3 2007/05/22 20:27:40 customdesigned
663 # Fix unpacker underflow.
665 # Revision 1.11.2.2 2007/05/22 20:25:53 customdesigned
666 # Use socket.inetntoa,inetaton.
668 # Revision 1.11.2.1 2007/05/22 20:20:39 customdesigned
669 # Mark utf-8 encoding
671 # Revision 1.11 2002/03/19 13:05:02 anthonybaxter
672 # converted to class based exceptions (there goes the python1.4 compatibility :)
674 # removed a quite gross use of 'eval()'.
676 # Revision 1.10 2002/03/19 12:41:33 anthonybaxter
677 # tabnannied and reindented everything. 4 space indent, no tabs.
680 # Revision 1.9 2002/03/19 10:30:33 anthonybaxter
681 # first round of major bits and pieces. The major stuff here (summarised
682 # from my local, off-net CVS server :/ this will cause some oddities with
685 # tests/testPackers.py:
686 # a large slab of unit tests for the packer and unpacker code in DNS.Lib
689 # placeholder for addSRV.
690 # added 'klass' to addA, make it the same as the other A* records.
691 # made addTXT check for being passed a string, turn it into a length 1 list.
692 # explicitly check for adding a string of length > 255 (prohibited).
693 # a bunch of cleanups from a first pass with pychecker
694 # new code for pack/unpack. the bitwise stuff uses struct, for a smallish
695 # (disappointly small, actually) improvement, while addr2bin is much
699 # added DiscoverNameServers. This automatically does the right thing
700 # on unix/ win32. No idea how MacOS handles this. *sigh*
701 # Incompatible change: Don't use ParseResolvConf on non-unix, use this
703 # a bunch of cleanups from a first pass with pychecker
705 # Revision 1.8 2001/08/09 09:08:55 anthonybaxter
706 # added identifying header to top of each file
708 # Revision 1.7 2001/07/19 07:50:44 anthony
709 # Added SRV (RFC 2782) support. Code from Michael Ströder.
711 # Revision 1.6 2001/07/19 07:39:18 anthony
712 # 'type' -> 'rrtype' in getRRheader(). Fix from Michael Ströder.
714 # Revision 1.5 2001/07/19 07:34:19 anthony
715 # oops. glitch in storeRR (fixed now).
716 # Reported by Bastian Kleineidam and by greg lin.
718 # Revision 1.4 2001/07/19 07:16:42 anthony
719 # Changed (opcode&0xF)<<11 to (opcode*0xF)<<11.
720 # Patch from Timothy J. Miller.
722 # Revision 1.3 2001/07/19 06:57:07 anthony