From 8cc7dcf06c0d01db140f9215cc7e18b8a792839b Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 6 Apr 2017 20:08:45 +0200 Subject: [PATCH] Reference #7 + minmod --- central-manager/central-manager.c | 48 +++++++++++++----------- central-manager/install/install.sh | 10 +++++ central-manager/install/sgca | 3 ++ central-manager/install/sgca.service | 10 +++++ central-manager/install/uninstall.sh | 11 ++++++ central-manager/lib/network/tcp/server.c | 6 +-- central-manager/lib/network/udp/server.c | 4 +- central-manager/lib/plane/plane.h | 1 - 8 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 central-manager/install/install.sh create mode 100755 central-manager/install/sgca create mode 100644 central-manager/install/sgca.service create mode 100644 central-manager/install/uninstall.sh delete mode 120000 central-manager/lib/plane/plane.h diff --git a/central-manager/central-manager.c b/central-manager/central-manager.c index 9d66d47..9eb1a90 100644 --- a/central-manager/central-manager.c +++ b/central-manager/central-manager.c @@ -153,15 +153,15 @@ void* LISTEN_TCP(){ void* LISTEN_UDP(){ /* [0] Initialisation des variables ==========================================================*/ - int CLIENT_SOCKET; // contiendra la socket UDP à envoyer sur un THREAD - struct sockaddr_in serverInfo; // contiendra les infos serveur - struct sockaddr_in clientInfo; // contiendra les infos client - char client_ip[20]; // IP du client - socklen_t len; // taille de la socket - int read; // compteurs - char buffer[MAX_BUF_LEN]; // buffer requête - struct bind_header request; // requête parsée - int i, index; // compteurs + int CLIENT_SOCKET; // contiendra la socket UDP à envoyer sur un THREAD + struct sockaddr_in serverInfo; // contiendra les infos serveur + struct sockaddr_in clientInfo; // contiendra les infos client + char client_ip[INET_ADDRSTRLEN]; // IP (string) du client + socklen_t len; // taille de la socket + int read; // compteurs + char buffer[MAX_BUF_LEN]; // buffer requête + struct bind_header request; // requête parsée + int i, index; // compteurs // retour de @DROP_UDP_SERVER int SOCKET; @@ -187,28 +187,28 @@ void* LISTEN_UDP(){ ============================================================================*/ while( 1 ){ - /* 0. On initialise les SOCKET en attendant la connexion et le rang du "manager" inactif */ + /* 0. On initialise les SOCKET en attendant la connexion d'un client */ CLIENT_SOCKET = -1; index = -1; /* 1. On attends une connection UDP */ read = recvfrom(SOCKET, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len); - /* 2. Si erreur reception ou taille incorrecte */ + /* 2. Si erreur reception ou taille incorrecte -> retour à l'écoute */ if( read != sizeof(char)+sizeof(unsigned short) ){ if( DEBUGMOD&BUF ) printf("[main][UDP_LISTEN_THREAD] read('%s') = %d bytes (expected: %d)\n", buffer, read, (int) (sizeof(char)+sizeof(unsigned short)) ); continue; } /* 3. On récupère l'adresse IP du client */ - inet_ntop(AF_INET, &(clientInfo.sin_addr), client_ip, 20); + inet_ntop(AF_INET, &clientInfo.sin_addr, client_ip, INET_ADDRSTRLEN); if( DEBUGMOD&SCK ) printf("[main][UDP_LISTEN_THREAD] '%s' connecté\n", client_ip); - /* 4. On parse la requête*/ - memcpy(&request, buffer, sizeof(struct bind_header)); - - printf("[main][UDP_LISTEN_THREAD] received: {flag = %d; port = %d}\n", (int) request.flags, request.port); + /* 4. On parse la requête */ + memcpy(&request.flags, buffer, sizeof(char)); + memcpy(&request.port, buffer+sizeof(char), sizeof(unsigned short)); + printf("[main][UDP_LISTEN_THREAD] received: bind_header{flag = %d; port = %d}\n", (int) request.flags, request.port); /* 5 Si on veut un port de communicatin */ if( request.flags&BINDHEAD_PRT ){ @@ -242,20 +242,26 @@ void* LISTEN_UDP(){ request.flags -= BINDHEAD_PRT; // Si on a le port -> on le met dans la reponse - }else - request.port = serverInfo.sin_port; + }else{ + request.port = htons(serverInfo.sin_port); + if( DEBUGMOD&SCK ) printf("[main][UDP_LISTEN_THREAD] Comm. socket is on port %d\n", request.port); + } } - if( DEBUGMOD&SCK ) printf("[main][UDP_LISTEN_THREAD] { port: %d }\n", serverInfo.sin_port); /* 8. On envoie la réponse */ - memcpy(buffer, &request, sizeof(struct bind_header)); - if( send(SOCKET, buffer, strlen(buffer), 0) < 0 ){ + bzero(buffer, MAX_BUF_LEN); + memcpy(buffer, &request.flags, sizeof(char)); + memcpy(buffer+sizeof(char), &request.port, sizeof(unsigned short)); + + if( send(SOCKET, buffer, sizeof(char)+sizeof(unsigned short), 0) < 0 ){ printf("[main][UDP_LISTEN_THREAD] Impossible de répondre au client!\n"); continue; } + printf("[main][UDP_LISTEN_THREAD] sent: bind_header{flag = %d; port = %d}\n", (int) request.flags, request.port); + diff --git a/central-manager/install/install.sh b/central-manager/install/install.sh new file mode 100644 index 0000000..c3673f6 --- /dev/null +++ b/central-manager/install/install.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# [1] Create service target (boot) +sudo ln -s $(pwd)/sgca /usr/sbin/sgca; + +# [2] Create service unit +sudo ln -s $(pwd)/sgca.service /lib/systemd/system/sgca.service; + +# [3] Enable service (optional if no [Install]) +sudo systemctl enable sgca.service; diff --git a/central-manager/install/sgca b/central-manager/install/sgca new file mode 100755 index 0000000..da60884 --- /dev/null +++ b/central-manager/install/sgca @@ -0,0 +1,3 @@ +#!/bin/bash + +/home/xdrm-brackets/Desktop/git.xdrm.io/sysdis-project/central-manager/boot >> $1 2>&1; \ No newline at end of file diff --git a/central-manager/install/sgca.service b/central-manager/install/sgca.service new file mode 100644 index 0000000..c2a0579 --- /dev/null +++ b/central-manager/install/sgca.service @@ -0,0 +1,10 @@ +[Unit] +Description=Systeme de Gestion du Controle Aerien +Requires=network.target + + +[Service] +Type=simple +User=xdrm-brackets +Group=xdrm-brackets +ExecStart=/usr/sbin/sgca /var/log/sgca \ No newline at end of file diff --git a/central-manager/install/uninstall.sh b/central-manager/install/uninstall.sh new file mode 100644 index 0000000..c3a2850 --- /dev/null +++ b/central-manager/install/uninstall.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# [1] Remove service target (boot) +sudo rm /usr/sbin/sgca; + +# [2] Disable service +sudo systemctl disable sgca.service; + +# [3] Remove service unit +sudo rm /lib/systemd/system/sgca.service; + diff --git a/central-manager/lib/network/tcp/server.c b/central-manager/lib/network/tcp/server.c index 311f444..afcb2b3 100644 --- a/central-manager/lib/network/tcp/server.c +++ b/central-manager/lib/network/tcp/server.c @@ -23,7 +23,7 @@ int DROP_TCP_SERVER(const int pPort, int* pListenSock){ =======================================================*/ *pListenSock = socket(AF_INET, SOCK_STREAM, 0); - if( DEBUGMOD&SCK ) printf("\t\tTCP SOCKET = %d\n", *pListenSock); + if( DEBUGMOD&SCK ) printf(" * /lib/tcp/server/drop_tcp_server SOCKET = %d\n", *pListenSock); // si erreur if( *pListenSock < 0 ) return -1; @@ -44,7 +44,7 @@ int DROP_TCP_SERVER(const int pPort, int* pListenSock){ =======================================================*/ STATUS = bind(*pListenSock, (struct sockaddr*) &addr, sizeof(addr)); - if( DEBUGMOD&SCK ) printf("\t\tTCP BIND = %d\n", STATUS); + if( DEBUGMOD&SCK ) printf(" * /lib/tcp/server/drop_tcp_server BIND = %d\n", STATUS); // si erreur if( STATUS < 0 ) return -1; @@ -54,7 +54,7 @@ int DROP_TCP_SERVER(const int pPort, int* pListenSock){ =======================================================*/ STATUS = listen(*pListenSock, MAX_TCP_THR); - if( DEBUGMOD&SCK ) printf("\t\tTCP LISTEN = %d\n", STATUS); + if( DEBUGMOD&SCK ) printf(" * /lib/tcp/server/drop_tcp_server LISTEN = %d\n", STATUS); // si erreur if( STATUS < 0 ) return -1; diff --git a/central-manager/lib/network/udp/server.c b/central-manager/lib/network/udp/server.c index 0677e28..2c3d677 100644 --- a/central-manager/lib/network/udp/server.c +++ b/central-manager/lib/network/udp/server.c @@ -23,7 +23,7 @@ int DROP_UDP_SERVER(const int pPort, int* pListenSock){ =======================================================*/ *pListenSock = socket(AF_INET, SOCK_DGRAM, 0); - if( DEBUGMOD&SCK ) printf("\t\tUDP SOCKET = %d\n", *pListenSock); + if( DEBUGMOD&SCK ) printf(" * /lib/udp/server/drop_udp_server SOCKET = %d\n", *pListenSock); // si erreur if( *pListenSock < 0 ) return -1; @@ -44,7 +44,7 @@ int DROP_UDP_SERVER(const int pPort, int* pListenSock){ =======================================================*/ STATUS = bind(*pListenSock, (struct sockaddr*) &addr, sizeof(struct sockaddr_in)); - if( DEBUGMOD&SCK ) printf("\t\tUDP BIND = %d\n", STATUS); + if( DEBUGMOD&SCK ) printf(" * /lib/udp/server/drop_udp_server BIND = %d\n", STATUS); // si erreur if( STATUS < 0 ) return -1; diff --git a/central-manager/lib/plane/plane.h b/central-manager/lib/plane/plane.h deleted file mode 120000 index b90a92b..0000000 --- a/central-manager/lib/plane/plane.h +++ /dev/null @@ -1 +0,0 @@ -../../../plane/plane.h \ No newline at end of file