|
|
@ -8,9 +8,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
#define BUF_SIZE 500
|
|
|
|
#define BUF_SIZE 500
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *result, *rp;
|
|
|
|
struct addrinfo *result, *rp;
|
|
|
|
int sfd, s;
|
|
|
|
int sfd, s;
|
|
|
@ -24,14 +22,10 @@ main(int argc, char *argv[])
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//remplissage de la structure hints contenants la configuration réseau du serveur
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
|
|
hints.ai_family = AF_INET6; // utilisation du serveur sur IPv6 (AF_UNSPEC pour IPv4/6)
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; // socket en mode datagramme
|
|
|
|
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
|
|
|
|
|
|
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
|
|
|
|
|
|
hints.ai_canonname = NULL;
|
|
|
|
|
|
|
|
hints.ai_addr = NULL;
|
|
|
|
|
|
|
|
hints.ai_next = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s = getaddrinfo(NULL, argv[1], &hints, &result);
|
|
|
|
s = getaddrinfo(NULL, argv[1], &hints, &result);
|
|
|
|
if (s != 0) {
|
|
|
|
if (s != 0) {
|
|
|
@ -39,53 +33,52 @@ main(int argc, char *argv[])
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* getaddrinfo() returns a list of address structures.
|
|
|
|
/* getaddrinfo () retourne une liste de structures d'adresses.
|
|
|
|
Try each address until we successfully bind(2).
|
|
|
|
Essayez chaque adresse jusqu'à ce que nous ayons réussi à bind(2).
|
|
|
|
If socket() (or bind()) fails, we (close the socket
|
|
|
|
Si socket() (ou bind()) échoue, nous (fermons le socket et)
|
|
|
|
and) try the next address. */
|
|
|
|
essayons l'adresse suivante */
|
|
|
|
|
|
|
|
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
|
|
|
sfd = socket(rp->ai_family, rp->ai_socktype,
|
|
|
|
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
rp->ai_protocol);
|
|
|
|
if (sfd == -1){
|
|
|
|
if (sfd == -1)
|
|
|
|
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
|
|
|
|
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0){
|
|
|
|
break; /* Success */
|
|
|
|
break; /* Succès */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
close(sfd);
|
|
|
|
close(sfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (rp == NULL) { /* No address succeeded */
|
|
|
|
if (rp == NULL) { /* Aucune adresse n'a pu marcher */
|
|
|
|
fprintf(stderr, "Could not bind\n");
|
|
|
|
fprintf(stderr, "Could not bind\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
freeaddrinfo(result); /* libère le buffer */
|
|
|
|
|
|
|
|
|
|
|
|
/* Read datagrams and echo them back to sender */
|
|
|
|
/* Lit les datagrammes et les renvois au client */
|
|
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
peer_addr_len = sizeof(struct sockaddr_storage);
|
|
|
|
peer_addr_len = sizeof(struct sockaddr_storage);
|
|
|
|
nread = recvfrom(sfd, buf, BUF_SIZE, 0,
|
|
|
|
nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len);
|
|
|
|
(struct sockaddr *) &peer_addr, &peer_addr_len);
|
|
|
|
if (nread == -1){
|
|
|
|
if (nread == -1)
|
|
|
|
continue; /* ignore la mauvaise requete */
|
|
|
|
continue; /* Ignore failed request */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char host[NI_MAXHOST], service[NI_MAXSERV];
|
|
|
|
char host[NI_MAXHOST], service[NI_MAXSERV];
|
|
|
|
|
|
|
|
|
|
|
|
s = getnameinfo((struct sockaddr *) &peer_addr,
|
|
|
|
s = getnameinfo((struct sockaddr *) &peer_addr, peer_addr_len, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV);
|
|
|
|
peer_addr_len, host, NI_MAXHOST,
|
|
|
|
if (s == 0){
|
|
|
|
service, NI_MAXSERV, NI_NUMERICSERV);
|
|
|
|
printf("Recu %ld octets de %s:%s\n", (long) nread, host, service);
|
|
|
|
if (s == 0)
|
|
|
|
}
|
|
|
|
printf("Received %ld bytes from %s:%s\n",
|
|
|
|
else{
|
|
|
|
(long) nread, host, service);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
|
|
|
|
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (sendto(sfd, buf, nread, 0,
|
|
|
|
if (sendto(sfd, buf, nread, 0, (struct sockaddr *) &peer_addr, peer_addr_len) != nread){
|
|
|
|
(struct sockaddr *) &peer_addr,
|
|
|
|
fprintf(stderr, "Erreur d'envoi de la reponse\n");
|
|
|
|
peer_addr_len) != nread)
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Error sending response\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|