|
|
|
@ -10,12 +10,9 @@
|
|
|
|
|
|
|
|
|
|
struct addrinfo *result; // tableau des adresses réseaux des clients
|
|
|
|
|
|
|
|
|
|
// to do : con_client, traite_connexion
|
|
|
|
|
// to do : traite_connexion
|
|
|
|
|
|
|
|
|
|
int cree_socket(struct addrinfo hints, char* ip){
|
|
|
|
|
//remplissage de la structure hints contenants la configuration réseau du serveur
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
|
|
|
|
|
|
int s = getaddrinfo(NULL, ip, &hints, &result);
|
|
|
|
|
if (s != 0) {
|
|
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
|
|
|
@ -48,15 +45,38 @@ void con_client(int *sfd){
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void traite_connexion(int sfd, int s){
|
|
|
|
|
ssize_t nread;
|
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
|
struct sockaddr_storage peer_addr;
|
|
|
|
|
socklen_t peer_addr_len;
|
|
|
|
|
// Lit les datagrammes et les renvoi au client
|
|
|
|
|
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 la mauvaise requete */
|
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
|
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
|
|
|
|
|
}
|
|
|
|
|
if (sendto(sfd, buf, nread, 0, (struct sockaddr *) &peer_addr, peer_addr_len) != nread){
|
|
|
|
|
fprintf(stderr, "Erreur d'envoi de la reponse\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
int s; // s code de retour de getaddrinfo
|
|
|
|
|
int sfd; // code de retour de l'ouverture du socket
|
|
|
|
|
struct sockaddr_storage peer_addr;
|
|
|
|
|
socklen_t peer_addr_len;
|
|
|
|
|
ssize_t nread;
|
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
|
|
|
|
|
|
if (argc != 2) { // test du nombre d'arguments entrés
|
|
|
|
|
fprintf(stderr, "Usage: %s port\n", argv[0]);
|
|
|
|
@ -65,7 +85,8 @@ int main(int argc, char *argv[]){
|
|
|
|
|
|
|
|
|
|
char* ip = argv[1]; // récupération de l'ip passée en paramètre du programme
|
|
|
|
|
|
|
|
|
|
// remplissage de la structure config réseau serveur
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
@ -78,23 +99,7 @@ int main(int argc, char *argv[]){
|
|
|
|
|
|
|
|
|
|
freeaddrinfo(result); // libère la structure d'adresse client
|
|
|
|
|
|
|
|
|
|
// Lit les datagrammes et les renvoi au client
|
|
|
|
|
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 la mauvaise requete */
|
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
|
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
|
|
|
|
|
}
|
|
|
|
|
if (sendto(sfd, buf, nread, 0, (struct sockaddr *) &peer_addr, peer_addr_len) != nread){
|
|
|
|
|
fprintf(stderr, "Erreur d'envoi de la reponse\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
traite_connexion(sfd, s);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|