Exemple #1
0
	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
Exemple #2
0
    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)        
Exemple #3
0
    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)
Exemple #4
0
 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
Exemple #5
0
 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)
Exemple #6
0
 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)
Exemple #7
0
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
Exemple #8
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)
Exemple #9
0
 def testTimeExceeded(self):
     buf = icmp.assemble(self.time_exceeded, cksum=1)
     new = icmp.disassemble(buf, cksum=1)
     self.assertEqual(self.time_exceeded, new)
Exemple #10
0
 def testUnreachableNotCksum(self):
     buf = icmp.assemble(self.unreachable, cksum=0)
     new = icmp.disassemble(buf, cksum=0)
     self.assertEqual(self.unreachable, new)
Exemple #11
0
 def testEchoReplyNotCksum(self):
     buf = icmp.assemble(self.echo_reply, cksum=0)
     new = icmp.disassemble(buf, cksum=0)
     self.assertEqual(self.echo_reply, new)
Exemple #12
0
 def testEcho(self):
     buf = icmp.assemble(self.echo, cksum=1)
     new = icmp.disassemble(buf, cksum=1)
     self.assertEqual(self.echo, new)