|
|
@ -8,84 +8,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
#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 *result, *rp;
|
|
|
|
struct addrinfo hints;
|
|
|
|
int sfd, s;
|
|
|
|
struct addrinfo *result, *rp;
|
|
|
|
struct sockaddr_storage peer_addr;
|
|
|
|
int sfd, s;
|
|
|
|
socklen_t peer_addr_len;
|
|
|
|
struct sockaddr_storage peer_addr;
|
|
|
|
ssize_t nread;
|
|
|
|
socklen_t peer_addr_len;
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
if (argc != 2) {
|
|
|
|
|
|
|
|
fprintf(stderr, "Usage: %s port\n", argv[0]);
|
|
|
|
if (argc != 2) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
fprintf(stderr, "Usage: %s port\n", argv[0]);
|
|
|
|
}
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
|
|
|
|
//remplissage de la structure hints contenants la configuration réseau du serveur
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
|
|
|
|
hints.ai_family = AF_INET6; // utilisation du serveur sur IPv6 (AF_UNSPEC pour IPv4/6)
|
|
|
|
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; // socket en mode datagramme
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s = getaddrinfo(NULL, argv[1], &hints, &result);
|
|
|
|
|
|
|
|
if (s != 0) {
|
|
|
|
|
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
|
|
|
|
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
|
|
|
|
if (sfd == -1){
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0){
|
|
|
|
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
|
|
break; /* Succès */
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (s != 0) {
|
|
|
|
|
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* getaddrinfo() returns a list of address structures.
|
|
|
|
close(sfd);
|
|
|
|
Try each address until we successfully bind(2).
|
|
|
|
}
|
|
|
|
If socket() (or bind()) fails, we (close the socket
|
|
|
|
|
|
|
|
and) try the next address. */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
|
|
|
if (rp == NULL) { /* Aucune adresse n'a pu marcher */
|
|
|
|
sfd = socket(rp->ai_family, rp->ai_socktype,
|
|
|
|
fprintf(stderr, "Could not bind\n");
|
|
|
|
rp->ai_protocol);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
if (sfd == -1)
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
|
|
|
|
freeaddrinfo(result); /* libère le buffer */
|
|
|
|
break; /* Success */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
close(sfd);
|
|
|
|
/* Lit les datagrammes et les renvois au client */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (rp == NULL) { /* No address succeeded */
|
|
|
|
for (;;) {
|
|
|
|
fprintf(stderr, "Could not bind\n");
|
|
|
|
peer_addr_len = sizeof(struct sockaddr_storage);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len);
|
|
|
|
|
|
|
|
if (nread == -1){
|
|
|
|
|
|
|
|
continue; /* ignore la mauvaise requete */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
freeaddrinfo(result); /* No longer needed */
|
|
|
|
char host[NI_MAXHOST], service[NI_MAXSERV];
|
|
|
|
|
|
|
|
|
|
|
|
/* Read datagrams and echo them back to sender */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
continue; /* Ignore failed request */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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){
|
|
|
|
s = getnameinfo((struct sockaddr *) &peer_addr,
|
|
|
|
printf("Recu %ld octets de %s:%s\n", (long) nread, host, service);
|
|
|
|
peer_addr_len, host, NI_MAXHOST,
|
|
|
|
}
|
|
|
|
service, NI_MAXSERV, NI_NUMERICSERV);
|
|
|
|
else{
|
|
|
|
if (s == 0)
|
|
|
|
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
|
|
|
|
printf("Received %ld bytes from %s:%s\n",
|
|
|
|
}
|
|
|
|
(long) nread, host, service);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|