/* ***********************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 "Liste_Sous_Reseau.h"
#include "Debug.h"
#include "Declaration_Constante.h"
#include "Thread.h"
#include "Scan_Arp.h"
#include "Macro.h"

void Scanner_Plage (struct Struct_Liste_Reseau *Reseau)
{
#ifdef __LOG_ENABLE
	Create_Log(CATEG_SCAN_PLAGE,NULL,g_strdup_printf("%d.%d.%d.%d",Reseau->Sous_Reseau[0],Reseau->Sous_Reseau[1],Reseau->Sous_Reseau[2],Reseau->Sous_Reseau[3]),TRUE);
#endif	
	ERROR_if (Reseau == NULL);
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Scanne plage : %d.%d.%d", Reseau->Sous_Reseau[0], Reseau->Sous_Reseau[1], Reseau->Sous_Reseau[2]);




	

	PROTECT_CODE(Semaphore_Scan_Arp_Sous_Reseau);
	Envoie_Arp (Reseau->Sous_Reseau, 2, 255,TRUE);
	DEPROTECT_CODE(Semaphore_Scan_Arp_Sous_Reseau);
	sleep(30*Nb_Sous_Reseau());
	PROTECT_CODE(Semaphore_Scan_Arp_Sous_Reseau);
	Envoie_Arp (Reseau->Sous_Reseau, 2, 255,TRUE);
	DEPROTECT_CODE(Semaphore_Scan_Arp_Sous_Reseau);
	
	Reseau->Deja_Scanner = TRUE;
	Stop_Thread (NULL);
}


gint Nb_Sous_Reseau (void)
{
	return g_list_length (GList_Sous_Reseau);
}

void Scanne_Liste_Sous_Reseau_Restaurer (gint Nb_Sous_Reseau_Restorer)
{
	struct Struct_Liste_Reseau *Reseau = NULL;
	gint      i;

	for (i = 0; i < Nb_Sous_Reseau_Restorer; i++) {
		Reseau = g_list_nth_data (GList_Sous_Reseau, i);
		demmarrage_Thread (*Scanner_Plage, (gpointer *) Reseau);
	}
}


gint Recherche_Index_Sous_Reseau (gchar * Sous_Reseau)
{
	struct Struct_Liste_Reseau *Reseau = NULL;
	gint      Nb_Objet_Liste, i;

	ERROR_if (Sous_Reseau == NULL);
	Nb_Objet_Liste = g_list_length (GList_Sous_Reseau);
	for (i = 0; i < Nb_Objet_Liste; i++) {
		Reseau = g_list_nth_data (GList_Sous_Reseau, i);
		if (memcmp (Reseau->Sous_Reseau, Sous_Reseau, 3) == 0) {
			return i;
		}
	}
	return -1;	
}

void Ajout_Sous_Reseau (gchar * Sous_Reseau, gboolean Scanne_Plage)
{
	struct Struct_Liste_Reseau *Reseau = NULL;

	ERROR_if (Sous_Reseau == NULL);
	MALLOC(Reseau,sizeof (struct Struct_Liste_Reseau));
	memcpy (Reseau->Sous_Reseau, Sous_Reseau, IP_ADDR_LEN - 1);
	Reseau->Deja_Scanner = FALSE;
	debug_printf (D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Nouveau Sous-Reseau : %d.%d.%d", Reseau->Sous_Reseau[0], Reseau->Sous_Reseau[1], Reseau->Sous_Reseau[2]);
	G_LIST_APPEND (GList_Sous_Reseau, Reseau);
	if (Scanne_Plage)
		demmarrage_Thread (*Scanner_Plage, (gpointer *) Reseau); 
}


void Initialisation_Liste_Sous_Reseau (void)
{
	GList_Sous_Reseau = NULL;
}
