From edad9a8f8fe1ba30c0c26065d5ddc406c821f7d5 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 18 Apr 2017 10:36:32 +0200 Subject: [PATCH] started multicast for planes --- plane/Makefile | 8 ++-- plane/lib/network/udp/server.c | 77 ++++++++++++++++++++++++++++++++++ plane/lib/network/udp/server.h | 45 ++++++++++++++++++++ plane/plane.c | 16 +++---- plane/plane.h | 2 +- 5 files changed, 136 insertions(+), 12 deletions(-) create mode 100644 plane/lib/network/udp/server.c create mode 100644 plane/lib/network/udp/server.h diff --git a/plane/Makefile b/plane/Makefile index 2e4e1c4..ddb363a 100644 --- a/plane/Makefile +++ b/plane/Makefile @@ -9,13 +9,13 @@ 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/udp/client.o: lib/header.h lib/network/udp/client.h lib/network/udp/client.c - gcc $(CFLAGS) -c -o lib/network/udp/client.o lib/network/udp/client.c +lib/network/udp/server.o: lib/header.h lib/network/udp/server.h lib/network/udp/server.c + gcc $(CFLAGS) -c -o lib/network/udp/server.o lib/network/udp/server.c # Compiles the Plane # -lm flag for math lib -boot: lib/network/tcp/client.o lib/network/udp/client.o plane.h plane.c - gcc $(CFLAGS) -o boot lib/network/tcp/client.o lib/network/udp/client.o plane.c -lm +boot: lib/network/tcp/client.o lib/network/udp/server.o plane.h plane.c + gcc $(CFLAGS) -o boot lib/network/tcp/client.o lib/network/udp/server.o plane.c -lm # Run full compilation diff --git a/plane/lib/network/udp/server.c b/plane/lib/network/udp/server.c new file mode 100644 index 0000000..cc980a7 --- /dev/null +++ b/plane/lib/network/udp/server.c @@ -0,0 +1,77 @@ +/**************************int DROP_MULTICAST_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo){ +* UDP Server Dependency * +*************************** +* Designed & Developed by * +* Adrien Marquès * +* * +*************************** +* doowap31@gmail.com * +**************************/ +#include "server.h" + + + + +int DROP_MULTICAST_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo){ + + /* [0] Initialisation des variables + =========================================================*/ + int STATUS; // status + struct ip_mreq mcastReq; + *pListenSock = -1; + struct timeval timeout; + + + /* [1] Création de la socket + =======================================================*/ + /* 1. Création socket */ + *pListenSock = socket(AF_INET, SOCK_DGRAM, 0); + + /* 2. Gestion erreur */ + if( *pListenSock < 0 ) return -1; + + /* 3. Timeout */ + timeout.tv_sec = SOCK_TIMEOUT; + timeout.tv_usec = 0; + + if( setsockopt(*pListenSock, SOL_SOCKET, SO_RCVTIMEO|SO_SNDTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){ + close(*pListenSock); + return -1; + } + + + /* [2] On définit les infos de la socket + =========================================================*/ + /* (1) Reset des valeurs */ + bzero(pInfo, sizeof(struct sockaddr_in)); + + /* (2) On définit les infos */ + pInfo->sin_family = AF_INET; + pInfo->sin_port = htons(pPort); + pInfo->sin_addr.s_addr = htonl(INADDR_ANY); + + + /* [3] On publie la SOCKET + =======================================================*/ + /* 1. Publication socket */ + STATUS = bind(*pListenSock, (struct sockaddr*) pInfo, sizeof(struct sockaddr_in)); + + /* 2. Gestion erreur */ + if( STATUS < 0 ) return -1; + + /* 3. Demande groupe multicast */ + mcastReq.imr_multiaddr.s_addr = inet_addr(pAddr); + mcastReq.imr_interface.s_addr = htonl(INADDR_ANY); + + if( setsockopt(*pListenSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcastReq, sizeof(mcastReq)) < 0 ){ + close(*pListenSock); + return -1; + } + + + + /* [n] Code succès + =========================================================*/ + return 0; +} + diff --git a/plane/lib/network/udp/server.h b/plane/lib/network/udp/server.h new file mode 100644 index 0000000..291461e --- /dev/null +++ b/plane/lib/network/udp/server.h @@ -0,0 +1,45 @@ +/************************** +* UDP Server Dependency * +*************************** +* Designed & Developed by * +* Adrien Marquès * +* * +*************************** +* doowap31@gmail.com * +**************************/ + + +#ifndef _LIB_NETWORK_UDP_SERVER_H_ + #define _LIB_NETWORK_UDP_SERVER_H_ + + + + #include "../../header.h" + + + /* Créé et met un serveur UDP d'écoute MULTICAST + * + * ==IN== + * @pAddr Adresse du groupe multicast UDP + * @pPort Port d'écoute UDP + * @pMcast Si multicast ou non + * + * ==OUT== + * @pListenSocket Pointeur sur le à remplir => contiendra un pointeur sur la socket d'écoute + * @pInfo Pointeur sur le à remplir => contiendra un pointeur sur les infos server + * + * ==RETURN== + * @status -1 si erreur, sinon 0 + * + * @history + * [1] Création de la socket d'écoute + * [2] On définit les infos de la socket + * [3] On publie la SOCKET (bind) + * [n] On renvoie la socket par référence + * + */ + int DROP_MULTICAST_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo); + + + +#endif \ No newline at end of file diff --git a/plane/plane.c b/plane/plane.c index b751c4f..9f6ddbf 100644 --- a/plane/plane.c +++ b/plane/plane.c @@ -38,7 +38,7 @@ int open_communication(){ /* (1) Création socket multicast ---------------------------------------------------------*/ - if( UDP_SOCKET(&mcast_socket, MCST_HOST, MCST_PORT, &udp) < 0 ) + if( DROP_MULTICAST_SERVER(MCST_HOST, MCST_PORT, &mcast_socket, &udp) < 0 ) return 0; @@ -48,17 +48,18 @@ int open_communication(){ // memcpy(buffer, numero_vol, sizeof(char)*6); request.flags = BINDHEAD_TCP; strcpy(request.addr, "000.000.000.000"); - request.port = 0; + request.port = htons(0); memcpy(buffer+sizeof(char)*0, &request.flags, sizeof(char)); memcpy(buffer+sizeof(char)*1, &request.addr, sizeof(char)*INET_ADDRSTRLEN); memcpy(buffer+sizeof(char)*(1+INET_ADDRSTRLEN), &request.port, sizeof(unsigned short)); /* 2. Envoi séquence */ - if( sendto(mcast_socket, buffer, BINDHDR_LEN/sizeof(char) +1, 0, (struct sockaddr*) &udp, sizeof(struct sockaddr_in)) < 0 ){ - close(mcast_socket); - return 0; - } + // if( sendto(mcast_socket, buffer, BINDHDR_LEN/sizeof(char) +1, 0, (struct sockaddr*) &udp, sizeof(struct sockaddr_in)) < 0 ){ + // close(mcast_socket); + // return 0; + // } + // printf("* sent bind_header{flags = %d; addr = %s; port = %d;}\n", request.flags, request.addr, htons(request.port)); /* (3) Attente réponse SGCA -> adresse+port TCP ---------------------------------------------------------*/ @@ -73,6 +74,7 @@ int open_communication(){ /* 3. Vérification taille */ if( status < BINDHDR_LEN ){ + printf("%d / %d\n", status, (int) BINDHDR_LEN); close(mcast_socket); return 0; } @@ -84,7 +86,7 @@ int open_communication(){ memcpy(&request.addr, buffer+sizeof(char), sizeof(char)*INET_ADDRSTRLEN ); memcpy(&request.port, buffer+sizeof(char)*(1+INET_ADDRSTRLEN), sizeof(unsigned short) ); - printf("bind_header{flags = %d; addr = %s; port = %d;}\n\n", request.flags, request.addr, request.port); + 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 7ea90b0..f12be07 100644 --- a/plane/plane.h +++ b/plane/plane.h @@ -15,7 +15,7 @@ #include #include "lib/header.h" - #include "lib/network/udp/client.h" + #include "lib/network/udp/server.h" #include "lib/network/tcp/client.h"