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:
parent
2221ab58a0
commit
4a2f7574f9
|
@ -12,7 +12,7 @@
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <netinet/ip.h>
|
#include <netinet/ip.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <net/if.h>
|
//#include <net/if.h>
|
||||||
|
|
||||||
|
|
||||||
/* Codes d'erreurs */
|
/* Codes d'erreurs */
|
||||||
|
|
|
@ -5,19 +5,29 @@
|
||||||
|
|
||||||
#include "../librtaputil/rtaputil.h"
|
#include "../librtaputil/rtaputil.h"
|
||||||
|
|
||||||
|
//#define DEBUG
|
||||||
|
|
||||||
/* Codes d'erreurs */
|
/* Codes d'erreurs */
|
||||||
#define ERR_CREATING_SOCKET 1
|
#define ERR_CREATING_SOCKET 1
|
||||||
#define ERR_BAD_NUMBER_OF_ARGS 2
|
#define ERR_BAD_NUMBER_OF_ARGS 2
|
||||||
#define ERR_SENDING_INFO 3
|
#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 */
|
/* Affiche le mode d'emploi du programme */
|
||||||
void print_usage(char *prog)
|
void print_usage(char *prog)
|
||||||
{
|
{
|
||||||
printf("Usage :\n\
|
printf("Usage :\n\
|
||||||
\t- Demande de localisation : %s ip_serveur\n\
|
\t- Demande de localisation : %s [iface] ip_serveur\n\
|
||||||
\t- Requête de calibration : %s ip_serveur direction x y z\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) ;
|
", prog, prog) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,48 +36,26 @@ void print_usage(char *prog)
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct timeval request_time ;
|
struct timeval request_time ;
|
||||||
char *buf = NULL ;
|
char *buf = NULL ; // Paquet à envoyer
|
||||||
int buf_offset ;
|
int buf_offset ; // Indice dans le paquet à envoyer, utilisé lors de sa création
|
||||||
ssize_t nsent ; // Retour de sendto
|
int buf_size ; // Taille du paquet envoyé
|
||||||
|
|
||||||
struct sockaddr_in server, client ;
|
struct sockaddr_in server, client ;
|
||||||
int sockfd ;
|
int sockfd ; // Descripteur de la socket d'envoi
|
||||||
int buf_size ;
|
ssize_t nsent ; // Retour de sendto
|
||||||
int i ;
|
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)) ;
|
strncpy(iface, argv[1], IFNAMSIZ + 1) ;
|
||||||
buf_size = sizeof(char) + sizeof(struct timeval) ;
|
argv++ ;
|
||||||
buf = malloc(buf_size) ;
|
argc-- ;
|
||||||
buf[0] = PACKET_TYPE_NORMAL ; // Type de paquet = demande
|
|
||||||
memcpy(&buf[1], &request_time, sizeof(request_time)) ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(argc == 6) // Paquet calibration
|
/* Test du nombre d'arguments (suite) */
|
||||||
{
|
if (argc != ARGC_NORMAL && argc != ARGC_CALIB)
|
||||||
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
|
|
||||||
{
|
{
|
||||||
print_usage(argv[0]) ;
|
print_usage(argv[0]) ;
|
||||||
return ERR_BAD_NUMBER_OF_ARGS ;
|
return ERR_BAD_NUMBER_OF_ARGS ;
|
||||||
|
@ -82,6 +70,54 @@ int main(int argc, char *argv[])
|
||||||
return ERR_CREATING_SOCKET ;
|
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 */
|
/* Envoi des infos au serveur d'aggrégation */
|
||||||
nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ;
|
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 ;
|
return ERR_SENDING_INFO ;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc == 6)
|
if (argc == ARGC_CALIB) // Dans le cas d'un paquet de calibration, on envoit une rafale de NBPKT_CALIB paquets?
|
||||||
for (i = 0 ; i < 19 ; i++)
|
{
|
||||||
{
|
int i ;
|
||||||
nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ;
|
for (i = 0 ; i < NBPKT_CALIB - 1 ; i++)
|
||||||
if (nsent != (ssize_t) buf_size)
|
{
|
||||||
{
|
nsent = sendto(sockfd, (void *) buf, buf_size, 0, (struct sockaddr *) &server, (socklen_t) sizeof(server)) ;
|
||||||
perror("Erreur lors de l'envoi des infos au serveur ") ;
|
if (nsent != (ssize_t) buf_size)
|
||||||
return ERR_SENDING_INFO ;
|
{
|
||||||
}
|
perror("Erreur lors de l'envoi des infos au serveur ") ;
|
||||||
}
|
return ERR_SENDING_INFO ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
(void) close(sockfd) ;
|
(void) close(sockfd) ;
|
||||||
|
|
||||||
|
|
|
@ -136,16 +136,7 @@ int create_udp_sending_socket(char *server_address, int server_port, struct sock
|
||||||
bzero((char *) client_description, sizeof(*client_description)) ;
|
bzero((char *) client_description, sizeof(*client_description)) ;
|
||||||
client_description->sin_family = AF_INET ; // Socket INET
|
client_description->sin_family = AF_INET ; // Socket INET
|
||||||
client_description->sin_addr.s_addr = htonl(INADDR_ANY) ; // Toutes les connexions
|
client_description->sin_addr.s_addr = htonl(INADDR_ANY) ; // Toutes les connexions
|
||||||
client_description->sin_port = htons(0) ; // N'importe quel port
|
// client_description->sin_port = htons(0) ; // N'importe quel port (ne sert à rien a priori)
|
||||||
|
|
||||||
/* 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 ;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Remise à zéro et initialisation de la structure du serveur */
|
/* Remise à zéro et initialisation de la structure du serveur */
|
||||||
bzero((char *) server_description, sizeof(*server_description)) ; // RÀZ
|
bzero((char *) server_description, sizeof(*server_description)) ; // RÀZ
|
||||||
|
|
|
@ -7,17 +7,22 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/types.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <errno.h>
|
#include <net/if.h>
|
||||||
|
|
||||||
|
|
||||||
#define AGGREGATE_DEFAULT_PORT 9900 // Port d'échange des données
|
#define AGGREGATE_DEFAULT_PORT 9900 // Port d'échange des données
|
||||||
|
|
Loading…
Reference in New Issue