From 79a1258a530b54f3bdf7b2da6ff58138fc18c22d Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 26 Apr 2017 14:25:05 +0200 Subject: [PATCH] [BIG UPDATE] Dynamic local ip --- central-manager/Makefile | 11 +++- central-manager/central-manager.c | 83 ++++++++++++++++-------- central-manager/central-manager.h | 2 + central-manager/lib/data.h | 4 +- central-manager/lib/debug.h | 2 +- central-manager/lib/header.h | 3 +- central-manager/lib/local/middleware.c | 26 +++++--- central-manager/lib/local/middleware.h | 2 +- central-manager/lib/network/common.c | 68 +++++++++++++++++++ central-manager/lib/network/common.h | 21 ++++++ central-manager/lib/network/udp/client.c | 4 +- central-manager/lib/network/udp/client.h | 4 +- central-manager/lib/network/udp/server.c | 11 ++-- central-manager/lib/network/udp/server.h | 4 +- plane/lib/network/tcp/client.c | 4 +- plane/lib/network/tcp/client.h | 4 +- plane/plane.c | 13 ++-- plane/plane.h | 4 +- 18 files changed, 204 insertions(+), 66 deletions(-) create mode 100644 central-manager/lib/network/common.c create mode 100644 central-manager/lib/network/common.h diff --git a/central-manager/Makefile b/central-manager/Makefile index 9381bcf..6e5aa9f 100644 --- a/central-manager/Makefile +++ b/central-manager/Makefile @@ -21,10 +21,13 @@ lib/local/middleware.o: lib/header.h lib/local/middleware.h lib/local/middleware lib/local/handler.o: lib/header.h lib/local/handler.h lib/local/handler.c gcc $(CFLAGS) -c -o lib/local/handler.o lib/local/handler.c +lib/network/common.o: lib/header.h lib/network/common.h lib/network/common.c + gcc $(CFLAGS) -c -o lib/network/common.o lib/network/common.c + # Compiles the SGCA -boot: lib/network/tcp/server.o lib/network/udp/server.o lib/network/udp/client.o lib/local/middleware.o lib/local/handler.o central-manager.h central-manager.c - gcc $(CFLAGS) -o boot lib/network/udp/server.o lib/network/udp/client.o lib/network/tcp/server.o lib/local/middleware.o lib/local/handler.o central-manager.c +boot: lib/network/common.o lib/network/tcp/server.o lib/network/udp/server.o lib/network/udp/client.o lib/local/middleware.o lib/local/handler.o central-manager.h central-manager.c + gcc $(CFLAGS) -o boot lib/network/udp/server.o lib/network/udp/client.o lib/network/common.o lib/network/tcp/server.o lib/local/middleware.o lib/local/handler.o central-manager.c # Run full compilation @@ -32,6 +35,8 @@ all: boot # cleans the compiled files clean: - rm boot; + rm lib/network/*.o; + rm lib/network/**/*.o; rm lib/network/**/*.o; rm lib/local/*.o; + rm boot; diff --git a/central-manager/central-manager.c b/central-manager/central-manager.c index 5d503e7..bca358b 100644 --- a/central-manager/central-manager.c +++ b/central-manager/central-manager.c @@ -17,7 +17,8 @@ * [0] Initialisation des variables * [1] Lancement des THREADS d'écoute * 1. On démarre le SERVEUR TCP d'écoute globale -* 2. On démarre le SERVEUR UDP d'écoute globale +* 2. On récupère l'IP locale +* 3. On démarre le SERVEUR UDP d'écoute globale * [2] On attends la fin de tous les THREADS * [3] On libère les variables globale * @@ -35,12 +36,22 @@ int main(int argc, char* argv[]){ /* 1. Variables globales */ sgca.n = 0; sgca.data = (struct plane*) malloc( sizeof(struct plane) ); + struct in_addr* SERV_HOST; - /* 2. Variables locales */ - struct listen_arg tcp_listn_arg = { SERV_HOST, TCP_LIST, NULL, &managePlane }; - struct listen_arg udp_mcast_arg = { MCST_HOST, UDP_MCST, NULL, NULL }; - struct listen_arg udp_vterm_arg = { MCST_VTER, UDP_VTER, &multicastTerminal, &manageViewTerm }; - struct listen_arg udp_cterm_arg = { MCST_CTER, UDP_CTER, &multicastTerminal, &manageCtrlTerm }; + /* 2. On récupère l'IP locale' */ + SERV_HOST = GET_LOCAL_IP(); + if( SERV_HOST == NULL ){ + printf("/!\\ Cannot fetch local ip address. Aborting!\n"); + free(sgca.data); + return EXIT_FAILURE; + } + printf("LOCAL IP: %s\n", inet_ntoa(*SERV_HOST)); + + /* 3. Variables locales */ + struct listen_arg tcp_listn_arg = { SERV_HOST->s_addr, TCP_LIST, NULL, &managePlane }; + struct listen_arg udp_mcast_arg = { inet_addr(MCST_HOST), UDP_MCST, NULL, NULL }; + struct listen_arg udp_vterm_arg = { inet_addr(MCST_VTER), UDP_VTER, &multicastTerminal, &manageViewTerm }; + struct listen_arg udp_cterm_arg = { inet_addr(MCST_CTER), UDP_CTER, &multicastTerminal, &manageCtrlTerm }; /* [1] Lancement des THREADS d'écoute @@ -71,6 +82,8 @@ int main(int argc, char* argv[]){ /* [3] On libère les variables globales ==========================================================*/ free(sgca.data); + + return EXIT_SUCCESS; } @@ -95,8 +108,10 @@ void* LISTEN_TCP(void* THREADABLE_ARGS){ struct sockaddr_in clientInfo; // contiendra les infos client socklen_t len; // taille de la socket int index, i; // compteurs - struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur + struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur + struct in_addr ip; // Pour afficher l'IP en dot notation + ip.s_addr = arg->addr; // retour de @DROP_TCP_SERVER int LISTENSOCK; // contiendra la socket d'écoute TCP @@ -117,7 +132,7 @@ void* LISTEN_TCP(void* THREADABLE_ARGS){ - printf("{tcp_listn} listen on %s:%d\n", arg->addr, arg->port); + printf("{tcp_listn} listen on %s:%d\n", inet_ntoa(ip), arg->port); @@ -206,28 +221,30 @@ void* LISTEN_UDP(void* THREADABLE_ARGS){ /* [0] Initialisation des variables ==========================================================*/ /* 1. On initialise les variables */ - int CLIENT_SOCKET; // contiendra la socket UDP à envoyer sur un THREAD - struct sockaddr_in listenInfo; // contiendra les infos de la socket LISTEN - int i, index; // compteurs - char entity[10]; // identifiant pour debug - int returned; // Contrôle d'exécution middleware - struct middleware_arg marg; // paramètres pour middleware - + int CLIENT_SOCKET; // contiendra la socket UDP à envoyer sur un THREAD + struct sockaddr_in listenInfo; // contiendra les infos de la socket LISTEN + int i, index; // compteurs + char entity[10]; // identifiant pour debug + int returned; // Contrôle d'exécution middleware + struct middleware_arg marg; // paramètres pour middleware + struct in_addr ip; // Permet d'afficher les IP en dot notation /* 2. On parse les arguments */ struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur - if( strcmp(arg->addr, MCST_VTER) == 0 && arg->port == UDP_VTER ) strcpy(entity, "udp_vterm"); - if( strcmp(arg->addr, MCST_CTER) == 0 && arg->port == UDP_CTER ) strcpy(entity, "udp_cterm"); - if( strcmp(arg->addr, MCST_HOST) == 0 && arg->port == UDP_MCST ) strcpy(entity, "udp_mcast"); + if( arg->port == UDP_VTER ) strcpy(entity, "udp_vterm"); + if( arg->port == UDP_CTER ) strcpy(entity, "udp_cterm"); + if( arg->port == UDP_MCST ) strcpy(entity, "udp_mcast"); // retour de @DROP_UDP_SERVER int SOCKET; + ip.s_addr = arg->addr; + /* [1] On démarre le SERVEUR UDP d'écoute globale ==========================================================*/ - if( DROP_UDP_SERVER(arg->addr, arg->port, &SOCKET, &listenInfo, strcmp(arg->addr, SERV_HOST) != 0) < 0 ){ + if( DROP_UDP_SERVER(arg->addr, arg->port, &SOCKET, &listenInfo, 1) < 0 ){ if( DEBUGMOD&SCK ) printf("{%s} Erreur de création socket d'écoute\n", entity); @@ -238,7 +255,7 @@ void* LISTEN_UDP(void* THREADABLE_ARGS){ return NULL; } - printf("{%s} listen on %s:%d\n", entity, arg->addr, arg->port); + printf("{%s} listen on %s:%d\n", entity, inet_ntoa(ip), arg->port); @@ -342,6 +359,7 @@ void* MCAST_PUBLISH(void* THREADABLE_ARGS){ struct sockaddr_in targetInfo; // contiendra les infos de la cible de la socket client char buffer[MAX_BUF_LEN]; // buffer de requêtes (envois) struct bind_header request; // requête brute + struct in_addr ip; // Permet d'afficher les IP en dot notation /* 2. On parse les arguments */ struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur @@ -349,6 +367,8 @@ void* MCAST_PUBLISH(void* THREADABLE_ARGS){ // retour de @UDP_SOCKET int SOCKET; + ip.s_addr = arg->addr; + /* [1] On initialise la socket CLIENT UDP ==========================================================*/ @@ -363,7 +383,7 @@ void* MCAST_PUBLISH(void* THREADABLE_ARGS){ return NULL; } - printf("{udp_mcast} bound to %s:%d\n", arg->addr, arg->port); + printf("{udp_mcast} bound to %s:%d\n", inet_ntoa(ip), arg->port); /* [2] On construit la requête à envoyer @@ -371,15 +391,24 @@ void* MCAST_PUBLISH(void* THREADABLE_ARGS){ /* 1. Initialisation du buffer */ bzero(buffer, MAX_BUF_LEN*sizeof(char)); - /* 2. Remplissage de la requête brute */ + /* 2. On récupère l'IP locale */ + struct in_addr* servip = GET_LOCAL_IP(); + if( servip == NULL ){ + printf("{udp_mcast} /!\\ Cannot fetch local ip address. Aborting!\n");// On ferme la SOCKET d'écoute globale + printf("{udp_mcast} FERMETURE SOCKET CLIENT UDP!\n"); + close(SOCKET); + return NULL; + } + + /* 3. Remplissage de la requête brute */ request.flags = BINDHEAD_TCP; - strcpy(request.addr, SERV_HOST); + request.addr = htonl(servip->s_addr); request.port = htons(TCP_LIST); - /* 3. Copie dans le buffer */ - memcpy(buffer, &request.flags, sizeof(char)); - memcpy(buffer+sizeof(char), &request.addr, sizeof(char)*INET_ADDRSTRLEN); - memcpy(buffer+sizeof(char)*(INET_ADDRSTRLEN+1), &request.port, sizeof(unsigned short)); + /* 4. Copie dans le buffer */ + memcpy(buffer, &request.flags, sizeof(char) ); + memcpy(buffer+sizeof(char), &request.addr, sizeof(in_addr_t) ); + memcpy(buffer+sizeof(char)+sizeof(in_addr_t), &request.port, sizeof(unsigned short) ); while( 1 ){ diff --git a/central-manager/central-manager.h b/central-manager/central-manager.h index c5a2f74..98b6ca9 100644 --- a/central-manager/central-manager.h +++ b/central-manager/central-manager.h @@ -8,10 +8,12 @@ * doowap31@gmail.com * **************************/ #include "lib/header.h" +#include /* local dependencies */ +#include "lib/network/common.h" #include "lib/network/tcp/server.h" #include "lib/network/udp/server.h" #include "lib/network/udp/client.h" diff --git a/central-manager/lib/data.h b/central-manager/lib/data.h index d5377b8..ea97258 100644 --- a/central-manager/lib/data.h +++ b/central-manager/lib/data.h @@ -40,14 +40,14 @@ }; - #define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) ) + #define BINDHDR_LEN ( sizeof(char)+sizeof(in_addr_t)+sizeof(unsigned short) ) #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; - char addr[INET_ADDRSTRLEN]; + in_addr_t addr; unsigned short port; }; diff --git a/central-manager/lib/debug.h b/central-manager/lib/debug.h index 254e2a4..21da8af 100644 --- a/central-manager/lib/debug.h +++ b/central-manager/lib/debug.h @@ -21,7 +21,7 @@ #define BUF 0x04 // FILTRE pour BUFFERS #define HDR 0x08 // FILTRE pour HEADERS #define THR 0x10 // FILTRE pour THREADS -#define COM 0x20 // debug des COMMUNICATIONS +#define COM 0x20 // FILTRE pour COMMUNICATIONS // possibilité de cumuler les DEBUGMODES #define DEBUGMOD ( SOCKETS | THREADS | BUFFERS ) // REVEALS + HEADER + THREADS \ No newline at end of file diff --git a/central-manager/lib/header.h b/central-manager/lib/header.h index a58b292..bce3ec0 100644 --- a/central-manager/lib/header.h +++ b/central-manager/lib/header.h @@ -36,7 +36,6 @@ #include "data.h" /* vars */ - #define SERV_HOST "127.0.0.1" // adresse serveur #define MCST_HOST "224.0.0.1" // adresse groupe multicast UDP -> plane #define MCST_VTER "224.0.0.2" // adresse groupe multicast UDP -> viewTerm #define MCST_CTER "224.0.0.3" // adresse groupe multicast UDP -> ctrlTerm @@ -75,7 +74,7 @@ }; struct listen_arg{ - const char addr[16]; // socket address + in_addr_t addr; // socket address const unsigned short port; // socket port int (*middleware)(struct middleware_arg*); // middleware function void* (*handler)(void*); // management function (thread) diff --git a/central-manager/lib/local/middleware.c b/central-manager/lib/local/middleware.c index 652489f..7d970b7 100644 --- a/central-manager/lib/local/middleware.c +++ b/central-manager/lib/local/middleware.c @@ -16,6 +16,7 @@ int multicastTerminal(struct middleware_arg* arg){ /* [0] Initialisation =========================================================*/ + /* 1. Initialisation des variables */ struct sockaddr_in comInfo; // contiendra les infos de la socket COM struct sockaddr_in clientInfo; // contiendra les infos client socklen_t len; // taille de la socket @@ -23,19 +24,24 @@ int multicastTerminal(struct middleware_arg* arg){ char buffer[MAX_BUF_LEN+1]; // buffer requête struct bind_header request; // requête parsée + /* 2. On récupère l'IP locale */ + struct in_addr* SERV_IP = GET_LOCAL_IP(); + if( SERV_IP == NULL ){ + printf("{%s} /!\\ Cannot fetch local ip address. Aborting!\n", arg->entity); + return -2; + } - - /* 1. On attends une connection UDP */ + /* 3. On attends une connection UDP */ len = sizeof(struct sockaddr_in); read = recvfrom(arg->listenSock, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len); - /* 2. Si erreur reception ou taille incorrecte -> retour à l'écoute */ + /* 4. Si erreur reception ou taille incorrecte -> retour à l'écoute */ if( read < BINDHDR_LEN ){ if( DEBUGMOD&BUF ) printf("{%s} read('%s') = %d bytes (expected: %d)\n", arg->entity, buffer, read, (int) (BINDHDR_LEN) ); return -1; } - /* 3. On récupère l'adresse IP du client */ + /* 5. On récupère l'adresse IP du client */ if( DEBUGMOD&SCK ) printf("{%s} %s:%d connecté\n", arg->entity, inet_ntoa(clientInfo.sin_addr), ntohs(clientInfo.sin_port)); @@ -46,7 +52,7 @@ int multicastTerminal(struct middleware_arg* arg){ memcpy(&request.flags, buffer, sizeof(char)); memcpy(&request.addr, buffer+sizeof(char), sizeof(char)*15); 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); + printf("{%s} received: bind_header{flag = %d; addr = %x; port = %d}\n", arg->entity, (int) request.flags, ntohl(request.addr), request. port); /* 2. Si mauvais flag (avion) */ if( request.flags&BINDHEAD_TCP || !(request.flags&BINDHEAD_UDP) ){ @@ -75,7 +81,7 @@ int multicastTerminal(struct middleware_arg* arg){ /* (1) Création socket de communication (si demandé) ---------------------------------------------------------*/ /* 1. On bind une socket sur un port random */ - if( DROP_UDP_SERVER(SERV_HOST, 0, arg->comSock, &comInfo, 0) < 0 ){ + if( DROP_UDP_SERVER(SERV_IP->s_addr, 0, arg->comSock, &comInfo, 0) < 0 ){ if( DEBUGMOD&SCK ) printf("{%s} Erreur de création de la socket COM\n", arg->entity); @@ -100,9 +106,9 @@ int multicastTerminal(struct middleware_arg* arg){ /* 3. Si on a le port -> on le met dans la reponse */ }else{ - strcpy(request.addr, SERV_HOST); + request.addr = SERV_IP->s_addr; request.port = comInfo.sin_port; - if( DEBUGMOD&SCK ) printf("{%s}{com} socket opened on %s:%d\n", arg->entity, request.addr, ntohs(request.port)); + if( DEBUGMOD&SCK ) printf("{%s}{com} socket opened on %s:%d\n", arg->entity, inet_ntoa(*SERV_IP), ntohs(request.port)); } @@ -112,7 +118,7 @@ int multicastTerminal(struct middleware_arg* arg){ /* 1. On sérialise la réponse */ bzero(buffer, MAX_BUF_LEN*sizeof(char)); memcpy(buffer, &request.flags, sizeof(char)); - memcpy(buffer+sizeof(char), &request.addr, sizeof(char)*15); + memcpy(buffer+sizeof(char), &request.addr, sizeof(in_addr_t)); memcpy(buffer+sizeof(char)*16, &request.port, sizeof(unsigned short)); /* 2. On envoie la réponse*/ @@ -122,7 +128,7 @@ int multicastTerminal(struct middleware_arg* arg){ return -2; } - printf("{%s} sent: bind_header{flag = %d; addr = '%s'; port = %d}\n", arg->entity, (int) request.flags, request.addr, ntohs(request.port)); + printf("{%s} sent: bind_header{flag = %d; addr = %x/'%s'; port = %d}\n", arg->entity, (int) request.flags, ntohl(request.addr), inet_ntoa(*SERV_IP), ntohs(request.port)); return 0; diff --git a/central-manager/lib/local/middleware.h b/central-manager/lib/local/middleware.h index 0907c0d..e190238 100644 --- a/central-manager/lib/local/middleware.h +++ b/central-manager/lib/local/middleware.h @@ -15,8 +15,8 @@ #include "../header.h" #include "../network/udp/server.h" + #include "../network/common.h" - // int multicastPlanes(struct middleware_arg* ARGS); int multicastTerminal(struct middleware_arg* ARGS); diff --git a/central-manager/lib/network/common.c b/central-manager/lib/network/common.c new file mode 100644 index 0000000..42592ef --- /dev/null +++ b/central-manager/lib/network/common.c @@ -0,0 +1,68 @@ +/************************** +* Network Common Depend. * +*************************** +* Designed & Developed by * +* Adrien Marquès * +* * +*************************** +* doowap31@gmail.com * +**************************/ +#include "common.h" + + + + + +/* Retourne l'adresse IP du serveur (première interface excepté "lo") +* +* @history +* [0] Initialisation des variables +* [1] On récupère la liste des interfaces +* [2] Parcourt des interfaces +* 1. Si "lo" -> suivant +* 2. Si pas IPV4 -> suivant +* 3. Si ok, on retourne l'ip +* [3] Cas échéant -> aucun résultat (NULL) +* +*/ +struct in_addr* GET_LOCAL_IP(){ + /* [0] Initialisation des variables + =========================================================*/ + struct ifaddrs *list; // list of interfaces + struct ifaddrs *it; // interface iterator + struct sockaddr_in *info; // interface info + struct in_addr *result; // ip trouvée + result = malloc( sizeof(struct in_addr) ); + + + /* [1] On récupère la liste des interfaces + =========================================================*/ + getifaddrs(&list); + + + /* [2] Parcourt des interfaces + =========================================================*/ + for( it = list ; it ; it = it->ifa_next ){ + + /* 1. Si "lo" -> suivant */ + if( strcmp(it->ifa_name, "lo") == 0 ) + continue; + + /* 2. Si pas IPV4 -> suivant */ + if( it->ifa_addr->sa_family != AF_INET ) + continue; + + /* 3. Si ok, on retourne l'ip */ + info = (struct sockaddr_in*) it->ifa_addr; + memcpy(result, &info->sin_addr, sizeof(struct in_addr)); + freeifaddrs(list); + return result; + + } + + + /* [3] Cas échéant -> aucun résultat + =========================================================*/ + freeifaddrs(list); + return NULL; +} \ No newline at end of file diff --git a/central-manager/lib/network/common.h b/central-manager/lib/network/common.h new file mode 100644 index 0000000..256590f --- /dev/null +++ b/central-manager/lib/network/common.h @@ -0,0 +1,21 @@ +/************************** +* Network Common Depend. * +*************************** +* Designed & Developed by * +* Adrien Marquès * +* * +*************************** +* doowap31@gmail.com * +**************************/ + +#ifndef _LIB_NETWORK_COMMON_H_ + #define _LIB_NETWORK_COMMON_H_ + + #include "../header.h" + #include + + + struct in_addr* GET_LOCAL_IP(); + + +#endif \ No newline at end of file diff --git a/central-manager/lib/network/udp/client.c b/central-manager/lib/network/udp/client.c index 3bf0ee8..a8974b5 100644 --- a/central-manager/lib/network/udp/client.c +++ b/central-manager/lib/network/udp/client.c @@ -2,7 +2,7 @@ -int UDP_SOCKET(int* pSocket, const char* pAddr, const int pPort, struct sockaddr_in* pInfo){ +int UDP_SOCKET(int* pSocket, in_addr_t pAddr, const int pPort, struct sockaddr_in* pInfo){ /* [0] Initialisation des variables =========================================================*/ @@ -37,7 +37,7 @@ int UDP_SOCKET(int* pSocket, const char* pAddr, const int pPort, struct sockaddr /* (2) On définit les infos */ pInfo->sin_family = AF_INET; pInfo->sin_port = htons(pPort); - pInfo->sin_addr.s_addr = inet_addr(pAddr); + pInfo->sin_addr.s_addr = pAddr; /* [n] Code succès diff --git a/central-manager/lib/network/udp/client.h b/central-manager/lib/network/udp/client.h index 8c1eeaa..2ad9c47 100644 --- a/central-manager/lib/network/udp/client.h +++ b/central-manager/lib/network/udp/client.h @@ -26,7 +26,7 @@ /* Créée une socket UDP + crée le sockaddr_in pour la suite * * ==IN== - * @pAddr Adresse du groupe multicast UDP + * @pAddr Adresse du groupe multicast UDP * @pPort Port d'écoute UDP * * ==OUT== @@ -43,7 +43,7 @@ * * */ - int UDP_SOCKET(int* pSocket, const char* pAddr, const int pPort, struct sockaddr_in* pInfo); + int UDP_SOCKET(int* pSocket, in_addr_t pAddr, const int pPort, struct sockaddr_in* pInfo); diff --git a/central-manager/lib/network/udp/server.c b/central-manager/lib/network/udp/server.c index 7fc9ef5..22d56ac 100644 --- a/central-manager/lib/network/udp/server.c +++ b/central-manager/lib/network/udp/server.c @@ -12,8 +12,8 @@ -int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo, const char pMcast){ - if( DEBUGMOD&HDR ) printf("====== DROP_UDP_MCAST(%s, %d, %p, %p, %d) ======\n\n", pAddr, pPort, (void*) pListenSock, (void*) pInfo, (int) pMcast); +int DROP_UDP_SERVER(in_addr_t pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo, const char pMcast){ + if( DEBUGMOD&HDR ) printf("====== DROP_UDP_MCAST(%x, %d, %p, %p, %d) ======\n\n", pAddr, pPort, (void*) pListenSock, (void*) pInfo, (int) pMcast); /* [0] Initialisation des variables =========================================================*/ @@ -21,6 +21,7 @@ int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct struct ip_mreq mcastReq; *pListenSock = -1; struct timeval timeout; + struct in_addr tmp; /* [1] Création de la socket @@ -67,8 +68,10 @@ int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct /* 3. Demande groupe multicast (si demandé) */ if( pMcast ){ - if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] join mcast group: %s\n", pAddr); - mcastReq.imr_multiaddr.s_addr = inet_addr(pAddr); + tmp.s_addr = pAddr; + + if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] join mcast group: %s\n", inet_ntoa(tmp)); + mcastReq.imr_multiaddr.s_addr = pAddr; mcastReq.imr_interface.s_addr = htonl(INADDR_ANY); if( setsockopt(*pListenSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcastReq, sizeof(mcastReq)) < 0 ){ diff --git a/central-manager/lib/network/udp/server.h b/central-manager/lib/network/udp/server.h index 59da738..e0ba090 100644 --- a/central-manager/lib/network/udp/server.h +++ b/central-manager/lib/network/udp/server.h @@ -20,7 +20,7 @@ /* Créé et met un serveur UDP d'écoute MULTICAST ou CLASSIQUE * * ==IN== - * @pAddr Adresse du groupe multicast UDP + * @pAddr Adresse du groupe multicast UDP * @pPort Port d'écoute UDP * @pMcast Si multicast ou non * @@ -38,7 +38,7 @@ * [n] On renvoie la socket par référence * */ - int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo, const char pMcast); + int DROP_UDP_SERVER(in_addr_t pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo, const char pMcast); diff --git a/plane/lib/network/tcp/client.c b/plane/lib/network/tcp/client.c index 396bd19..db9f1f0 100644 --- a/plane/lib/network/tcp/client.c +++ b/plane/lib/network/tcp/client.c @@ -10,7 +10,7 @@ #include "client.h" -int TCP_CONNECT(int* pSocket, const char* pAddr, const int pPort, struct sockaddr_in* pInfo){ +int TCP_CONNECT(int* pSocket, const in_addr_t pAddr, const int pPort, struct sockaddr_in* pInfo){ /* [0] Initialisation des variables =========================================================*/ @@ -35,7 +35,7 @@ int TCP_CONNECT(int* pSocket, const char* pAddr, const int pPort, struct sockadd /* (2) On définit les infos */ pInfo->sin_family = AF_INET; pInfo->sin_port = htons(pPort); - pInfo->sin_addr.s_addr = inet_addr(pAddr); + pInfo->sin_addr.s_addr = pAddr; /* [3] On se connecte au serveur diff --git a/plane/lib/network/tcp/client.h b/plane/lib/network/tcp/client.h index 404e590..bdd5cc9 100644 --- a/plane/lib/network/tcp/client.h +++ b/plane/lib/network/tcp/client.h @@ -18,7 +18,7 @@ /* Créée une socket TCP et la connecte + crée le sockaddr_in pour la suite * * ==IN== - * @pAddr Adresse du serveur TCP + * @pAddr Adresse du serveur TCP * @pPort Port du serveur TCP * * ==OUT== @@ -34,6 +34,6 @@ * [3] On se connecte au serveur * */ - int TCP_CONNECT(int* pSocket, const char* pAddr, const int pPort, struct sockaddr_in* pInfo); + int TCP_CONNECT(int* pSocket, const in_addr_t pAddr, const int pPort, struct sockaddr_in* pInfo); #endif \ No newline at end of file diff --git a/plane/plane.c b/plane/plane.c index ac80e3a..49cf9d3 100644 --- a/plane/plane.c +++ b/plane/plane.c @@ -31,6 +31,7 @@ int open_communication(){ char buffer[MAX_BUF_LEN] = {0}; // buffer int status; struct bind_header request; + struct in_addr ip; // pour afficher l'ip en dot notation /* [1] Socket Multicast UDP =========================================================*/ @@ -64,11 +65,15 @@ int open_communication(){ /* 4. On récupère les données */ bzero(&request, sizeof(struct bind_header)); - 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) ); + memcpy(&request.flags, buffer, sizeof(char) ); + memcpy(&request.addr, buffer+sizeof(char), sizeof(in_addr_t) ); + memcpy(&request.port, buffer+sizeof(char)+sizeof(in_addr_t), sizeof(unsigned short) ); + // on indian-switch + printf("received : %x\n", request.addr); + request.addr = ntohl(request.addr); request.port = ntohs(request.port); + ip.s_addr = request.addr; /* 5. On vérifie les flags */ if( !(request.flags&BINDHEAD_TCP) || request.flags&BINDHEAD_UDP ){ @@ -78,7 +83,7 @@ int open_communication(){ // 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); + printf("* received bind_header{flags = %d; addr = %x/%s; port = %d;}\n\n", request.flags, request.addr, inet_ntoa(ip), request.port); diff --git a/plane/plane.h b/plane/plane.h index 022d4d4..8e35ca9 100644 --- a/plane/plane.h +++ b/plane/plane.h @@ -49,14 +49,14 @@ int spd; }; - #define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) ) + #define BINDHDR_LEN ( sizeof(char)+sizeof(in_addr_t)+sizeof(unsigned short) ) #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; - char addr[INET_ADDRSTRLEN]; + in_addr_t addr; unsigned short port; };