From 61581e393dc0e8ba28251ec8db2f46b75f5f912f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 8 Apr 2017 18:37:14 +0200 Subject: [PATCH] [plane] fix *** stack smashing detected *** (because time_t for random) + removed serializer --- plane/Makefile | 7 +-- plane/lib/header.h | 4 +- plane/lib/network/format/serializer.c | 64 -------------------------- plane/lib/network/format/serializer.h | 65 --------------------------- plane/plane.c | 34 +++++++------- 5 files changed, 21 insertions(+), 153 deletions(-) delete mode 100644 plane/lib/network/format/serializer.c delete mode 100644 plane/lib/network/format/serializer.h diff --git a/plane/Makefile b/plane/Makefile index df9b1c1..e64a998 100644 --- a/plane/Makefile +++ b/plane/Makefile @@ -9,12 +9,9 @@ default: all lib/network/tcp/client.o: lib/header.h lib/network/tcp/client.h lib/network/tcp/client.c gcc $(CFLAGS) -c -o lib/network/tcp/client.o lib/network/tcp/client.c -lib/network/format/serializer.o: lib/network/format/serializer.h lib/network/format/serializer.c - gcc $(CFLAGS) -c -o lib/network/format/serializer.o lib/network/format/serializer.c - # Compiles the Plane -boot: lib/network/tcp/client.o lib/network/format/serializer.o plane.h plane.c - gcc $(CFLAGS) -o boot lib/network/tcp/client.o lib/network/format/serializer.o plane.c -lm +boot: lib/network/tcp/client.o plane.h plane.c + gcc $(CFLAGS) -o boot lib/network/tcp/client.o plane.c -lm # Run full compilation diff --git a/plane/lib/header.h b/plane/lib/header.h index 1f6a209..eb50d23 100644 --- a/plane/lib/header.h +++ b/plane/lib/header.h @@ -22,8 +22,8 @@ #include /* vars */ - #define SRV_HOST "localhost" - #define SRV_PORT 4444 + #define SRV_HOST "127.0.0.1" + #define SRV_PORT 0x504c #define MAX_BUF_LEN 512 diff --git a/plane/lib/network/format/serializer.c b/plane/lib/network/format/serializer.c deleted file mode 100644 index 2074c3d..0000000 --- a/plane/lib/network/format/serializer.c +++ /dev/null @@ -1,64 +0,0 @@ -#include "serializer.h" - - -int parse_plane_request(const char* pIn, struct plane_request* pOut){ - - /* 1. Check buffer length */ - if( strlen(pIn)*sizeof(char) != sizeof(struct plane_request) ) - return -1; - - /* 2. Parse buffer */ - memcpy(pOut, pIn, (size_t) sizeof(struct plane_request)); - return 0; - -} - - - - - - -int parse_viewterm_request(const char* pIn, struct vterm_request* pOut){ - - /* 1. Check buffer length */ - if( strlen(pIn)*sizeof(char) != sizeof(struct vterm_request) ) - return -1; - - /* 2. Parse buffer */ - memcpy(pOut, pIn, sizeof(struct vterm_request)); - return 0; - -} - - - - - - -int parse_ctrlterm_request(const char* pIn, struct cterm_request* pOut){ - - /* 1. Check buffer length */ - if( strlen(pIn)*sizeof(char) != sizeof(struct cterm_request) ) - return -1; - - /* 2. Parse buffer */ - memcpy(pOut, pIn, sizeof(struct cterm_request)); - return 0; - -} - - - - - -int serialize_term_response(const struct term_response* pIn, char* pOut, const size_t pSize){ - - /* 1. Check buffer length */ - if( sizeof(struct term_response) > pSize*sizeof(char) ) - return -1; - - /* 2. Serialize response into buffer */ - memcpy(pOut, pIn, sizeof(struct term_response)); - return 0; - -} diff --git a/plane/lib/network/format/serializer.h b/plane/lib/network/format/serializer.h deleted file mode 100644 index d11ed8d..0000000 --- a/plane/lib/network/format/serializer.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef _LIB_NETWORK_FORMAT_SERIALIZER_H_ - #define _LIB_NETWORK_FORMAT_SERIALIZER_H_ - - #include - #include "../../../plane.h" - - struct plane_request{ // Plane Request - struct coord co; - struct control ct; - }; - - struct vterm_request{ // ViewTerminal Request - char flags; - }; - - struct term_response{ // Terminal-s Response - char flags; - struct coord co; - struct control ct; - }; - - struct cterm_request{ // ControlTerminal Request - char flags; - int z; - struct control ct; - }; - - /* Parse a plane request ('char*' to 'struct plane_request') - * - * @history - * [1] Check if buffer have correct size (according to destination struct) - * [2] Parse buffer to struct - * - */ - int parse_plane_request(const char* pIn, struct plane_request* pOut); - - /* Parse a viewTerminal request ('char*' to 'struct vt_request') - * - * @history - * [1] Check if buffer have correct size (according to destination struct) - * [2] Parse buffer to struct - * - */ - int parse_viewterm_request(const char* pIn, struct vterm_request* pOut); - - /* Parse a ctrlTerminal request ('char*' to 'struct ct_request') - * - * @history - * [1] Check if buffer have correct size (according to destination struct) - * [2] Parse buffer to struct - * - */ - int parse_ctrlterm_request(const char* pIn, struct cterm_request* pOut); - - /* Serialize a Terminal response ('struct t_response' to 'char*') - * - * @history - * [1] Check if buffer have correct size (according to input struct) - * [2] Serialize struct into buffer - * - */ - int serialize_term_response(const struct term_response* pIn, char* pOut, const size_t pSize); - - -#endif \ No newline at end of file diff --git a/plane/plane.c b/plane/plane.c index f040921..8a210ca 100644 --- a/plane/plane.c +++ b/plane/plane.c @@ -51,11 +51,11 @@ int open_communication(){ return 1; } -void fermer_communication(){ +void fermer_communication(){ // fonction à implémenter qui permet de fermer la communication // avec le gestionnaire de vols } - + void envoyer_caracteristiques(){ // fonction à implémenter qui envoie l'ensemble des caractéristiques // courantes de l'plane au gestionnaire de vols @@ -68,19 +68,19 @@ void envoyer_caracteristiques(){ // initialise aléatoirement les paramètres initiaux de l'plane void init_plane(){ // initialisation aléatoire du compteur aléatoire - unsigned int seed; - time( (time_t*) &seed); + time_t seed; + time( &seed); srandom(seed); // intialisation des paramètres de l'plane crd.x = 1000 + random() % 1000; crd.y = 1000 + random() % 1000; crd.z = 900 + random() % 100; - + ctrl.cap = random() % 360; ctrl.speed = 600 + random() % 200; - - // initialisation du numero de l'plane : chaine de 5 caractères + + // initialisation du numero de l'plane : chaine de 5 caractères // formée de 2 lettres puis 3 chiffres numero_vol[0] = (random() % 26) + 'A'; numero_vol[1] = (random() % 26) + 'A'; @@ -100,7 +100,7 @@ void update_cap(int cap){ // modifie l'z de l'plane avec la valeur passée en paramètre void update_z(int alt){ - if (alt < 0) + if (alt < 0) crd.z = alt; } @@ -113,7 +113,7 @@ void display_data(){ // recalcule la localisation de l'plane en fonction de sa speed et de son cap void calc_ctrl(){ float ctrl_x, ctrl_y; - + if (ctrl.speed < VITMIN){ fermer_communication(); exit(2); @@ -122,7 +122,7 @@ void calc_ctrl(){ fermer_communication(); exit(3); } - + ctrl_x = cos(ctrl.cap * 2 * M_PI / 360) * ctrl.speed * 10 / VITMIN; ctrl_y = sin(ctrl.cap * 2 * M_PI / 360) * ctrl.speed * 10 / VITMIN; @@ -130,14 +130,14 @@ void calc_ctrl(){ // sauf si cap est un des angles droit if ((ctrl_x > 0) && (ctrl_x < 1)) ctrl_x = 1; if ((ctrl_x < 0) && (ctrl_x > -1)) ctrl_x = -1; - + if ((ctrl_y > 0) && (ctrl_y < 1)) ctrl_y = 1; if ((ctrl_y < 0) && (ctrl_y > -1)) ctrl_y = -1; - - + + crd.x = crd.x + (int)ctrl_x; crd.y = crd.y + (int)ctrl_y; - + display_data(); } @@ -154,9 +154,9 @@ void update(){ } -int main(){ +int main(){ // on initialise le plane - init_plane(); + init_plane(); display_data(); @@ -167,5 +167,5 @@ int main(){ } // on se déplace une fois toutes les initialisations faites - update(); + update(); } \ No newline at end of file