/* ***********************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.h"
#include "Debug.h"
#include "Com/Commun.h"
#include "Liste_Equipement.h"
#include "Liste_Sous_Reseau.h"
#include "Scan_Port.h"
#include "Info.h"
#include "Macro.h"

void Ajout_Equipement_Restoration (gpointer * Donnee)
{
	ERROR_if (Donnee == NULL);
	struct struct_Restauration_Machine *Restoration_Equipement = (struct struct_Restauration_Machine *) Donnee;
	struct Struct_Liste_Equipement *Equipement = NULL;
	struct Struct_Port *Port_Equipement = NULL;
	gint      Erreur_Sscanf, Nb_Port_Restaurer, i;

	Nb_Machine_Dans_La_Liste++;
	MALLOC(Equipement,sizeof (struct Struct_Liste_Equipement));
	if ((Erreur_Sscanf = sscanf (Restoration_Equipement->Adresse_MAC_Char, "%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 (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Recuperation Erreur(%d) MAC ID_Equipement %02X:%02X:%02X:%02X:%02X:%02X", Erreur_Sscanf, Equipement->ID_Equipement[0], Equipement->ID_Equipement[1], Equipement->ID_Equipement[2], Equipement->ID_Equipement[3], Equipement->ID_Equipement[4], Equipement->ID_Equipement[5]);
	memcpy (Equipement->Adresse_MAC, Equipement->ID_Equipement, MAC_ADDR_LEN);
	if (sscanf (Restoration_Equipement->Adresse_Ip_Char, "%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 (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Recuperation IP ID_Equipement");
	if (!Restoration_Equipement->Ip_Dynamic) 
		memcpy (Equipement->Adresse_Ip, &Equipement->ID_Equipement[MAC_ADDR_LEN], 4);
	else
		strcpy (Restoration_Equipement->Adresse_Ip_Char, "0.0.0.0");

	sprintf (Equipement->Adresse_MAC_Char, "%02X:%02X:%02X:%02X:%02X:%02X", Equipement->Adresse_MAC[0], Equipement->Adresse_MAC[1], Equipement->Adresse_MAC[2], Equipement->Adresse_MAC[3], Equipement->Adresse_MAC[4], Equipement->Adresse_MAC[5]);
	g_hash_table_insert(Hash_Mac_Equipement,Equipement->Adresse_MAC_Char,Equipement);
	sprintf (Equipement->Adresse_Ip_Char, "%d.%d.%d.%d", Equipement->Adresse_Ip[0], Equipement->Adresse_Ip[1], Equipement->Adresse_Ip[2], Equipement->Adresse_Ip[3]);
	Equipement->Scan_Port_En_Cours = FALSE;
	Equipement->Equipement_Restaurer = TRUE;
	Equipement->Equipement_Rescanner_En_Cours = FALSE;
	Equipement->Equipement_Allumer = FALSE;
	Equipement->Derniere_Modification_Etat_Equipement = 0;
	Equipement->Notification_Equipement = FALSE;
	Equipement->Compteur_Notification = 0;
	Equipement->GList_Port = NULL;
	Equipement->Scanne_Port_En_Cours = FALSE;
	Equipement->Scan_Signature = FALSE;
	Equipement->Intrus = FALSE;
	Equipement->Marquage_Change_Ip = FALSE;
		
	Equipement->Alerte_Equipement_Active = FALSE;
#ifdef __LOG_ENABLE
	Log_Equipement(Equipement,"--------- RESTORATION  ---------");
	Log_Equipement(Equipement,Equipement->Adresse_Ip_Char);
	Log_Equipement(Equipement,Equipement->Adresse_MAC_Char);
	Log_Equipement(Equipement,"--------------------------------");
#endif

	G_LIST_APPEND (GList_Equipement, Equipement);
	if (Recherche_Index_Sous_Reseau (&Equipement->ID_Equipement[MAC_ADDR_LEN]) == -1)
		Ajout_Sous_Reseau (&Equipement->ID_Equipement[MAC_ADDR_LEN], FALSE);
	if (Restoration_Equipement->Ip_Dynamic) {
		for (i = 6; i <= 9; i++)
			Equipement->ID_Equipement[i] = 0;
	}

	Creer_Liste_Port_Equipement (Equipement);
	Nb_Port_Restaurer = 0;
	while ((Restoration_Equipement->Port[Nb_Port_Restaurer].Num_Port != 0) && (Nb_Port_Restaurer < MAX_PORTS)) {
		Port_Equipement = Recupere_Pointeur_Port_Glist (Equipement, Restoration_Equipement->Port[Nb_Port_Restaurer].Num_Port);
		if (Port_Equipement != NULL){
			Port_Equipement->Etat_Port = OUVERT;
			Port_Equipement->Derniere_Modification_Etat_Port = Restoration_Equipement->Port[Nb_Port_Restaurer].Derniere_Modification_Etat_Port;
			Nb_Port_Restaurer++;
		}
	}
}
