From 4a2f7574f9e373e4c949a6149c7bbb36e83a0660 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Sat, 16 Feb 2008 09:58:12 +0000 Subject: [PATCH] Modifs dans librtaputil et locclient MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit M code/ap/ap.h M code/librtaputil/rtaputil.h M code/librtaputil/librtaputil.c : Suppression du bind() dans create_udp_sending_socket(). M code/client/locclient.c : Ajout de la gestion de l'interface d'envoi. En précisant par exemple "eth1" comme premier argument, on peut maintenant choisir l'interface source utilisée lors de l'envoi de la demande. git-svn-id: https://pif.pu-pm.univ-fcomte.fr/svn/loc@7 785a6c6c-259e-4ff1-8b91-dc31627914f0 --- loc-bts/code/ap/ap.h | 2 +- loc-bts/code/client/locclient.c | 135 ++++++++++++++++--------- loc-bts/code/librtaputil/librtaputil.c | 11 +- loc-bts/code/librtaputil/rtaputil.h | 9 +- 4 files changed, 96 insertions(+), 61 deletions(-) diff --git a/loc-bts/code/ap/ap.h b/loc-bts/code/ap/ap.h index 91e77c4..f80763e 100644 --- a/loc-bts/code/ap/ap.h +++ b/loc-bts/code/ap/ap.h @@ -12,7 +12,7 @@ #include #include #include -#include +//#include /* Codes d'erreurs */ diff --git a/loc-bts/code/client/locclient.c b/loc-bts/code/client/locclient.c index 5a1f149..08ae5f2 100644 --- a/loc-bts/code/client/locclient.c +++ b/loc-bts/code/client/locclient.c @@ -5,19 +5,29 @@ #include "../librtaputil/rtaputil.h" +//#define DEBUG /* Codes d'erreurs */ #define ERR_CREATING_SOCKET 1 #define ERR_BAD_NUMBER_OF_ARGS 2 #define ERR_SENDING_INFO 3 +/* Nombre d'arguments du programme */ +#define ARGC_NORMAL 2 +#define ARGC_CALIB 6 + +/* Nombre de paquets envoyés */ +#define NBPKT_CALIB 20 // Rafale de 20 paquets lors de la calibration + + /* Affiche le mode d'emploi du programme */ void print_usage(char *prog) { printf("Usage :\n\ -\t- Demande de localisation : %s ip_serveur\n\ -\t- Requête de calibration : %s ip_serveur direction x y z\n\ +\t- Demande de localisation : %s [iface] ip_serveur\n\ +\t- Requête de calibration : %s [iface] ip_serveur direction x y z\n\ +iface est une chaîne désignant le nom de interface réseau à partir de laquelle envoyer la demande (par exemple \"eth2\"). Si elle n'est pas spécifiée, le choix de l'interface source est automatique.\n\ ", prog, prog) ; } @@ -26,48 +36,26 @@ void print_usage(char *prog) int main(int argc, char *argv[]) { struct timeval request_time ; - char *buf = NULL ; - int buf_offset ; - ssize_t nsent ; // Retour de sendto + char *buf = NULL ; // Paquet à envoyer + int buf_offset ; // Indice dans le paquet à envoyer, utilisé lors de sa création + int buf_size ; // Taille du paquet envoyé + struct sockaddr_in server, client ; - int sockfd ; - int buf_size ; - int i ; + int sockfd ; // Descripteur de la socket d'envoi + ssize_t nsent ; // Retour de sendto + char iface[IFNAMSIZ + 1] = "" ; // Interface réseau depuis laquelle on envoit le paquet - gettimeofday(&request_time, NULL) ; - if(argc == 2) // Paquet normal + /* Test du nombre d'arguments */ + if (argc == ARGC_NORMAL + 1 || argc == ARGC_CALIB + 1) // Si on a spécifié un nom d'interface { - printf("Envoi normal effectué à : %lu\n", timeval_to_ms(request_time)) ; - buf_size = sizeof(char) + sizeof(struct timeval) ; - buf = malloc(buf_size) ; - buf[0] = PACKET_TYPE_NORMAL ; // Type de paquet = demande - memcpy(&buf[1], &request_time, sizeof(request_time)) ; + strncpy(iface, argv[1], IFNAMSIZ + 1) ; + argv++ ; + argc-- ; } - else if(argc == 6) // Paquet calibration - { - printf("Envoi Calibration effectué à : %lu\n", timeval_to_ms(request_time)) ; - - buf_offset = 0 ; - buf_size = sizeof(char) * 2 + sizeof(struct timeval) + sizeof(float) * 3 ; - buf = malloc(buf_size) ; - - buf[buf_offset++] = PACKET_TYPE_CALIBRATION ; // Type de paquet = calibration - memcpy(&buf[buf_offset], &request_time, sizeof(request_time)) ; - buf_offset += sizeof(request_time) ; - buf[buf_offset++] = atoi(argv[2]) ; // Direction - float posX = atof(argv[3]) ; - float posY = atof(argv[4]) ; - float posZ = atof(argv[5]) ; - memcpy(&buf[buf_offset], &posX, sizeof(float)) ; - buf_offset += sizeof(float) ; - memcpy(&buf[buf_offset], &posY, sizeof(float)) ; - buf_offset += sizeof(float) ; - memcpy(&buf[buf_offset], &posZ, sizeof(float)) ; - } - - else + /* Test du nombre d'arguments (suite) */ + if (argc != ARGC_NORMAL && argc != ARGC_CALIB) { print_usage(argv[0]) ; return ERR_BAD_NUMBER_OF_ARGS ; @@ -82,6 +70,54 @@ int main(int argc, char *argv[]) return ERR_CREATING_SOCKET ; } + if (iface[0] != '\0') // Si on a spécifié un nom d'interface + { + if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) + { + fprintf(stderr, "Erreur ! Impossible de sélectionner l'interface %s pour l'envoi du paquet : ", iface) ; + perror("") ; + fprintf(stderr, "Envoi sur l'interface par défaut.\n") ; + } + } + + + /* Création du paquet à envoyer */ + gettimeofday(&request_time, NULL) ; + + if(argc == ARGC_NORMAL) // Paquet normal + { + printf("Envoi normal effectué à : %lu\n", timeval_to_ms(request_time)) ; + buf_size = sizeof(char) + sizeof(struct timeval) ; + buf = malloc(buf_size) ; + buf[0] = PACKET_TYPE_NORMAL ; // Type de paquet = demande + memcpy(&buf[1], &request_time, sizeof(request_time)) ; + } + + else if(argc == ARGC_CALIB) // Paquet calibration + { + printf("Envoi Calibration effectué à : %lu\n", timeval_to_ms(request_time)) ; + + buf_offset = 0 ; + buf_size = sizeof(char) * 2 + sizeof(struct timeval) + sizeof(float) * 3 ; + buf = malloc(buf_size) ; + + buf[buf_offset++] = PACKET_TYPE_CALIBRATION ; // Type de paquet = calibration + memcpy(&buf[buf_offset], &request_time, sizeof(request_time)) ; + buf_offset += sizeof(request_time) ; + buf[buf_offset++] = atoi(argv[2]) ; // Direction + float posX = atof(argv[3]) ; + float posY = atof(argv[4]) ; + float posZ = atof(argv[5]) ; +#ifdef DEBUG + printf("direction = %d, posX = %f, posY = %f, posZ = %f\n", buf[buf_offset - 1], posX, posY, posZ) ; +#endif + memcpy(&buf[buf_offset], &posX, sizeof(float)) ; + buf_offset += sizeof(float) ; + memcpy(&buf[buf_offset], &posY, sizeof(float)) ; + buf_offset += sizeof(float) ; + memcpy(&buf[buf_offset], &posZ, sizeof(float)) ; + } + /* Envoi des infos au serveur d'aggrégation */ nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ; @@ -91,16 +127,19 @@ int main(int argc, char *argv[]) return ERR_SENDING_INFO ; } - if (argc == 6) - for (i = 0 ; i < 19 ; i++) - { - nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ; - if (nsent != (ssize_t) buf_size) - { - perror("Erreur lors de l'envoi des infos au serveur ") ; - return ERR_SENDING_INFO ; - } - } + if (argc == ARGC_CALIB) // Dans le cas d'un paquet de calibration, on envoit une rafale de NBPKT_CALIB paquets? + { + int i ; + for (i = 0 ; i < NBPKT_CALIB - 1 ; i++) + { + nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ; + if (nsent != (ssize_t) buf_size) + { + perror("Erreur lors de l'envoi des infos au serveur ") ; + return ERR_SENDING_INFO ; + } + } + } (void) close(sockfd) ; diff --git a/loc-bts/code/librtaputil/librtaputil.c b/loc-bts/code/librtaputil/librtaputil.c index b0924a1..2603446 100644 --- a/loc-bts/code/librtaputil/librtaputil.c +++ b/loc-bts/code/librtaputil/librtaputil.c @@ -136,16 +136,7 @@ int create_udp_sending_socket(char *server_address, int server_port, struct sock bzero((char *) client_description, sizeof(*client_description)) ; client_description->sin_family = AF_INET ; // Socket INET client_description->sin_addr.s_addr = htonl(INADDR_ANY) ; // Toutes les connexions - client_description->sin_port = htons(0) ; // N'importe quel port - - /* Réservation d'un port quelconque pour le client */ - ret = bind(sockfd, (struct sockaddr *) client_description, sizeof(*client_description)) ; - if (ret < 0) - { - perror("Impossible de créer la socket (bind) ") ; - (void) close (sockfd) ; - return ret ; - } + // client_description->sin_port = htons(0) ; // N'importe quel port (ne sert à rien a priori) /* Remise à zéro et initialisation de la structure du serveur */ bzero((char *) server_description, sizeof(*server_description)) ; // RÀZ diff --git a/loc-bts/code/librtaputil/rtaputil.h b/loc-bts/code/librtaputil/rtaputil.h index 226094b..7679233 100644 --- a/loc-bts/code/librtaputil/rtaputil.h +++ b/loc-bts/code/librtaputil/rtaputil.h @@ -7,17 +7,22 @@ #include #include #include + #include #include + #include + #include + #include -#include +#include + #include #include #include #include -#include +#include #define AGGREGATE_DEFAULT_PORT 9900 // Port d'échange des données