def from_values(cls, info, offer_hops): inst = cls() inst.info = info inst.offer_hops = offer_hops # Pad number of offer hops to a full line inst.offer_hops += calc_padding(offer_hops, cls.OFFERS_PER_LINE) for _ in range(inst.offer_hops): inst.offers.append(BWClass()) return inst
def pack(self): packed = [] packed.append(struct.pack("!B", self.path_type)) path_packed = self.path.pack() packed.append(path_packed) # Add possible padding. packed.append(bytes(calc_padding(len(path_packed) - 4, self.LINE_LEN))) raw = b"".join(packed) self._check_len(raw) return raw
def from_pkt(cls, pkt, if_id, ingress, rev_info): rev_token = rev_info.pack() inst = cls() padding_length = calc_padding(inst.LEN + len(rev_token), LINE_LEN) rev_token += bytes(padding_length) iof_offset, hof_offset = inst._calc_offsets(pkt) inst._set_vals((iof_offset, hof_offset, if_id, ingress)) inst.rev_info = rev_token return inst
def from_pkt(cls, pkt, if_id, ingress, srev_info): rawRev = srev_info.pack() inst = cls() padding_length = calc_padding(inst.LEN + len(rawRev), LINE_LEN) rawRev += bytes(padding_length) iof_offset, hof_offset = inst._calc_offsets(pkt) inst._set_vals((iof_offset, hof_offset, if_id, ingress)) inst.srev_info = rawRev return inst
def calc_lens(cls, dst_type, src_type): try: data_len = SCIONAddr.calc_len(dst_type) except HostAddrInvalidType: raise SCMPBadDstType( "Unsupported dst address type: %s" % dst_type) from None try: data_len += SCIONAddr.calc_len(src_type) except HostAddrInvalidType: raise SCMPBadSrcType( "Unsupported src address type: %s" % src_type) from None pad_len = calc_padding(data_len, cls.BLK_SIZE) total_len = data_len + pad_len assert total_len % cls.BLK_SIZE == 0 return total_len, pad_len
def from_pkt(cls, class_, type_, pkt, *args, **kwargs): inst = cls() inst.info = build_scmp_info(class_, type_, pkt, *args, **kwargs) inc_list = scmp_get_inc_parts(class_, type_) if SCMPIncParts.CMN in inc_list: inst._cmn_hdr = pkt.cmn_hdr.pack() if SCMPIncParts.ADDRS in inc_list: inst._addrs = pkt.addrs.pack() if SCMPIncParts.PATH in inc_list: inst._path = pkt.path.pack() if SCMPIncParts.EXTS in inc_list: inst._exts = pkt.pack_exts() if SCMPIncParts.L4 in inc_list: if pkt.l4_hdr: inst._l4_hdr = pkt.l4_hdr.pack(b"") inst.l4_proto = pkt.l4_hdr.TYPE else: payload = pkt.get_payload() pld = payload.pack()[:LINE_LEN * 4] inst._l4_hdr = pld + bytes(calc_padding(len(pld), LINE_LEN)) inst.l4_proto = L4Proto.NONE else: inst.l4_proto = L4Proto.NONE return inst
def _calc_fmt(self, vlen): return "!H%ss%sx" % (vlen, calc_padding(self.VLEN_LEN + vlen, LINE_LEN))
def _calc_len(self, vlen): # pragma: no cover l = self.VLEN_LEN + vlen l += calc_padding(l, LINE_LEN) return l
def _check(self, length, expected): ntools.eq_(calc_padding(length, 8), expected)