Accepts a single Packet object and tracks the connection it belongs to. If it is the first packet in a connection, it creates a new Connection object and passes it to connection_init_handler. Otherwise, it will find the existing Connection in self.connection_tracker
(self, packet: "Packet")
| 580 | self._cleanup_connections() |
| 581 | |
| 582 | def _connection_handler(self, packet: "Packet"): |
| 583 | """ |
| 584 | Accepts a single Packet object and tracks the connection it belongs to. |
| 585 | |
| 586 | If it is the first packet in a connection, it creates a new Connection |
| 587 | object and passes it to connection_init_handler. Otherwise, it will |
| 588 | find the existing Connection in self.connection_tracker. |
| 589 | |
| 590 | The Connection will then be passed to connection_handler. |
| 591 | |
| 592 | If a connection changes direction with this packet, blob_handler will |
| 593 | be called. |
| 594 | |
| 595 | Finally, if this packet is a FIN or RST, it will determine if the |
| 596 | connection should close. |
| 597 | """ |
| 598 | # Sort the addr value for consistent dictionary key purposes |
| 599 | connkey = tuple(sorted(packet.addr) + [packet.protocol_num]) |
| 600 | |
| 601 | # If this is a new connection, initialize it and call the init handler |
| 602 | if connkey not in self._connection_tracker: |
| 603 | conn = Connection(packet) |
| 604 | self._connection_tracker[connkey] = conn |
| 605 | try: |
| 606 | self.connection_init_handler(conn) |
| 607 | except Exception as e: |
| 608 | print_handler_exception(e, self, 'connection_init_handler') |
| 609 | return |
| 610 | with self.seen_conn_count.get_lock(): |
| 611 | self.seen_conn_count.value += 1 |
| 612 | else: |
| 613 | conn = self._connection_tracker[connkey] |
| 614 | conn.add_packet(packet) |
| 615 | |
| 616 | # TODO: Do we need this? This flag is set to False when the connection is initialized and not |
| 617 | # set to true until it is closed. |
| 618 | # Is there any scenario where we would want to undo a True handled state? |
| 619 | # # If connection data is about to change, we set it to a "dirty" state |
| 620 | # # for future calls to connection_handler |
| 621 | # if pkt.data: |
| 622 | # conn.handled = False |
| 623 | |
| 624 | if conn.closed: |
| 625 | # Both sides have closed the connection, process blobs (messages) and |
| 626 | # close connection. |
| 627 | for blob in conn.blobs: |
| 628 | self._blob_handler(conn, blob) |
| 629 | self._close_connection(conn, full=True) |
| 630 | |
| 631 | # TODO: Switch to a max_packets option. |
| 632 | # elif len(conn.blobs) > self.maxblobs: |
| 633 | # # Max blobs hit, so we will run connection_handler and decode.py |
| 634 | # # will clear the connection's blob cache |
| 635 | # self._close_connection(conn) |
| 636 | |
| 637 | # Check for and close old connections every so often. |
| 638 | if self.handled_packet_count.value % self.timeout_frequency == 0: |
| 639 | self._timeout_connections(packet.dt) |
no test coverage detected