From 3c241a2a95cb1515168daef3ced74541439bb62f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 26 Apr 2017 13:10:35 +0200 Subject: [PATCH] [fixed] indianness sgca (plane viewTerm) --- central-manager/lib/local/handler.c | 59 ++++++++++++++++++----------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/central-manager/lib/local/handler.c b/central-manager/lib/local/handler.c index a736ade..39e6122 100644 --- a/central-manager/lib/local/handler.c +++ b/central-manager/lib/local/handler.c @@ -81,7 +81,7 @@ void* managePlane(void* THREADABLE_ARGS){ } - /* 3. On parse la requête*/ + /* 3. On parse la requête (indianness: network order) */ memcpy(&data.code, buffer+sizeof(char)*0+sizeof(int)*0, sizeof(char)*6); memcpy(&data.x, buffer+sizeof(char)*6+sizeof(int)*0, sizeof(int)); memcpy(&data.y, buffer+sizeof(char)*6+sizeof(int)*1, sizeof(int)); @@ -89,6 +89,14 @@ void* managePlane(void* THREADABLE_ARGS){ memcpy(&data.spd, buffer+sizeof(char)*6+sizeof(int)*4, sizeof(int)); memcpy(&data.cap, buffer+sizeof(char)*6+sizeof(int)*3, sizeof(int)); + /* 4. Gestion de l'indianness */ + data.x = ntohl(data.x); + data.y = ntohl(data.y); + data.z = ntohl(data.z); + data.cap = ntohl(data.cap); + data.spd = ntohl(data.spd); + + /* (3) Gestion de la requête -> enregistrement ---------------------------------------------------------*/ @@ -119,7 +127,7 @@ void* managePlane(void* THREADABLE_ARGS){ /* 3. On copie les constantes */ memcpy(&arg->sgca->data[pindex], &data, sizeof(struct plane)); - printf("{tcp_com}(%d) stored (%d)'%s': {x = %d; y = %d; z = %d; cap = %d; spd = %d}\n", index, pindex, arg->sgca->data[pindex].code, arg->sgca->data[pindex].x, arg->sgca->data[pindex].y, arg->sgca->data[pindex].z, arg->sgca->data[pindex].cap, arg->sgca->data[pindex].spd); + if( DEBUGMOD&COM ) printf("{tcp_com}(%d) stored (%d)'%s': {x = %d; y = %d; z = %d; cap = %d; spd = %d}\n", index, pindex, arg->sgca->data[pindex].code, arg->sgca->data[pindex].x, arg->sgca->data[pindex].y, arg->sgca->data[pindex].z, arg->sgca->data[pindex].cap, arg->sgca->data[pindex].spd); @@ -128,11 +136,11 @@ void* managePlane(void* THREADABLE_ARGS){ ---------------------------------------------------------*/ /* 1. ACK data */ strcpy(data.code, arg->sgca->data[pindex].code); - data.x = arg->sgca->data[pindex].x; - data.y = arg->sgca->data[pindex].y; - data.z = arg->sgca->data[pindex].z; - data.cap = arg->sgca->data[pindex].cap; - data.spd = arg->sgca->data[pindex].spd; + data.x = htonl(arg->sgca->data[pindex].x); + data.y = htonl(arg->sgca->data[pindex].y); + data.z = htonl(arg->sgca->data[pindex].z); + data.cap = htonl(arg->sgca->data[pindex].cap); + data.spd = htonl(arg->sgca->data[pindex].spd); /* 2. Vérification du FLAG (modifications) si requêtes d'update */ //TODO: Gestion arg->sgca->update[pindex].flags|UPD_SPEED etc.. pour ajout du flag+elements dans reponse @@ -211,7 +219,7 @@ void* manageViewTerm(void* THREADABLE_ARGS){ struct sockaddr_in clientInfo; socklen_t len; char loop = 1; - int count, /*to_send, */sent; // compteurs d'envoi + int count, last, sent; // compteurs d'envoi int i, index = -1; // Compteurs globaux char buffer[MAX_BUF_LEN]; // Buffer d'envoi struct term_res request; // Requête @@ -245,41 +253,48 @@ void* manageViewTerm(void* THREADABLE_ARGS){ free(request.data); request.data = malloc( request.n * sizeof(struct plane) + 1 ); - /* 2. On récupère la liste des avions */ - for( i = 0 ; i < request.n ; i++ ) - memcpy(&request.data[i], &arg->sgca->data[i], sizeof(struct plane)); + /* 2. On récupère la liste des avions (network byte order) */ + for( i = 0 ; i < request.n ; i++ ){ + memcpy(&request.data[i].code, &arg->sgca->data[i].code, sizeof(char)*6); + request.data[i].x = htonl( arg->sgca->data[i].x ); + request.data[i].y = htonl( arg->sgca->data[i].y ); + request.data[i].z = htonl( arg->sgca->data[i].z ); + request.data[i].cap = htonl( arg->sgca->data[i].cap ); + request.data[i].spd = htonl( arg->sgca->data[i].spd ); + + } /* [3] Construction de la requête =========================================================*/ /* 1. Initialisation du buffer */ bzero(buffer, MAX_BUF_LEN*sizeof(char)); - count = 0; /* 2. Copie des données globales */ - memcpy(buffer+count, &request.flags, sizeof(char)); count += sizeof(char); - memcpy(buffer+count, &request.n, sizeof(char)); count += sizeof(char); + count = 0; last = sizeof(char); memcpy(buffer+count, &request.flags, last); + count += last; last = sizeof(char); memcpy(buffer+count, &request.n, last); /* 3. Copie des données des avions */ for( i = 0 ; i < request.n ; i++ ){ - memcpy(buffer+count, &request.data[i].code, sizeof(char)*6 ); count += sizeof(char)*6; - memcpy(buffer+count, &request.data[i].x, sizeof(int) ); count += sizeof(int); - memcpy(buffer+count, &request.data[i].y, sizeof(int) ); count += sizeof(int); - memcpy(buffer+count, &request.data[i].z, sizeof(int) ); count += sizeof(int); - memcpy(buffer+count, &request.data[i].cap, sizeof(int) ); count += sizeof(int); - memcpy(buffer+count, &request.data[i].spd, sizeof(int) ); count += sizeof(int); + count += last; last = sizeof(char)*6; memcpy(buffer+count, &request.data[i].code, last); + count += last; last = sizeof(int); memcpy(buffer+count, &request.data[i].x, last); + count += last; last = sizeof(int); memcpy(buffer+count, &request.data[i].y, last); + count += last; last = sizeof(int); memcpy(buffer+count, &request.data[i].z, last); + count += last; last = sizeof(int); memcpy(buffer+count, &request.data[i].cap, last); + count += last; last = sizeof(int); memcpy(buffer+count, &request.data[i].spd, last); } + count += last; + /* [4] Envoi de la requête =========================================================*/ /* 1. Gestion de l'envoi en plusieurs requêtes */ /* 2. Envoi */ - len = sizeof(struct sockaddr_in); - sent = sendto(arg->socket, buffer, count/sizeof(char), 0, (struct sockaddr*) &clientInfo, len); + sent = sendto(arg->socket, buffer, count/sizeof(char) + 1, 0, (struct sockaddr*) &clientInfo, len); /* 3. Gestion erreur */ if( sent <= 0 ){