Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _dissect(self, buf):
# update dynamic header parts. buf: 1010???? -clear reserved-> 1010 -> *4
ol = ((buf[12] >> 4) << 2) - 20 # dataoffset - TCP-standard length
if ol > 0:
# parse options, add offset-length to standard-length
opts_bytes = buf[20: 20 + ol]
self._init_triggerlist("opts", opts_bytes, self._parse_opts)
elif ol < 0:
raise Exception("invalid header length")
ports = [unpack_H(buf[0:2])[0], unpack_H(buf[2:4])[0]]
try:
# source or destination port should match
# logger.debug("TCP handler: %r" % self._id_handlerclass_dct[TCP])
htype = [x for x in ports if x in self._id_handlerclass_dct[TCP]][0]
#logger.debug("TCP: trying to set handler, type: %d = %s" %
#(type, self._id_handlerclass_dct[TCP][type]))
self._init_handler(htype, buf[20 + ol:])
except:
# no type found
pass
return 20 + ol
def fletcher32(data_to_checksum, amount_words):
# 1 word = 2 Bytes
sum1 = 0xFFFF
sum2 = 0xFFFF
datapos = 0
while amount_words > 0:
tlen = 359 if amount_words > 359 else amount_words
amount_words -= tlen
while tlen > 0:
# sum1 += unpack_word_be(data_to_checksum[datapos:datapos+2])[0]
# print("%d" % sum1)
sum1 += unpack_H(data_to_checksum[datapos: datapos + 2])[0]
datapos += 2
sum2 += sum1
# print("%d" % sum1)
# print("%d" % sum2)
# print("--")
tlen -= 1
sum1 = (sum1 & 0xFFFF) + (sum1 >> 16)
sum2 = (sum2 & 0xFFFF) + (sum2 >> 16)
# Second reduction step to reduce sums to 16 bits
sum1 = (sum1 & 0xFFFF) + (sum1 >> 16)
sum2 = (sum2 & 0xFFFF) + (sum2 >> 16)
return (sum2 << 16) | sum1
def _dissect(self, buf):
ports = [unpack_H(buf[0:2])[0], unpack_H(buf[2:4])[0]]
try:
# source or destination port should match
htype = [x for x in ports if x in pypacker.Packet._id_handlerclass_dct[UDP]][0]
self._init_handler(htype, buf[8:])
except:
# no type found
# logger.debug("could not parse type: %d because: %s" % (type, e))
pass
return 8
def _parse_icmp6opt(buf):
# TODO: create generic TLV-parser
opts = []
off = 0
while off < len(buf):
optlen = unpack_H(buf[1:3])[0] * 8
opt = ICMP6.ICMPv6Opt(buf[off: off + optlen])
opts.append(opt)
off += optlen
return opts
def count_and_dissect_tlvs(buf):
"""
Count and dissect TLVs. Return length of LLDP layer
buf -- buffer to dissect
return -- parsed_bytes_total, [(clz, bts), ...]
"""
shift = 0
tlv_type, tlv_len = 1, 1
clz_bts_list = []
while (tlv_type | tlv_len) != 0:
type_and_len = unpack_H(buf[shift:shift + TLV_HEADER_LEN])[0]
# get tlv length and type
tlv_type = (type_and_len & TYPE_MASK) >> LENGTH_FIELD_BITS
tlv_len = type_and_len & LENGTH_MASK
if tlv_type != ORG_SPEC_TYPE:
clz = LLDP_TLV_CLS.get(tlv_type, LLDPGeneric)
else:
oui_subtype = unpack_I(buf[shift + TLV_HEADER_LEN:shift + ORG_SPEC_HEADER_LEN + TLV_HEADER_LEN])[0]
oui = (oui_subtype & OUI_MASK) >> SUBTYPE_LEN_BITS
subtype = oui_subtype & SUBTYPE_MASK
clz = LLDP_ORG_SPEC_TLV_CLS.get((oui, subtype), LLDPOrgSpecGeneric)
# get body bytes
tlv_body = buf[shift: tlv_len + shift + TLV_HEADER_LEN]
# update shift to begin of next TLV (TLV_HEADER_LEN:2 + content:x)
shift += TLV_HEADER_LEN + tlv_len
clz_bts_list.append((clz, tlv_body))
def _dissect(self, buf):
htype = unpack_H(buf[14: 16])[0]
# logger.debug("type: %X" % type)
self._init_handler(htype, buf[16:])
return 16
def __get_time(self):
return [unpack_H(x)[0] for x in self.time]
def _dissect(self, buf):
hlen = 2
opcode = unpack_H(buf[: 2])
# logger.debug("opcode: %d" % opcode)
if opcode in OPCODES_DATA_ACK:
pass
elif opcode in OPCODES_READ_WRITE:
self.block = None
file, ttype = split_nullbyte(buf[2:], maxsplit=2)
# logger.debug("file/ttype = %r / %r" % (file, ttype))
self.file = file + b"\x00"
self.ttype = ttype + b"\x00"
hlen = 2 + len(self.file) + len(self.ttype)
elif opcode == OP_ERR:
# TODO: update
pass
return hlen
def __parse_attrs(buf):
attributes = []
off = 0
# t:2 l:2 v:x
while off < len(buf):
l_content = unpack_H(buf[off + 2: off + 4])[0]
padding = (4 - (l_content % 4)) % 4
l_total = l_content + padding + 2 + 2
#logger.debug("STUN attr l_content: %d, padding: %d, value: %s" %
# (l_content, padding, buf[off : off + l_total]))
attributes.append(StunAttr(buf[off: off + l_total]))
off += l_total
return attributes
def _dissect(self, buf):
# withdrawn Routes
off = 4
off_end = off + unpack_H(buf[:2])[0]
while off < off_end:
rlen = 3 + 0
route = Route(buf[off:])
self.wroutes.append(route)
off += rlen
# path attributes
off_end = off + unpack_H(buf[2:4])[0]
#logger.debug("unpacking attributes")
while off < off_end:
alen = 3 + buf[off + 2]
#logger.debug("bytes for attribute: %r" % buf[off: off + alen])
attr = BGP.Update.Attribute(buf[off: off + alen])
self.pathattrs.append(attr)
off += alen
# announced routes
off_end = len(buf)
#logger.debug("unpacking routes")
while off < off_end:
rlen = 3 + 0
#logger.debug("bytes for route: %r" % buf[off:off + rlen])