Accepts a Packet object and stores it. Args: packet: a Packet object
(self, packet)
| 1887 | # Do we want to ensure all segments are acknowledged or should we avoid that so we can handle |
| 1888 | # partial/corrupt pcaps? |
| 1889 | def add_packet(self, packet): |
| 1890 | """ |
| 1891 | Accepts a Packet object and stores it. |
| 1892 | |
| 1893 | Args: |
| 1894 | packet: a Packet object |
| 1895 | """ |
| 1896 | # Clear old data and segment cache. |
| 1897 | self._data = None |
| 1898 | self._segments = None |
| 1899 | |
| 1900 | seq = packet.sequence_number |
| 1901 | |
| 1902 | # If packet is not TCP just add packet to list. |
| 1903 | if seq is None: |
| 1904 | self.packets.append(packet) |
| 1905 | if packet.ts < self.starttime: self.starttime = packet.ts |
| 1906 | if packet.ts > self.endtime: self.endtime = packet.ts |
| 1907 | return |
| 1908 | |
| 1909 | # If this a new sequence number we haven't seen before, add it to the map. |
| 1910 | if seq not in self._seq_map: |
| 1911 | self._seq_map[seq] = packet |
| 1912 | if seq < self.seq_min: self.seq_min = seq |
| 1913 | if seq > self.seq_max: self.seq_max = seq |
| 1914 | if packet.ts < self.starttime: self.starttime = packet.ts |
| 1915 | if packet.ts > self.endtime: self.endtime = packet.ts |
| 1916 | self.packets.append(packet) |
| 1917 | return |
| 1918 | |
| 1919 | # Otherwise, if we already have the packet for the given sequence |
| 1920 | # then we have a retransmission and will need to determine which packet to keep |
| 1921 | # and possibly remove other packets if this packet overlaps them. |
| 1922 | orig_packet = self._seq_map[seq] |
| 1923 | |
| 1924 | # ignore duplicate packet. |
| 1925 | # if len(packet.data) <= len(orig_packet.data): |
| 1926 | if packet.data == orig_packet.data: |
| 1927 | # TODO: should we still handle duplicate packets. |
| 1928 | logger.debug(f'Ignoring duplicate packet: {packet.frame}') |
| 1929 | return |
| 1930 | |
| 1931 | # If this packet would create more inconsistencies in our sequence numbers (more holes) |
| 1932 | # than the packet to be replaced, then this is most likely an out-of-order packet that the |
| 1933 | # sender has ignored, and we should too. |
| 1934 | orig_next_seq = seq + len(orig_packet.data) |
| 1935 | next_seq = seq + len(packet.data) |
| 1936 | if ( |
| 1937 | # next_seq < max(self.sequence_numbers) |
| 1938 | next_seq < self.seq_max |
| 1939 | and orig_packet.data |
| 1940 | and next_seq not in self._seq_map |
| 1941 | and orig_next_seq in self._seq_map |
| 1942 | ): |
| 1943 | logger.debug(f'Ignoring out-of-order packet: {packet.frame}') |
| 1944 | return |
| 1945 | |
| 1946 | # Replace packet(s) with retransmitted packet |
no test coverage detected