136 lines
5.3 KiB
C
136 lines
5.3 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
|
|
|
|
/* 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));
|
|
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) ){
|
|
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} Erreur de création de la socket COM\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;
|
|
}
|
|
|
|
|
|
|
|
/* (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} Erreur de recherche du port COM ouvert\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} Impossible de répondre au client!\n", arg->entity);
|
|
return -2;
|
|
}
|
|
|
|
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;
|
|
|
|
} |