/* ***********************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 <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <signal.h>

#ifdef         __WINDOWS_ENABLE
#include <windows.h>
#include "Win32.h"
#endif
#include "Declaration_Constante.h"
#include "Erreur.h"
#include "Quit.h"
#include "BackTrace.h"
#include "Debug.h"
#include "Semaphore.h"
#include "Com/Communication.h"
#include "Com/Commun.h"
#include "Com/Initialisation_Com.h"
#include "Scan_Port.h"
#include "Scan_Signature.h"
#include "Sous_Reseau_Autorise.h"
#include "Liste_Sous_Reseau.h"
#include "Liste_Equipement.h"
#include "Notification_Equipement.h"
#include "Notification_Port.h"
#include "Thread_Detect.h"
#include "Thread.h"
#include "Intrus.h"
#include "Info.h"
#include "Snmp.h"



void Instance_Agent(void){
	
	Demarrage_Complet_Agent = FALSE;
	GList_Liste_Thread = NULL;
	Id_Thread = 0;
	Nb_Thead = 0;
	client_id = -1;
	mainThreadId = pthread_self ();
	graine_rand_r= 0xabcdef;
	
	Nb_Socket_Ouverte = 0;
	client_id = -1; 
#ifndef         __WINDOWS_ENABLE
	Initialisation_Signal_Erreur ();
#endif
	
	Initialisation_Semaphore ();
	Initialisation_Debug ();
	Initialisation_Info ();
#ifdef        __SNMP_ENABLE
	Initialisation_Snmp();
#endif
	Initialisation_Liste_port ();
	Initialisation_Liste_Sous_Reseau_Autorise ();
	Initialisation_Liste_Sous_Reseau ();
	Initialisation_Liste_Equipement ();
	Initialisation_Notification_Equipement ();
	Initialisation_Notification_Port ();
#ifdef         __NESSUS_ENABLE
	Initialisation_Nessus_Variable();
#endif
	Initialise_Alerte_intrusion();
	Communication_Initialisation ();
	Envoie_Version_Agent ();
	while (1) {
#ifndef         __WINDOWS_ENABLE
		pause ();
#else
		sleep(100);
#endif
	}
	exit(EXIT_SUCCESS);
}



int main (int argc, char *argv[])
{
#ifdef __BACKTRACE_ENABLE
	binary_name = g_strdup(argv[0]);
#endif
	printf("Starting %s (http://autoscan-network.com)\n", VERSION_AGENT);
	
  	if (sizeof(int)    != 4) { printf("This platform's integers are not 32 bits.");exit(-1);}
  	if (sizeof(double) != 8) { printf("This platform's floats are not 32 bits.");exit(-1);}
  	if (sizeof(time_t) != 4) { printf("This platform's time_t is not 32 bits.");exit(-1);}
	  
	int pid,pid_Instance;
	
	pid=fork();
	if(pid<0)  {  
		printf("Fork : Error\n"); 
		exit(255); 
	}
	if (pid){		
		exit(EXIT_SUCCESS);
	}		
	
	while (1) {
      switch (fork()) {
       case -1:   
         printf("Fork : Error\n"); 
         exit(EXIT_FAILURE);
       case 0:   
         Instance_Agent();
         exit(EXIT_FAILURE);
       default:   
         wait(NULL);
	   	 sleep(3);
      }
      printf("Démarrage d'une nouvelle instance\n");
   } 
   exit(EXIT_SUCCESS);
#ifndef         __WINDOWS_ENABLE
#endif
}
