Example #1
0
File: anv2.py Project: abelpc/next
def callback(hdr, data):
	#print hdr.getts()[0]
	global first_pack_timestamp 
	if (first_pack_timestamp == None):
		#first_pack_timestamp = (hdr.getts()[0] * (10**(-6))) + hdr.getts()[1] #hdr.getts()[1] * (10**(-6))
		first_pack_timestamp = hdr.getts()[0]

	#timestamp = hdr.getts()[1] * (10**(-6))
	#timestamp = (hdr.getts()[0] * (10**(-6))) + hdr.getts()[1]
	timestamp = hdr.getts()[0]	

	delta_time = timestamp - first_pack_timestamp
	#print "[%i] %f - %f = %f" % (num_packs, timestamp, first_pack_timestamp, delta_time )

	global num_packs
	global num_ips

	if num_packs <= 10:
		#print "callback()"
		packet=decoder.decode(data)
		l2=packet.child()

		if isinstance(l2, IP):
			#print "IP",
			l3=l2.child()
			src_ip = l2.get_ip_src()
			dst_ip = l2.get_ip_dst()

			if isinstance(l3, TCP):
				src_port = l3.get_th_sport()
				dst_port = l3.get_th_dport()
            
			else:
				if isinstance(l3, UDP):
					#print "UDP"
					src_port = l2.child().get_uh_sport()
					dst_port = l2.child().get_uh_dport()

				else:
					#print "Breaking!"
					return

			#print "Packet from %s (%s) to %s(%s) " % (src_ip, src_port, dst_ip, dst_port)

			if ((src_port == 3908) and (src_ip == my_ip)):			
			#if ((src_port == 3908) and (src_ip == my_ip) and (dst_port == 3908)):			
				num_packs = num_packs + 1

				if not (temp.has_key(src_ip)):
					tmp_packet = Pacote(src_ip, src_port, dst_ip, dst_port)
					tmp_packet.inc_dst_calls()
					#temp[src_ip] = tmp_packet
					temp[dst_ip] = tmp_packet

					#num_ips = num_ips + 1 # Increment the number of gotten IPs

				else:
					tmp_packet = temp[dst_ip]
					tmp_packet.inc_dst_calls()	# Increment the source's number of destination calls
					temp[dst_ip] = tmp_packet	# Saves the incrementation
class MyThread( threading.Thread ):
   
   #definições iniciais das propriedades
   def __init__(self,host_obj,server,cont,log):
      threading.Thread.__init__(self)
      self.__serv = server
      self.__host=host_obj
      self.__packet=Pacote(1,0,0)
      self.__host_id = cont
      self.__server_log = log

   #executa a função de monitoramento
   def run( self ):
      while(True):
         print 'Fazendo ping no host;' , self.__host.getName()
         self.__status=ping(self.__host.getName())
         self.__server_log.setMsg(MSG_PING.format(self.__host.getName()))
         self.__server_log.writeLog()
         self.__status_before = self.__host.getStatus() 
         if self.__status==2:
			   self.__status=DESCONHECIDO
         #se status for alterado, avisa o cliente
         if self.__status_before != self.__status:
            self.__packet.setHostID(self.__host_id)
           
            #tratando as mudanças de estado
            if ((self.__status_before == DESCONHECIDO) and (self.__status == ATIVO)):
               self.__host.setStatus(ATIVO)
               if self.__host.getIp == 0:
                  self.__host.setIp = resolveHost(self.__host.getName())
               #print 'O host' , self.__host.getIp() , 'mudou para ATIVO'
               self.__server_log.setMsg(MSG_CHANGE_STATUS.format(self.__host.getName(),'ATIVO'))
               self.__server_log.writeLog()
            elif ((self.__status_before == ATIVO) and (self.__status == DESCONHECIDO or self.__status == 2)):
               self.__host.setStatus(FALHO)
               self.__server_log.setMsg(MSG_CHANGE_STATUS.format(self.__host.getName(),' FALHO'))
               self.__server_log.writeLog()
               #print 'O host' , self.__host.getIP() , 'mudou para FALHO'
            elif ((self.__status_before == FALHO) and (self.__status ==ATIVO)):
               self.__host.setStatus(ATIVO)
               self.__server_log.setMsg(MSG_CHANGE_STATUS.format(self.__host.getName(),'ATIVO'))
               self.__server_log.writeLog()
               #print 'O host' , self.__host.getIp() , 'mudou para ATIVO'
            else:
               self.__host.setStatus(DESCONHECIDO)
               self.__server_log.setMsg(MSG_CHANGE_STATUS.format(self.__host.getIp(),' DESCONHECIDO'))
               self.__server_log.writeLog()
               #print 'O host' , self.__host.getIp() , 'mudou para DESCONHECIDO'
            self.__packet.setData(self.__host.getStatus())
            self.__data_send=serialize(self.__packet)
            self.__serv.sendData(self.__data_send)
         time.sleep(30)
Example #3
0
    def __init__(self, name, destino, dado, tipoDado):
        self.name = name
        self.destino = destino
        self.dado = dado
        self.tipoDado = tipoDado
        self.UDP = "Protocolo UDP"
        self.TCP = "Protocolo TCP"

        ##  ENLACE  ##
        self.rede = Rede()
        self.pacote = Pacote(name, destino, dado)

        self.defineProtocol()
        self.send()
 def __init__(self,host_obj,server,cont,log):
    threading.Thread.__init__(self)
    self.__serv = server
    self.__host=host_obj
    self.__packet=Pacote(1,0,0)
    self.__host_id = cont
    self.__server_log = log
Example #5
0
class Transporte:
    def __init__(self, name, destino, dado, tipoDado):
        self.name = name
        self.destino = destino
        self.dado = dado
        self.tipoDado = tipoDado
        self.UDP = "Protocolo UDP"
        self.TCP = "Protocolo TCP"

        ##  ENLACE  ##
        self.rede = Rede()
        self.pacote = Pacote(name, destino, dado)

        self.defineProtocol()
        self.send()

    def defineProtocol(self):
        if (self.tipoDado == "chat"):
            self.pacote.setProtocol(self.TCP)

    def send(self):
        self.rede.send(self.name, self.destino, self.dado)
Example #6
0
File: anv1.py Project: abelpc/next
def callback(hdr, data):
	#print "callback()"
	packet=decoder.decode(data)
	l2=packet.child()
	if isinstance(l2, IP):
		#print "IP",
		l3=l2.child()
		src_ip = l2.get_ip_src()
		dst_ip = l2.get_ip_dst()

		if isinstance(l3, TCP):
			dst_port = l3.get_th_sport()
			src_port = l3.get_th_dport()
            
			#print "TCP from %s (%s) to %s(%s) " % (src_ip, src_port, dst_ip, dst_port)

		if isinstance(l3, UDP):
			#print "UDP"
			dst_port = l2.child().get_uh_sport()
			src_port = l2.child().get_uh_dport() 

						
		if not (temp.has_key(src_ip)):
			tmp_packet = Pacote(src_ip, src_port, dst_ip, dst_port)
			tmp_packet.inc_dst_calls()
			temp[src_ip] = tmp_packet

		else:
			tmp_packet = temp[src_ip]
			tmp_packet.inc_dst_calls()	# Increment the source's number of destination calls
			temp[src_ip] = tmp_packet	# Saves the incrementation

			if not (temp.has_key(dst_ip)):
				tmp_packet = Pacote(dst_ip, dst_port, src_ip, src_port)
				temp[dst_ip] = tmp_packet

			tmp_packet = temp[dst_ip]
			tmp_packet.inc_dst_calls()	# Increment the destination's number of destination calls
							# Notice that this number represents different things for the 
							# viewer IP and for all the other sources
			temp[dst_ip] = tmp_packet
Example #7
0
    def send(self, address, destino, dados):
        pacote = Pacote(address, self.rota[destino], dados)

        ## Mudando forma de envio do pacote.
        flag = False
        self.enlace.transmission(pacote, flag)
	os.remove( EVENT_LOG_FILENAME );

# Lê os hosts da entrada padrão
hostsList = ReadHosts();

# Transforma a lista de hosts em uma string para mandar ao monitor (servidor)
# Insere os nomes dos hosts em uma lista, depois usa o método join()
hostArr = []
for objHost in hostsList:
	hostArr.append( objHost.getNome() )
hostsListAsString = str( HOSTS_SEP ).join( hostArr )



# monta a string a ser enviada ao monitor
pack = Pacote( PROTOCOL_COD_SEND_HOSTS, None, None, None, hostsListAsString )
msgHosts = Protocol.Pack2Str( pack )

# host e porta passados por parâmetro
host = str( sys.argv[1] )
port = int( sys.argv[2] )

try:
	sockSend = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
except socket.error, ( errno, errmsg ):
	if sockSend:
		sockSend.close();
	ClientLog.LogMsg( "Erro ao criar socket: " + str( errno ) + ": " + str( errmsg ) );
	sys.exit( 1 );

ClientLog.LogMsg( "Socket criado com sucesso" );
Example #9
0
def cria_pacote(data, i, endereco, fim):
    pack = Pacote()
    pack.set_num_seq(bin(i))
    pack.set_dados(data)
    pack.set_sender_adress((host, port))
    pack.set_receiver_adress(endereco)
    pack.set_sended(True)
    pack.set_sended_time(time())
    pack.set_last(fim)
    return pack
Example #10
0
def decoder_dict(dados_json):
    # dados_json = json.loads(dados_json)
    pack = Pacote()
    pack.set_num_seq(dados_json["num_seq"])
    pack.set_dados(dados_json["data"])
    pack.set_sender_adress(dados_json["sender_adress"])
    pack.set_receiver_adress(dados_json["receiver_adress"])
    pack.set_sended(dados_json["sended"])
    pack.set_sended_time(dados_json["sended_time"])
    pack.set_last(dados_json["last"])
    return pack