Wait for next packet captured with a timeout :param timeout: How long to wait for the packet :returns: Captured packet if packet arrived within timeout :raises CaptureTimeoutError: if no packet arrives within timeout
(self, timeout, filter_out_fn=is_ipv6_misc)
| 499 | return enough_data |
| 500 | |
| 501 | def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc): |
| 502 | """ |
| 503 | Wait for next packet captured with a timeout |
| 504 | |
| 505 | :param timeout: How long to wait for the packet |
| 506 | |
| 507 | :returns: Captured packet if packet arrived within timeout |
| 508 | :raises CaptureTimeoutError: if no packet arrives within timeout |
| 509 | """ |
| 510 | deadline = time.time() + timeout |
| 511 | if self._pcap_reader is None: |
| 512 | if not os.path.isfile(self.out_path): |
| 513 | remaining = deadline - time.time() |
| 514 | if remaining <= 0 or not self._wait_for_file_inotify( |
| 515 | self.out_path, remaining |
| 516 | ): |
| 517 | raise CaptureTimeoutError( |
| 518 | "Capture file %s did not appear within timeout" % self.out_path |
| 519 | ) |
| 520 | while time.time() < deadline: |
| 521 | try: |
| 522 | self._pcap_reader = PcapReader(self.out_path) |
| 523 | break |
| 524 | except: |
| 525 | self.test.logger.debug( |
| 526 | "Exception in scapy.PcapReader(%s): %s" |
| 527 | % (self.out_path, format_exc()) |
| 528 | ) |
| 529 | if not self._pcap_reader: |
| 530 | raise CaptureTimeoutError( |
| 531 | "Capture file %s did not appear within timeout" % self.out_path |
| 532 | ) |
| 533 | |
| 534 | poll = False |
| 535 | if timeout > 0: |
| 536 | self.test.logger.debug("Waiting for packet") |
| 537 | else: |
| 538 | poll = True |
| 539 | self.test.logger.debug("Polling for packet") |
| 540 | while time.time() < deadline or poll: |
| 541 | if not self.verify_enough_packet_data_in_pcap(): |
| 542 | self._test.sleep(0) # yield |
| 543 | poll = False |
| 544 | continue |
| 545 | p = self._pcap_reader.recv() |
| 546 | if p is not None: |
| 547 | if filter_out_fn is not None and filter_out_fn(p): |
| 548 | self.test.logger.debug( |
| 549 | "Packet received after %ss was filtered out" |
| 550 | % (time.time() - (deadline - timeout)) |
| 551 | ) |
| 552 | else: |
| 553 | self.test.logger.debug( |
| 554 | "Packet received after %fs" |
| 555 | % (time.time() - (deadline - timeout)) |
| 556 | ) |
| 557 | return p |
| 558 | self._test.sleep(0) # yield |