def mk_new_obj(type_in, data_in): # Make pi and mom for a new object if isinstance(data_in, ffi.CData): mom = data_in; in_len = ffi.sizeof(data_in) elif isinstance(data_in, bytearray): mom = ffi.new("uint8_t[]", tuple(data_in)); in_len = len(data_in) elif str(type(data_in))[-6:-2] == '_obj': pi_in = data_in.pi if pi_in.o_type < TYPE_L3 or pi_in.o_type >= TYPE_L4: raise PltError("Expected a layer 3 plt object") if pi_in.l3_rem != 0: # We have l3 data pi_new = ffi.new("struct pi *") if lib.get_trans_payload(pi_new, data_in.pi): # Data_dump(pi_new, data_in.mom, "pi_new") return pi_new, data_in.mom raise PltError("Layer 3 plt object has no payload") raise PltError("Expected a plt object with layer 3 data") else: raise TypeError("Expected 'cdata uint8_t[]' or bytearray!") pi = new_pi(type_in, KIND_CPY, mom, # type, kind, data mom, in_len, 0, mom, in_len) # l3p, l3_rem, proto, dp, rem if type_in == TYPE_IP: pi.proto = mom[9] elif type_in == TYPE_IP6: if not lib.get_ip6_proto(pi): raise PltError("Couldn't get proto for IP6 object") return pi, mom
def get_udp_payload(self): self.check_obj_l3() new_pi = ffi.new("struct pi *") if lib.get_trans_payload(new_pi, self.pi): if new_pi.proto == 17: return _udp_obj(new_pi, self.mom).payload return None
def get_icmp6(self): self.check_obj_l3() if self.pi.ethertype == 0x86DD: new_pi = ffi.new("struct pi *") if lib.get_trans_payload(new_pi, self.pi): if new_pi.proto == 58: return _icmp6_obj(new_pi, self.mom) return None
def get_icmp(self): self.check_obj_l3() if self.pi.ethertype == 0x0800: # Data_dump(self.pi, self.mom, "starting get_icmp") new_pi = ffi.new("struct pi *") if lib.get_trans_payload(new_pi, self.pi): # Data_dump(new_pi, self.mom, "get_icmp new_pi") if new_pi.proto == 1: return _icmp_obj(new_pi, self.mom) return None
def get_payload(self): new_pi = ffi.new("struct pi *") if lib.get_trans_payload(new_pi, self.pi): return new_pi.dp[0:new_pi.rem] return None