186 lines
7.4 KiB
C
186 lines
7.4 KiB
C
/**************************
|
|
* Middleware Dependency *
|
|
***************************
|
|
* Designed & Developed by *
|
|
* Adrien Marquès *
|
|
* <xdrm-brackets> *
|
|
***************************
|
|
* doowap31@gmail.com *
|
|
**************************/
|
|
#include "middleware.h"
|
|
|
|
|
|
|
|
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 flag manquant -> on quitte */
|
|
if( !(request.flags&BINDHEAD_TCP) )
|
|
return -1;
|
|
|
|
|
|
|
|
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){
|
|
/* [0] Initialisation
|
|
=========================================================*/
|
|
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
|
|
|
|
|
|
|
|
/* 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) Création socket de communication (si demandé)
|
|
---------------------------------------------------------*/
|
|
/* 1. Si on veut un port de communicatin */
|
|
if( request.flags&BINDHEAD_SCK ){
|
|
|
|
/* 2. On bind une socket sur un port random */
|
|
if( DROP_UDP_SERVER(SERV_HOST, 0, arg->comSock, &comInfo, 0) < 0 ){
|
|
|
|
if( DEBUGMOD&SCK ) printf("{%s} Erreur de création de la socket COM\n", arg->entity);
|
|
|
|
/* 3. On ferme la socket et enleve le FLAG pour dire qu'on a pas pu ouvrir le port */
|
|
close(*arg->comSock);
|
|
request.flags -= BINDHEAD_SCK;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/* 2.2 Si on veut on port de communication */
|
|
/* (2) Récupération port random
|
|
---------------------------------------------------------*/
|
|
if( request.flags&BINDHEAD_SCK ){
|
|
|
|
/* 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_SCK;
|
|
|
|
/* 3. Si on a le port -> on le met dans la reponse */
|
|
}else{
|
|
strcpy(request.addr, SERV_HOST);
|
|
request.port = htons(comInfo.sin_port);
|
|
if( DEBUGMOD&SCK ) printf("{%s}{udp_com} socket opened on %s:%d\n", arg->entity, request.addr, request.port);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/* [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)*15);
|
|
memcpy(buffer+sizeof(char)*16, &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;
|
|
|
|
} |