/* ***********************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.
 **************************************************************************************/
#ifndef         __LISTE_EQUIPEMENT
#define         __LISTE_EQUIPEMENT

#include <glib.h>
#include <time.h>

#ifndef         __WINDOWS_ENABLE
#include <netinet/in.h>
#else
#include <pcap.h>
#include <remote-ext.h>
#endif

#include "Reseau.h"
#include "Declaration_Constante.h"

extern struct struct_Nouveau_Equipement;
	
unsigned int  graine_rand_r;

#define NB_SCAN_OS 15
#define NUM_MAX_REGISTRATION 25
#define EQUIPEMENT_UNKNOWN -1
#define EQUIPEMENT_NO_IP -2
#define EQUIPEMENT_KNOWN -3

struct struct_Reponse {
	unsigned char TaillePacketReponse;
	
	unsigned char HeaderLengthIP;
	unsigned char DSF;
	unsigned short TailleTotal;
	unsigned char Id;
	unsigned short Flags_IP_Offset;
	unsigned char TTL;
	
	unsigned char NumSeq;
	unsigned char HeaderLengthTCP;
	unsigned char Flags_TCP;
	unsigned short WindowSize;
};

struct struct_Reponse_debug{
	 
	gint      Num_Port_Utiliser;  
	time_t64  Time_Debut;  
	time_t64  Time_Fin;  
	gint  Temps_Reponse; 
};


struct struct_Scan_Os {
	gint      Port_Scan;
	gint      Port_Client;
	gint      Id;
	gint      Seq;
	gboolean  Reponse_Scan;
};

struct Struct_Liste_Port_UDP {
	gint      Id_Port_UDP;
	gint 	  Num_Port_UDP;	
};

enum enum_Port_UDP {
	DHCPC = 0,
	SVRLOC,
	LISTENER,
	UPNP,
	SIP,
	ZEROCONF,
	NB_PORT_UDP 
};

struct struct_Smb_Registration {
	unsigned char Type;
	unsigned char Name[16 + 1];
	unsigned char Numero;
};

struct struct_Smb {
	unsigned char HostName[16 + 1];
	unsigned char DomainName[16 + 1];
	unsigned char Utilisateur[16 + 1];
	struct struct_Smb_Registration Smb_Registration[NUM_MAX_REGISTRATION];
};


#define CHAINE_NORMALE 100
#define CHAINE_MAX 255
#define CHAINE_MINI 30	

#define SURCHARGE_EQUIPEMENT 50000


struct Struct_Snmp {

	gchar     System_Name[CHAINE_NORMALE];
	gchar     Description[CHAINE_MAX];
	gchar     Contact[CHAINE_NORMALE];
	gchar     Location[CHAINE_NORMALE];
	gchar     Uptime[CHAINE_NORMALE];
	gint      Ram;

	gchar     Ip_Forwarding[CHAINE_MINI];
	gint      Default_TTL;
	gint      Reassembly_Timeout;
	gint      Need_To_Be_Reassembled;
	gint      Reassembled;
	gint      Reassembly_Failures;
	gint      Successfully_Fragmented;
	gint      Fragmentation_Failures;
	gint      Created_Fragments;
	gint      Discarded_Routing_Entries;

	gint      All;
	gint      Forwarded;
	gint      Delivered;
	gint      Header_Errors;
	gint      Address_Errors;
	gint      Unknown_Protocol;
	gint      Input_Buffer_Errors;

	gint      Requested;
	gint      Output_Buffer_Errors;
	gint      Not_Routed;

	gint	  Mem_Total_Swap;
	gint	  Mem_Available_Swap;
	gint	  Mem_Total_Real;
	gint	  Mem_Available_Real;
	gint	  Mem_Total_Free;
	gint	  Mem_Shared;
	gint	  Mem_Buffer;
	gint	  Mem_Cached;
	gint	  Mem_Swap_Error;
	gchar	  Mem_Swap_Error_Message[CHAINE_NORMALE];

	gint	  Cpu_Raw_User;
	gint	  Cpu_Raw_Nice;
	gint	  Cpu_Raw_System;
	gint	  Cpu_Raw_Idle;
	gint	  Cpu_Raw_Wait;
	gint	  Cpu_Raw_Kernel;
	gint	  Cpu_Raw_Interrupt;
};


#define NB_MAX_LIGNE_LISTE_SNMP 30
struct struct_Info_Liste_Snmp {
	gint      Colonne;
	gint      Ligne;
	gchar     Info[CHAINE_NORMALE];
};


#define TAILLE_REQUETE_SERVICE 2000	
#define TAILLE_BUFFER_NETBIOS 1000
struct Struct_Liste_Equipement {
	unsigned char ID_Equipement[MAC_ADDR_LEN + IP_ADDR_LEN + 1];
	unsigned char Adresse_Ip_Char[15 + 1];
	unsigned char Adresse_Ip[4 + 1];
	unsigned char Adresse_MAC_Char[17 + 1];
	unsigned char Adresse_MAC[6 + 1];
	gboolean  Agent_AutoScan;
	gboolean  Scan_Port_En_Cours;
	gboolean  Intrus;
	gboolean  Equipement_Restaurer;
	gboolean  Equipement_Rescanner_En_Cours;
	gboolean  Equipement_Allumer;
	gboolean  Rafraichissement_Etat_En_Cours;
	time_t64  Derniere_Modification_Etat_Equipement;
	gboolean  Notification_Equipement;
	gint      Compteur_Notification;
	gboolean  Alerte_Equipement_Active;
	GList    *GList_Port;
	gint	  Liste_Etat_Udp[NB_PORT_UDP]; 
	gboolean  Scanne_Port_En_Cours;

	gchar	 *Fichier_Log;  
	int       ID_Fichier_Log;  

	gboolean  Scan_Signature;
	struct struct_Reponse Reponse_Signature[NB_SCAN_OS];
	struct struct_Reponse_debug Reponse_Signature_Debug[NB_SCAN_OS];
	struct struct_Scan_Os Scan_OS[NB_SCAN_OS];
	struct struct_Smb Smb;
	gboolean  Snmp_Ouvert;
	struct Struct_Snmp Snmp;

	gboolean  GateWay;
	gboolean  Routage_Activer;
	gint      Port_Source_Test_GateWay;
	gint      Port_Destination_Test_GateWay;
	gboolean  Marquage_Change_Ip;
	
	time_t64  Time_Debut;  
	time_t64  Time_Fin;  
	gint  Temps_Scan_Complet; 

	time_t64  Time_Debut_Resolv_DNS;  
	time_t64  Time_Fin_Resolv_DNS;  
	gint  Temps_Resolv_DNS; 

	time_t64  Time_Debut_Scan_Port;  
	time_t64  Time_Fin_Scan_Port;  
	gint  Temps_Scan_Port; 

	time_t64  Time_Debut_Scan_Port_Udp;  
	time_t64  Time_Fin_Scan_Port_Udp;  
	gint  Temps_Scan_Port_Udp; 

	time_t64  Time_Debut_Scan_Signature;  
	time_t64  Time_Fin_Scan_Signature;  
	gint  Temps_Scan_Signature; 

	time_t64  Time_Debut_Scan_Service_Ftp;  
	time_t64  Time_Fin_Scan_Service_Ftp;  
	gint  Temps_Scan_Service_Ftp; 

	time_t64  Time_Debut_Scan_Service_Http;  
	time_t64  Time_Fin_Scan_Service_Http;  
	gint  Temps_Scan_Service_Http; 

	time_t64  Time_Debut_Scan_Service_Imap;  
	time_t64  Time_Fin_Scan_Service_Imap;  
	gint  Temps_Scan_Service_Imap; 

	time_t64  Time_Debut_Scan_Service_Smtp;  
	time_t64  Time_Fin_Scan_Service_Smtp;  
	gint  Temps_Scan_Service_Smtp; 

	time_t64  Time_Debut_Scan_Service_Ssh;  
	time_t64  Time_Fin_Scan_Service_Ssh;  
	gint  Temps_Scan_Service_Ssh; 

	time_t64  Time_Debut_Scan_Service_Telnet;  
	time_t64  Time_Fin_Scan_Service_Telnet;  
	gint  Temps_Scan_Service_Telnet; 

	time_t64  Time_Debut_Scan_Snmp;  
	time_t64  Time_Fin_Scan_Snmp;  
	gint  		 Temps_Scan_Snmp; 

	time_t64  Time_Debut_Samba;  
	time_t64  Time_Fin_Samba;  
	gint  		 Temps_Samba; 





	struct Struct_Port *Port_Equipement;

	struct struct_Nouveau_Equipement *M;
	struct struct_Retour_Rafraichir_Equipement *MR;
	struct struct_Retour_Rachaichir_Etat_Equipement *ME;
	gboolean  Sauve_Etat;	
	gint      Timeout;
	struct struct_Cle_Privee *vs_Donnee_Cle;
	gchar   *vs_banniere;
	gint    vs_i;
	gint 	vs_Nb_Port_Ajouter_Ouvert;
	gint 	vs_Nb_Port_Ajouter_Fermer;
	gint 	vs_Nb_Port_Ajouter_Firewalled;
	gint 	vs_Tentative_NetBios;
	gint 	vs_Tentative_NetBios_MAX;

	struct hostent hostbuf;
	struct hostent 	*host;
	size_t    Taille_Buffer;
	char     *Buffer;
	int       erreur;
	int		  Taille_Chaine;
	int       herr;
	unsigned long int  addr;

	int       IdSocket;
	int		  NbOctet;
	int		  TailleBanniere;
	int		  nbLigne;
	int 	  Tentative;
	char      Requete[TAILLE_REQUETE_SERVICE];
	char 	  *chaine;
	char	  *banniere;
	char 	  *Finale;
	char 	  *Finale2;
	char 	  *retour_envoie;
	gboolean  fin;
	char      DonneeBrut[TAILLE_REQUETE_SERVICE]; 
	int 	  Telnet_Recuperer;
	int		  i;
	int		  j;
	int       Nb_Objet;
	gboolean  Test_Retour_Port; 
#ifndef         __WINDOWS_ENABLE
	struct sockaddr sock;
#else
	pcap_t    * captureHandle;
	char errbuf[PCAP_ERRBUF_SIZE];
#endif

	gboolean  Resultat_Test_Port_Ouvert;	

	unsigned short Id_Ip;
	unsigned short Port;
	unsigned int MonNumSeq;
	struct struct_trame PacketScan;

	int		Tentative_UDP;	
	gboolean  Test_Retour_Scan_Equip; 
	struct struct_trame PacketOs;
	char     *trame;
	unsigned short Icmp_Id;
	unsigned short	Icmp_Seq;

	unsigned short CheckSum_Calc;
	int       ck_i;
	int       ck_somme;
	int       ck_Nombre;

	int       Socket_Netbios;
	int		  octet_recu;
	int 	  err;
	struct sockaddr_in SockName;
	unsigned char	 buf[TAILLE_BUFFER_NETBIOS];
	unsigned char*	FinChaine;
	int sockaddr_in_len;
	gboolean  Resultat_Netbios;

	gboolean  Resultat_waiting_donnee;
#ifndef         __WINDOWS_ENABLE
#else
	fd_set ensemble;
	struct timeval tv;
	int retval,NbSeconde;
#endif
};

GList    *GList_Equipement; 
GHashTable *Hash_Mac_Equipement; 


void      Debug_ID_Equipement (unsigned char *ID_Equipement);
void      Efface_Reponse_Signature (struct Struct_Liste_Equipement *Equipement);
void      Reception_Donnee_Equipement_Connu (struct Struct_Liste_Equipement *Equipement);
void      Equipement_Arreter (struct Struct_Liste_Equipement *Equipement);
void 	  Retour_Pointeur_Equipement (struct Struct_Liste_Equipement **Equipement,gint Index_Equipement);
void Recherche_Index_Equipement (unsigned char *ID_Equipement, gboolean No_ip, int *Index);
struct Struct_Liste_Equipement *Ajout_Equipement (unsigned char *ID_Equipement);
void      Initialisation_Liste_Equipement (void);
struct Struct_Liste_Equipement *Recherche_MAC_Gateway (unsigned char *MAC);
gint Recherche_Equipement(unsigned char *ID_Equipement,struct Struct_Liste_Equipement **Equipement);
#endif
