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

155 lines
6.4 KiB
C
Raw Normal View History

/**************************
* Middleware Dependency *
***************************
* Designed & Developed by *
* Adrien Marquès *
* <xdrm-brackets> *
***************************
* doowap31@gmail.com *
**************************/
#include "middleware.h"
int multicastTerminal(struct middleware_arg* arg){
/* [0] Initialisation
=========================================================*/
2017-04-26 12:25:05 +00:00
/* 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
2017-04-26 21:04:31 +00:00
struct in_addr ip; // Pour afficher une ip en dot notation
2017-04-26 12:25:05 +00:00
/* 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;
}
2017-04-26 12:25:05 +00:00
/* 3. On attends une connection UDP */
len = sizeof(struct sockaddr_in);
read = recvfrom(arg->listenSock, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
2017-04-26 12:25:05 +00:00
/* 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;
}
2017-04-26 12:25:05 +00:00
/* 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));
2017-04-26 21:04:31 +00:00
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);
2017-04-25 09:54:15 +00:00
/* 2. Si mauvais flag (avion) */
if( request.flags&BINDHEAD_TCP || !(request.flags&BINDHEAD_UDP) ){
printf("{%s} Invalid flag\n", arg->entity);
return -1;
}
2017-04-25 09:54:15 +00:00
/* 3. Si mauvais flag (mauvais type de terminal) */
if( request.flags&BINDHEAD_CTL ){
2017-04-25 09:54:15 +00:00
// vterm avec flag cterm
if( strcmp(arg->entity, "udp_vterm") == 0 ){
printf("{%s} Invalid flag\n", arg->entity);
return -1;
}
2017-04-25 09:54:15 +00:00
// cterm avec flag vterm
}else if( strcmp(arg->entity, "udp_cterm") == 0 ){
printf("{%s} Invalid flag\n", arg->entity);
return -1;
}
2017-04-25 09:54:15 +00:00
/* (1) Création socket de communication (si demandé)
---------------------------------------------------------*/
2017-04-25 09:54:15 +00:00
/* 1. On bind une socket sur un port random */
2017-04-26 12:25:05 +00:00
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);
2017-04-25 09:54:15 +00:00
/* 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) */
2017-05-04 09:56:07 +00:00
/* 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);
2017-05-04 09:56:07 +00:00
/* 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);
2017-05-04 09:56:07 +00:00
2017-04-25 09:54:15 +00:00
/* (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);
2017-04-25 09:54:15 +00:00
/* 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{
2017-04-26 12:25:05 +00:00
request.addr = SERV_IP->s_addr;
2017-04-25 09:54:15 +00:00
request.port = comInfo.sin_port;
2017-04-26 12:25:05 +00:00
if( DEBUGMOD&SCK ) printf("{%s}{com} socket opened on %s:%d\n", arg->entity, inet_ntoa(*SERV_IP), ntohs(request.port));
}
2017-04-25 09:54:15 +00:00
/* [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;
}