def send_packet(self): pkt = icmp.Echo(id=self.ping_ident, seq=self.sent, data='licorn pinger') buf = icmp.assemble(pkt) self.times[self.sent] = time.time() self.sock.sendto(buf) self.plen = len(buf) self.sent = self.sent + 1
def probe_packet(self): """ Build the outgoing probe packet """ id = 0 seq = 0 probe_icmp = icmp.Echo(id, seq, '') # calculate the length of ICMP data header_len = len(icmp.assemble(probe_icmp)) if self.packet_len <= header_len: raise TraceError, "packet length must be > %d" % (header_len) data = '\000' * (self.packet_len - header_len) # I can't figure out how to determine how to distinguish the respones to different probe packet, return icmp.assemble(icmp.Echo(id, seq, data), self.check_sum)
def send_packet(self): pkt = icmp.Echo(id=self.pid, seq=self.sent, data='python pinger') buf = icmp.assemble(pkt) self.times[self.sent] = time.time() self.sock.sendto(buf) self.plen = len(buf) self.sent = self.sent + 1
def __send_packet(self): """ Internal function to send icmp ECHO packet to target address. """ buf = icmp.assemble(icmp.Echo(id=self.pid, seq=self.sent, data=PAYLOAD)) self.times[self.sent] = time.time() self.sent += 1 self.socket.sendto(buf)
def loop(hosts, callback, sleep_interval = 1, timeout_interval = 1): sendq = [] recvq = {} jitter = 0 vario = 0.1 for host in hosts: heappush(sendq, (time.time()+sleep_interval+jitter, host)) jitter += vario psocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname('icmp')) psocket.setblocking(0) poller = select.poll() poller.register(psocket.fileno(), select.POLLOUT | select.POLLIN) my_id = os.getpid() seq = 0 while True: events = poller.poll(1) current_time = time.time() for fileno, event in events: if event & select.POLLIN: #curent_time = time.time() (data, addr) = psocket.recvfrom(2048) (r_id, r_seq, time_sent) = icmp.disassemble(data) if r_id: rtt = (current_time - time_sent) * 1000 if r_id == my_id: callback(addr[0], rtt) heappush(sendq, (time.time() + sleep_interval, addr[0])) if addr[0] in recvq: del recvq[addr[0]] if event & select.POLLOUT: if sendq: (time_to_send, ip) = heappop(sendq) if time_to_send < current_time: icmp_packet = icmp.assemble(ip, my_id, seq, current_time) seq += 1 psocket.sendto(icmp_packet, (ip,0)) recvq[ip] = current_time + timeout_interval else: heappush(sendq, (time_to_send, ip)) #TODO optimize here for ip in [ host for (host, timeout) in recvq.items() if timeout < current_time ]: callback(ip, None) heappush(sendq, (current_time + sleep_interval, ip)) del recvq[ip] if seq > 10000: seq=0
def sendPacket(self, pingJob): """Take a pingjob and send an ICMP packet for it""" #### sockets with bad addresses fail pkt = icmp.Echo(self.procId, pingJob.sent, self.pktdata) buf = icmp.assemble(pkt) print "Pinging %s with %d buf bytes of data:" % (pingJob.ipaddr,len(buf)) for i in xrange(self.tries): try: pingJob.start = time.time() # print "send icmp to '%s'"% pingJob.ipaddr self.pingsocket.sendto(buf, (pingJob.ipaddr, 0)) # reactor.callLater(self.timeout, self.checkTimeout, pingJob) pingJob.sent += 1 self.recvPackets(pingJob) except (SystemExit, KeyboardInterrupt): raise except Exception, e: # pingJob.rtt = -1 pingJob.message = "%s sendto error %s" % (pingJob.ipaddr, e) print pingJob.message # self.reportPingJob(pingJob) time.sleep(1)
def testTimeExceeded(self): buf = icmp.assemble(self.time_exceeded, cksum=1) new = icmp.disassemble(buf, cksum=1) self.assertEqual(self.time_exceeded, new)
def testUnreachableNotCksum(self): buf = icmp.assemble(self.unreachable, cksum=0) new = icmp.disassemble(buf, cksum=0) self.assertEqual(self.unreachable, new)
def testEchoReplyNotCksum(self): buf = icmp.assemble(self.echo_reply, cksum=0) new = icmp.disassemble(buf, cksum=0) self.assertEqual(self.echo_reply, new)
def testEcho(self): buf = icmp.assemble(self.echo, cksum=1) new = icmp.disassemble(buf, cksum=1) self.assertEqual(self.echo, new)