def unpack_rdata(self, buf, off): if self.type == DNS_A: self.ip = self.rdata elif self.type == DNS_NS or self.type == DNS_CNAME or \ self.type == DNS_PTR: self.name, off = unpack_name(buf, off) elif self.type == DNS_SOA: self.mname, off = unpack_name(buf, off) self.rname, off = unpack_name(buf, off) self.serial, self.refresh, self.retry, self.expire, \ self.minimum = _st_unpack('>IIIII', buf[off:off+20]) elif self.type == DNS_MX: self.preference = _st_unpack('>H', self.rdata[:2]) self.exchange, off = unpack_name(buf, off+2) elif self.type == DNS_TXT or self.type == DNS_HINFO: self.text = [] buf = self.rdata while buf: n = ord(buf[0]) self.text.append(buf[1:1+n]) buf = buf[1+n:] elif self.type == DNS_AAAA: self.ip6 = self.rdata elif self.type == DNS_SRV: self.priority, self.weight, self.port = \ _st_unpack('>HHH', self.rdata[:6]) self.target, off = unpack_name(buf, off+6)
def unpack_rdata(self, buf, off): if self.type == DNS_A: self.ip = self.rdata elif self.type == DNS_NS or self.type == DNS_CNAME or \ self.type == DNS_PTR: self.name, off = unpack_name(buf, off) elif self.type == DNS_SOA: self.mname, off = unpack_name(buf, off) self.rname, off = unpack_name(buf, off) self.serial, self.refresh, self.retry, self.expire, \ self.minimum = _st_unpack('>IIIII', buf[off:off+20]) elif self.type == DNS_MX: self.preference = _st_unpack('>H', self.rdata[:2]) self.exchange, off = unpack_name(buf, off + 2) elif self.type == DNS_TXT or self.type == DNS_HINFO: self.text = [] buf = self.rdata while buf: n = ord(buf[0]) self.text.append(buf[1:1 + n]) buf = buf[1 + n:] elif self.type == DNS_AAAA: self.ip6 = self.rdata elif self.type == DNS_SRV: self.priority, self.weight, self.port = \ _st_unpack('>HHH', self.rdata[:6]) self.target, off = unpack_name(buf, off + 6)
def unpack(self, buf): """Unpack packet header fields from buf, and set self.data.""" for k, v in _it_izip( self.__slots__, _st_unpack(self.__hdr_fmt__, buf[:self.__hdr_len__])): setattr(self, k, v) self.data = buf[self.__hdr_len__:]
def unpack_qd(self, buf, off): """Return DNS question and new offset.""" q = self.Q() q.name, off = self.unpack_name(buf, off) q.type, q.cls = _st_unpack('>HH', buf[off:off+4]) off += 4 return q, off
def unpack_qd(self, buf, off): """Return DNS question and new offset.""" q = self.Q() q.name, off = self.unpack_name(buf, off) q.type, q.cls = _st_unpack('>HH', buf[off:off + 4]) off += 4 return q, off
def unpack_rr(self, buf, off): """Return DNS RR and new offset.""" rr = self.RR() rr.name, off = self.unpack_name(buf, off) rr.type, rr.cls, rr.ttl, rdlen = _st_unpack('>HHIH', buf[off:off+10]) off += 10 rr.rdata = buf[off:off+rdlen] rr.unpack_rdata(buf, off) off += rdlen return rr, off
def unpack_rr(self, buf, off): """Return DNS RR and new offset.""" rr = self.RR() rr.name, off = self.unpack_name(buf, off) rr.type, rr.cls, rr.ttl, rdlen = _st_unpack('>HHIH', buf[off:off + 10]) off += 10 rr.rdata = buf[off:off + rdlen] rr.unpack_rdata(buf, off) off += rdlen return rr, off
def unpack_rdata(self, buf, off): if self.type == NS_A: self.ip = self.rdata elif self.type == NS_NBSTAT: num = ord(self.rdata[0]) off = 1 l = [] for i in range(num): name = self.rdata[off:off+15].split(None, 1)[0].split('\x00', 1)[0] service = ord(self.rdata[off+15]) off += 16 flags = _st_unpack('>H', self.rdata[off:off+2])[0] off += 2 l.append((name, service, flags)) self.nodenames = l
def unpack(self, buf): """Unpack packet header fields from buf, and set self.data.""" res = list(_st_unpack(self.__hdr_fmt__, buf[:self.__hdr_len__])) for e, k in enumerate(self.__slots__): sfmt = self.__hdr__[e][1] mat = intchr.match(sfmt) if mat and mat.group('chr') != 's': cnt = int(mat.group('int')) setattr(self, k, list(res[:cnt])) del res[:cnt] else: if sfmt[-1] == 's': i = res[0].find('\x00') if i != -1: res[0] = res[0][:i] setattr(self, k, res[0]) del res[0] assert len(res) == 0 self.data = buf[self.__hdr_len__:]
def unpack_name(buf, off): name = '' saved_off = 0 for i in range(100): # XXX n = ord(buf[off]) if n == 0: off += 1 break elif (n & 0xc0) == 0xc0: ptr = _st_unpack('>H', buf[off:off+2])[0] & 0x3fff off += 2 if not saved_off: saved_off = off # XXX - don't use recursion!@#$ name = name + unpack_name(buf, ptr)[0] + '.' break else: off += 1 name = name + buf[off:off+n] + '.' if len(name) > 255: raise ValueError, 'name longer than 255 bytes' off += n return name.strip('.'), off
def unpack_name(buf, off): name = '' saved_off = 0 for i in range(100): # XXX n = ord(buf[off]) if n == 0: off += 1 break elif (n & 0xc0) == 0xc0: ptr = _st_unpack('>H', buf[off:off + 2])[0] & 0x3fff off += 2 if not saved_off: saved_off = off # XXX - don't use recursion!@#$ name = name + unpack_name(buf, ptr)[0] + '.' break else: off += 1 name = name + buf[off:off + n] + '.' if len(name) > 255: raise ValueError, 'name longer than 255 bytes' off += n return name.strip('.'), off
def unpack(self, buf): # don't bother with data for k, v in _it_izip(self.__slots__, _st_unpack(self.__hdr_fmt__, buf[:self.__hdr_len__])): setattr(self, k, v) self.data = ""
def unpack(self, buf): """Unpack packet header fields from buf, and set self.data.""" for k, v in _it_izip(self.__slots__, _st_unpack(self.__hdr_fmt__, buf[:self.__hdr_len__])): setattr(self, k, v) self.data = buf[self.__hdr_len__:]