Commit 629c100c3e6a548989189c1b19bc15bf1cccbdb9
1 parent
b58103ff
feat: clients...
Showing
5 changed files
with
106 additions
and
3 deletions
Show diff stats
Makefile
... | ... | @@ -3,7 +3,7 @@ |
3 | 3 | # |
4 | 4 | |
5 | 5 | OBJS_SWITCH=libnet.o virtual_bridge.o server.o |
6 | -OBJS_CLIENT=libnet.o virtual_client.o | |
6 | +OBJS_CLIENT=libnet.o virtual_client.o client.o | |
7 | 7 | |
8 | 8 | CFLAGS += -Wall -DDEBUG |
9 | 9 | |
... | ... | @@ -28,5 +28,6 @@ virtual_client: $(OBJS_CLIENT) |
28 | 28 | |
29 | 29 | libnet.o: libnet.c libnet.h |
30 | 30 | virtual_bridge.o: virtual_bridge.c libnet.h server.h |
31 | -virtual_client.o: virtual_client.c libnet.h | |
31 | +virtual_client.o: virtual_client.c libnet.h client.h | |
32 | 32 | server.o: server.c |
33 | +client.o: client.c | ... | ... |
... | ... | @@ -0,0 +1,83 @@ |
1 | +#define _GNU_SOURCE | |
2 | +#include <sys/types.h> | |
3 | +#include <sys/socket.h> | |
4 | +#include <stdlib.h> | |
5 | +#include <stdio.h> | |
6 | +#include <string.h> | |
7 | +#include <netinet/in.h> | |
8 | +#include <sys/un.h> | |
9 | +#include <netdb.h> | |
10 | +#include <netinet/tcp.h> | |
11 | +#include <poll.h> | |
12 | +#include "client.h" | |
13 | +#include <unistd.h> | |
14 | +#define MAX_TAMPON 512 | |
15 | + | |
16 | + | |
17 | + | |
18 | + | |
19 | + | |
20 | +int connexionServeur(char *hote,char *service){ | |
21 | +struct addrinfo precisions,*resultat,*origine; | |
22 | +int statut; | |
23 | +int s; | |
24 | + | |
25 | +/* Creation de l'adresse de socket */ | |
26 | +memset(&precisions,0,sizeof precisions); | |
27 | +precisions.ai_family=AF_UNSPEC; | |
28 | +precisions.ai_socktype=SOCK_STREAM; | |
29 | +statut=getaddrinfo(hote,service,&precisions,&origine); | |
30 | +if(statut<0){ perror("connexionServeur.getaddrinfo"); exit(EXIT_FAILURE); } | |
31 | +struct addrinfo *p; | |
32 | +for(p=origine,resultat=origine;p!=NULL;p=p->ai_next) | |
33 | + if(p->ai_family==AF_INET6){ resultat=p; break; } | |
34 | + | |
35 | +/* Creation d'une socket */ | |
36 | +s=socket(resultat->ai_family,resultat->ai_socktype,resultat->ai_protocol); | |
37 | +if(s<0){ perror("connexionServeur.socket"); exit(EXIT_FAILURE); } | |
38 | + | |
39 | +/* Connection de la socket a l'hote */ | |
40 | +if(connect(s,resultat->ai_addr,resultat->ai_addrlen)<0) return -1; | |
41 | + | |
42 | +/* Liberation de la structure d'informations */ | |
43 | +freeaddrinfo(origine); | |
44 | + | |
45 | +return s; | |
46 | +} | |
47 | + | |
48 | + | |
49 | + | |
50 | + | |
51 | +int boucle(int s) | |
52 | +{ | |
53 | + | |
54 | + /* Boucle de communication avec le serveur */ | |
55 | + struct pollfd descripteurs[2]; | |
56 | + descripteurs[0].fd=s; | |
57 | + descripteurs[0].events=POLLIN; | |
58 | + descripteurs[1].fd=0; | |
59 | + descripteurs[1].events=POLLIN; | |
60 | + while(1){ | |
61 | + char tampon[MAX_TAMPON]; | |
62 | + int nb=poll(descripteurs,2,-1); | |
63 | + if(nb<0){ perror("main.poll"); exit(EXIT_FAILURE); } | |
64 | + if((descripteurs[0].revents&POLLIN)!=0){ | |
65 | + int taille=read(s,tampon,MAX_TAMPON); | |
66 | + if(taille<=0) break; | |
67 | + write(1,tampon,taille); | |
68 | + } | |
69 | + if((descripteurs[1].revents&POLLIN)!=0){ | |
70 | + int taille=read(0,tampon,MAX_TAMPON); | |
71 | + if(taille<=0) break; | |
72 | + write(s,tampon,taille); | |
73 | + } | |
74 | + } | |
75 | + | |
76 | + /* On termine la connexion */ | |
77 | + shutdown(s,SHUT_RDWR); | |
78 | + | |
79 | + return 0; | |
80 | + } | |
81 | + | |
82 | + | |
83 | + | ... | ... |
server.c
... | ... | @@ -145,6 +145,7 @@ int boucleServeur2(int ecoute) |
145 | 145 | |
146 | 146 | //if longueurPoll <1024 //TODO A GERER |
147 | 147 | descripteurs[LongueurPoll].fd = dialogue; |
148 | + descripteurs[LongueurPoll].revents=0; | |
148 | 149 | LongueurPoll ++; |
149 | 150 | printf("Nouveau LongueurPoll : %i\n", LongueurPoll); |
150 | 151 | ... | ... |
virtual_client.c
1 | + | |
1 | 2 | /**** Fichier principal pour le client du pont virtuel ****/ |
2 | 3 | |
3 | 4 | /** Fichiers d'inclusion **/ |
... | ... | @@ -7,9 +8,10 @@ |
7 | 8 | |
8 | 9 | #include <sys/types.h> |
9 | 10 | #include <sys/socket.h> |
10 | - | |
11 | +#include "client.h" | |
11 | 12 | #include "libnet.h" |
12 | 13 | |
14 | + | |
13 | 15 | /** Quelques constantes **/ |
14 | 16 | |
15 | 17 | /** Variables globales */ |
... | ... | @@ -30,10 +32,13 @@ fprintf(stdout,"Pont sur %s port %s\n",serveur,service); |
30 | 32 | #endif |
31 | 33 | |
32 | 34 | // Connexion au serveur |
35 | +int s=connexionServeur(serveur, service); | |
36 | +if(s<0){ fprintf(stderr,"Erreur de connexion au serveur\n"); exit(EXIT_FAILURE); } | |
33 | 37 | |
34 | 38 | // Ouverture de l'interface reseau |
35 | 39 | |
36 | 40 | // Communication avec le serveur |
41 | +boucle(s); | |
37 | 42 | |
38 | 43 | return 0; |
39 | 44 | } | ... | ... |