How to use the pypacker.triggerlist function in pypacker

To help you get started, we’ve selected a few pypacker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mike01 / pypacker / pypacker / layer12 / lacp.py View on Github external
def get_property_of_state_field(varname):
	"""Create a get/set-property for bits encoding of state field in Actor and Partner TLVs."""
	mask = STATE_FIELD_BIT_ORDERING.get(varname)[0]
	bit_order = STATE_FIELD_BIT_ORDERING.get(varname)[1]
	return property(
		lambda obj: (obj.state & mask) >> bit_order,
		lambda obj, val: obj.__setattr__("state",
			(obj.state & ~mask) | (val << bit_order)),
	)


class LACP(pypacker.Packet):
	__hdr__ = (
		("subtype", "B", 1),
		("version", "B", 1),
		("tlvlist", None, triggerlist.TriggerList),
	)

	def _dissect(self, buf):
		self._init_triggerlist("tlvlist", buf[LACP_HEADER_LEN:], self.__parse_tlv)
		return len(buf)

	@staticmethod
	def __parse_tlv(buf):
		"""Parse LACP TLVs and return them as list."""
		tlvlist = []
		shift = 0
		tlv_type, tlv_len = 1, 1
		while (tlv_type | tlv_len) != 0:
			tlv_type, tlv_len = unpack_BB(buf[shift:shift + 2])
			clz = LACP_TLV_CLS.get(tlv_type)
			if not clz == LACP_TLV_CLS.get(0):
github mike01 / pypacker / pypacker / layer12 / ieee80211.py View on Github external
#
	# mgmt frames
	#
	class Beacon(pypacker.Packet):
		__hdr__ = (
			("dst", "6s", b"\x00" * 6),
			("src", "6s", b"\x00" * 6),
			("bssid", "6s", b"\x00" * 6),
			# 12 Bits: 0->4095 | 4 Bits
			# SF SS (LE)
			("seq_frag", "H", 0),
			# _ts (integer) is saved as LE
			("_ts", "Q", 0),
			("interval", "H", 0x6400),
			("capa", "H", 0x0100),
			("params", None, triggerlist.TriggerList)
		)

		def _get_seq(self):
			return (self.seq_frag & 0xFF) << 4 | (self.seq_frag >> 12)

		def _set_seq(self, val):
			self.seq_frag = (val & 0xF) << 12 | (val & 0xFF0) >> 4 | (self.seq_frag & 0x0F00)

		def _get_ts(self):
			# LE->BE: dirty but simple
			return unpack_Q_le(pack_Q(self._ts))[0]

		def _set_ts(self, val):
			self._ts = unpack_Q_le(pack_Q(val))[0]

		seq = property(_get_seq, _set_seq)
github mike01 / pypacker / pypacker / layer4 / sctp.py View on Github external
class Chunk(pypacker.Packet):
	__hdr__ = (
		("type", "B", INIT),
		("flags", "B", 0),
		("len", "H", 0)		# length of header + data = 4 + x Bytes
	)


class SCTP(pypacker.Packet):
	__hdr__ = (
		("sport", "H", 0),
		("dport", "H", 0),
		("vtag", "I", 0),
		("sum", "I", 0, FIELD_FLAG_AUTOUPDATE),
		("chunks", None, triggerlist.TriggerList)
	)

	# handle padding attribute
	def __get_padding(self):
		try:
			return self._padding
		except:
			return b""

	def __set_padding(self, padding):
		self._padding = padding
	padding = property(__get_padding, __set_padding)

	__handler__ = {
		123: diameter.Diameter,
	}
github mike01 / pypacker / examples / examples_new_protocol.py View on Github external
("src", "4s", b"\xff" * 4),
		("dst", "4s", b"\xff" * 4),
		# Simple constant field, marked for auto update, see _update_fields(...).
		#  Stores the full header length inclusive options.
		("hlen", "H", 14, FIELD_FLAG_AUTOUPDATE),
		# Simple constant field, deactivated (see Ethernet -> vlan)
		# Switching between active/inactive should be avoided because of performance penalty :/
		("idk", "H", None),
		# Again a simple constant field
		("flags", "B", 0),
		# Dynamic field: bytestring format, *can* change in length, see dns.DNS
		# Field type should be avoided because of performance penalty :/
		("yolo", None, b"1234"),
		# TriggerList field: variable length, can contain: raw bytes, key/value-tuples (see HTTP) and Packets (see IP)
		# Here TriggerList will contain key/value Tuples like (b"\x00", b"1")
		("options", None, triggerlist.TriggerList)
	)

	# Conveniant access should be enabled using properties eg using pypacker.get_property_xxx(...)
	src_s = pypacker.get_property_ip4("src")
	dst_s = pypacker.get_property_ip4("dst")
	# xxx_s = pypacker.get_property_mac("xxx")
	# xxx_s = pypacker.get_property_dnsname("xxx")

	# Setting/getting values smaller then 1 Byte should be enabled using properties (see layer3/ip.IP -> v, hl)
	def __get_flag_fluxcapacitor(self):
		return (self.flags & 0x80) >> 15

	def __set_flag_fluxcapacitor(self, value):
		value_shift = (value & 1) << 15
		self.flags = (self.flags & ~0x80) | value_shift
github mike01 / pypacker / pypacker / layer12 / btle.py View on Github external
class ScanRequest(pypacker.Packet):
	__hdr__ = (
		("scan_addr", "6s", b"\xFF" * 6),
		("adv_addr", "6s", b"\xFF" * 6),
	)

	scan_addr_s = property(lambda obj: reverse_bts_to_str(obj.scan_addr))
	adv_addr_s = property(lambda obj: reverse_bts_to_str(obj.adv_addr))


class ScanResponse(pypacker.Packet):
	__hdr__ = (
		("adv_addr", "6s", b"\xFF" * 6),
		("adv_data", None, triggerlist.TriggerList)
	)

	def _dissect(self, buf):
		self._init_triggerlist("adv_data", buf[6:], parse_advdata)
		return len(buf)

	adv_addr_s = property(lambda obj: reverse_bts_to_str(obj.adv_addr))
	adv_data_s = property(lambda obj: reverse_bts_to_str(obj.adv_data))


class ConnRequest(pypacker.Packet):
	__hdr__ = (
		("init_addr", "6s", b"\xFF" * 6),
		("adv_addr", "6s", b"\xFF" * 6),
		("access_addr", "4s", b"\xFF" * 6),
		("crcinit", "3s", b"\xFF" * 3),
github mike01 / pypacker / pypacker / layer12 / ethernet.py View on Github external
def __set_vid(self, value):
		self.tci = self.tci & 0xF000 | value
	vid = property(__get_vid, __set_vid)


# standard or double vlan tag
# ETH_TYPE_TUNNELING as outer tag is NON-standard!
# see: https://en.wikipedia.org/wiki/IEEE_802.1ad
VLAN_TAG_START = {ETH_TYPE_8021Q, ETH_TYPE_PBRIDGE, ETH_TYPE_TUNNELING}


class Ethernet(pypacker.Packet):
	__hdr__ = (
		("dst", "6s", b"\xff" * 6),
		("src", "6s", b"\xff" * 6),
		("vlan", None, triggerlist.TriggerList),
		("type", "H", ETH_TYPE_IP, FIELD_FLAG_AUTOUPDATE | FIELD_FLAG_IS_TYPEFIELD)
	)

	dst_s = pypacker.get_property_mac("dst")
	src_s = pypacker.get_property_mac("src")

	__handler__ = {
		ETH_TYPE_IP: ip.IP,
		ETH_TYPE_ARP: arp.ARP,
		ETH_TYPE_DTP: dtp.DTP,
		ETH_TYPE_IPX: ipx.IPX,
		ETH_TYPE_IP6: ip6.IP6,
		ETH_TYPE_PPOE_DISC: pppoe.PPPoE,
		ETH_TYPE_PPOE_SESS: pppoe.PPPoE,
		ETH_TYPE_PTPv2: ptpv2.PTPv2,
		ETH_TYPE_EFC: flow_control.FlowControl,