def pack_rdata(self, off, label_ptrs): # XXX - yeah, this sux if self.rdata: return self.rdata if self.type == DNS_A: return self.ip elif self.type == DNS_NS: return pack_name(self.nsname, off, label_ptrs) elif self.type == DNS_CNAME: return pack_name(self.cname, off, label_ptrs) elif self.type == DNS_PTR: return pack_name(self.ptrname, off, label_ptrs) elif self.type == DNS_SOA: l = [] l.append(pack_name(self.mname, off, label_ptrs)) l.append(pack_name(self.rname, off + len(l[0]), label_ptrs)) l.append( struct.pack('>IIIII', self.serial, self.refresh, self.retry, self.expire, self.minimum)) return ''.join(l) elif self.type == DNS_MX: return struct.pack('>H', self.preference) + \ pack_name(self.mxname, off + 2, label_ptrs) elif self.type == DNS_TXT or self.type == DNS_HINFO: return ''.join(['%s%s' % (chr(len(x)), x) for x in self.text]) elif self.type == DNS_AAAA: return self.ip6 elif self.type == DNS_SRV: return struct.pack('>HHH', self.priority, self.weight, self.port) + \ pack_name(self.srvname, off + 6, label_ptrs) elif self.type == DNS_OPT: return '' # self.rdata else: raise dpkt.PackError('RR type %s is not supported' % self.type)
def pack_hdr(self): try: if self.p > 0xff: return struct.pack('>H', self.p) return dpkt.Packet.pack_hdr(self) except struct.error, e: raise dpkt.PackError(str(e))
def pack_hdr(self): try: # Protocol compression is *not* recommended (RFC2516), but we do it anyway if self.p > 0xff: return struct.pack('>H', self.p) return dpkt.Packet.pack_hdr(self) except struct.error, e: raise dpkt.PackError(str(e))
def pack_hdr(self): tags_buf = '' new_type = self.type is_isl = False # ISL wraps Ethernet, this determines order of packing # initial type is based on next layer, pointed by self.data; # try to find an ETH_TYPE matching the data class if isinstance(self.data, dpkt.Packet): new_type = self._typesw_rev.get(self.data.__class__, new_type) if getattr(self, 'mpls_labels', None): # mark all labels with s=0, last one with s=1 for lbl in self.mpls_labels: lbl.s = 0 lbl.s = 1 # set encapsulation type if not (self.type == ETH_TYPE_MPLS or self.type == ETH_TYPE_MPLS_MCAST): new_type = ETH_TYPE_MPLS tags_buf = ''.join(lbl.pack_hdr() for lbl in self.mpls_labels) elif getattr(self, 'vlan_tags', None): # set encapsulation types t1 = self.vlan_tags[0] if len(self.vlan_tags) == 1: if isinstance(t1, VLANtag8021Q): t1.type = self.type new_type = ETH_TYPE_8021Q elif isinstance(t1, VLANtagISL): t1.type = 0 # 0 means Ethernet is_isl = True elif len(self.vlan_tags) == 2: t2 = self.vlan_tags[1] if isinstance(t1, VLANtag8021Q) and isinstance( t2, VLANtag8021Q): t2.type = self.type new_type = t1.type = ETH_TYPE_8021Q else: raise dpkt.PackError( 'maximum is 2 VLAN tags per Ethernet frame') tags_buf = ''.join(tag.pack_hdr() for tag in self.vlan_tags) # if self.data is LLC then this is IEEE 802.3 Ethernet and self.type # then actually encodes the length of data if isinstance(self.data, llc.LLC): new_type = len(self.data) hdr_buf = dpkt.Packet.pack_hdr(self)[:-2] + struct.pack('>H', new_type) if not is_isl: return hdr_buf + tags_buf else: return tags_buf + hdr_buf
def pack_addr(addrtype, addr): if addrtype == IP_V4: s = socket.inet_pton(socket.AF_INET, addr) # @UndefinedVariable elif addrtype == IP_V6: s = socket.inet_pton(socket.AF_INET6, addr) # @UndefinedVariable elif addrtype == DOMAIN_NAME: s = struct.pack('B', len(addr)) s += addr else: raise dpkt.PackError("Unknown address type %s" % addrtype.encode('hex')) return s
def _do_pack_options(self): if not getattr(self, 'opts', None): return '' if self.opts[-1].code != PCAPNG_OPT_ENDOFOPT: raise dpkt.PackError('options must end with opt_endofopt') return ''.join(str(o) for o in self.opts)
def pack_hdr(self): try: return dpkt.Packet.pack_hdr(self) except struct.error, e: raise dpkt.PackError(str(e))