Modifs dans librtaputil et locclient

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
This commit is contained in:
Matteo Cypriani 2008-02-16 09:58:12 +00:00
parent 2221ab58a0
commit 4a2f7574f9
4 changed files with 96 additions and 61 deletions

View File

@ -12,7 +12,7 @@
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <sys/ioctl.h>
#include <net/if.h>
//#include <net/if.h>
/* Codes d'erreurs */

View File

@ -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) ;

View File

@ -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

View File

@ -7,17 +7,22 @@
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <math.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <net/if.h>
#define AGGREGATE_DEFAULT_PORT 9900 // Port d'échange des données