def analyze_ip_header(data): ip_hdr = struct.unpack("!4H4s4s", data[:20]) ver = ip_hdr[0] >> 12 # Shift 12 bits to the right ihl = (ip_hdr[0] >> 8) & 0x0F # 00001111 tos = ip_hdr[0] & 0x00ff # 0000000011111111 tot_length = ip_hdr[1] ip_id = ip_hdr[2] flags = ip_hdr[3] >> 13 # Only going to get the first 3 bits frag_offset = ip_hdr[3] & 0x1FFFF src_addr = socket.ntoa(ip_hdr[4]) dst_addr = socket.ntoa(ip_hdr[5]) return
def __http_request(self, url): try: payload = {'info_hash': self.torrent.info_hash, 'peer_id': self.peer_id, 'port': self.port, 'uploaded': self.uploaded, 'downloaded': self.downloaded, 'left': self.left, 'event': self.event, 'compact': self.compact, 'numwant': self.compact} req = requests.get(url, params = payload, timeout = CONNECTION_TIME_OUT) print "req.url = ", req.url # print "req : ", req res = req.content # print "res type = ", type(res) # print "tracker response = ", res res = BEncode(res).parse() print res if res.get('peers'): bpeers = res.get('peers') blen = len(bpeers) i = 0 while i < blen: ip, port = struct.unpack('lH', bpeers[i: i + 6]) # ip = byte_helper.bytes42int(bpeers[i: i + 4]) print "ip before = ", ip, port ip = socket.ntoa(struct.pack('l', socket.inet_htol(ip))) print ip ip_port = (socket.inet_ntoa(bpeers[i: i + 4]), byte_helper.bytes22int(bpeers[i + 4: i + 6])) print "peer ip port = ", ip_port self.peers.add(ip_port) i += 6 except Exception, e: print "Exception", e
def ip_hdr(data): ip_hdr = struct.unpack("!6H4s4s", data[:20]) ver = ip_hdr[0] >> 12 #shift 12 bits the data is binary not decimal ihl = (ip_hdr[0] >> 8) & 0x0f # shif 8 bits and have to remove version so hence logical tos = ip_hdr[0] & 0x00ff total_length = ip_hdr[1] ip_id = ip_hdr[2] flags = ip_hdr[3] >> 13 # we require only the first 3 bits frag_offset = ip_hdr[3] & 0x1fff ip_ttl = ip_hdr[4] >> 8 ip_proto = ip_hdr[4] & 0x00ff ch_sum = ip_hdr[5] src_addr = socket.ntoa(ip_hdr[6]) dest_addr = socket.ntoa(ip_hdr[7]) no_frag = flags >> 1 more_frag = flags & 0x1 print "===========================IP Header==============================" print " Version \t %hu" % ver print " IHL \t %hu" % ihl print " TOS \t %hu" % tos print " Total Length \t %hu" % total_length print " ID \t %hu" % ip_id print " No Frag \t %hu" % no_frag print " More Frag \t %hu" % more_frag print " Fragment offset \t %hu" % frag_offset print " TTL \t %hu" % ip_ttl print " Next Proto \t %hu" % ip_proto print " CheckSum \t %hu" % ch_sum print " Source IP \t %s" % src_addr print " Destination IP \t %s" % dest_addr if ip_proto == 6: #TCP next_proto = "TCP" elif ip_proto == 7: #UDP next_proto = "UDP" else: next_proto = "OTHER" data = data[20:] return data, next_proto
def __repr__(self): return "%s/%s" % (ntoa(pack('I', htonl(self.addr))), ntoa(pack('I', htonl(self.mask))) )
def __repr__(self): return ntoa(pack('I', htonl(self.addr)))
def __repr__(self): return "%s/%s" % (ntoa(pack('I', htonl( self.addr))), ntoa(pack('I', htonl(self.mask))))