Handles an incoming pcap packet. This method only knows how to recognize TCP/IP connections. Be sure that only TCP packets are passed onto this handler (or fix the code to ignore the others). Setting r"ip proto \tcp" as part of the pcap filter expression suff
(self, hdr, data)
| 88 | self.pcap.loop(0, self.packetHandler) |
| 89 | |
| 90 | def packetHandler(self, hdr, data): |
| 91 | """Handles an incoming pcap packet. This method only knows how |
| 92 | to recognize TCP/IP connections. |
| 93 | Be sure that only TCP packets are passed onto this handler (or |
| 94 | fix the code to ignore the others). |
| 95 | |
| 96 | Setting r"ip proto \tcp" as part of the pcap filter expression |
| 97 | suffices, and there shouldn't be any problem combining that with |
| 98 | other expressions. |
| 99 | """ |
| 100 | |
| 101 | # Use the ImpactDecoder to turn the rawpacket into a hierarchy |
| 102 | # of ImpactPacket instances. |
| 103 | p = self.decoder.decode(data) |
| 104 | ip = p.child() |
| 105 | tcp = ip.child() |
| 106 | |
| 107 | # Build a distinctive key for this pair of peers. |
| 108 | src = (ip.get_ip_src(), tcp.get_th_sport() ) |
| 109 | dst = (ip.get_ip_dst(), tcp.get_th_dport() ) |
| 110 | con = Connection(src,dst) |
| 111 | |
| 112 | # If there isn't an entry associated yetwith this connection, |
| 113 | # open a new pcapdumper and create an association. |
| 114 | if ('%s%s' % (con.p1, con.p2)) not in self.connections: |
| 115 | fn = con.getFilename() |
| 116 | print("Found a new connection, storing into:", fn) |
| 117 | try: |
| 118 | dumper = self.pcap.dump_open(fn) |
| 119 | except pcapy.PcapError: |
| 120 | print("Can't write packet to:", fn) |
| 121 | return |
| 122 | self.connections['%s%s' % (con.p1, con.p2)] = dumper |
| 123 | |
| 124 | # Write the packet to the corresponding file. |
| 125 | self.connections['%s%s' % (con.p1, con.p2)].dump(hdr, data) |
| 126 | |
| 127 | |
| 128 |
nothing calls this directly
no test coverage detected