def send(self, dest_addr, callback, psize=64): """ Send a ICMP echo request. :dest_addr - where to send it :callback - what to call when we get a response :psize - how much data to send with it """ # make sure we dont have too many outstanding requests while len(self.pings) >= self.max_outstanding: gevent.sleep() # figure out our id packet_id = self.id # increment our id, but wrap if we go over the max size for USHORT self.id = (self.id + 1) % 2 ** 16 # make a spot for this ping in self.pings self.pings[packet_id] = {'sent':False,'success':False,'error':False,'dest_addr':dest_addr,'dest_ip':None,'callback':callback} # Resolve hostname try: dest_ip = socket.gethostbyname(dest_addr) self.pings[packet_id]['dest_ip'] = dest_ip except socket.gaierror as ex: self.pings[packet_id]['error'] = True self.pings[packet_id]['message'] = str(ex) return # Remove header size from packet size psize = psize - 8 # Header is type (8), code (8), checksum (16), id (16), sequence (16) my_checksum = 0 # Make a dummy heder with a 0 checksum. header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, packet_id, 1) bytes = struct.calcsize("d") data = (psize - bytes) * "Q" data = struct.pack("d", time.time()) + data # Calculate the checksum on the data and the dummy header. my_checksum = checksum(header + data) # Now that we have the right checksum, we put that in. It's just easier # to make up a new header than to stuff it into the dummy. header = struct.pack( "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), packet_id, 1 ) packet = header + data # note the send_time for checking for timeouts self.pings[packet_id]['send_time'] = time.time() # send the packet self.socket.sendto(packet, (dest_ip, 1)) # Don't know about the 1 #mark the packet as sent self.pings[packet_id]['sent'] = True
def send(self, dest_addr, callback, psize=64): """ Send a ICMP echo request. :dest_addr - where to send it :callback - what to call when we get a response :psize - how much data to send with it """ # make sure we dont have too many outstanding requests while len(self.pings) >= self.max_outstanding: print 'Waiting' gevent.sleep(self.timeout / 2) #resolve hostnames dest_addr = socket.gethostbyname(dest_addr) # figure out our id packet_id = self.id # increment our id, but wrap if we go over the max size for USHORT self.id = (self.id + 1) % 2**16 # make a spot for this ping in self.pings self.pings[packet_id] = { 'sent': False, 'success': False, 'error': False, 'dest_addr': dest_addr, 'callback': callback } # Remove header size from packet size psize = psize - 8 # Header is type (8), code (8), checksum (16), id (16), sequence (16) my_checksum = 0 # Make a dummy heder with a 0 checksum. header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, packet_id, 1) bytes = struct.calcsize("d") data = (psize - bytes) * "Q" data = struct.pack("d", time.time()) + data # Calculate the checksum on the data and the dummy header. my_checksum = checksum(header + data) # Now that we have the right checksum, we put that in. It's just easier # to make up a new header than to stuff it into the dummy. header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), packet_id, 1) packet = header + data # note the send_time for checking for timeouts self.pings[packet_id]['send_time'] = time.time() # send the packet self.socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1 #mark the packet as sent self.pings[packet_id]['sent'] = True print "Sent %s" % dest_addr
def send(self, dest_addr, callback, idx, current_data, data, datapsize=64): """ Send a ICMP echo request. :dest_addr - where to send it :callback - what to call when we get a response :psize - how much data to send with it """ # make sure we dont have too many outstanding requests number_of_packages = current_data[1] while len(self.pings) >= self.max_outstanding: gevent.sleep() psize = datapsize # figure out our id packet_id = self.id # increment our id, but wrap if we go over the max size for USHORT self.id = (self.id + 1) % 2 ** 16 # make a spot for this ping in self.pings self.pings[packet_id] = {'sent':False,'success':False,'error':False,'dest_addr':dest_addr,'dest_ip':None,'callback':callback, 'idx': idx, 'current_data': current_data, 'data_to_write_to': data, 'dtime': time.time(), 'packages_received': 0 } # Resolve hostname try: dest_ip = socket.gethostbyname(dest_addr) self.pings[packet_id]['dest_ip'] = dest_ip except socket.gaierror as ex: self.pings[packet_id]['error'] = True self.pings[packet_id]['message'] = str(ex) return # Remove header size from packet size psize = psize - 8 # Header is type (8), code (8), checksum (16), id (16), sequence (16) my_checksum = 0 # Make a dummy heder with a 0 checksum. header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, packet_id, 1) my_bytes = struct.calcsize("d") data = (psize - my_bytes) * "Q" data = struct.pack("d", time.time()) + bytes(data, "utf-8") # Calculate the checksum on the data and the dummy header. my_checksum = checksum(header + data) # Now that we have the right checksum, we put that in. It's just easier # to make up a new header than to stuff it into the dummy. header = struct.pack( "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), packet_id, 1 ) packet = header + data # note the send_time for checking for timeouts self.pings[packet_id]['data'] = data self.pings[packet_id]['application_id'] = current_data[4] self.pings[packet_id]['send_time'] = time.time() # send the packet for i in range(number_of_packages): self.socket.sendto(packet, (dest_ip, 1)) # Don't know about the 1 #mark the packet as sent self.pings[packet_id]['sent'] = True