sysdis-project/sgca/lib/local/middleware.c

155 lines
6.4 KiB
C

/**************************
* Middleware Dependency *
***************************
* Designed & Developed by *
* Adrien Marquès *
* <xdrm-brackets> *
***************************
* doowap31@gmail.com *
**************************/
#include "middleware.h"
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
int read; // compteurs
char buffer[MAX_BUF_LEN+1]; // buffer requête
struct bind_header request; // requête parsée
struct in_addr ip; // Pour afficher une ip en dot notation
/* 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;
}
/* 3. On attends une connection UDP */
len = sizeof(struct sockaddr_in);
read = recvfrom(arg->listenSock, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
/* 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;
}
/* 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));
/* [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(in_addr_t));
memcpy(&request.port, buffer+sizeof(char)+sizeof(in_addr_t), sizeof(unsigned short));
request.addr = ntohl(request.addr);
ip.s_addr = request.addr;
if( DEBUGMOD&COM ) printf("{%s} received: bind_header{flag = %d; addr = %x/'%s'; port = %d}\n", arg->entity, (int) request.flags, ntohl(request.addr), inet_ntoa(ip), 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. On bind une socket sur un port random */
if( DROP_UDP_SERVER(SERV_IP->s_addr, 0, arg->comSock, &comInfo, 0) < 0 ){
if( DEBUGMOD&SCK ) printf("{%s} Cannot create COMmunication socket\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;
}
/* 3. On définit un timeout (si aucune connection) */
/* 3.1. Si viewTerm -> default timeout (4sec) */
if( strcmp(arg->entity, "udp_vterm") == 0 ){
if( setTimeout(*arg->comSock, SOCK_TIMEOUT, TIMEOUT_RECV|TIMEOUT_SEND) < 0 ){
if( DEBUGMOD&SCK ) printf("{%s} Cannot set RECV not SEND timeout\n", arg->entity);
}else
if( DEBUGMOD&SCK ) printf("{%s} SEND/RECV timeout set to %d\n", arg->entity, SOCK_TIMEOUT);
/* 3.2. Si ctrlTerm -> 10sec timeout */
}else
if( setTimeout(*arg->comSock, CTRL_TIMEOUT, TIMEOUT_RECV|TIMEOUT_SEND) < 0 ){
if( DEBUGMOD&SCK ) printf("{%s} Cannot set RECV not SEND timeout\n", arg->entity);
}else
if( DEBUGMOD&SCK ) printf("{%s} SEND/RECV timeout set to %d\n", arg->entity, CTRL_TIMEOUT);
/* (2) Récupération port random
---------------------------------------------------------*/
/* 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} Cannot find opened port from socket\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;
/* 3. Si on a le port -> on le met dans la reponse */
}else{
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, inet_ntoa(*SERV_IP), ntohs(request.port));
}
/* [2] Envoi de la réponse
=========================================================*/
/* 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(in_addr_t));
memcpy(buffer+sizeof(char)+sizeof(in_addr_t), &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} Cannot answer to client\n", arg->entity);
return -2;
}
if( DEBUGMOD&COM ) 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;
}