def test_gtp_ping(self): """ Send a gtp request packet from a source port to a destination port and verify that the pkt recvd on the destination port is the same as the source packet. """ test_tun_id = 2087 self.create_match_flow(test_tun_id, self._port_no[self.SRC_NAME], self._port_no[self.DST_NAME]) gtp = IP(src=self.TEST_IPS[self.SRC_NAME], dst=self.TEST_IPS[self.DST_NAME]) / \ UDP(dport=self.GTP_C_PORT) / GTPHeader(teid=test_tun_id) / \ GTPCreatePDPContextRequest() src_socket = L2Socket(iface=self.SRC_NAME) dst_socket = L2Socket(iface=self.DST_NAME) sent_len = src_socket.send(gtp) recv = dst_socket.recv() self.assertEqual(sent_len, len(recv)) print(sent_len)
def send_icmp6(icmp6_pkt, iface, hwsrcaddr=None): """Send an ICMPv6 packet to a specific interface This does not take into account routing table, so that allows sending packets to multicast address on the specified interface """ s = L2Socket(iface=iface) # Add IPv6 header if it didn't exist if IPv6 not in icmp6_pkt: icmp6_pkt = IPv6() / icmp6_pkt try: s.send(Ether(src=hwsrcaddr) / icmp6_pkt) finally: s.close()
def __init__(self, batch_size, sending_interval, wireless_interface, data_store): """ Initialize an aDTN instance and its respective key manager and message store, as well as a sending message pool from which the next sending batch gets generated. Define aDTNInnerPacket to be the payload of aDTNPacket. Define aDTNPacket to be the payload of Ethernet frames of type 0xcafe. Set up a scheduler to handle message sending. Define a thread to handle received messages. The wireless interface should be previously set to ad-hoc mode and its ESSID should be the same in other devices running aDTN. :param batch_size: number of packets to transmit at each sending operation :param sending_interval: number of seconds between two sending operations :param wireless_interface: wireless interface to send and receive packets """ self._batch_size = batch_size self._sending_freq = sending_interval self._wireless_interface = wireless_interface self._km = KeyManager() self.data_store = DataStore(data_store) self._sending_pool = [] self._scheduler = sched.scheduler(time, sleep) self._sending = None self._sniffing = None self._thread_send = None self._thread_receive = None self._sent_pkt_counter = None self._received_pkt_counter = None self._decrypted_pkt_counter = None self._start_time = None self._mac_address = macget(getcard(wireless_interface)) self._sending_socket = L2Socket(iface=self._wireless_interface) bind_layers(aDTNPacket, aDTNInnerPacket) bind_layers(Ether, aDTNPacket, type=ETHERTYPE) log_debug("MAC address in use: {}".format(self._mac_address)) self._stats_file_name = '{}_{}.stats'.format(batch_size, sending_interval)
if __name__ == '__main__': try: opts, args = getopt.getopt(sys.argv[1:], 'hi:t:s:p:m:g:u:l:') except getopt.GetoptError as e: print(str(e)) showhelp() exit(1) opts = dict([(k.lstrip('-'), v) for (k, v) in opts]) sul, mode, iface = set_up_sul(opts) # TODO: Add bpf filter, ether host = local mac or broadcast s = L2Socket(iface=iface, filter=None, nofilter=0, type=ETH_P_ALL) rdpipe, wrpipe = os.pipe() rdpipe = os.fdopen(rdpipe) wrpipe = os.fdopen(wrpipe, 'w') sul.sniffPipe = rdpipe logger = None if 'l' in opts: log_file = opts.get('l') logger = Logger(log_file) sniffer_pid = 0 query_pid = 0