def __init__(self, sender_cap_file, receiver_cap_file, rto_file, sender_ip=None, receiver_ip=None, sender_mac = None, receiver_mac = None, sender_port=None, receiver_port=None, nat=False, betabutterfly=True): self.sender_cap_file = sender_cap_file self.receiver_cap_file = receiver_cap_file self.rto_file = rto_file self.sender_mac = sender_mac self.receiver_mac = receiver_mac self.rto_list = RTO_List(self.rto_file) self.nat = nat self.betabutterfly = betabutterfly if self.rto_list.rto_number==0: return self.data_sender = TCPCapReader(filename = self.sender_cap_file, my_mac=self.sender_mac) self.data_sender.summary() self.ack_sender = TCPCapReader(filename = self.receiver_cap_file, my_mac=self.receiver_mac) self.ack_sender.summary() print "self.nat = %s" % self.nat if sender_ip and sender_port and receiver_ip and receiver_port: assert(False) self.sender_ip = sender_ip self.receiver_ip = receiver_ip self.sender_port = sender_port self.receiver_port = receiver_port ip1 = IPTool.ip2num(self.sender_ip) ip2 = IPTool.ip2num(self.receiver_ip) self.flow = Flow((ip1, self.sender_port), (ip2, self.receiver_port)) else: # f1 = set(self.data_sender.flows_map.keys()) # f2 = set(self.ack_sender.flows_map.keys()) f1 = set([f for f, (s, r, a) in self.data_sender.flows_map.iteritems() if (len(s)>0 and len(r)>0)]) #solo flussi bidirezionali f2 = set([f for f, (s, r, a) in self.ack_sender.flows_map.iteritems() if (len(s)>0 and len(r)>0)]) bidirectional_flows = f1.intersection(f2) if len(bidirectional_flows)>1: print "Too many flows between two peers, we should choose one. Not implemented yet." assert(False) elif len(bidirectional_flows)==0: if not self.nat: print "No bidirectional flows (maybe there is nat, use --nat option)" assert(False) elif self.nat and len(f1)==1 and len(f2)==1: f1 = list(f1)[0] f2 = list(f2)[0] self.nat_connection = NatFlowConnection(f1, f2) print "Nat Enabled:", self.nat_connection elif self.nat and (len(f1)>1 or len(f2)>1): print "Too many flows... You have to choose one manually. Not IMPLEMENTED" print f1, f2 assert(0) print "Chosen Flow:", self.flow else: print "Unforeseen Event!" assert(0) elif len(bidirectional_flows)==1 and not self.nat: self.flow=list(bidirectional_flows)[0] else: print "Unforeseen Event!" assert(0)
def __init__(self, filename, my_mac=None, cap_filter=r'ip proto \tcp'): self.myreader = RawCapReader(filename, cap_filter) # inheritance is slower #self.my_ip = IPTool.ip2num(my_ip) self.flows_map={} self.pcap_array=[] self.packet_number=0 if my_mac is not None: self.my_mac = [int(i, 16) for i in my_mac.split(":")] else: self.my_mac = None #bytes = struct.unpack('!H', hdr[0:2]) #if bytes == 4: #my_ip = packet.myip for p in self.myreader: if p is not None: hdr, data = p # assert(len(data)>0) packet = FastTCPDecoder(self.myreader.datalink, data, my_mac=self.my_mac) if packet.is_tcp: sec, usec = hdr.getts() p = packet conn = Flow((p.ip_src, p.tcp_sport), (p.ip_dst, p.tcp_dport)) sent_packets, rcvd_packets, rcvd_acks = self.flows_map.get(conn, [{}, {}, {}]) #if p.ip_src == self.my_ip: #pacchetto inviato if p.sent_by_us==1: #pacchetto inviato self.pcap_array.append((sec, usec, 1, data)) self.packet_number+=1 tmp = sent_packets.get(p.tcp_seq, [None, {}]) if(tmp[0])==None: tmp[0]=p.ip_id tmp[1][p.ip_id]=self.packet_number-1 sent_packets[p.tcp_seq]=tmp #elif p.ip_dst == self.my_ip: # paccketto ricevuto elif p.sent_by_us==0: # paccketto ricevuto self.pcap_array.append((sec, usec, 0, data)) self.packet_number+=1 tmp = rcvd_packets.get(p.tcp_seq, [None, {}]) if(tmp[0])==None: tmp[0]=p.ip_id tmp[1][p.ip_id]=self.packet_number-1 rcvd_packets[p.tcp_seq]=tmp tmp = rcvd_acks.get(p.tcp_ack, [None, {}]) if(tmp[0])==None: tmp[0]=p.ip_id tmp[1][p.ip_id]=self.packet_number-1 rcvd_acks[p.tcp_ack]=tmp else: print i, IPTool.num2ip(p.ip_src), IPTool.num2ip(p.ip_dst), p.tcp_sport, p.tcp_dport assert(False) self.flows_map[conn]=[sent_packets, rcvd_packets, rcvd_acks] else: print "None" assert(0)
def getObject(): global iptaskinstance if iptaskinstance is None: iptaskinstance = IPTool(1) return iptaskinstance
def __str__(self): #return "%s:%d <-> %s:%d" % ( socket.inet_ntop(socket.AF_INET, self.p1[0]), self.p1[1],socket.inet_ntop(socket.AF_INET, self.p2[0]), self.p2[1]) #return "%s:%d <-> %s:%d" % ( socket.inet_ntoa(struct.pack('!L',self.p1[0])), self.p1[1],socket.inet_ntoa(struct.pack('!L',self.p2[0])), self.p2[1]) return "%s:%d <-> %s:%d" % ( IPTool.num2ip(self.p1[0]), self.p1[1], IPTool.num2ip(self.p2[0]), self.p2[1])