/* ***********************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 "Restoration_Interface.h"
#include "Equipement.h"
#include "Equipement_Port.h"
#include "Equipement_Historique.h"
#include "Reseau.h"
#include "Debug.h"
#include "Objet.h"
#include "Com/Commun.h"
#include "Com/Initialisation_Com.h"
#include "Reseau.h"
#include "Equipement_Nessus.h"
#include "Animation.h"
#include "Objet_Liste.h"
#include "Gconf.h"
#include "Macro.h"

extern struct Struct_Liste_Port_UDP Liste_Port_UDP[];


void Lit_Donnee_Xml_Restoration_Interface (xmlNodePtr xmlnode, gint ID_Reseau, gboolean Ip_Dynamic)
{
#define FORMAT_DATE "%a %b %e %H:%M:%S %Y"
	struct Struct_Equipement *Equipement = NULL;
	struct Struct_Port *Port_Equipement = NULL;
	struct Struct_Modele_Objet *Objet_Equipement_Par_Ip = NULL,*Objet_Equipement_Par_Type = NULL;
	struct Struct_Historique *Historique = NULL;
	struct struct_Demande_Notication_Critique_Etat_Equipement Message;
	struct Struct_Liste_Reseau *Reseau = NULL;
	struct struct_Demande_Notication_Critique_Etat_Port Message_Port;
	GtkTreeModel *model = NULL;
	GtkTreePath *path = NULL;
	xmlNodePtr xml_Enfant = NULL, xml_Enfant_Port = NULL;
	gchar    *CharPort = NULL, *Date = NULL, *Date_Erreur = NULL, *Address_IP = NULL, *Address_MAC = NULL, *cNotification = NULL;
	gchar    *cSmb = NULL, *cSignature = NULL, *cHistorique = NULL, *CharNiveau = NULL, *cTemp = NULL, *cNessus = NULL, *cAgent = NULL;
	struct tm tm;
	gint      NumPort, Erreur_Sscanf, niveau_Historique, NumRegistration, niveau_Nessus;
	gchar    *Point_DNS = NULL, Dns[TAILLE_CHAINE_NOM_DNS + 1];
	gint      Taille_Chaine,udp,etat_udp;

	ERROR_if (xmlnode == NULL);
	Reseau = Recupere_Pointeur_Structure_Reseau (ID_Reseau);
	MALLOC (Equipement, sizeof (struct Struct_Equipement));

	if (!xmlnode->name || g_strcasecmp (xmlnode->name, "Equipment") != 0)
		return;

	Address_MAC = xmlGetProp (xmlnode, "Address_MAC");
	if ((Erreur_Sscanf = sscanf (Address_MAC, "%02X:%02X:%02X:%02X:%02X:%02X", (int *) &Equipement->ID_Equipement[0], (int *) &Equipement->ID_Equipement[1], (int *) &Equipement->ID_Equipement[2], (int *) &Equipement->ID_Equipement[3], (int *) &Equipement->ID_Equipement[4], (int *) &Equipement->ID_Equipement[5])) != 6) {
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Recuperation Erreur(%d) MAC ID_Equipement %02X:%02X:%02X:%02X:%02X:%02X\n", Erreur_Sscanf);
	}
	strcpy (Equipement->Adresse_MAC_Char, Address_MAC);
	MYFREE (Address_MAC);

	cAgent = xmlGetProp (xmlnode, "Agent_AutoScan");
	if (cAgent != NULL) {
		if (cAgent[0] == '1') {
			Equipement->Agent_AutoScan = TRUE;
			Equipement->Equipement_Allumer = TRUE;
		}
		else {
			Equipement->Agent_AutoScan = FALSE;
			Equipement->Equipement_Allumer = FALSE;
		}
		MYFREE (cAgent);
	}	
	
	Address_IP = xmlGetProp (xmlnode, "Address_IP");
	if ((!Ip_Dynamic) || (Equipement->Agent_AutoScan)) {
		if ((Erreur_Sscanf = sscanf (Address_IP, "%d.%d.%d.%d", (int *) &Equipement->ID_Equipement[6], (int *) &Equipement->ID_Equipement[7], (int *) &Equipement->ID_Equipement[8], (int *) &Equipement->ID_Equipement[9])) != 4) {
			debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Recuperation IP ID_Equipement  (%d)", Erreur_Sscanf);
		}
	
		strcpy (Equipement->Adresse_Ip_Char, Address_IP);
	}
	else
		strcpy (Equipement->Adresse_Ip_Char, "0.0.0.0");








	MYFREE (Address_IP);




	Date = xmlGetProp (xmlnode, "Last_State_Change");
	Date_Erreur = (gchar *) strptime (Date, FORMAT_DATE, &tm);

	if (Date_Erreur == NULL)
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration Equipement date : [%s] [%s]", Date, Date_Erreur);
	Equipement->Derniere_Modification_Etat_Equipement = mktime (&tm);
	MYFREE (Date);
	Equipement->User_Smb = xmlGetProp (xmlnode, "User_Smb");
	Equipement->Password_Smb = xmlGetProp (xmlnode, "Password_Smb");
	cTemp = xmlGetProp (xmlnode, "Admin_Email");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Contact_Admin = NULL;
	}
	else
		Equipement->Contact_Admin = cTemp;
	cTemp = xmlGetProp (xmlnode, "Localisation");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Localisation = NULL;
	}
	else
		Equipement->Localisation = cTemp;
	cTemp = xmlGetProp (xmlnode, "Serial_Number");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Num_Serie = NULL;
	}
	else
		Equipement->Num_Serie = cTemp;
	cTemp = xmlGetProp (xmlnode, "Serial_Number_Display");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Num_Serie_Ecran = NULL;
	}
	else
		Equipement->Num_Serie_Ecran = cTemp;
	cTemp = xmlGetProp (xmlnode, "Serial_Number_Printer");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Num_Serie_Imprimante = NULL;
	}
	else
		Equipement->Num_Serie_Imprimante = cTemp;
	cTemp = xmlGetProp (xmlnode, "Serial_Number_OS");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Num_Serie_OS = NULL;
	}
	else
		Equipement->Num_Serie_OS = cTemp;
	cTemp = xmlGetProp (xmlnode, "Serial_Number_Office_Suite");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Num_Serie_Office_Suite = NULL;
	}
	else
		Equipement->Num_Serie_Office_Suite = cTemp;
	cTemp = xmlGetProp (xmlnode, "Processor");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Processeur = NULL;
	}
	else
		Equipement->Processeur = cTemp;
	cTemp = xmlGetProp (xmlnode, "Memory");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Memoire = NULL;
	}
	else
		Equipement->Memoire = cTemp;
	cTemp = xmlGetProp (xmlnode, "Disk");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Disque = NULL;
	}
	else
		Equipement->Disque = cTemp;
	cTemp = xmlGetProp (xmlnode, "Contact");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Contact = NULL;
	}
	else
		Equipement->Contact = cTemp;
	cTemp = xmlGetProp (xmlnode, "Tel_Contact");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Tel_Contact = NULL;
	}
	else
		Equipement->Tel_Contact = cTemp;
	cTemp = xmlGetProp (xmlnode, "Notes");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Notes = NULL;
	}
	else
		Equipement->Notes = cTemp;


	cTemp = xmlGetProp (xmlnode, "Dns");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Nom_DNS_Complet = NULL;
	}
	else {
		Equipement->Nom_DNS_Complet = cTemp;
		if (Equipement->Nom_DNS_Complet != NULL) {
			Point_DNS = index (Equipement->Nom_DNS_Complet, '.');
			if (Point_DNS != NULL) {
				Taille_Chaine = Point_DNS - Equipement->Nom_DNS_Complet;
				strncpy (Dns, Equipement->Nom_DNS_Complet, Taille_Chaine);
				Dns[Taille_Chaine] = 0;
				Equipement->Nom_DNS = g_strdup (Dns);
			
			}
		}
	}

	cTemp = xmlGetProp (xmlnode, "Alias");
	if ((cTemp != NULL) && (strlen (cTemp) == 0)) {
		MYFREE (cTemp);
		Equipement->Alias = NULL;
	}
	else
		Equipement->Alias = cTemp;


	cNotification = xmlGetProp (xmlnode, "Notification");
	if (cNotification[0] == '1')
		Equipement->Notification_En_Cours = TRUE;
	else
		Equipement->Notification_En_Cours = FALSE;
	MYFREE (cNotification);

	cTemp = xmlGetProp (xmlnode, "Gateway");
	if ((cTemp != NULL) && (strlen (cTemp) == 1)) {
		if (cTemp[0] == '1')
			Equipement->GateWay = TRUE;
		else
			Equipement->GateWay = FALSE;
		MYFREE (cTemp);
	}

	cTemp = xmlGetProp (xmlnode, "Routage");
	if ((cTemp != NULL) && (strlen (cTemp) == 1)) {
		if (cTemp[0] == '1')
			Equipement->Routage = TRUE;
		else
			Equipement->Routage = FALSE;
		MYFREE (cTemp);
	}

	cTemp = xmlGetProp (xmlnode, "Snmp_Open");
	if ((cTemp != NULL) && (strlen (cTemp) == 1)) {
		if (cTemp[0] == '1')
			Equipement->Snmp_Ouvert = TRUE;
		else
			Equipement->Snmp_Ouvert = FALSE;
		MYFREE (cTemp);
	}
	
	
	cTemp = xmlGetProp (xmlnode, "Expand");
	if ((cTemp != NULL) && (strlen (cTemp) == 1)) {
		if (cTemp[0] == '1')
			Equipement->Deplier = TRUE;
		else
			Equipement->Deplier = FALSE;
		MYFREE (cTemp);
	}
	else
		Equipement->Deplier = FALSE;
	
	

	xmlnode = xmlnode->children;
	while ((xmlnode != NULL) && (xmlnode->type != XML_ELEMENT_NODE))
		xmlnode = xmlnode->next;

	if (xmlnode) {
	
		if (!xmlnode->name || g_strcasecmp (xmlnode->name, "FingerPrinting") != 0)
			return;
		cSignature = xmlGetProp (xmlnode, "MD5");
		strcpy (Equipement->Md5_Signature, cSignature);
		MYFREE (cSignature);
		cSignature = xmlGetProp (xmlnode, "MD5_W");
		strcpy (Equipement->Md5_Signature_W, cSignature);
		MYFREE (cSignature);		
		Equipement->Entete_FTP = xmlGetProp (xmlnode, "Header_FTP");
		Equipement->Entete_HTTP = xmlGetProp (xmlnode, "Header_HTTP");
		Equipement->Entete_IMAP = xmlGetProp (xmlnode, "Header_IMAP");
		Equipement->Entete_SMTP = xmlGetProp (xmlnode, "Header_SMTP");
		Equipement->Entete_SSH = xmlGetProp (xmlnode, "Header_SSH");
		Equipement->Entete_TELNET = xmlGetProp (xmlnode, "Header_TELNET");
		Equipement->Debug_Signature = xmlGetProp (xmlnode, "Debug");
	
		xmlnode = xmlnode->next;
		while ((xmlnode != NULL) && (xmlnode->type != XML_ELEMENT_NODE))
			xmlnode = xmlnode->next;
		if (!xmlnode->name || g_strcasecmp (xmlnode->name, "Ports") != 0)
			return;
		cTemp = xmlGetProp (xmlnode, "Open");
		if (sscanf (cTemp, "%d", &Equipement->Nb_Port_Ouvert) != 1) {
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration nb port ouvert : [%s]", cTemp);
			Equipement->Nb_Port_Ouvert = 0;
		}
		MYFREE (cTemp);
		cTemp = xmlGetProp (xmlnode, "Total");
		if (sscanf (cTemp, "%d", &Equipement->Nb_Port_Total_Scanner) != 1) {
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration nb port total : [%s]", cTemp);
			Equipement->Nb_Port_Total_Scanner = 0;
		}
		MYFREE (cTemp);
		cTemp = xmlGetProp (xmlnode, "firewalled");
		if (sscanf (cTemp, "%d", &Equipement->Nb_Port_Firewaller) != 1) {
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration nb port firewalled : [%s]", cTemp);
			Equipement->Nb_Port_Firewaller = 0;
		}
		MYFREE (cTemp);
		
		xml_Enfant_Port = xmlnode->children;
		while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
			xml_Enfant_Port = xml_Enfant_Port->next;

	
		while (xml_Enfant_Port->name && g_strcasecmp (xml_Enfant_Port->name, "Open") != 0) {
		
			xml_Enfant_Port = xml_Enfant_Port->next;
			while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
				xml_Enfant_Port = xml_Enfant_Port->next;
		}
	
		if (!xml_Enfant_Port->name || g_strcasecmp (xml_Enfant_Port->name, "Open") != 0)
			return;
		xml_Enfant_Port = xml_Enfant_Port->children;
		while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
			xml_Enfant_Port = xml_Enfant_Port->next;
		
		
		while (xml_Enfant_Port) {
			if (!xml_Enfant_Port->name || g_strcasecmp (xml_Enfant_Port->name, "Port") != 0)
				return;
			CharPort = xmlGetProp (xml_Enfant_Port, "Num");
		
			if (sscanf (CharPort, "%d", &NumPort) != 1) {
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration port : [%s]", CharPort);
				NumPort = 0;
			}
			MYFREE (CharPort);
			Port_Equipement = Ajouter_Nouveau_Port_Equipement (Equipement, NumPort, 0);
			Date = xmlGetProp (xml_Enfant_Port, "Last_State_Change");
			Date_Erreur = (gchar *) strptime (Date, FORMAT_DATE, &tm);
			if (Date_Erreur == NULL)
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration Port date : [%s] [%s]", Date, Date_Erreur);
			Port_Equipement->Derniere_Modification_Etat_Port = mktime (&tm);
			MYFREE (Date);
			cNotification = xmlGetProp (xml_Enfant_Port, "Notification");
			if (cNotification[0] == '1') {
				Port_Equipement->Notification_En_Cours = TRUE;
				bzero (&Message_Port, sizeof (struct struct_Demande_Notication_Critique_Etat_Port));
				Message_Port.ID_Reseau = ID_Reseau;
				memcpy (Message_Port.ID_Equipement, Equipement->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
				Message_Port.Num_Port = Port_Equipement->Num_Port;
				Communication_Envoie_Donnee (Reseau->Pid_Agent, DEMANDE_NOTIFICATION_CRITIQUE_ETAT_PORT, (gpointer *) & Message_Port, sizeof (Message_Port), Reseau);
			}
			else
				Port_Equipement->Notification_En_Cours = FALSE;
			MYFREE (cNotification);
			xml_Enfant_Port = xml_Enfant_Port->next;
			while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
				xml_Enfant_Port = xml_Enfant_Port->next;
		}
		
	
	
		xml_Enfant_Port = xmlnode->children;
		while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
			xml_Enfant_Port = xml_Enfant_Port->next;
	
	
		while (xml_Enfant_Port->name && g_strcasecmp (xml_Enfant_Port->name, "Close") != 0) {
		
			xml_Enfant_Port = xml_Enfant_Port->next;
			while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
				xml_Enfant_Port = xml_Enfant_Port->next;
		}
	
		
		if (!xml_Enfant_Port->name || g_strcasecmp (xml_Enfant_Port->name, "Close") != 0)
			return;
		xml_Enfant_Port = xml_Enfant_Port->children;
		while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
			xml_Enfant_Port = xml_Enfant_Port->next;
		
		while (xml_Enfant_Port) {
			if (!xml_Enfant_Port->name || g_strcasecmp (xml_Enfant_Port->name, "Port") != 0)
				return;
			
			CharPort = xmlGetProp (xml_Enfant_Port, "Num");
		
			if (sscanf (CharPort, "%d", &NumPort) != 1) {
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration port Fermer : [%s]", CharPort);
				NumPort = 0;
			}
			MYFREE (CharPort);
			Ajouter_Nouveau_Port_Equipement_Fermer_Firewall (Equipement, NumPort,FERMER,0);	
		
			xml_Enfant_Port = xml_Enfant_Port->next;
			while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
				xml_Enfant_Port = xml_Enfant_Port->next;
		}
		
		
		
		
		
	
	
		xml_Enfant_Port = xmlnode->children;
		while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
			xml_Enfant_Port = xml_Enfant_Port->next;
	
	
		while (xml_Enfant_Port->name && g_strcasecmp (xml_Enfant_Port->name, "Firewall") != 0) {
		
			xml_Enfant_Port = xml_Enfant_Port->next;
			while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
				xml_Enfant_Port = xml_Enfant_Port->next;
		}
	
		
		if (!xml_Enfant_Port->name || g_strcasecmp (xml_Enfant_Port->name, "Firewall") != 0)
			return;
		xml_Enfant_Port = xml_Enfant_Port->children;
		while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
			xml_Enfant_Port = xml_Enfant_Port->next;
		
		while (xml_Enfant_Port) {
			if (!xml_Enfant_Port->name || g_strcasecmp (xml_Enfant_Port->name, "Port") != 0)
				return;
			
			CharPort = xmlGetProp (xml_Enfant_Port, "Num");
		
			if (sscanf (CharPort, "%d", &NumPort) != 1) {
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration port Firewaller : [%s]", CharPort);
				NumPort = 0;
			}
			MYFREE (CharPort);
			Ajouter_Nouveau_Port_Equipement_Fermer_Firewall (Equipement, NumPort,FIREWALLED,0);	
		
			xml_Enfant_Port = xml_Enfant_Port->next;
			while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
				xml_Enfant_Port = xml_Enfant_Port->next;
		}
		
		
		
		
		
	
	
		xml_Enfant_Port = xmlnode->children;
		while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
			xml_Enfant_Port = xml_Enfant_Port->next;
	
	
		while (xml_Enfant_Port->name && g_strcasecmp (xml_Enfant_Port->name, "UDP") != 0) {
		
			xml_Enfant_Port = xml_Enfant_Port->next;
			while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
				xml_Enfant_Port = xml_Enfant_Port->next;
		}
	
		
		if (!xml_Enfant_Port->name || g_strcasecmp (xml_Enfant_Port->name, "UDP") != 0)
			return;
		xml_Enfant_Port = xml_Enfant_Port->children;
		while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
			xml_Enfant_Port = xml_Enfant_Port->next;
		
		
		udp = 0;
		while (xml_Enfant_Port) {
			if (!xml_Enfant_Port->name || g_strcasecmp (xml_Enfant_Port->name, "Port") != 0)
				return;
		
			CharPort = xmlGetProp (xml_Enfant_Port, "Num");
		
			if (sscanf (CharPort, "%d", &NumPort) != 1) {
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration port UDP : [%s]", CharPort);
				NumPort = 0;
			}
			MYFREE (CharPort);
			ERROR_if (NumPort!=Liste_Port_UDP[udp].Num_Port_UDP);
		
			CharPort = xmlGetProp (xml_Enfant_Port, "State");
		
			if (sscanf (CharPort, "%d", &etat_udp) != 1) {
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration port UDP : [%s]", CharPort);
				etat_udp = 0;
			}
			MYFREE (CharPort);
			Equipement->Liste_Etat_Udp[udp] = etat_udp;
		
			xml_Enfant_Port = xml_Enfant_Port->next;
			while ((xml_Enfant_Port != NULL) && (xml_Enfant_Port->type != XML_ELEMENT_NODE))
				xml_Enfant_Port = xml_Enfant_Port->next;
			udp++;
		}
		
		
		
		
		
		
		
		
		
		
		
		
	
		xmlnode = xmlnode->next;
		while ((xmlnode != NULL) && (xmlnode->type != XML_ELEMENT_NODE))
			xmlnode = xmlnode->next;
		if (!xmlnode->name || g_strcasecmp (xmlnode->name, "History") != 0)
			return;
	
		xml_Enfant = xmlnode->children;
		while ((xml_Enfant != NULL) && (xml_Enfant->type != XML_ELEMENT_NODE))
			xml_Enfant = xml_Enfant->next;
		while (xml_Enfant) {
			if (!xml_Enfant->name || g_strcasecmp (xml_Enfant->name, "History") != 0)
				return;
			CharNiveau = xmlGetProp (xml_Enfant, "Level");
			if (sscanf (CharNiveau, "%d", &niveau_Historique) != 1) {
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration historique: [%s]", CharNiveau);
				niveau_Historique = 0;
			}
			MYFREE (CharNiveau);
			cHistorique = xmlGetProp (xml_Enfant, "Text");
			Historique = Ajouter_Historique (Equipement, cHistorique, niveau_Historique);
			MYFREE (cHistorique);
			Date = xmlGetProp (xml_Enfant, "Date");
			Date_Erreur = (gchar *) strptime (Date, FORMAT_DATE, &tm);
			
			if (Date_Erreur == NULL)
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration date : [%s] [%s]", Date, Date_Erreur);
			Historique->Date = mktime (&tm);
			MYFREE (Date);
			xml_Enfant = xml_Enfant->next;
			while ((xml_Enfant != NULL) && (xml_Enfant->type != XML_ELEMENT_NODE))
				xml_Enfant = xml_Enfant->next;
		}
	
		xmlnode = xmlnode->next;
		while ((xmlnode != NULL) && (xmlnode->type != XML_ELEMENT_NODE))
			xmlnode = xmlnode->next;
		if (!xmlnode->name || g_strcasecmp (xmlnode->name, "Smb") != 0)
			return;
		cSmb = xmlGetProp (xmlnode, "Host_Name");
		strcpy (Equipement->Smb.HostName, cSmb);
		MYFREE (cSmb);
		cSmb = xmlGetProp (xmlnode, "Domain_Name");
		strcpy (Equipement->Smb.DomainName, cSmb);
		MYFREE (cSmb);
		cSmb = xmlGetProp (xmlnode, "User");
		strcpy (Equipement->Smb.Utilisateur, cSmb);
		MYFREE (cSmb);
	
		NumRegistration = 0;
	
		xml_Enfant = xmlnode->children;
		while ((xml_Enfant != NULL) && (xml_Enfant->type != XML_ELEMENT_NODE))
			xml_Enfant = xml_Enfant->next;
		while (xml_Enfant) {
			if (!xml_Enfant->name || g_strcasecmp (xml_Enfant->name, "Registration") != 0)
				return;
			cSmb = xmlGetProp (xml_Enfant, "Number");
			if (sscanf (cSmb, "%c", &Equipement->Smb.Smb_Registration[NumRegistration].Numero) != 1) {
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration Registration: [%s]", cSmb);
				Equipement->Smb.Smb_Registration[NumRegistration].Numero = 0;
			}
			MYFREE (cSmb);
			cSmb = xmlGetProp (xml_Enfant, "Type");
			if (sscanf (cSmb, "%c", &Equipement->Smb.Smb_Registration[NumRegistration].Type) != 1) {
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration Registration: [%s]", cSmb);
				Equipement->Smb.Smb_Registration[NumRegistration].Type = 0;
			}
			MYFREE (cSmb);
			cSmb = xmlGetProp (xml_Enfant, "Name");
			strcpy (Equipement->Smb.Smb_Registration[NumRegistration].Name, cSmb);
			MYFREE (cSmb);
			xml_Enfant = xml_Enfant->next;
			while ((xml_Enfant != NULL) && (xml_Enfant->type != XML_ELEMENT_NODE))
				xml_Enfant = xml_Enfant->next;
			NumRegistration++;
		}
	
		xmlnode = xmlnode->next;
		while ((xmlnode != NULL) && (xmlnode->type != XML_ELEMENT_NODE))
			xmlnode = xmlnode->next;
		if (!xmlnode->name || g_strcasecmp (xmlnode->name, "Nessus") != 0)
			return;
	
		xml_Enfant = xmlnode->children;
		while ((xml_Enfant != NULL) && (xml_Enfant->type != XML_ELEMENT_NODE))
			xml_Enfant = xml_Enfant->next;
		while (xml_Enfant) {
			if (!xml_Enfant->name || g_strcasecmp (xml_Enfant->name, "Nessus") != 0)
				break;
			CharNiveau = xmlGetProp (xml_Enfant, "Level");
			if (sscanf (CharNiveau, "%d", &niveau_Nessus) != 1) {
				debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Restoration Nessus: [%s]", CharNiveau);
				niveau_Historique = 0;
			}
			MYFREE (CharNiveau);
#ifdef         __NESSUS_ENABLE
			cNessus = xmlGetProp (xml_Enfant, "Info");
			Ajouter_Info_Nessus (Equipement, cNessus, niveau_Nessus);
			MYFREE (cNessus);
			cNessus = xmlGetProp (xml_Enfant, "Text");
			Ajouter_Texte_Nessus (Equipement, cNessus);
			MYFREE (cNessus);
			xml_Enfant = xml_Enfant->next;
			while ((xml_Enfant != NULL) && (xml_Enfant->type != XML_ELEMENT_NODE))
				xml_Enfant = xml_Enfant->next;
#endif
		}
	
		xmlnode = xmlnode->next;
		while ((xmlnode != NULL) && (xmlnode->type != XML_ELEMENT_NODE))
			xmlnode = xmlnode->next;
	
		if (!xmlnode->name || g_strcasecmp (xmlnode->name, "Snmp") != 0)
			return;
	
	
	
		if (xmlnode){
			cTemp = xmlGetProp (xmlnode, "System_Name");
			strcpy (Equipement->Snmp.System_Name, cTemp);
		
			MYFREE (cTemp);
			
			cTemp = xmlGetProp (xmlnode, "Description");
			strcpy (Equipement->Snmp.Description, cTemp);
		
			MYFREE (cTemp);
			
			Equipement->Snmp_Info_Device  = xmlGetProp (xmlnode, "Info_Device");
		
		}
	}
	
	Optimisation_Info_Equipement(Equipement);

	Objet_Equipement_Par_Type = Afficher_Nouveau_Equipement (Equipement, ID_Reseau);
	if (Equipement->Deplier) {
		model = gtk_tree_view_get_model (GTK_TREE_VIEW (Widget_Liste (Objet_Equipement_Par_Type, Reseau)));
		path = gtk_tree_model_get_path (model, &Objet_Equipement_Par_Type->ID_Arbre_Ligne);
		gtk_tree_view_expand_to_path (GTK_TREE_VIEW (Widget_Liste (Objet_Equipement_Par_Type, Reseau)), path);
	}
	
	if (!Equipement->Agent_AutoScan){
		Affiche_Icone_Defaut_Griser_Objet_liste (Objet_Equipement_Par_Type, Reseau);
		Change_Couleur_Gris_Texte_liste (Objet_Equipement_Par_Type, Reseau);
	}

	Objet_Equipement_Par_Ip = Afficher_Nouveau_Equipement_Liste_Detaille (Equipement, ID_Reseau);
	if (!Equipement->Agent_AutoScan){
		Affiche_Icone_Defaut_Griser_Objet_liste (Objet_Equipement_Par_Ip, Reseau);
		Change_Couleur_Gris_Texte_liste (Objet_Equipement_Par_Ip, Reseau);
	}
	

	if (Equipement->Notification_En_Cours) {
		Objet_Equipement_Par_Type->Notification_En_Cours = TRUE;
		Objet_Equipement_Par_Ip->Notification_En_Cours = TRUE;
		bzero (&Message, sizeof (struct struct_Demande_Notication_Critique_Etat_Equipement));
		Message.ID_Reseau = Objet_Equipement_Par_Type->ID_Reseau;
		memcpy (Message.ID_Equipement, Equipement->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
		Communication_Envoie_Donnee (Reseau->Pid_Agent, DEMANDE_NOTIFICATION_CRITIQUE_ETAT_EQUIPEMENT, (gpointer *) & Message, sizeof (Message), Reseau);
	
		model = gtk_tree_view_get_model (GTK_TREE_VIEW (Widget_Liste (Objet_Equipement_Par_Ip, Reseau)));
		gtk_tree_store_set (GTK_TREE_STORE (model), &Objet_Equipement_Par_Ip->ID_Arbre_Ligne, NOTIFICATION, "YES", -1);
	}
}
	


void Lit_Xml_Restoration_Interface (xmlDocPtr doc, gint ID_Reseau, gboolean Ip_Dynamic)
{
	struct Struct_Liste_Reseau *Reseau = NULL;
	xmlNode  *root_element = NULL;
	GtkTreeModel *model = NULL;
	xmlNodePtr node;
	
	ERROR_if (doc == NULL);
	Reseau = Recupere_Pointeur_Structure_Reseau (ID_Reseau);



	root_element = xmlDocGetRootElement (doc);
	for (node = root_element->children; node != NULL; node = node->next) {
		if (node->type == XML_ELEMENT_NODE)
			Lit_Donnee_Xml_Restoration_Interface (node, ID_Reseau, Ip_Dynamic);
	}



	




}


gboolean Restoration_Interface (char *fname, gint ID_Reseau)
{
	xmlDocPtr doc = NULL;
	xmlNode  *root_element = NULL;
	struct Struct_Liste_Reseau *Reseau = NULL;
	gchar    *Tbool = NULL;
	gboolean  Ip_Dynamic;
	

	ERROR_if (fname == NULL);
	if (!g_file_test(fname,G_FILE_TEST_EXISTS))
		debug_printf (INTERFACE, D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Error loading file : [%s]", fname);

	doc = xmlParseFile (fname);
	if (!doc) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Fichier XML restoration : [%s]", fname);
		return FALSE;
	}
	root_element = xmlDocGetRootElement (doc);

	if (!root_element || !root_element->name || g_strcasecmp (root_element->name, "AutoScan_V2") != 0) {
		if (!root_element)
			debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "l'element n'est pas root");
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Fichier XML Port mal formaté : [%s]", fname);
		xmlFreeDoc (doc);
		return FALSE;
	}
	/*if (!doc->root || !doc->root->name || g_strcasecmp (doc->root->name, "AutoScan") != 0) {
	 * if (!doc->root)
	 * debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "l'element n'est pas root");
	 * debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Fichier XML Port mal formaté : [%s]", fname);
	 * xmlFreeDoc (doc);
	 * return FALSE;
	 * } */
	Reseau = Recupere_Pointeur_Structure_Reseau (ID_Reseau);
	Tbool = xmlGetProp (root_element, "Mode_IP");
	if ((Tbool != NULL) && (Tbool[0] == '1')) {
		Reseau->Mode_Liste_Detaille = TRUE;
	}
	MYFREE (Tbool);
	Tbool = xmlGetProp (root_element, "Ip_Dynamic");
	if (Tbool != NULL) {
		if (Tbool[0] == '1')
			Ip_Dynamic = TRUE;
		else
			Ip_Dynamic = FALSE;
	}
	else
		Ip_Dynamic = FALSE;
	MYFREE (Tbool);

	Lit_Xml_Restoration_Interface (doc, ID_Reseau, Ip_Dynamic);//Lecture_Gconf_Boolean (BOOL_IP_DYNAMIC));

	xmlFreeDoc (doc);
	return TRUE;
}
