示例#1
0
def _replay(pcap_path: str, dst_ip: str, dst_lidar_port: int,
            dst_imu_port: int) -> Iterator[bool]:
    """Replay UDP packets out over the network.

    Todo:
        Not really sure about this interface

    Args:
        pcap_path: Path to the pcap file to replay
        dst_ip: IP to send packets to
        dst_lidar_port: Destination port for lidar packets
        dst_imu_port: Destination port for imu packets

    Returns:
        An iterator that reports whether packets were sent successfully as it's
        consumed.
    """

    pcap_info = _pcap.replay_get_pcap_info(pcap_path, 10000)
    pcap_port_guess = _guess_ports(pcap_info)

    pcap_handle = _pcap.replay_initialize(pcap_path, dst_ip, dst_ip, {
        pcap_port_guess[0]: dst_lidar_port,
        pcap_port_guess[1]: dst_imu_port
    })

    try:
        packet_info = _pcap.packet_info()
        while _pcap.next_packet_info(pcap_handle, packet_info):
            yield _pcap.replay_packet(pcap_handle)
    finally:
        _pcap.replay_uninitialize(pcap_handle)

    yield False
示例#2
0
def _pcap_info(path: str) -> Iterator[_pcap.packet_info]:
    handle = _pcap.replay_initialize(path)
    try:
        while True:
            if handle is None:
                raise ValueError("I/O operation on closed packet source")
            packet_info = _pcap.packet_info()
            if not _pcap.next_packet_info(handle, packet_info):
                break
            yield packet_info
    finally:
        if handle:
            _pcap.replay_uninitialize(handle)
示例#3
0
def test_read_write_lidar_imu(lidar_packets, imu_packets):
    lidar_size = 12608
    imu_size = 48
    frag_size = 1480
    lidar_src_port = 7000
    lidar_dst_port = 7001
    imu_src_port = 8000
    imu_dst_port = 8001
    src_address = "127.0.0.1"
    dst_address = src_address
    lidar_buf = bytearray(lidar_size)
    imu_buf = bytearray(imu_size)

    tmp_dir = tempfile.mkdtemp()
    file_path = os.path.join(tmp_dir, "pcap_test.pcap")

    try:
        lidar_data, record = write_test_pcap(file_path, lidar_packets,
                                             lidar_size, frag_size,
                                             lidar_src_port, lidar_dst_port)
        imu_data, record = write_test_pcap(file_path,
                                           imu_packets,
                                           imu_size,
                                           frag_size,
                                           imu_src_port,
                                           imu_dst_port,
                                           record=record)
        _pcap.record_uninitialize(record)
        lidar_result = bytearray()
        imu_result = bytearray()
        pcap_read = _pcap.replay_initialize(file_path, src_address,
                                            dst_address, {})

        info = _pcap.packet_info()
        while _pcap.next_packet_info(pcap_read, info):
            if info.dst_port == imu_dst_port:
                _pcap.read_packet(pcap_read, imu_buf)
                imu_result += imu_buf
            if info.dst_port == lidar_dst_port:
                _pcap.read_packet(pcap_read, lidar_buf)
                lidar_result += lidar_buf

        assert lidar_data == lidar_result
        assert imu_data == imu_result

        _pcap.replay_uninitialize(pcap_read)
    finally:
        os.remove(file_path)
        os.rmdir(tmp_dir)
示例#4
0
def _guess_ports(pcap_path: str) -> Tuple[Optional[int], Optional[int]]:
    p = _pcap.replay_initialize(pcap_path)
    packet_info = _pcap.packet_info()
    loop = True
    count = 10000
    sizes: Dict[int, Dict[int, int]] = {}

    while loop and count > 0:
        if not _pcap.next_packet_info(p, packet_info):
            loop = False
        else:
            if packet_info.payload_size not in sizes:
                sizes[packet_info.payload_size] = {}
            if packet_info.dst_port not in sizes[packet_info.payload_size]:
                sizes[packet_info.payload_size][packet_info.dst_port] = 0
            sizes[packet_info.payload_size][packet_info.dst_port] += 1
        count -= 1
    _pcap.replay_uninitialize(p)

    return _guess_lidar_port(sizes), _guess_imu_port(sizes)
示例#5
0
 def close(self) -> None:
     """Release resources. Thread-safe."""
     with self._lock:
         if self._handle is not None:
             _pcap.replay_uninitialize(self._handle)
             self._handle = None