def work(self, input_items, output_items): out = output_items[0] # Get single IP packet from TUN Device buf = self.tun_handler.read() if self.use_ot_packets: # Create smaller OT packet from IP packet #packet = PacketHandler.to_ot(buf[4:]) packet = PacketHandler.to_ot(buf) else: #packet = dpkt.ip.IP(buf[4:]) packet = dpkt.ip.IP(buf) # TODO: DEBUG message, remove in production print "TUN Source: ", PacketHandler.show(packet) # Write packet to output buffer result = np.fromstring(str(packet), dtype=np.uint8) print "Packetlänge source "+ str(len(result)) out[:len(result)] = result # Write length tag to output buffer self.add_item_tag(0, self.nitems_written(0), pmt.string_to_symbol(self.len_tag_key), pmt.from_long(len(result)), pmt.PMT_NIL) return len(result)
def handle_msg(self, msg): buf = pmt.to_python(msg)[1] # Cast numpy buf to string to prevent doomsday buf = ''.join(map(chr, buf)) if self.use_ot_packets: addr = self.tun_handler.tun.addr dstaddr = self.tun_handler.tun.dstaddr packet = PacketHandler.to_ip(buf, dstaddr, addr) else: packet = dpkt.ip.IP(buf) # TODO: DEBUG message, remove in production print "TUN Sink: ", PacketHandler.show(packet) # Write packet to TUN Device #print len(str(packet)) # TODO: IFF_NO_PI (remove 4 byte) #self.tun_handler.write(' ' * 4 + str(packet)) self.tun_handler.write(str(packet))