From a20cc18377fa7618c661667be22224ba7662c935 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 25 Apr 2017 11:54:15 +0200 Subject: [PATCH] Refactor flags bind_header --- central-manager/lib/data.h | 6 +-- central-manager/lib/local/middleware.c | 72 ++++++++++++++++---------- plane/plane.c | 9 +++- plane/plane.h | 6 +-- 4 files changed, 58 insertions(+), 35 deletions(-) diff --git a/central-manager/lib/data.h b/central-manager/lib/data.h index 9997619..f159b5d 100644 --- a/central-manager/lib/data.h +++ b/central-manager/lib/data.h @@ -41,9 +41,9 @@ #define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) ) - #define BINDHEAD_CTL 0x01 // is command terminal (else: view terminal) - #define BINDHEAD_SCK 0x02 // ask for com socket (terminal) - #define BINDHEAD_TCP 0x04 // ask TCP com socket (plane) + #define BINDHEAD_UDP 0x01 // is terminal (ask for UDP socket) + #define BINDHEAD_TCP 0x02 // is plane (ask for TCP socket) + #define BINDHEAD_CTL 0x04 // is ctrlTerm (else: viewTerm) struct bind_header{ char flags; diff --git a/central-manager/lib/local/middleware.c b/central-manager/lib/local/middleware.c index f969012..c789d8c 100644 --- a/central-manager/lib/local/middleware.c +++ b/central-manager/lib/local/middleware.c @@ -48,49 +48,65 @@ int multicastTerminal(struct middleware_arg* arg){ memcpy(&request.port, buffer+sizeof(char)*16, sizeof(unsigned short)); printf("{%s} received: bind_header{flag = %d; addr = '%s'; port = %d}\n", arg->entity, (int) request.flags, request.addr, request.port); + /* 2. Si mauvais flag (avion) */ + if( request.flags&BINDHEAD_TCP || !(request.flags&BINDHEAD_UDP) ){ + printf("{%s} Invalid flag\n", arg->entity); + return -1; + } + + /* 3. Si mauvais flag (mauvais type de terminal) */ + if( request.flags&BINDHEAD_CTL ){ + + // vterm avec flag cterm + if( strcmp(arg->entity, "udp_vterm") == 0 ){ + printf("{%s} Invalid flag\n", arg->entity); + return -1; + } + + // cterm avec flag vterm + }else if( strcmp(arg->entity, "udp_cterm") == 0 ){ + printf("{%s} Invalid flag\n", arg->entity); + return -1; + } + + + + /* (1) Création socket de communication (si demandé) ---------------------------------------------------------*/ - /* 1. Si on veut un port de communicatin */ - if( request.flags&BINDHEAD_SCK ){ + /* 1. On bind une socket sur un port random */ + if( DROP_UDP_SERVER(SERV_HOST, 0, arg->comSock, &comInfo, 0) < 0 ){ - /* 2. On bind une socket sur un port random */ - if( DROP_UDP_SERVER(SERV_HOST, 0, arg->comSock, &comInfo, 0) < 0 ){ - - if( DEBUGMOD&SCK ) printf("{%s} Erreur de création de la socket COM\n", arg->entity); - - /* 3. On ferme la socket et enleve le FLAG pour dire qu'on a pas pu ouvrir le port */ - close(*arg->comSock); - request.flags -= BINDHEAD_SCK; - } + if( DEBUGMOD&SCK ) printf("{%s} Erreur de création de la socket COM\n", arg->entity); + /* 2. On ferme la socket et enleve le FLAG pour dire qu'on a pas pu ouvrir le port */ + close(*arg->comSock); + request.flags -= BINDHEAD_UDP; } - /* 2.2 Si on veut on port de communication */ + /* (2) Récupération port random ---------------------------------------------------------*/ - if( request.flags&BINDHEAD_SCK ){ + /* 1. On récupère le port de la socket de communication */ + len = sizeof(struct sockaddr_in); + if( getsockname(*arg->comSock, (struct sockaddr*) &comInfo, &len) < 0 ){ - /* 1. On récupère le port de la socket de communication */ - len = sizeof(struct sockaddr_in); - if( getsockname(*arg->comSock, (struct sockaddr*) &comInfo, &len) < 0 ){ + if( DEBUGMOD&SCK ) printf("{%s} Erreur de recherche du port COM ouvert\n", arg->entity); - if( DEBUGMOD&SCK ) printf("{%s} Erreur de recherche du port COM ouvert\n", arg->entity); - - /* 2. On ferme la socket et enleve le FLAG pour dire qu'on a pas pu ouvrir le port */ - close(*arg->comSock); - request.flags -= BINDHEAD_SCK; - - /* 3. Si on a le port -> on le met dans la reponse */ - }else{ - strcpy(request.addr, SERV_HOST); - request.port = comInfo.sin_port; - if( DEBUGMOD&SCK ) printf("{%s}{udp_com} socket opened on %s:%d\n", arg->entity, request.addr, ntohs(request.port)); - } + /* 2. On ferme la socket et enleve le FLAG pour dire qu'on a pas pu ouvrir le port */ + close(*arg->comSock); + request.flags -= BINDHEAD_UDP; + /* 3. Si on a le port -> on le met dans la reponse */ + }else{ + strcpy(request.addr, SERV_HOST); + request.port = comInfo.sin_port; + if( DEBUGMOD&SCK ) printf("{%s}{udp_com} socket opened on %s:%d\n", arg->entity, request.addr, ntohs(request.port)); } + /* [2] Envoi de la réponse =========================================================*/ /* 1. On sérialise la réponse */ diff --git a/plane/plane.c b/plane/plane.c index 84fdd69..039ffef 100644 --- a/plane/plane.c +++ b/plane/plane.c @@ -64,12 +64,19 @@ int open_communication(){ /* 4. On récupère les données */ bzero(&request, sizeof(struct bind_header)); - memcpy(&request.addr, buffer, sizeof(char) ); + memcpy(&request.flags, buffer, sizeof(char) ); memcpy(&request.addr, buffer+sizeof(char), sizeof(char)*INET_ADDRSTRLEN ); memcpy(&request.port, buffer+sizeof(char)*(1+INET_ADDRSTRLEN), sizeof(unsigned short) ); // on indian-switch request.port = ntohs(request.port); + /* 5. On vérifie les flags */ + if( !(request.flags&BINDHEAD_TCP) || request.flags&BINDHEAD_UDP ){ + close(mcast_socket); + return 0; + } + + // printf("* hex("); for( int i = 0 ; i < BINDHDR_LEN/sizeof(char) ; i++ ) printf("\\x%02X", buffer[i]); printf(")\n"); printf("* received bind_header{flags = %d; addr = %s; port = %d;}\n\n", request.flags, request.addr, request.port); diff --git a/plane/plane.h b/plane/plane.h index f12be07..022d4d4 100644 --- a/plane/plane.h +++ b/plane/plane.h @@ -50,9 +50,9 @@ }; #define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) ) - #define BINDHEAD_CTL 0x01 // is command terminal (else: view terminal) - #define BINDHEAD_SCK 0x02 // ask for com socket - #define BINDHEAD_TCP 0x04 // ask TCP instead of UDP + #define BINDHEAD_UDP 0x01 // is terminal (ask for UDP socket) + #define BINDHEAD_TCP 0x02 // is plane (ask for TCP socket) + #define BINDHEAD_CTL 0x04 // is ctrlTerm (else: viewTerm) struct bind_header{ char flags;