/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License.
 *
 *
 *                      ********************
 *
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#include <errno.h>
#include "Scan_Equipement.h"
#include "Thread_Detect.h"
#include "Debug.h"
#include "Scan_Port.h"
#include "Scan_Signature.h"
#include "Scan_Netbios.h"
#include "Thread.h"
#include "Scan_Service.h"
#include "Com/Commun.h"
#include "Com/Communication.h"
#include "Com/Initialisation_Com.h"
#include "Semaphore.h"
#include "Snmp.h"
#include "Scan_Arp.h"
#include "Macro.h"

void Rescanne_Equipement (struct Struct_Liste_Equipement *Equip)
{
	ERROR_if (Equip == NULL);
#ifdef __LOG_ENABLE
	Create_Log_Equipement(Equip);  

  	pthread_once(&once_cle_privee,create_cle_privee);
	Equip->vs_Donnee_Cle = NULL;
	MALLOC(Equip->vs_Donnee_Cle,sizeof(struct struct_Cle_Privee));
	Equip->vs_Donnee_Cle->Categorie = CATEG_THREAD_SCAN;
	Equip->vs_Donnee_Cle->Pointeur_Data = Equip;
	Equip->vs_Donnee_Cle->ID_Fichier_Log = Equip->ID_Fichier_Log; 
	pthread_setspecific(Cle_privee_thread,Equip->vs_Donnee_Cle);
#endif
	
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Rescanne_Equipement  Socket ouvert[%d]  Thread[%d] ",Nb_Socket_Ouverte, Info_Demarrage.Nb_Thread_Max - Valeur_Semaphore (&Semaphore_Thread_Scan));
	Equip->Sauve_Etat = Equip->Equipement_Allumer;
	Equip->Rafraichissement_Etat_En_Cours = TRUE;
	Equip->Compteur_Notification = 0;
	Equip->Equipement_Allumer = FALSE;

	Equip->Timeout = 0;
	while ((Equip->Timeout<5) && (!Equip->Equipement_Allumer)){
		Envoie_Arp_Equipement (Equip, 4);
	
		Ping_equipement(Equip);
		sleep (1);
		Equip->Timeout++;
	}
	Equip->Rafraichissement_Etat_En_Cours = FALSE;


	MALLOC(Equip->MR,sizeof(struct struct_Retour_Rafraichir_Equipement));
	Equip->MR->ID_Reseau = Info_Demarrage.ID_Reseau;
	memcpy (Equip->MR->ID_Equipement, Equip->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
	if (Equip->Sauve_Etat != Equip->Equipement_Allumer) {
	
		MALLOC(Equip->ME,sizeof(struct struct_Retour_Rachaichir_Etat_Equipement));
		Equip->ME->ID_Reseau = Info_Demarrage.ID_Reseau;
		memcpy (Equip->ME->ID_Equipement, Equip->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
		Equip->ME->Equipement_Allumer = Equip->Equipement_Allumer;
		Equip->ME->Derniere_Modification_Etat_Equipement = Equip->Derniere_Modification_Etat_Equipement;
		Communication_Envoie_Donnee (RETOUR_RAFRAICHIR_ETAT_EQUIMEMENT, (gpointer *) Equip->ME, sizeof (struct struct_Retour_Rachaichir_Etat_Equipement));
		MYFREE(Equip->ME);
	}

	if (!Equip->Equipement_Allumer) {	
		debug_printf (D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Equipment : DOWN");
		Equip->MR->Scan_Accompli = FALSE;
		Communication_Envoie_Donnee (RETOUR_RAFRAICHIR_EQUIPEMENT, (gpointer *) Equip->MR, sizeof (struct struct_Retour_Rafraichir_Equipement));
		MYFREE(Equip->MR);
		Stop_Thread (NULL);
	}
	Equip->Scan_Port_En_Cours = TRUE;

	Equip->Time_Debut = time(NULL);
	


	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Resolution du nom DNS");
	Equip->Time_Debut_Resolv_DNS = time(NULL);

	Equip->Time_Fin_Resolv_DNS = time(NULL);
	Equip->Temps_Resolv_DNS = (gint) difftime(Equip->Time_Fin_Resolv_DNS, Equip->Time_Debut_Resolv_DNS);
	Equip->MR->Temps_Resolv_DNS = Equip->Temps_Resolv_DNS;


	


	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scanne des Ports");
	Equip->Time_Debut_Scan_Port = time(NULL);
	Scanne_Toutes_Les_Ports_TCP (Equip);
	Equip->Time_Fin_Scan_Port = time(NULL);
	Equip->Temps_Scan_Port = (gint) difftime(Equip->Time_Fin_Scan_Port, Equip->Time_Debut_Scan_Port);
	Equip->MR->Temps_Scan_Port = Equip->Temps_Scan_Port;

	
	

	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scanne des Ports UDP");
	Equip->Time_Debut_Scan_Port_Udp = time(NULL);
	Scanne_Toutes_Les_Ports_UDP (Equip);
	Equip->Time_Fin_Scan_Port_Udp = time(NULL);
	Equip->Temps_Scan_Port_Udp = (gint) difftime(Equip->Time_Fin_Scan_Port_Udp, Equip->Time_Debut_Scan_Port_Udp);
	Equip->MR->Temps_Scan_Port_Udp = Equip->Temps_Scan_Port_Udp;

	


	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Traitement des Ports");
	Equip->vs_i= 0;
	Equip->MR->Nb_Port_Total_Scanner = g_list_length (Equip->GList_Port);

	Equip->MR->Nb_Port_Ouvert = 0;
	Equip->MR->Nb_Port_Fermer = 0;
	Equip->MR->Nb_Port_Firewaller = 0;
	Equip->vs_Nb_Port_Ajouter_Ouvert = 0;
	Equip->vs_Nb_Port_Ajouter_Fermer = 0;
	Equip->vs_Nb_Port_Ajouter_Firewalled = 0;
	while (Equip->vs_i < Equip->MR->Nb_Port_Total_Scanner)  {
		Equip->Port_Equipement = g_list_nth_data (Equip->GList_Port, Equip->vs_i);
	
		if (Equip->Port_Equipement->Etat_Port == OUVERT){			
			if (Equip->vs_Nb_Port_Ajouter_Ouvert<MAX_PORTS) {
				Equip->MR->Port_Ouvert[Equip->vs_Nb_Port_Ajouter_Ouvert].Num_Port = Equip->Port_Equipement->Num_Port;
				Equip->MR->Port_Ouvert[Equip->vs_Nb_Port_Ajouter_Ouvert].Derniere_Modification_Etat_Port = Equip->Port_Equipement->Derniere_Modification_Etat_Port;
				Equip->MR->Port_Ouvert[Equip->vs_Nb_Port_Ajouter_Ouvert].Flags = Equip->Port_Equipement->Flags;
				Equip->vs_Nb_Port_Ajouter_Ouvert++;
			}
			Equip->MR->Nb_Port_Ouvert++;
		}
	
		if  (Equip->Port_Equipement->Etat_Port == FERMER) {
			if (Equip->vs_Nb_Port_Ajouter_Fermer<MAX_PORTS){
				Equip->MR->Port_Fermer[Equip->vs_Nb_Port_Ajouter_Fermer].Num_Port = Equip->Port_Equipement->Num_Port;
				Equip->MR->Port_Fermer[Equip->vs_Nb_Port_Ajouter_Fermer].Derniere_Modification_Etat_Port = Equip->Port_Equipement->Derniere_Modification_Etat_Port;
				Equip->MR->Port_Fermer[Equip->vs_Nb_Port_Ajouter_Fermer].Flags = Equip->Port_Equipement->Flags;
				Equip->vs_Nb_Port_Ajouter_Fermer++;
			}
			Equip->MR->Nb_Port_Fermer++;
		}
	
		if (Equip->Port_Equipement->Etat_Port == FIREWALLED) {
			if (Equip->vs_Nb_Port_Ajouter_Firewalled<MAX_PORTS){
				Equip->MR->Port_Firewalled[Equip->vs_Nb_Port_Ajouter_Firewalled].Num_Port = Equip->Port_Equipement->Num_Port;
				Equip->MR->Port_Firewalled[Equip->vs_Nb_Port_Ajouter_Firewalled].Derniere_Modification_Etat_Port = Equip->Port_Equipement->Derniere_Modification_Etat_Port;
				Equip->vs_Nb_Port_Ajouter_Firewalled++;
			}
			Equip->MR->Nb_Port_Firewaller++;
		}
		Equip->vs_i++;
	}

	for (Equip->vs_i = 0; Equip->vs_i < NB_PORT_UDP; Equip->vs_i++)
		Equip->MR->Liste_Etat_Udp[Equip->vs_i] = Equip->Liste_Etat_Udp[Equip->vs_i];

	
	
	if (memcmp (Equip->ID_Equipement, Mon_Adresse_MAC, MAC_ADDR_LEN) != 0) {
		Equip->Agent_AutoScan = FALSE;
		Equip->MR->Agent_AutoScan = FALSE;
		Cherche_Un_Port_Avec_Etat (Equip, OUVERT, &Equip->MR->Port_Signature_Ouvert);
		Cherche_Un_Port_Avec_Etat (Equip, FERMER, &Equip->MR->Port_Signature_Fermer);
		debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scan os (%s) avec comme num port ouvert[%d]   num port fermer[%d]", Equip->Adresse_Ip_Char,Equip->MR->Port_Signature_Ouvert,Equip->MR->Port_Signature_Fermer);

		debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scan  Signature");
		Equip->Time_Debut_Scan_Signature = time(NULL);
		if ((Equip->MR->Port_Signature_Ouvert != -1) || (Equip->MR->Port_Signature_Fermer!= -1))
			Envoie_Trames_Scanne_Os (Equip, Equip->MR->Port_Signature_Ouvert, Equip->MR->Port_Signature_Fermer);
		Equip->Time_Fin_Scan_Signature = time(NULL);
		Equip->Temps_Scan_Signature = (gint) difftime(Equip->Time_Fin_Scan_Signature, Equip->Time_Debut_Scan_Signature);
		Equip->MR->Temps_Scan_Signature = Equip->Temps_Scan_Signature;
		
		
	
		memcpy (Equip->MR->Reponse_Signature, Equip->Reponse_Signature, sizeof (struct struct_Reponse) * NB_SCAN_OS);
		memcpy (Equip->MR->Reponse_Signature_Debug, Equip->Reponse_Signature_Debug, sizeof (struct struct_Reponse_debug) * NB_SCAN_OS);
	
		Equip->MR->GateWay = Equip->GateWay;
	
		Equip->MR->Routage = Equip->Routage_Activer;
		

		Cherche_Si_Port_Ouvert (Equip, PORT_FTP);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Ftp = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port FTP ouvert");
			Equip->vs_banniere = Recupere_Banniere_FTP (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->MR->Entete_FTP, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->MR->Entete_FTP, Equip->vs_banniere);
				else {
					strncpy (Equip->MR->Entete_FTP, Equip->vs_banniere, 499);
					Equip->MR->Entete_FTP[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Ftp = time(NULL);
			Equip->Temps_Scan_Service_Ftp = (gint) difftime(Equip->Time_Fin_Scan_Service_Ftp, Equip->Time_Debut_Scan_Service_Ftp);
			Equip->MR->Temps_Scan_Service_Ftp = Equip->Temps_Scan_Service_Ftp;
		}
		else
			Equip->MR->Temps_Scan_Service_Ftp = -1;
		
		

		Cherche_Si_Port_Ouvert (Equip, PORT_HTTP);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Http = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port HTTP ouvert");
			Equip->vs_banniere = Recupere_Banniere_HTTP (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->MR->Entete_HTTP, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->MR->Entete_HTTP, Equip->vs_banniere);
				else {
					strncpy (Equip->MR->Entete_HTTP, Equip->vs_banniere, 499);
					Equip->MR->Entete_HTTP[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Http = time(NULL);
			Equip->Temps_Scan_Service_Http = (gint) difftime(Equip->Time_Fin_Scan_Service_Http, Equip->Time_Debut_Scan_Service_Http);
			Equip->MR->Temps_Scan_Service_Http = Equip->Temps_Scan_Service_Http;
		}
		else
			Equip->MR->Temps_Scan_Service_Http = -1;
		
				

		Cherche_Si_Port_Ouvert (Equip, PORT_IMAP);	
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Imap = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port IMAP ouvert");
			Equip->vs_banniere = Recupere_Banniere_IMAP (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->MR->Entete_IMAP, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->MR->Entete_IMAP, Equip->vs_banniere);
				else {
					strncpy (Equip->MR->Entete_IMAP, Equip->vs_banniere, 499);
					Equip->MR->Entete_IMAP[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Imap = time(NULL);
			Equip->Temps_Scan_Service_Imap = (gint) difftime(Equip->Time_Fin_Scan_Service_Imap, Equip->Time_Debut_Scan_Service_Imap);
			Equip->MR->Temps_Scan_Service_Imap = Equip->Temps_Scan_Service_Imap;
		}
		else
			Equip->MR->Temps_Scan_Service_Imap = -1;


		Cherche_Si_Port_Ouvert (Equip, PORT_IMAP);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Smtp = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port SMTP ouvert");
			Equip->vs_banniere = Recupere_Banniere_SMTP (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->MR->Entete_SMTP, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->MR->Entete_SMTP, Equip->vs_banniere);
				else {
					strncpy (Equip->MR->Entete_SMTP, Equip->vs_banniere, 499);
					Equip->MR->Entete_SMTP[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Smtp = time(NULL);
			Equip->Temps_Scan_Service_Smtp = (gint) difftime(Equip->Time_Fin_Scan_Service_Smtp, Equip->Time_Debut_Scan_Service_Smtp);
			Equip->MR->Temps_Scan_Service_Smtp = Equip->Temps_Scan_Service_Smtp;
		}
		else
			Equip->MR->Temps_Scan_Service_Smtp = -1;

		
		Cherche_Si_Port_Ouvert (Equip, PORT_SSH);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Ssh = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port SSH ouvert");
			Equip->vs_banniere = Recupere_Banniere_SSH (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->MR->Entete_SSH, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->MR->Entete_SSH, Equip->vs_banniere);
				else {
					strncpy (Equip->MR->Entete_SSH, Equip->vs_banniere, 499);
					Equip->MR->Entete_SSH[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Ssh = time(NULL);
			Equip->Temps_Scan_Service_Ssh = (gint) difftime(Equip->Time_Fin_Scan_Service_Ssh, Equip->Time_Debut_Scan_Service_Ssh);
			Equip->MR->Temps_Scan_Service_Ssh = Equip->Temps_Scan_Service_Ssh;
		}
		else
			Equip->MR->Temps_Scan_Service_Smtp = -1;
		
		
		Cherche_Si_Port_Ouvert (Equip, PORT_TELNET);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Telnet = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port TELNET ouvert");
			Equip->vs_banniere = Recupere_Banniere_TELNET (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->MR->Entete_TELNET, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 999)
					strcpy (Equip->MR->Entete_TELNET, Equip->vs_banniere);
				else {
					strncpy (Equip->MR->Entete_TELNET, Equip->vs_banniere, 999);
					Equip->MR->Entete_TELNET[999] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Telnet = time(NULL);
			Equip->Temps_Scan_Service_Telnet = (gint) difftime(Equip->Time_Fin_Scan_Service_Telnet, Equip->Time_Debut_Scan_Service_Telnet);
			Equip->MR->Temps_Scan_Service_Telnet = Equip->Temps_Scan_Service_Telnet;
		}
		else
			Equip->MR->Temps_Scan_Service_Telnet = -1;
	}
	else {
		Equip->Agent_AutoScan = TRUE;
		Equip->MR->Agent_AutoScan = TRUE;
	}
	
		
	

	Cherche_Si_Port_Ouvert (Equip, PORT_NETBIOS);
	if (Equip->Resultat_Test_Port_Ouvert) {
		debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scan  NETBIOS");
		Equip->Time_Debut_Samba = time(NULL);
		Equip->vs_Tentative_NetBios = 0;
		if (Info_Demarrage.Normal_Mode)
			Equip->vs_Tentative_NetBios_MAX = 5;
		else
			Equip->vs_Tentative_NetBios_MAX = 2;
	
		if (Equip->Agent_AutoScan)
			Equip->vs_Tentative_NetBios_MAX = 1;
		Demande_NetBios (Equip, Equip->vs_Tentative_NetBios);
		while ((Equip->vs_Tentative_NetBios < Equip->vs_Tentative_NetBios_MAX) && (!Equip->Resultat_Netbios)) {
			Equip->vs_Tentative_NetBios++;
			sleep (1);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Nouvelle tentative NETBIOS tentatice(%d) (%s)", Equip->vs_Tentative_NetBios, Equip->Adresse_Ip_Char);
			Demande_NetBios (Equip, Equip->vs_Tentative_NetBios);
		}
		memcpy (&Equip->MR->Smb, &Equip->Smb, sizeof (struct struct_Smb)); 
		Equip->Time_Fin_Samba = time(NULL);
		Equip->Temps_Samba = (gint) difftime(Equip->Time_Fin_Samba, Equip->Time_Debut_Samba);
		Equip->MR->Temps_Samba = Equip->Temps_Samba;
	}

	
	


	Equip->Time_Fin = time(NULL);
	Equip->Temps_Scan_Complet = (gint) difftime(Equip->Time_Fin, Equip->Time_Debut);
	Equip->MR->Temps_Scan_Complet = Equip->Temps_Scan_Complet;
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Rescan Nb_Socket_Ouverte [%d] %s Total : %d - ResolvDNS :%d - ScanPort :%d - ScanSignature :%d - Ftp : %d - http:%d - imap:%d - telnet:%d - samba:%d - snmp:%d",Nb_Socket_Ouverte,Equip->Adresse_Ip_Char,Equip->MR->Temps_Scan_Complet,Equip->MR->Temps_Resolv_DNS,	Equip->MR->Temps_Scan_Port,Equip->MR->Temps_Scan_Signature,Equip->MR->Temps_Scan_Service_Ftp,Equip->MR->Temps_Scan_Service_Http,Equip->MR->Temps_Scan_Service_Imap,Equip->MR->Temps_Scan_Service_Telnet,Equip->MR->Temps_Samba,Equip->MR->Temps_Scan_Snmp);

	Equip->MR->Scan_Accompli = TRUE;
	Communication_Envoie_Donnee (RETOUR_RAFRAICHIR_EQUIPEMENT, (gpointer *) Equip->MR, sizeof (struct struct_Retour_Rafraichir_Equipement));
	Equip->Scan_Signature = FALSE;
	Equip->Scan_Port_En_Cours = FALSE;
	MYFREE(Equip->MR);


	Stop_Thread (NULL);
}

void Demande_Rafraichir_Equipement (gpointer * Donnee)
{
	ERROR_if (Donnee == NULL);
	struct struct_Demande_Rachaichir_Etat_Equipement *Message = (struct struct_Demande_Rachaichir_Etat_Equipement *) Donnee;
	struct Struct_Liste_Equipement *Equipement = NULL;
	gint      ID_Equipement;

	Recherche_Index_Equipement (Message->ID_Equipement,TRUE, &ID_Equipement);
	if (ID_Equipement == EQUIPEMENT_UNKNOWN) {
		debug_printf (D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Refresh (Equipment unknown)");
		return;	
	}
	if (ID_Equipement == EQUIPEMENT_NO_IP) {
		debug_printf (D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Refresh (no IP)");
		return;	
	}
	Retour_Pointeur_Equipement (&Equipement,ID_Equipement);

	demmarrage_Thread (&Rescanne_Equipement, (gpointer *) Equipement);
}


void Scanne_Equipement (struct Struct_Liste_Equipement *Equip)
{
	struct struct_Retour_Affiche_Nouveau_Equipement Affiche_Nouveau_Equipement;
		
	ERROR_if (Equip == NULL);
#ifdef __LOG_ENABLE
  	pthread_once(&once_cle_privee,create_cle_privee);
	MALLOC(Equip->vs_Donnee_Cle,sizeof(struct struct_Cle_Privee));
	Equip->vs_Donnee_Cle->Categorie = CATEG_THREAD_SCAN;
	Equip->vs_Donnee_Cle->Pointeur_Data = Equip;
	Equip->vs_Donnee_Cle->ID_Fichier_Log = Equip->ID_Fichier_Log; 
	pthread_setspecific(Cle_privee_thread,Equip->vs_Donnee_Cle);
#endif
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scanne_Equipement  Socket ouvert[%d]  Thread[%d] ",Nb_Socket_Ouverte, Info_Demarrage.Nb_Thread_Max - Valeur_Semaphore (&Semaphore_Thread_Scan));

	MALLOC(Equip->M,sizeof(struct struct_Nouveau_Equipement));

	Equip->Time_Debut = time(NULL);

	Equip->M->ID_Reseau = Info_Demarrage.ID_Reseau;
	memcpy (Equip->M->ID_Equipement, Equip->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
	memcpy (Equip->M->Adresse_Ip_Char, Equip->Adresse_Ip_Char, 15 + 1);
	memcpy (Equip->M->Adresse_MAC_Char, Equip->Adresse_MAC_Char, 17 + 1);

	Equip->Scan_Port_En_Cours = TRUE;
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Resolution du nom DNS");
	Equip->Time_Debut_Resolv_DNS = time(NULL);
	Recupere_Nom_DNS (Equip);
	Equip->Time_Fin_Resolv_DNS = time(NULL);
	Equip->Temps_Resolv_DNS = (gint) difftime(Equip->Time_Fin_Resolv_DNS, Equip->Time_Debut_Resolv_DNS);
	Equip->M->Temps_Resolv_DNS = Equip->Temps_Resolv_DNS;
		
	
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scanne des Ports");
	Equip->Time_Debut_Scan_Port = time(NULL);
	Scanne_Toutes_Les_Ports_TCP (Equip);
	Equip->Time_Fin_Scan_Port = time(NULL);
	Equip->Temps_Scan_Port = (gint) difftime(Equip->Time_Fin_Scan_Port, Equip->Time_Debut_Scan_Port);
	Equip->M->Temps_Scan_Port = Equip->Temps_Scan_Port;
	
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scanne des Ports UDP");
	Equip->Time_Debut_Scan_Port_Udp = time(NULL);
	Scanne_Toutes_Les_Ports_UDP (Equip);
	Equip->Time_Fin_Scan_Port_Udp = time(NULL);
	Equip->Temps_Scan_Port_Udp = (gint) difftime(Equip->Time_Fin_Scan_Port_Udp, Equip->Time_Debut_Scan_Port_Udp);
	Equip->M->Temps_Scan_Port_Udp = Equip->Temps_Scan_Port_Udp;
	

	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Traitement des Ports");
	Equip->vs_i= 0;
	Equip->M->Nb_Port_Total_Scanner = g_list_length (Equip->GList_Port);

	Equip->M->Nb_Port_Ouvert = 0;
	Equip->M->Nb_Port_Fermer = 0;
	Equip->M->Nb_Port_Firewaller = 0;
	Equip->vs_Nb_Port_Ajouter_Ouvert = 0;
	Equip->vs_Nb_Port_Ajouter_Fermer = 0;
	Equip->vs_Nb_Port_Ajouter_Firewalled = 0;
	while (Equip->vs_i< Equip->M->Nb_Port_Total_Scanner)  {
		Equip->Port_Equipement = g_list_nth_data (Equip->GList_Port, Equip->vs_i);
	
		if (Equip->Port_Equipement->Etat_Port == OUVERT){			
			if (Equip->vs_Nb_Port_Ajouter_Ouvert<MAX_PORTS) {
				Equip->M->Port_Ouvert[Equip->vs_Nb_Port_Ajouter_Ouvert].Num_Port = Equip->Port_Equipement->Num_Port;
				Equip->M->Port_Ouvert[Equip->vs_Nb_Port_Ajouter_Ouvert].Derniere_Modification_Etat_Port = Equip->Port_Equipement->Derniere_Modification_Etat_Port;
				Equip->M->Port_Ouvert[Equip->vs_Nb_Port_Ajouter_Ouvert].Flags = Equip->Port_Equipement->Flags;
				Equip->vs_Nb_Port_Ajouter_Ouvert++;
			}
			Equip->M->Nb_Port_Ouvert++;
		}
	
		if  (Equip->Port_Equipement->Etat_Port == FERMER) {
			if (Equip->vs_Nb_Port_Ajouter_Fermer<MAX_PORTS){
				Equip->M->Port_Fermer[Equip->vs_Nb_Port_Ajouter_Fermer].Num_Port = Equip->Port_Equipement->Num_Port;
				Equip->M->Port_Fermer[Equip->vs_Nb_Port_Ajouter_Fermer].Derniere_Modification_Etat_Port = Equip->Port_Equipement->Derniere_Modification_Etat_Port;
				Equip->M->Port_Fermer[Equip->vs_Nb_Port_Ajouter_Fermer].Flags = Equip->Port_Equipement->Flags;
				Equip->vs_Nb_Port_Ajouter_Fermer++;
			}
			Equip->M->Nb_Port_Fermer++;
		}
	
		if (Equip->Port_Equipement->Etat_Port == FIREWALLED) {
			if (Equip->vs_Nb_Port_Ajouter_Firewalled<MAX_PORTS){
				Equip->M->Port_Firewalled[Equip->vs_Nb_Port_Ajouter_Firewalled].Num_Port = Equip->Port_Equipement->Num_Port;
				Equip->M->Port_Firewalled[Equip->vs_Nb_Port_Ajouter_Firewalled].Derniere_Modification_Etat_Port = Equip->Port_Equipement->Derniere_Modification_Etat_Port;
				Equip->vs_Nb_Port_Ajouter_Firewalled++;
			}
			Equip->M->Nb_Port_Firewaller++;
		}
		Equip->vs_i++;
	}

	for (Equip->vs_i = 0; Equip->vs_i < NB_PORT_UDP; Equip->vs_i++)
		Equip->M->Liste_Etat_Udp[Equip->vs_i] = Equip->Liste_Etat_Udp[Equip->vs_i];
	
	
	
	if (memcmp (Equip->ID_Equipement, Mon_Adresse_MAC, MAC_ADDR_LEN) != 0) {
		Equip->Agent_AutoScan = FALSE;
		Equip->M->Agent_AutoScan = FALSE;
		Cherche_Un_Port_Avec_Etat (Equip, OUVERT, &Equip->M->Port_Signature_Ouvert);
		Cherche_Un_Port_Avec_Etat (Equip, FERMER, &Equip->M->Port_Signature_Fermer);
		debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scan os (%s) avec comme num port ouvert[%d]   num port fermer[%d]", Equip->Adresse_Ip_Char,Equip->M->Port_Signature_Ouvert,Equip->M->Port_Signature_Fermer);

		debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scan  Signature");
		Equip->Time_Debut_Scan_Signature = time(NULL);
		if ((Equip->M->Port_Signature_Ouvert != -1) || (Equip->M->Port_Signature_Fermer!= -1))
			Envoie_Trames_Scanne_Os (Equip, Equip->M->Port_Signature_Ouvert, Equip->M->Port_Signature_Fermer);
		Equip->Time_Fin_Scan_Signature = time(NULL);
		Equip->Temps_Scan_Signature = (gint) difftime(Equip->Time_Fin_Scan_Signature, Equip->Time_Debut_Scan_Signature);
		Equip->M->Temps_Scan_Signature = Equip->Temps_Scan_Signature;
		
		
	
		memcpy (Equip->M->Reponse_Signature, Equip->Reponse_Signature, sizeof (struct struct_Reponse) * NB_SCAN_OS);
		memcpy (Equip->M->Reponse_Signature_Debug, Equip->Reponse_Signature_Debug, sizeof (struct struct_Reponse_debug) * NB_SCAN_OS);
	
		Equip->M->GateWay = Equip->GateWay;
	
		Equip->M->Routage = Equip->Routage_Activer;
		

		Cherche_Si_Port_Ouvert (Equip, PORT_FTP);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Ftp = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port FTP ouvert");
			Equip->vs_banniere = Recupere_Banniere_FTP (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->M->Entete_FTP, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->M->Entete_FTP, Equip->vs_banniere);
				else {
					strncpy (Equip->M->Entete_FTP, Equip->vs_banniere, 499);
					Equip->M->Entete_FTP[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Ftp = time(NULL);
			Equip->Temps_Scan_Service_Ftp = (gint) difftime(Equip->Time_Fin_Scan_Service_Ftp, Equip->Time_Debut_Scan_Service_Ftp);
			Equip->M->Temps_Scan_Service_Ftp = Equip->Temps_Scan_Service_Ftp;
		}
		else
			Equip->M->Temps_Scan_Service_Ftp = -1;
		
		

		Cherche_Si_Port_Ouvert (Equip, PORT_HTTP);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Http = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port HTTP ouvert");
			Equip->vs_banniere = Recupere_Banniere_HTTP (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->M->Entete_HTTP, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->M->Entete_HTTP, Equip->vs_banniere);
				else {
					strncpy (Equip->M->Entete_HTTP, Equip->vs_banniere, 499);
					Equip->M->Entete_HTTP[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Http = time(NULL);
			Equip->Temps_Scan_Service_Http = (gint) difftime(Equip->Time_Fin_Scan_Service_Http, Equip->Time_Debut_Scan_Service_Http);
			Equip->M->Temps_Scan_Service_Http = Equip->Temps_Scan_Service_Http;
		}
		else
			Equip->M->Temps_Scan_Service_Http = -1;
		
				

		Cherche_Si_Port_Ouvert (Equip, PORT_IMAP);	
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Imap = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port IMAP ouvert");
			Equip->vs_banniere = Recupere_Banniere_IMAP (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->M->Entete_IMAP, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->M->Entete_IMAP, Equip->vs_banniere);
				else {
					strncpy (Equip->M->Entete_IMAP, Equip->vs_banniere, 499);
					Equip->M->Entete_IMAP[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Imap = time(NULL);
			Equip->Temps_Scan_Service_Imap = (gint) difftime(Equip->Time_Fin_Scan_Service_Imap, Equip->Time_Debut_Scan_Service_Imap);
			Equip->M->Temps_Scan_Service_Imap = Equip->Temps_Scan_Service_Imap;
		}
		else
			Equip->M->Temps_Scan_Service_Imap = -1;


		Cherche_Si_Port_Ouvert (Equip, PORT_IMAP);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Smtp = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port SMTP ouvert");
			Equip->vs_banniere = Recupere_Banniere_SMTP (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->M->Entete_SMTP, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->M->Entete_SMTP, Equip->vs_banniere);
				else {
					strncpy (Equip->M->Entete_SMTP, Equip->vs_banniere, 499);
					Equip->M->Entete_SMTP[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Smtp = time(NULL);
			Equip->Temps_Scan_Service_Smtp = (gint) difftime(Equip->Time_Fin_Scan_Service_Smtp, Equip->Time_Debut_Scan_Service_Smtp);
			Equip->M->Temps_Scan_Service_Smtp = Equip->Temps_Scan_Service_Smtp;
		}
		else
			Equip->M->Temps_Scan_Service_Smtp = -1;

		
		Cherche_Si_Port_Ouvert (Equip, PORT_SSH);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Ssh = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port SSH ouvert");
			Equip->vs_banniere = Recupere_Banniere_SSH (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->MR->Entete_SSH, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 499)
					strcpy (Equip->M->Entete_SSH, Equip->vs_banniere);
				else {
					strncpy (Equip->M->Entete_SSH, Equip->vs_banniere, 499);
					Equip->M->Entete_SSH[499] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Ssh = time(NULL);
			Equip->Temps_Scan_Service_Ssh = (gint) difftime(Equip->Time_Fin_Scan_Service_Ssh, Equip->Time_Debut_Scan_Service_Ssh);
			Equip->M->Temps_Scan_Service_Ssh = Equip->Temps_Scan_Service_Ssh;
		}
		else
			Equip->M->Temps_Scan_Service_Ssh = -1;
		
		Cherche_Si_Port_Ouvert (Equip, PORT_TELNET);
		if (Equip->Resultat_Test_Port_Ouvert) {
			Equip->Time_Debut_Scan_Service_Telnet = time(NULL);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Port TELNET ouvert");
			Equip->vs_banniere = Recupere_Banniere_TELNET (Equip);
			if (Equip->vs_banniere == NULL) {
				strcpy (Equip->M->Entete_TELNET, "");
			}
			else {
				if (strlen (Equip->vs_banniere) < 999)
					strcpy (Equip->M->Entete_TELNET, Equip->vs_banniere);
				else {
					strncpy (Equip->M->Entete_TELNET, Equip->vs_banniere, 999);
					Equip->M->Entete_TELNET[999] = '\0';
				}
				MYFREE (Equip->vs_banniere);
			}
			Equip->Time_Fin_Scan_Service_Telnet = time(NULL);
			Equip->Temps_Scan_Service_Telnet = (gint) difftime(Equip->Time_Fin_Scan_Service_Telnet, Equip->Time_Debut_Scan_Service_Telnet);
			Equip->M->Temps_Scan_Service_Telnet = Equip->Temps_Scan_Service_Telnet;
		}
		else
			Equip->M->Temps_Scan_Service_Telnet = -1;
	}
	else {
		Equip->Agent_AutoScan = TRUE;
		Equip->M->Agent_AutoScan = TRUE;
	}

	
	Cherche_Si_Port_Ouvert (Equip, PORT_NETBIOS);
	if (Equip->Resultat_Test_Port_Ouvert) {
		debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scan  NETBIOS");
		Equip->Time_Debut_Samba = time(NULL);
		Equip->vs_Tentative_NetBios = 0;
		if (Info_Demarrage.Normal_Mode)
			Equip->vs_Tentative_NetBios_MAX = 5;
		else
			Equip->vs_Tentative_NetBios_MAX = 2;
	
		if (Equip->Agent_AutoScan)
			Equip->vs_Tentative_NetBios_MAX = 1;
		Demande_NetBios (Equip, Equip->vs_Tentative_NetBios);
		while ((Equip->vs_Tentative_NetBios < Equip->vs_Tentative_NetBios_MAX) && (!Equip->Resultat_Netbios)) {
			Equip->vs_Tentative_NetBios++;
			sleep (1);
			debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Nouvelle tentative NETBIOS tentatice(%d) (%s)", Equip->vs_Tentative_NetBios, Equip->Adresse_Ip_Char);
			Demande_NetBios (Equip, Equip->vs_Tentative_NetBios);
		}
		memcpy (&Equip->M->Smb, &Equip->Smb, sizeof (struct struct_Smb)); 
		Equip->Time_Fin_Samba = time(NULL);
		Equip->Temps_Samba = (gint) difftime(Equip->Time_Fin_Samba, Equip->Time_Debut_Samba);
		Equip->M->Temps_Samba = Equip->Temps_Samba;
	}
		
	
#ifdef        __SNMP_ENABLE
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scan  Snmp");
	Equip->Time_Debut_Scan_Snmp = time(NULL);
	Demande_Snmp_Equipement (Equip);
	Equip->Time_Fin_Scan_Snmp = time(NULL);
	Equip->Temps_Scan_Snmp = (gint) difftime(Equip->Time_Fin_Scan_Snmp, Equip->Time_Debut_Scan_Snmp);
	Equip->M->Temps_Scan_Snmp = Equip->Temps_Scan_Snmp;
	Equip->M->Snmp_Ouvert = Equip->Snmp_Ouvert;
	memcpy (&Equip->M->Snmp, &Equip->Snmp, sizeof (struct Struct_Snmp));
#endif

	

	Equip->Time_Fin = time(NULL);
	Equip->Temps_Scan_Complet = (gint) difftime(Equip->Time_Fin, Equip->Time_Debut);
	Equip->M->Temps_Scan_Complet = Equip->Temps_Scan_Complet;
	
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Nb_Socket_Ouverte [%d]   %s Total : %d - ResolvDNS :%d - ScanPort :%d - ScanSignature :%d - Ftp : %d - http:%d - imap:%d - telnet:%d - samba:%d - snmp:%d",Nb_Socket_Ouverte,Equip->M->Adresse_Ip_Char,Equip->M->Temps_Scan_Complet,	Equip->M->Temps_Resolv_DNS,	Equip->M->Temps_Scan_Port,Equip->M->Temps_Scan_Signature,Equip->M->Temps_Scan_Service_Ftp,Equip->M->Temps_Scan_Service_Http,Equip->M->Temps_Scan_Service_Imap,Equip->M->Temps_Scan_Service_Telnet,Equip->M->Temps_Samba,Equip->M->Temps_Scan_Snmp);
		
	Communication_Envoie_Donnee (NOUVEAU_EQUIPEMENT, (gpointer *) Equip->M, sizeof (struct struct_Nouveau_Equipement));
	Equip->Scan_Signature = FALSE;
	MYFREE(Equip->M);

#ifdef        __SNMP_ENABLE
	if (Equip->Snmp_Ouvert){
		debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Lecture snmp --> SNMP_SYSTEM_DEVICE");
		Envoie_Snmp_Liste(Equip,SNMP_SYSTEM_DEVICE);
		debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Lecture snmp --> SNMP_SYSTEM_DEVICE = OK");

	}
#endif

	bzero (&Affiche_Nouveau_Equipement, sizeof (struct struct_Retour_Affiche_Nouveau_Equipement));
	Affiche_Nouveau_Equipement.ID_Reseau = Info_Demarrage.ID_Reseau;
	memcpy (Affiche_Nouveau_Equipement.ID_Equipement, Equip->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);

	Affiche_Nouveau_Equipement.Intrus = Equip->Intrus;
	Communication_Envoie_Donnee (AFFICHE_NOUVEAU_EQUIPEMENT, (gpointer *) &Affiche_Nouveau_Equipement, sizeof (struct struct_Retour_Affiche_Nouveau_Equipement));


	DEPROTECT_CODE(Semaphore_Thread_Scan);
	if (Equip->Agent_AutoScan){
		Scan_Machine_Local_Fait = TRUE;
	
	}
	Equip->Scan_Port_En_Cours = FALSE;
	Stop_Thread (NULL);
}


void Envoie_WakeUp (struct Struct_Liste_Equipement *Equipement)
{ 
	ERROR_if (Equipement == NULL);
	PROTECT_CODE(Semaphore_WakeUp);
	
	char unsigned WakeUp_Trame[6 + 16*MAC_ADDR_LEN],*pTrame = NULL;
	struct sockaddr_in SockName;
	int Socket_WakeUp,i,j;
	int sockaddr_in_len = sizeof(struct sockaddr_in);
#ifdef __LOG_ENABLE		
	Create_Log(CATEG_THREAD_WAKEUP,NULL,NULL,TRUE);
#endif
	if ((Socket_WakeUp = socket_control(PF_INET, SOCK_DGRAM, 0)) < 0)
		debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "WakeUp :%s",Equipement->Adresse_MAC_Char);
	bzero((char *)&SockName,sizeof(SockName));
    SockName.sin_family = AF_INET;
	SockName.sin_port = htons(9);
	SockName.sin_addr.s_addr = inet_addr(Mon_Adresse_Ip);  
	
	int opt = 1;
	setsockopt(Socket_WakeUp, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(int));
	pTrame = WakeUp_Trame;
    for (i=0; i < 6; i++)
		*pTrame++ = 0xFF;
    for (i=0; i < 16; i++)
		for (j=0; j < MAC_ADDR_LEN; j++)
	   		*pTrame++ = Equipement->Adresse_MAC[j];
	Nouveau_Octet_Envoye (sizeof (WakeUp_Trame));
	if (sendto (Socket_WakeUp, WakeUp_Trame, sizeof (WakeUp_Trame), 0, (struct sockaddr *) &SockName, sockaddr_in_len) <= 0) {
		if ((errno != EPIPE) && (errno != ESPIPE))
			debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%s",strerror (errno));
		debug_printf (D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Socket_WakeUp ??? %s",strerror (errno));
	}
	if (close_control (Socket_WakeUp) < 0)
		debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%s\n[%s] Socket ouvert[%d]  Thread[%d][%d]\n Nb equipement (%d)", Equipement->Adresse_Ip_Char,strerror (errno), Nb_Socket_Ouverte, Info_Demarrage.Nb_Thread_Max, Info_Demarrage.Nb_Thread_Max - Valeur_Semaphore (&Semaphore_Thread_Scan), g_list_length (GList_Equipement));
	DEPROTECT_CODE(Semaphore_WakeUp); 
	Stop_Thread (NULL);
}



void Demande_WakeUp (gpointer * Donnee)
{
	ERROR_if (Donnee == NULL);
	struct struct_Demande_WakeUp *Message = (struct struct_Demande_WakeUp *) Donnee;
	struct Struct_Liste_Equipement *Equipement = NULL;
	gint      ID_Equipement;
	
	Recherche_Index_Equipement (Message->ID_Equipement,FALSE, &ID_Equipement);
	Retour_Pointeur_Equipement (&Equipement,ID_Equipement);
	demmarrage_Thread (&Envoie_WakeUp, (gpointer *) Equipement);
}
