/* ***********************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 <errno.h>
#include "Scan_Arp.h"
#include "Debug.h"
#include "Com/Initialisation_Com.h"
#include "Info.h"
#include "Liste_Equipement.h"

char      Scan[] = { 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	

	
void Envoie_Arp (unsigned char *Ip_Debut, gint Nb_Essai, gint Nb_Equipement, gboolean Delay)
{
	ERROR_if (Ip_Debut == NULL);
	struct struct_trame Packet_Arp;
	int       Socket, i, j;
	struct sockaddr sock;
	unsigned char Ip_Cible[IP_ADDR_LEN];

#ifndef         __WINDOWS_ENABLE
	strcpy (sock.sa_data, Info_Demarrage.Interface);
	sock.sa_family = AF_INET;
	if ((Socket = socket_control (AF_INET, SOCK_PACKET, htons (ETH_P_ALL))) < 0)
		debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%s\nSocket ouvert[%d]  Thread[%d][%d]\n Nb equipement (%d)", strerror (errno), Nb_Socket_Ouverte, Info_Demarrage.Nb_Thread_Max, Info_Demarrage.Nb_Thread_Max - Valeur_Semaphore (&Semaphore_Thread_Scan), g_list_length (GList_Equipement));
#else
	pcap_t    * captureHandle;
	char errbuf[PCAP_ERRBUF_SIZE];
	
	if ( (captureHandle = pcap_open(Info_Demarrage.nom, 65535, PCAP_OPENFLAG_PROMISCUOUS, 1, NULL,  errbuf ) ) == NULL)
		debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Unable to open the adapter. %s is not supported by WinPcap\n", Info_Demarrage.nom);
#endif
	memcpy (&Ip_Cible, Ip_Debut, IP_ADDR_LEN);
	memcpy (&Packet_Arp.Source_MAC, Mon_Adresse_MAC, ETH_ALEN);
	for (j = 0; j < ETH_ALEN; j++)
		Packet_Arp.Dest_MAC[j] = 255;
	Packet_Arp.Proto = htons (TRAME_ARP);
	memcpy (&Packet_Arp.Datagramme, Scan, sizeof (Scan));
	memcpy (&Packet_Arp.Datagramme[8], Mon_Adresse_MAC, ETH_ALEN);
	memcpy (&Packet_Arp.Datagramme[14], &Mon_Adresse_Ip, IP_ADDR_LEN);
	for (j = 0; j < Nb_Essai; j++) {
		memcpy (&Packet_Arp.Datagramme[24], &Ip_Cible, IP_ADDR_LEN);
		for (i = 0; i < Nb_Equipement; i++) {
			if (Delay){
				if (Info_Demarrage.Normal_Mode)
#ifndef         __WINDOWS_ENABLE
					usleep (40000);
#else
					usleep (4000);
#endif
				else
					usleep (10000);	
			}
			Nouveau_Octet_Envoye (42);
#ifndef         __WINDOWS_ENABLE
			if ((sendto (Socket, &Packet_Arp, 42, 0, &sock, sizeof (sock))) < 0) {
				debug_printf (D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Socket send : ", strerror (errno));
				if (close_control (Socket) < 0)
					debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%s\nSocket ouvert[%d]  Thread[%d][%d]\n Nb equipement (%d)", strerror (errno), Nb_Socket_Ouverte, Info_Demarrage.Nb_Thread_Max, Info_Demarrage.Nb_Thread_Max - Valeur_Semaphore (&Semaphore_Thread_Scan), g_list_length (GList_Equipement));
			}
#else
 			if (pcap_sendpacket(captureHandle,(u_char *) &Packet_Arp, 42) != 0)
	 			debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Error sending the packet: \n", pcap_geterr(captureHandle));
#endif
			Packet_Arp.Datagramme[27]++;
		}
		if (Delay){
			if (Info_Demarrage.Normal_Mode)
#ifndef         __WINDOWS_ENABLE
				usleep (40000);
#else
				usleep (4000);
#endif
			else
				usleep (10000);	
		}
	}
	
#ifndef         __WINDOWS_ENABLE
	if (close_control (Socket) < 0)
		debug_printf (D_ERRO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%s\nSocket ouvert[%d]  Thread[%d][%d]\n Nb equipement (%d)", strerror (errno), Nb_Socket_Ouverte, Info_Demarrage.Nb_Thread_Max, Info_Demarrage.Nb_Thread_Max - Valeur_Semaphore (&Semaphore_Thread_Scan), g_list_length (GList_Equipement));
#else
	pcap_close(captureHandle);
#endif
}


void Envoie_Arp_Equipement (struct Struct_Liste_Equipement *Equipement, gint Nb_Essai)
{
	ERROR_if (Equipement == NULL);
	Envoie_Arp (Equipement->Adresse_Ip, Nb_Essai, 1, TRUE);
}
