sonde.c 1.01 KB
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include "libnet.h"


int sock_fd;

struct sigaction action;



void handler(int sig)
{
	if(sig==SIGINT)
	{
		printf("\nINTERRUPTION SOCKET : %d\n\n", sock_fd);
		close(sock_fd);
		exit(1);
	}
}


void argPortParsing(int argc, char* argv[], char* device)
{
	struct option port_arg={"dev", 1, NULL, 'd'};
	char opt;
	int longindex;

	while( (opt=getopt_long(argc, argv, "d:", &port_arg, &longindex)) !='d' && opt!=-1) {}

	if(opt=='d') 
	{
		strcpy(device, optarg);
		printf("%s\n", optarg);
	}
	else
	{
		strcpy(device, "eth0");
	}
}


int main(int argc, char* argv[])
{
	char device[100]; // On stockera le nom de l'interface
	argPortParsing(argc, argv, device); // On vérifie les arguments si il y a

	// On met en place la détection d'interruption	
	action.sa_handler=&handler; 
	sigaction(SIGINT, &action, NULL);	

	// Fonction d'écoute du réseau
	if(ecouteReseau(device)!=0){ return -1;}
	
	return 0;
}