Commit 5940bf6e60d8f8f9d2d0f1ef2c55324abf97d67e
1 parent
6c0230af
Quelques modifs
Showing
15 changed files
with
92 additions
and
101 deletions
Show diff stats
Makefile
Network/Makefile
@@ -26,7 +26,7 @@ tcpserver.o: tcpserver.c | @@ -26,7 +26,7 @@ tcpserver.o: tcpserver.c | ||
26 | $(CC) -o $@ -c $^ -I$(IDIR) $(CFLAGS) | 26 | $(CC) -o $@ -c $^ -I$(IDIR) $(CFLAGS) |
27 | 27 | ||
28 | sniffer.o: sniffer.c | 28 | sniffer.o: sniffer.c |
29 | - $(CC) -o $@ -c $^ $(CFLAGS) | 29 | + $(CC) -o $@ -c $^ -I$(IDIR) $(CFLAGS) |
30 | 30 | ||
31 | sender.o: sender.c | 31 | sender.o: sender.c |
32 | $(CC) -o $@ -c $^ $(CFLAGS) | 32 | $(CC) -o $@ -c $^ $(CFLAGS) |
Network/sender.c
@@ -21,7 +21,7 @@ int initializeSocketUDP(char* service) | @@ -21,7 +21,7 @@ int initializeSocketUDP(char* service) | ||
21 | //precisions.ai_flags = AI_PASSIVE; | 21 | //precisions.ai_flags = AI_PASSIVE; |
22 | 22 | ||
23 | /*"172.26.145.35"*/ | 23 | /*"172.26.145.35"*/ |
24 | - if(getaddrinfo(NULL, service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); } | 24 | + if(getaddrinfo("192.168.1.66", service, &precisions, &origine)<0) { fprintf(stderr, "Erreur getaddrinfo\n"); return(-1); } |
25 | 25 | ||
26 | int n=0; | 26 | int n=0; |
27 | 27 |
Network/sniffer.c
@@ -4,12 +4,15 @@ | @@ -4,12 +4,15 @@ | ||
4 | #include <string.h> | 4 | #include <string.h> |
5 | #include <netinet/in.h> | 5 | #include <netinet/in.h> |
6 | #include <arpa/inet.h> | 6 | #include <arpa/inet.h> |
7 | +#include <time.h> | ||
8 | +#include "libnet.h" | ||
9 | + | ||
7 | 10 | ||
8 | #define SZ_DEV_PCAP 10 | 11 | #define SZ_DEV_PCAP 10 |
9 | 12 | ||
10 | 13 | ||
11 | // Fonction lors de la detection d'un paquet | 14 | // Fonction lors de la detection d'un paquet |
12 | -void arp_detect(/*u_char *args, const struct pcap_pkthdr *header, const u_char *packet*/) | 15 | +void arp_detect() |
13 | { | 16 | { |
14 | static int num=0, loop=1; | 17 | static int num=0, loop=1; |
15 | num++; | 18 | num++; |
@@ -18,6 +21,25 @@ void arp_detect(/*u_char *args, const struct pcap_pkthdr *header, const u_char * | @@ -18,6 +21,25 @@ void arp_detect(/*u_char *args, const struct pcap_pkthdr *header, const u_char * | ||
18 | if(num==10) { loop++; num=0; printf("10 paquets ARP ont été reçus pour la boucle %d !\n", loop);} | 21 | if(num==10) { loop++; num=0; printf("10 paquets ARP ont été reçus pour la boucle %d !\n", loop);} |
19 | } | 22 | } |
20 | 23 | ||
24 | + | ||
25 | +// Issue du dépassement de seuil de paquets, gère en fonction du débit moyen de reception de paquets arp | ||
26 | +void reponseSonde(time_t last_time, time_t seuil_temps) | ||
27 | +{ | ||
28 | + time_t ecart = time(NULL) - last_time; | ||
29 | + char* msg1="00"; | ||
30 | + char* msg2="01"; | ||
31 | + | ||
32 | + if(ecart > seuil_temps) | ||
33 | + { | ||
34 | + sendUDPBroadcast(msg1,2020); | ||
35 | + } | ||
36 | + else | ||
37 | + { | ||
38 | + sendUDPBroadcast(msg2,2020); | ||
39 | + } | ||
40 | +} | ||
41 | + | ||
42 | +// La fonction va initialiser la sonde réseau | ||
21 | int initialize_sniffer(pcap_t** dev_handle, char dev[]) | 43 | int initialize_sniffer(pcap_t** dev_handle, char dev[]) |
22 | { | 44 | { |
23 | bpf_u_int32 mask; // Masque de l'adresse réseau de l'interface à sniffer | 45 | bpf_u_int32 mask; // Masque de l'adresse réseau de l'interface à sniffer |
@@ -30,15 +52,11 @@ int initialize_sniffer(pcap_t** dev_handle, char dev[]) | @@ -30,15 +52,11 @@ int initialize_sniffer(pcap_t** dev_handle, char dev[]) | ||
30 | int status; | 52 | int status; |
31 | 53 | ||
32 | 54 | ||
33 | - | ||
34 | - | ||
35 | // Permet d'obtenir l'adresse ip de l'interface demandée*/ | 55 | // Permet d'obtenir l'adresse ip de l'interface demandée*/ |
36 | /* pcap permet de lister les interfaces et obtenir les adresses ip de ces dernières | 56 | /* pcap permet de lister les interfaces et obtenir les adresses ip de ces dernières |
37 | * On utilise la fonction pcap_findalldevs | 57 | * On utilise la fonction pcap_findalldevs |
38 | */ | 58 | */ |
39 | 59 | ||
40 | - | ||
41 | - | ||
42 | pcap_if_t* interfaces; | 60 | pcap_if_t* interfaces; |
43 | 61 | ||
44 | if(pcap_findalldevs(&interfaces, buf_err)!=0) { printf("%s\n", buf_err); return(EXIT_FAILURE); } | 62 | if(pcap_findalldevs(&interfaces, buf_err)!=0) { printf("%s\n", buf_err); return(EXIT_FAILURE); } |
@@ -120,56 +138,27 @@ int initialize_sniffer(pcap_t** dev_handle, char dev[]) | @@ -120,56 +138,27 @@ int initialize_sniffer(pcap_t** dev_handle, char dev[]) | ||
120 | 138 | ||
121 | } | 139 | } |
122 | 140 | ||
123 | -int ecouteReseau(int argc, char* argv[]) | ||
124 | -{ | ||
125 | - int cnt=10; // Correspond au nombre de paquets arp | ||
126 | - | ||
127 | - char dev[SZ_DEV_PCAP]; // Nom de l'interface à sniffer | ||
128 | - pcap_t *dev_handle; // Handle vers le device (correspondant à l'interface à sniffer) | ||
129 | - | ||
130 | - | ||
131 | 141 | ||
132 | - // Vérifie si le device a été donné | ||
133 | - // ------------------------ | 142 | +int ecouteReseau(char* dev) |
143 | +{ | ||
144 | + int cnt=10; // Correspond au seuil de paquets arp à compter | ||
134 | 145 | ||
135 | - int dev_donne=0; | 146 | + time_t last_time_reponse; // Correspond au moment du démarrage du cycle de la sonde |
136 | 147 | ||
137 | - for(int i=1; i<argc-1; i++) | ||
138 | - { | ||
139 | - if(strcmp(argv[i], "-dev")==0 || strcmp(argv[i], "-d")==0) | ||
140 | - { | ||
141 | - strcpy(dev, argv[i+1]); | ||
142 | - dev_donne=1; | ||
143 | - } | ||
144 | - } | ||
145 | - | ||
146 | - if(!dev_donne) | ||
147 | - strcpy(dev, "eth0"); | 148 | + pcap_t *dev_handle; // Handle vers le device (correspondant à l'interface à sniffer) |
148 | 149 | ||
149 | printf("Device: %s\n", dev); | 150 | printf("Device: %s\n", dev); |
150 | - | ||
151 | - // ------------------------ | ||
152 | - | ||
153 | - | ||
154 | - | ||
155 | - | ||
156 | - // struct pcap_pkthdr packet_header; // Header du paquet comportant des informations sur la réception du paquet (temps de reception etc...) | ||
157 | - // const u_char *packet; // Contenant du paquet | ||
158 | 151 | ||
159 | - if(initialize_sniffer(&dev_handle, dev)!=0) return(-1); | 152 | + if(initialize_sniffer(&dev_handle, dev)!=0) return(-1); // Initialisation du sniffer |
160 | 153 | ||
161 | - pcap_loop(dev_handle, cnt, arp_detect, NULL); // Boucle jusque 10 paquets arp reçus | 154 | + while(1) |
155 | + { | ||
156 | + last_time_reponse = time(NULL); | ||
157 | + pcap_loop(dev_handle, cnt, arp_detect, NULL); // Boucle jusque [cnt] paquets arp reçus | ||
158 | + reponseSonde(last_time_reponse, 20); // Une fois que le seuil de temps est passé | ||
159 | + } | ||
162 | 160 | ||
163 | return 0; | 161 | return 0; |
164 | } | 162 | } |
165 | 163 | ||
166 | -/* | ||
167 | - | ||
168 | -int main(int argc, char *argv[]) | ||
169 | -{ | ||
170 | - ecouteReseau(argc, argv); | ||
171 | - | ||
172 | - return(0); | ||
173 | -} | ||
174 | 164 | ||
175 | -*/ |
Network/tcpserver.c
@@ -6,6 +6,9 @@ | @@ -6,6 +6,9 @@ | ||
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <netdb.h> | 7 | #include <netdb.h> |
8 | #include <arpa/inet.h> | 8 | #include <arpa/inet.h> |
9 | +#include <pthread.h> | ||
10 | + | ||
11 | + | ||
9 | #include "libthrd.h" | 12 | #include "libthrd.h" |
10 | 13 | ||
11 | 14 | ||
@@ -68,6 +71,9 @@ int boucleServeur(int socket, void(* fctConnex)(int)) | @@ -68,6 +71,9 @@ int boucleServeur(int socket, void(* fctConnex)(int)) | ||
68 | //On utilisera la structure Arg_Thread (définie dans libthrd.h) qui comprend la socket de dialogue et le tableau pointeurs vers une structure donnant les infos des interfaces. | 71 | //On utilisera la structure Arg_Thread (définie dans libthrd.h) qui comprend la socket de dialogue et le tableau pointeurs vers une structure donnant les infos des interfaces. |
69 | //En effet, cette structure d'informations d'interface permet d'être utilisée comme une base de données, comprenant l'adresse de l'interface et son statut | 72 | //En effet, cette structure d'informations d'interface permet d'être utilisée comme une base de données, comprenant l'adresse de l'interface et son statut |
70 | //Les threads partageant la mémoire, ces infos seront partagées. | 73 | //Les threads partageant la mémoire, ces infos seront partagées. |
74 | + //Afin de gérer les requêtes, il est nécessaire d'instaurer un mutex. | ||
75 | + //En effet, la ressource interfaces ne doit être accessible que par un seul thread à la fois | ||
76 | + | ||
71 | Arg_Thread arg_sock_interf; | 77 | Arg_Thread arg_sock_interf; |
72 | 78 | ||
73 | //On initialise les interfaces | 79 | //On initialise les interfaces |
@@ -75,6 +81,9 @@ int boucleServeur(int socket, void(* fctConnex)(int)) | @@ -75,6 +81,9 @@ int boucleServeur(int socket, void(* fctConnex)(int)) | ||
75 | arg_sock_interf.interfaces[i]=NULL; | 81 | arg_sock_interf.interfaces[i]=NULL; |
76 | 82 | ||
77 | 83 | ||
84 | + //On initialiser le mutex | ||
85 | + pthread_mutex_init(&(arg_sock_interf.requete_mutex), NULL); | ||
86 | + | ||
78 | printf("------- Début boucle serveur TCP -------\n\n"); | 87 | printf("------- Début boucle serveur TCP -------\n\n"); |
79 | 88 | ||
80 | while(1) | 89 | while(1) |
@@ -87,5 +96,7 @@ int boucleServeur(int socket, void(* fctConnex)(int)) | @@ -87,5 +96,7 @@ int boucleServeur(int socket, void(* fctConnex)(int)) | ||
87 | 96 | ||
88 | lanceThread((void*)fctConnex, (void*)&arg_sock_interf, sizeof(arg_sock_interf)); | 97 | lanceThread((void*)fctConnex, (void*)&arg_sock_interf, sizeof(arg_sock_interf)); |
89 | } | 98 | } |
99 | + | ||
100 | + pthread_mutex_destroy(&(arg_sock_interf.requete_mutex)); | ||
90 | } | 101 | } |
91 | 102 |
Threads/threadSocket.c
@@ -34,7 +34,7 @@ int httpReponse(FILE* sockdial_stream, struct interface_info* interfaces[20]) | @@ -34,7 +34,7 @@ int httpReponse(FILE* sockdial_stream, struct interface_info* interfaces[20]) | ||
34 | 34 | ||
35 | struct PageWeb* page; | 35 | struct PageWeb* page; |
36 | 36 | ||
37 | - sleep(0.5); // Temps d'attente avant de recevoir les informations | 37 | + sleep(2); // Temps d'attente avant de recevoir les informations |
38 | 38 | ||
39 | createPage(&page, interfaces); // Permet de creer la pageweb (http.c) | 39 | createPage(&page, interfaces); // Permet de creer la pageweb (http.c) |
40 | 40 | ||
@@ -59,7 +59,7 @@ int httpReponse(FILE* sockdial_stream, struct interface_info* interfaces[20]) | @@ -59,7 +59,7 @@ int httpReponse(FILE* sockdial_stream, struct interface_info* interfaces[20]) | ||
59 | 59 | ||
60 | int interfaceReponse(FILE* sockdial_stream, char packet[BUFF_SIZE], struct interface_info* interfaces[20], char adresse[20]) | 60 | int interfaceReponse(FILE* sockdial_stream, char packet[BUFF_SIZE], struct interface_info* interfaces[20], char adresse[20]) |
61 | { | 61 | { |
62 | - printf("Interface : %s - Adresse : %s\n", packet, adresse); | 62 | + printf("Interface : %x %x - Adresse : %s - Taille : %ld\n", packet[0], packet[1], adresse, strlen(packet)); |
63 | 63 | ||
64 | char buffer[BUFF_SIZE]; | 64 | char buffer[BUFF_SIZE]; |
65 | 65 | ||
@@ -79,7 +79,7 @@ int interfaceReponse(FILE* sockdial_stream, char packet[BUFF_SIZE], struct inter | @@ -79,7 +79,7 @@ int interfaceReponse(FILE* sockdial_stream, char packet[BUFF_SIZE], struct inter | ||
79 | interfaces[i]->status=-1; | 79 | interfaces[i]->status=-1; |
80 | 80 | ||
81 | 81 | ||
82 | - if(strlen(packet)>=2 && packet[0]=='A' && (packet[1]=='A' || packet[1]=='B') ) | 82 | + if(packet[0]==0x20 && (packet[1]==0x01 || packet[1]==0x00) ) |
83 | { | 83 | { |
84 | //L'interface a correctement répondu, on la rajoute à la liste des interfaces | 84 | //L'interface a correctement répondu, on la rajoute à la liste des interfaces |
85 | printf("L'interface a correctement répondu\n"); | 85 | printf("L'interface a correctement répondu\n"); |
@@ -96,15 +96,18 @@ int interfaceReponse(FILE* sockdial_stream, char packet[BUFF_SIZE], struct inter | @@ -96,15 +96,18 @@ int interfaceReponse(FILE* sockdial_stream, char packet[BUFF_SIZE], struct inter | ||
96 | interfaces[i]->status=0; | 96 | interfaces[i]->status=0; |
97 | 97 | ||
98 | } | 98 | } |
99 | - else // L'interface n a pas correctement répondu | 99 | + else |
100 | + { // L'interface n a pas correctement répondu | ||
101 | + printf("L'interface n'a pas correctement répondu\n"); | ||
100 | return -1; | 102 | return -1; |
103 | + } | ||
101 | 104 | ||
102 | - | ||
103 | - | 105 | + /* |
104 | strcpy(buffer, "\x60\x00"); // Demande la commande en mémoire | 106 | strcpy(buffer, "\x60\x00"); // Demande la commande en mémoire |
105 | 107 | ||
106 | fprintf(sockdial_stream, "%s", buffer); // Transmission du message | 108 | fprintf(sockdial_stream, "%s", buffer); // Transmission du message |
107 | 109 | ||
110 | + | ||
108 | fgets(packet, BUFF_SIZE, sockdial_stream); // Attente de reception du message | 111 | fgets(packet, BUFF_SIZE, sockdial_stream); // Attente de reception du message |
109 | 112 | ||
110 | 113 | ||
@@ -116,6 +119,7 @@ int interfaceReponse(FILE* sockdial_stream, char packet[BUFF_SIZE], struct inter | @@ -116,6 +119,7 @@ int interfaceReponse(FILE* sockdial_stream, char packet[BUFF_SIZE], struct inter | ||
116 | interfaces[i]->commande = -1; | 119 | interfaces[i]->commande = -1; |
117 | return -1; // Si le message n'est pas reçu correctement | 120 | return -1; // Si le message n'est pas reçu correctement |
118 | } | 121 | } |
122 | + */ | ||
119 | 123 | ||
120 | return 0; | 124 | return 0; |
121 | } | 125 | } |
@@ -134,10 +138,14 @@ void* reponseConnexion(void* arg_sock_interf) | @@ -134,10 +138,14 @@ void* reponseConnexion(void* arg_sock_interf) | ||
134 | char buff_rec[BUFF_SIZE]; // Le buffer de message à la reception | 138 | char buff_rec[BUFF_SIZE]; // Le buffer de message à la reception |
135 | // char buffer[BUFF_SIZE]; // Le buffer de message pour l'emission | 139 | // char buffer[BUFF_SIZE]; // Le buffer de message pour l'emission |
136 | 140 | ||
141 | + printf("FGETS\n"); | ||
137 | fgets(buff_rec, BUFF_SIZE, sockdial_stream); | 142 | fgets(buff_rec, BUFF_SIZE, sockdial_stream); |
138 | 143 | ||
139 | printf("Le message reçu : %s\n", buff_rec); | 144 | printf("Le message reçu : %s\n", buff_rec); |
140 | 145 | ||
146 | + | ||
147 | + //pthread_mutex_lock(&(argument->requete_mutex)); | ||
148 | + | ||
141 | //Distingue la requête HTTP... | 149 | //Distingue la requête HTTP... |
142 | if(strstr(buff_rec, "GET")!=NULL && strstr(buff_rec, "HTTP")!=NULL) | 150 | if(strstr(buff_rec, "GET")!=NULL && strstr(buff_rec, "HTTP")!=NULL) |
143 | { | 151 | { |
@@ -156,6 +164,8 @@ void* reponseConnexion(void* arg_sock_interf) | @@ -156,6 +164,8 @@ void* reponseConnexion(void* arg_sock_interf) | ||
156 | } | 164 | } |
157 | } | 165 | } |
158 | 166 | ||
167 | + //pthread_mutex_unlock(&(argument->requete_mutex)); | ||
168 | + | ||
159 | printf("Sock : %d // Stream : %p \n", sock, sockdial_stream); | 169 | printf("Sock : %d // Stream : %p \n", sock, sockdial_stream); |
160 | 170 | ||
161 | printf("Fin de la connexion\n"); | 171 | printf("Fin de la connexion\n"); |
bin/serverTCP deleted
No preview for this file type
bin/sonde deleted
No preview for this file type
includes/libnet.h
@@ -11,9 +11,9 @@ int initialisationServeur(char* service); | @@ -11,9 +11,9 @@ int initialisationServeur(char* service); | ||
11 | int initializeSocketUDP(char* service); | 11 | int initializeSocketUDP(char* service); |
12 | int sendUDPBroadcast(char* message, int port); | 12 | int sendUDPBroadcast(char* message, int port); |
13 | int sendUDPUnicast(char* address, char* message, int port); | 13 | int sendUDPUnicast(char* address, char* message, int port); |
14 | -void arp_detect(u_char *args, const struct pcap_pkthdr *header, const u_char *packet); | 14 | +void arp_detect(); |
15 | int initialize_sniffer(pcap_t** dev_handle, char dev[]); | 15 | int initialize_sniffer(pcap_t** dev_handle, char dev[]); |
16 | -int ecouteReseau(int argc, char* argv[]); | 16 | +int ecouteReseau(char* device); |
17 | 17 | ||
18 | 18 | ||
19 | 19 |
includes/libthrd.h
1 | #ifndef __LIBTHRD_H__ | 1 | #ifndef __LIBTHRD_H__ |
2 | #define __LIBTHRD_H__ | 2 | #define __LIBTHRD_H__ |
3 | 3 | ||
4 | + | ||
4 | #include "http.h" | 5 | #include "http.h" |
6 | +#include <pthread.h> | ||
5 | 7 | ||
6 | -typedef struct{int socket; struct interface_info* interfaces[20];} Arg_Thread; | 8 | +typedef struct{int socket; struct interface_info* interfaces[20]; pthread_mutex_t requete_mutex;} Arg_Thread; |
7 | 9 | ||
8 | void* reponseConnexion(void* arg_sock_interf); | 10 | void* reponseConnexion(void* arg_sock_interf); |
9 | 11 |
libs/libnet.a deleted
No preview for this file type
libs/libthrd.a deleted
No preview for this file type
bin/Makefile renamed to src/Makefile
1 | 1 | ||
2 | CC=gcc | 2 | CC=gcc |
3 | CFLAGS=-W -Wall -Wextra -g | 3 | CFLAGS=-W -Wall -Wextra -g |
4 | -CLIBS=ar -rcs | ||
5 | -LIB=libnet.a libthrd.a | 4 | +CDIR=../bin/ |
5 | +LIB=libthrd.a libnet.a | ||
6 | LDIR=../libs/ | 6 | LDIR=../libs/ |
7 | -LIBS=$(LDIR)libnet.a $(LDIR)libthrd.a | 7 | +LIBS=$(LDIR)libthrd.a $(LDIR)libnet.a |
8 | IDIR=../includes/ | 8 | IDIR=../includes/ |
9 | LDFLAGS=-lpthread -lpcap | 9 | LDFLAGS=-lpthread -lpcap |
10 | 10 | ||
@@ -13,16 +13,12 @@ LDFLAGS=-lpthread -lpcap | @@ -13,16 +13,12 @@ LDFLAGS=-lpthread -lpcap | ||
13 | all: sonde serverTCP | 13 | all: sonde serverTCP |
14 | 14 | ||
15 | serverTCP: serverTCP.c | 15 | serverTCP: serverTCP.c |
16 | - cd ../Threads/ && make | ||
17 | - cd ../Network/ && make | ||
18 | - $(CC) -o $@ $< -I$(IDIR) -L$(LDIR) $(LDIR)libthrd.a $(LDIR)libnet.a $(CFLAGS) $(LDFLAGS) | 16 | + $(CC) -o $(CDIR)$@ $< -I$(IDIR) -L$(LDIR) $(LIBS) $(CFLAGS) $(LDFLAGS) |
19 | rm -f *.o | 17 | rm -f *.o |
20 | 18 | ||
21 | 19 | ||
22 | sonde: sonde.c | 20 | sonde: sonde.c |
23 | - cd ../Threads/ && make | ||
24 | - cd ../Network/ && make | ||
25 | - $(CC) -o $@ $< -I$(IDIR) $(LDIR)libnet.a $(LDIR)libthrd.a $(CFLAGS) $(LDFLAGS) | 21 | + $(CC) -o $(CDIR)$@ $< -I$(IDIR) $(LIBS) $(CFLAGS) $(LDFLAGS) |
26 | rm -f *.o | 22 | rm -f *.o |
27 | 23 | ||
28 | # | 24 | # |
@@ -30,11 +26,6 @@ sonde: sonde.c | @@ -30,11 +26,6 @@ sonde: sonde.c | ||
30 | # | 26 | # |
31 | 27 | ||
32 | clean: | 28 | clean: |
33 | - cd ../Threads/ && make clean | ||
34 | - cd ../Network/ && make clean | ||
35 | - rm -f *.o sonde serverTCP | 29 | + rm -f *.o $(CDIR)sonde $(CDIR)serverTCP |
36 | 30 | ||
37 | 31 | ||
38 | -# cd ../Threads/ && make | ||
39 | -# cd ../Network/ && make | ||
40 | - |
bin/serverTCP.c renamed to src/serverTCP.c
bin/sonde.c renamed to src/sonde.c
@@ -5,12 +5,14 @@ | @@ -5,12 +5,14 @@ | ||
5 | #include <string.h> | 5 | #include <string.h> |
6 | #include <signal.h> | 6 | #include <signal.h> |
7 | #include "libnet.h" | 7 | #include "libnet.h" |
8 | -#include <time.h> | 8 | + |
9 | 9 | ||
10 | int sock_fd; | 10 | int sock_fd; |
11 | 11 | ||
12 | struct sigaction action; | 12 | struct sigaction action; |
13 | 13 | ||
14 | + | ||
15 | + | ||
14 | void handler(int sig) | 16 | void handler(int sig) |
15 | { | 17 | { |
16 | if(sig==SIGINT) | 18 | if(sig==SIGINT) |
@@ -22,52 +24,38 @@ void handler(int sig) | @@ -22,52 +24,38 @@ void handler(int sig) | ||
22 | } | 24 | } |
23 | 25 | ||
24 | 26 | ||
25 | -void argPortParsing(int argc, char* argv[], char* port) | 27 | +void argPortParsing(int argc, char* argv[], char* device) |
26 | { | 28 | { |
27 | - struct option port_arg={"port", 1, NULL, 'p'}; // | 29 | + struct option port_arg={"dev", 1, NULL, 'd'}; |
28 | char opt; | 30 | char opt; |
29 | int longindex; | 31 | int longindex; |
30 | 32 | ||
31 | - while( (opt=getopt_long(argc, argv, "p:", &port_arg, &longindex)) !='p' && opt!=-1) {} | 33 | + while( (opt=getopt_long(argc, argv, "d:", &port_arg, &longindex)) !='d' && opt!=-1) {} |
32 | 34 | ||
33 | - if(opt=='p') | 35 | + if(opt=='d') |
34 | { | 36 | { |
35 | - strcpy(port, optarg); | 37 | + strcpy(device, optarg); |
36 | printf("%s\n", optarg); | 38 | printf("%s\n", optarg); |
37 | } | 39 | } |
38 | else | 40 | else |
39 | { | 41 | { |
40 | - printf("La syntaxe doit être de la forme %s -p <port> ou %s --port <port>\n\n", argv[0], argv[0]); | 42 | + strcpy(device, "eth0"); |
41 | } | 43 | } |
42 | } | 44 | } |
43 | 45 | ||
44 | 46 | ||
45 | int main(int argc, char* argv[]) | 47 | int main(int argc, char* argv[]) |
46 | { | 48 | { |
47 | -/* | ||
48 | - char port[10]="80"; | ||
49 | - argPortParsing(argc, argv, port); | ||
50 | - | ||
51 | - action.sa_handler=&handler; | ||
52 | - sigaction(SIGINT, &action, NULL); | ||
53 | - | ||
54 | - if( (sock_fd=initialisationServeur(port)) ==-1 ) { fprintf(stderr, "Initialisation du serveur impossible\n"); return -1; } | 49 | + char device[100]; // On stockera le nom de l'interface |
50 | + argPortParsing(argc, argv, device); // On vérifie les arguments si il y a | ||
55 | 51 | ||
56 | - boucleServeur(sock_fd, (void*)&reponseConnexion); | 52 | + // On met en place la détection d'interruption |
53 | + action.sa_handler=&handler; | ||
54 | + sigaction(SIGINT, &action, NULL); | ||
57 | 55 | ||
56 | + // Fonction d'écoute du réseau | ||
57 | + if(ecouteReseau(device)!=0){ return -1;} | ||
58 | 58 | ||
59 | - sendUDPBroadcast("BroadWesh", 2020); | ||
60 | - sendUDPUnicast("192.168.0.37", "Coucou", 2020); | ||
61 | - */ | ||
62 | - | ||
63 | - char msg[2] = {0x40,0x01}; | ||
64 | - | ||
65 | - while(1) | ||
66 | - { | ||
67 | - if(ecouteReseau(argc, argv)!=0){ return -1;} | ||
68 | - if(sendUDPBroadcast(msg, 2020)){ return -1;} | ||
69 | - //sendUDPUnicast("172.26.145.205",msg, 2020); | ||
70 | - } | ||
71 | return 0; | 59 | return 0; |
72 | } | 60 | } |
73 | 61 |