2019-10-28 17:31:10 +01:00
|
|
|
#include <sys/types.h>
|
2019-09-23 14:37:16 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2019-10-28 17:31:10 +01:00
|
|
|
#include <string.h>
|
2019-09-23 14:37:16 +02:00
|
|
|
#include <sys/socket.h>
|
2019-10-28 17:31:10 +01:00
|
|
|
#include <netdb.h>
|
2019-09-23 14:37:16 +02:00
|
|
|
|
2019-10-28 17:31:10 +01:00
|
|
|
#define BUF_SIZE 500
|
|
|
|
|
2019-10-31 19:49:39 +01:00
|
|
|
struct addrinfo *result; /* tableau des adresses réseaux des clients */
|
2019-10-29 12:42:43 +01:00
|
|
|
|
2019-10-29 15:08:21 +01:00
|
|
|
int cree_socket(struct addrinfo hints, char* port){
|
|
|
|
int s = getaddrinfo(NULL, port, &hints, &result);
|
2019-10-29 12:42:43 +01:00
|
|
|
if (s != 0) {
|
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
|
|
s = 1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:47:05 +01:00
|
|
|
void con_client(int *sfd){
|
2019-10-31 19:49:39 +01:00
|
|
|
struct addrinfo *rp; /* structure de l'adresse du client */
|
2019-10-29 13:47:05 +01:00
|
|
|
/* getaddrinfo () retourne une liste de structures d'adresses.
|
|
|
|
Essayez chaque adresse jusqu'à ce que nous ayons réussi à bind(2).
|
|
|
|
Si socket() (ou bind()) échoue, nous (fermons le socket et)
|
|
|
|
essayons l'adresse suivante */
|
|
|
|
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
2019-10-31 19:49:39 +01:00
|
|
|
/* socket crée un point de communication et renvoie un descripteur */
|
2019-10-29 13:47:05 +01:00
|
|
|
*sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if (*sfd == -1){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bind(*sfd, rp->ai_addr, rp->ai_addrlen) == 0){
|
2019-10-31 19:49:39 +01:00
|
|
|
break; /* Succès */
|
2019-10-29 13:47:05 +01:00
|
|
|
}
|
|
|
|
close(*sfd);
|
|
|
|
}
|
2019-10-31 19:49:39 +01:00
|
|
|
if (rp == NULL) { /* Aucune adresse n'a pu marcher */
|
|
|
|
fprintf(stderr, "Erreur de bind\n");
|
2019-10-29 13:47:05 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 14:23:30 +01:00
|
|
|
void traite_connexion(int sfd, int s){
|
|
|
|
ssize_t nread;
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
struct sockaddr_storage peer_addr;
|
|
|
|
socklen_t peer_addr_len;
|
2019-10-31 19:49:39 +01:00
|
|
|
/* Lit les datagrammes et les renvoi au client */
|
2019-10-29 14:23:30 +01:00
|
|
|
for (;;) {
|
|
|
|
peer_addr_len = sizeof(struct sockaddr_storage);
|
|
|
|
nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len);
|
|
|
|
if (nread == -1){
|
2019-10-31 19:49:39 +01:00
|
|
|
continue; /* ignore la mauvaise requete */
|
2019-10-29 14:23:30 +01:00
|
|
|
}
|
|
|
|
char host[NI_MAXHOST], service[NI_MAXSERV];
|
|
|
|
s = getnameinfo((struct sockaddr *) &peer_addr, peer_addr_len, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV);
|
|
|
|
if (s == 0){
|
|
|
|
printf("Recu %ld octets de %s:%s\n", (long) nread, host, service);
|
|
|
|
}
|
|
|
|
else{
|
2019-10-31 19:49:39 +01:00
|
|
|
fprintf(stderr, "erreur sur l'addresse client %s\n", gai_strerror(s));
|
2019-10-29 14:23:30 +01:00
|
|
|
}
|
|
|
|
if (sendto(sfd, buf, nread, 0, (struct sockaddr *) &peer_addr, peer_addr_len) != nread){
|
|
|
|
fprintf(stderr, "Erreur d'envoi de la reponse\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 09:05:24 +01:00
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
struct addrinfo hints;
|
2019-10-31 19:49:39 +01:00
|
|
|
int s; /* s code de retour de getaddrinfo */
|
|
|
|
int sfd; /* code de retour de l'ouverture du socket */
|
2019-10-29 09:05:24 +01:00
|
|
|
|
2019-10-31 19:49:39 +01:00
|
|
|
if (argc != 2) { /* test du nombre d'arguments entrés */
|
2019-10-29 09:05:24 +01:00
|
|
|
fprintf(stderr, "Usage: %s port\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-10-31 19:49:39 +01:00
|
|
|
char* port = argv[1]; /* récupération du port passée en paramètre du programme */
|
2019-10-29 09:05:24 +01:00
|
|
|
|
2019-10-31 19:49:39 +01:00
|
|
|
/* remplissage de la structure hints contenants la configuration réseau du serveur */
|
2019-10-29 14:23:30 +01:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2019-10-31 19:49:39 +01:00
|
|
|
hints.ai_family = AF_UNSPEC; /* utilisation du serveur sur IPv4/v6 (AF_INET/AF_INET6) */
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /* socket en mode datagramme */
|
2019-10-29 12:42:43 +01:00
|
|
|
|
2019-10-31 19:49:39 +01:00
|
|
|
s = cree_socket(hints, port); /* création du socket */
|
2019-10-29 12:42:43 +01:00
|
|
|
if(s == 1) {
|
2019-10-29 09:05:24 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:47:05 +01:00
|
|
|
con_client(&sfd);
|
2019-10-29 12:42:43 +01:00
|
|
|
|
2019-10-31 19:49:39 +01:00
|
|
|
freeaddrinfo(result); /* libère la structure d'adresse client */
|
2019-10-29 12:42:43 +01:00
|
|
|
|
2019-10-29 14:23:30 +01:00
|
|
|
traite_connexion(sfd, s);
|
|
|
|
|
2019-09-23 14:37:16 +02:00
|
|
|
}
|