diff --git a/central-manager/lib/data.h b/central-manager/lib/data.h index ea9a96b..31e8a20 100644 --- a/central-manager/lib/data.h +++ b/central-manager/lib/data.h @@ -40,14 +40,14 @@ }; - #define BINDHDR_LEN ( sizeof(char)*16+sizeof(unsigned short) ) + #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 struct bind_header{ char flags; - char addr[15]; + char addr[INET_ADDRSTRLEN]; unsigned short port; }; diff --git a/central-manager/lib/local/middleware.c b/central-manager/lib/local/middleware.c index 49db663..a358013 100644 --- a/central-manager/lib/local/middleware.c +++ b/central-manager/lib/local/middleware.c @@ -11,7 +11,77 @@ -int multicastPlanes(struct middleware_arg* arg){ return -2; } +int multicastPlanes(struct middleware_arg* arg){ + /* [0] Initialisation + =========================================================*/ + struct sockaddr_in clientInfo; // contiendra les infos client + socklen_t len; // taille de la socket + int read; // compteurs + char buffer[MAX_BUF_LEN+1]; // buffer requête + struct bind_header request; // requête parsée + + + + /* 1. 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 */ + 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 */ + if( DEBUGMOD&SCK ) printf("{%s} %s:%d connecté\n", arg->entity, inet_ntoa(clientInfo.sin_addr), ntohs(clientInfo.sin_port)); + + + + /* [1] Gestion de la requête + =========================================================*/ + /* 1. On parse la requête */ + 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); + + /* (1) Envoi socket de communication (TCP) + ---------------------------------------------------------*/ + /* 1. Si on veut un port de communicatin */ + if( request.flags&BINDHEAD_SCK ){ + + strcpy(request.addr, SERV_HOST); + request.port = TCP_LIST; + + } + + /* [2] Envoi de la réponse + =========================================================*/ + /* 1. On sérialise la réponse */ + bzero(buffer, MAX_BUF_LEN); + memcpy(buffer, &request.flags, sizeof(char)); + memcpy(buffer+sizeof(char), &request.addr, sizeof(char)*INET_ADDRSTRLEN); + memcpy(buffer+sizeof(char)*(1+INET_ADDRSTRLEN), &request.port, sizeof(unsigned short)); + + /* 2. On envoie la réponse*/ + len = sizeof(struct sockaddr_in); + if( sendto(arg->listenSock, buffer, BINDHDR_LEN/sizeof(char) + 1, 0, (struct sockaddr*) &clientInfo, len) < 0 ){ + printf("{%s} Impossible de répondre au client!\n", arg->entity); + return -2; + } + + printf("{%s} sent: bind_header{flag = %d; addr = '%s'; port = %d}\n", arg->entity, (int) request.flags, request.addr, request.port); + + + return 0; + +} + + + + + + int multicastTerminal(struct middleware_arg* arg){ @@ -92,7 +162,7 @@ int multicastTerminal(struct middleware_arg* arg){ } - /* [4] Envoi de la réponse + /* [2] Envoi de la réponse =========================================================*/ /* 1. On sérialise la réponse */ bzero(buffer, MAX_BUF_LEN); diff --git a/plane/plane.c b/plane/plane.c index 6558265..ed495d1 100644 --- a/plane/plane.c +++ b/plane/plane.c @@ -32,6 +32,7 @@ int open_communication(){ unsigned short serverPort; // port TCP donné par requête multicast UDP char serverAddr[INET_ADDRSTRLEN+1]; // adresse TCP donné par requête multicast UDP int status; + const int REQ_SIZE = sizeof(char)*INET_ADDRSTRLEN+sizeof(unsigned short); /* [1] Socket Multicast UDP =========================================================*/ @@ -46,10 +47,11 @@ int open_communication(){ /* (2) Envoi séquence initialisation avion ---------------------------------------------------------*/ /* 1. Construction séquence d'initialisation */ - memcpy(buffer, numero_vol, sizeof(char)*6); + // memcpy(buffer, numero_vol, sizeof(char)*6); + strcpy(buffer, "\003000.000.000.000\x01\x00"); /* 2. Envoi séquence */ - if( sendto(mcast_socket, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &udp, sizeof(struct sockaddr_in)) < 0 ){ + if( sendto(mcast_socket, buffer, REQ_SIZE+1, 0, (struct sockaddr*) &udp, sizeof(struct sockaddr_in)) < 0 ){ close(mcast_socket); return 0; } @@ -67,14 +69,16 @@ int open_communication(){ } /* 3. Vérification taille */ - if( status < sizeof(char)*INET_ADDRSTRLEN+sizeof(unsigned short) ){ + if( status < REQ_SIZE ){ close(mcast_socket); return 0; } /* 4. On récupère les données */ - memcpy(serverAddr, buffer, sizeof(char)*INET_ADDRSTRLEN ); - memcpy(&serverPort, buffer+sizeof(char)*INET_ADDRSTRLEN, sizeof(unsigned short) ); + memcpy(serverAddr, buffer+sizeof(char), sizeof(char)*INET_ADDRSTRLEN ); + memcpy(&serverPort, buffer+sizeof(char)*(INET_ADDRSTRLEN+1), sizeof(unsigned short) ); + + printf("bind_header{flags = %d; addr = %s; port = %d;}\n\n", (int) buffer[0], serverAddr, serverPort);