| 23 | |
| 24 | |
| 25 | class Packet: |
| 26 | def __init__(self, ip_src: Address, ip_dst: Address, l4_sport: int, l4_dport: int): |
| 27 | self.ip_src = ip_src |
| 28 | self.ip_dst = ip_dst |
| 29 | self.l4_sport = l4_sport |
| 30 | self.l4_dport = l4_dport |
| 31 | |
| 32 | def reverse(self): |
| 33 | return Packet( |
| 34 | ip_src=self.ip_dst, |
| 35 | l4_sport=self.l4_dport, |
| 36 | ip_dst=self.ip_src, |
| 37 | l4_dport=self.l4_sport, |
| 38 | ) |
| 39 | |
| 40 | def hash_data(self, use_l4_port: bool = False) -> bytes: |
| 41 | data = self.ip_src.packed + self.ip_dst.packed |
| 42 | if use_l4_port: |
| 43 | data += struct.pack(">H", self.l4_sport) |
| 44 | data += struct.pack(">H", self.l4_dport) |
| 45 | return data |
| 46 | |
| 47 | |
| 48 | class TrafficTemplate: |