2017-04-09 16:56:11 +00:00
|
|
|
/**************************
|
|
|
|
* Central-Manager *
|
|
|
|
***************************
|
|
|
|
* Designed & Developed by *
|
|
|
|
* Adrien Marquès *
|
|
|
|
* <xdrm-brackets> *
|
|
|
|
***************************
|
|
|
|
* doowap31@gmail.com *
|
|
|
|
**************************/
|
2017-03-28 16:45:18 +00:00
|
|
|
#include "central-manager.h"
|
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* @argv : {0:program name}
|
|
|
|
*
|
|
|
|
* @history
|
2017-04-09 16:56:11 +00:00
|
|
|
* [0] Initialisation des variables
|
2017-04-03 16:04:57 +00:00
|
|
|
* [1] Lancement des THREADS d'écoute
|
|
|
|
* 1. On démarre le SERVEUR TCP d'écoute globale
|
2017-04-26 12:25:05 +00:00
|
|
|
* 2. On récupère l'IP locale
|
|
|
|
* 3. On démarre le SERVEUR UDP d'écoute globale
|
2017-04-03 16:04:57 +00:00
|
|
|
* [2] On attends la fin de tous les THREADS
|
2017-04-07 18:25:53 +00:00
|
|
|
* [3] On libère les variables globale
|
2017-04-01 15:01:36 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
int main(int argc, char* argv[]){
|
2017-03-28 16:45:18 +00:00
|
|
|
|
2017-05-04 10:36:45 +00:00
|
|
|
printf("[Copyright (R) 2017]\n");
|
|
|
|
printf(" - Adrien Marquès aka. xdrm-brackets (C shit)\n");
|
|
|
|
printf(" - Lucas Mascaro aka. SeekDaSky (java shit)\n");
|
|
|
|
printf("* From the developers of the 'WebSocket' library for the UPPA University\n\n\n");
|
|
|
|
printf("Notice: {threadName}{subThread}(subThreadId)\n\n");
|
2017-04-07 18:25:53 +00:00
|
|
|
printf("**** Execution tree\n");
|
2017-04-07 16:14:21 +00:00
|
|
|
|
2017-04-09 16:56:11 +00:00
|
|
|
/* [0] Initialisation des variables
|
2017-04-07 18:25:53 +00:00
|
|
|
=========================================================*/
|
2017-04-09 16:56:11 +00:00
|
|
|
/* 1. Variables globales */
|
2017-05-03 09:58:41 +00:00
|
|
|
sgca.n = 0;
|
|
|
|
sgca.unit = (struct context_unit*) malloc( sizeof(struct context_unit) );
|
2017-04-26 12:25:05 +00:00
|
|
|
struct in_addr* SERV_HOST;
|
|
|
|
|
|
|
|
/* 2. On récupère l'IP locale' */
|
|
|
|
SERV_HOST = GET_LOCAL_IP();
|
|
|
|
if( SERV_HOST == NULL ){
|
|
|
|
printf("/!\\ Cannot fetch local ip address. Aborting!\n");
|
2017-05-03 09:58:41 +00:00
|
|
|
free(sgca.unit);
|
2017-04-26 12:25:05 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
printf("LOCAL IP: %s\n", inet_ntoa(*SERV_HOST));
|
2017-03-28 16:45:18 +00:00
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
/* 3. Variables locales */
|
|
|
|
struct listen_arg tcp_listn_arg = { SERV_HOST->s_addr, TCP_LIST, NULL, &managePlane };
|
|
|
|
struct listen_arg udp_mcast_arg = { inet_addr(MCST_HOST), UDP_MCST, NULL, NULL };
|
|
|
|
struct listen_arg udp_vterm_arg = { inet_addr(MCST_VTER), UDP_VTER, &multicastTerminal, &manageViewTerm };
|
|
|
|
struct listen_arg udp_cterm_arg = { inet_addr(MCST_CTER), UDP_CTER, &multicastTerminal, &manageCtrlTerm };
|
2017-04-09 14:06:31 +00:00
|
|
|
|
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* [1] Lancement des THREADS d'écoute
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Ecoute TCP */
|
2017-04-18 18:06:14 +00:00
|
|
|
pthread_create(&listenManagers[0], NULL, LISTEN_TCP, (void*) &tcp_listn_arg);
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&THR ) printf("{tcp_listn} started\n");
|
2017-04-07 16:14:21 +00:00
|
|
|
|
|
|
|
/* (2) Ecoute UDP multicast */
|
2017-04-18 18:06:14 +00:00
|
|
|
pthread_create(&listenManagers[1], NULL, MCAST_PUBLISH, (void*) &udp_mcast_arg);
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&THR ) printf("{udp_mcast} started\n");
|
2017-04-07 16:14:21 +00:00
|
|
|
|
|
|
|
/* (3) Ecoute UDP viewTerm */
|
2017-04-18 18:06:14 +00:00
|
|
|
pthread_create(&listenManagers[2], NULL, LISTEN_UDP, (void*) &udp_vterm_arg);
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&THR ) printf("{udp_vterm} started\n");
|
2017-03-28 16:45:18 +00:00
|
|
|
|
2017-04-07 16:14:21 +00:00
|
|
|
/* (4) Ecoute UDP ctrlTerm */
|
2017-04-18 18:06:14 +00:00
|
|
|
pthread_create(&listenManagers[3], NULL, LISTEN_UDP, (void*) &udp_cterm_arg);
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&THR ) printf("{udp_cterm} started\n");
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] On attends la fin de tous les THREADS
|
|
|
|
==========================================================*/
|
2017-04-07 16:14:21 +00:00
|
|
|
for( char i = 0 ; i < 4 ; i++ )
|
|
|
|
pthread_join(listenManagers[(int)i], NULL);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-07 18:25:53 +00:00
|
|
|
|
|
|
|
/* [3] On libère les variables globales
|
2017-04-01 15:01:36 +00:00
|
|
|
==========================================================*/
|
2017-05-03 09:58:41 +00:00
|
|
|
free(sgca.unit);
|
2017-04-26 12:25:05 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2017-04-01 15:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* Attente de connection TCP
|
2017-03-28 16:45:18 +00:00
|
|
|
*
|
|
|
|
* @history
|
|
|
|
* [0] Initialisation des variables
|
2017-04-01 15:01:36 +00:00
|
|
|
* [1] On démarre le SERVEUR TCP d'écoute globale
|
|
|
|
* [2] Attente d'une demande de connection TCP -> création d'un THREAD
|
2017-03-28 16:45:18 +00:00
|
|
|
* [3] On attends la fin de tous les THREADS
|
2017-04-01 15:01:36 +00:00
|
|
|
* [4] On ferme la SOCKET d'écoute TCP globale
|
|
|
|
*
|
2017-03-28 16:45:18 +00:00
|
|
|
*/
|
2017-04-09 17:33:27 +00:00
|
|
|
void* LISTEN_TCP(void* THREADABLE_ARGS){
|
2017-03-28 16:45:18 +00:00
|
|
|
/* [0] Initialisation des variables
|
|
|
|
==========================================================*/
|
2017-04-09 14:06:31 +00:00
|
|
|
int CLIENT_SOCKET; // contiendra la socket TCP à envoyer sur un THREAD
|
|
|
|
struct sockaddr_in clientInfo; // contiendra les infos client
|
|
|
|
socklen_t len; // taille de la socket
|
|
|
|
int index, i; // compteurs
|
2017-04-26 12:25:05 +00:00
|
|
|
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
|
|
|
struct in_addr ip; // Pour afficher l'IP en dot notation
|
2017-04-07 18:25:53 +00:00
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
ip.s_addr = arg->addr;
|
2017-03-28 16:45:18 +00:00
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
// retour de @DROP_TCP_SERVER
|
|
|
|
int LISTENSOCK; // contiendra la socket d'écoute TCP
|
2017-03-28 16:45:18 +00:00
|
|
|
|
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* [1] On démarre le SERVEUR TCP d'écoute globale
|
2017-03-28 16:45:18 +00:00
|
|
|
==========================================================*/
|
2017-04-09 14:06:31 +00:00
|
|
|
if( DROP_TCP_SERVER(arg->port, &LISTENSOCK) < 0 ){
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&SCK ) printf("{tcp_listn} Error creating listener socket\n");
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
// On ferme la SOCKET d'écoute globale
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("{tcp_listn} Closing listener socket!\n");
|
2017-04-01 15:01:36 +00:00
|
|
|
close(LISTENSOCK);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-28 16:45:18 +00:00
|
|
|
|
|
|
|
|
2017-04-09 14:06:31 +00:00
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
printf("{tcp_listn} listen on %s:%d\n", inet_ntoa(ip), arg->port);
|
2017-03-28 16:45:18 +00:00
|
|
|
|
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
/* [2] Attente d'une demande de connection, pour création d'un THREAD
|
2017-03-28 16:45:18 +00:00
|
|
|
============================================================================*/
|
|
|
|
while( 1 ){
|
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* 1. On initialise les SOCKET en attendant la connexion et le rang du "manager" inactif */
|
|
|
|
CLIENT_SOCKET = -1;
|
|
|
|
index = -1;
|
|
|
|
|
|
|
|
/* 2. On attends une connection TCP */
|
2017-04-09 16:41:44 +00:00
|
|
|
len = sizeof(struct sockaddr_in);
|
2017-04-01 15:01:36 +00:00
|
|
|
CLIENT_SOCKET = accept(LISTENSOCK, (struct sockaddr*) &clientInfo, &len);
|
2017-03-28 16:45:18 +00:00
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* 3. Si erreur, on attend une nouvelle connection */
|
|
|
|
if( CLIENT_SOCKET < 0 ){
|
2017-04-07 18:25:53 +00:00
|
|
|
if( DEBUGMOD&SCK ) printf("{tcp_listn} accept: Erreur connection\n");
|
2017-04-01 15:01:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-28 16:45:18 +00:00
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&SCK ) printf("{tcp_listn} %s:%d connected\n", inet_ntoa(clientInfo.sin_addr), ntohs(clientInfo.sin_port));
|
2017-04-09 16:41:44 +00:00
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* 4. On cherche un "manager" libre (inactif) */
|
|
|
|
for( i = 0 ; i < MAX_TCP_THR ; i++ )
|
|
|
|
if( activeTCPManagers[i] == 0 ){ index = i; break; }
|
2017-03-28 16:45:18 +00:00
|
|
|
|
|
|
|
// si on a trouvé un "manager" libre
|
|
|
|
if( index != -1 ){
|
|
|
|
|
2017-04-09 17:33:27 +00:00
|
|
|
// Construction arguments thread
|
2017-05-03 13:45:19 +00:00
|
|
|
struct handler_arg thread_args = { TCPManagers, UDPManagers, activeTCPManagers, activeUDPManagers, CLIENT_SOCKET, &sgca };
|
2017-04-09 17:33:27 +00:00
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* 5. On lance un thread pour le traitement de ce client */
|
2017-04-09 17:33:27 +00:00
|
|
|
pthread_create(&TCPManagers[index], NULL, arg->handler, (void*) &thread_args);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&THR ) printf("{tcp_listn}{com}(%d) started\n", index);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
/* 6. On signale que ce "manager" est maintenant actif */
|
|
|
|
activeTCPManagers[index] = 1;
|
2017-03-28 16:45:18 +00:00
|
|
|
|
|
|
|
}else
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&THR ) printf("{tcp_listn} No available thread\n");
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-03-28 16:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On attends la fin de tous les THREADS
|
|
|
|
==========================================================*/
|
2017-04-01 15:01:36 +00:00
|
|
|
for( i = 0 ; i < MAX_TCP_THR ; i++ )
|
|
|
|
pthread_join(TCPManagers[i], NULL);
|
|
|
|
|
2017-04-07 16:14:21 +00:00
|
|
|
|
2017-03-28 16:45:18 +00:00
|
|
|
/* [4] On ferme la SOCKET d'écoute globale
|
|
|
|
==========================================================*/
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("{tcp_listn} Closing listener socket\n");
|
2017-03-28 16:45:18 +00:00
|
|
|
close(LISTENSOCK);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
return NULL;
|
2017-03-28 16:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-03 16:04:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* Attente de connection UDP
|
|
|
|
*
|
|
|
|
* @history
|
|
|
|
* [0] Initialisation des variables
|
|
|
|
* [1] On démarre le SERVEUR UDP d'écoute globale
|
|
|
|
* [2] On attends un client
|
|
|
|
* [3] On gère la requête
|
2017-04-07 18:25:53 +00:00
|
|
|
* 1. On parse la requête
|
|
|
|
* 2. Si demande de socket de communication
|
|
|
|
* 1. Création socket port random
|
|
|
|
* 2. On récupère le port en question
|
|
|
|
* 3. On envoie la réponse
|
|
|
|
* [4] On envoie la réponse
|
|
|
|
* [5] On démarre un thread de gestion
|
|
|
|
* [N] On ferme la SOCKET d'écoute globale
|
2017-04-01 15:01:36 +00:00
|
|
|
*
|
|
|
|
*/
|
2017-04-09 17:33:27 +00:00
|
|
|
void* LISTEN_UDP(void* THREADABLE_ARGS){
|
2017-04-01 15:01:36 +00:00
|
|
|
/* [0] Initialisation des variables
|
|
|
|
==========================================================*/
|
2017-04-11 17:12:34 +00:00
|
|
|
/* 1. On initialise les variables */
|
2017-04-26 12:25:05 +00:00
|
|
|
int CLIENT_SOCKET; // contiendra la socket UDP à envoyer sur un THREAD
|
|
|
|
struct sockaddr_in listenInfo; // contiendra les infos de la socket LISTEN
|
|
|
|
int i, index; // compteurs
|
|
|
|
char entity[10]; // identifiant pour debug
|
|
|
|
int returned; // Contrôle d'exécution middleware
|
|
|
|
struct middleware_arg marg; // paramètres pour middleware
|
|
|
|
struct in_addr ip; // Permet d'afficher les IP en dot notation
|
2017-04-11 17:12:34 +00:00
|
|
|
|
|
|
|
/* 2. On parse les arguments */
|
|
|
|
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
2017-04-09 14:06:31 +00:00
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
if( arg->port == UDP_VTER ) strcpy(entity, "udp_vterm");
|
|
|
|
if( arg->port == UDP_CTER ) strcpy(entity, "udp_cterm");
|
|
|
|
if( arg->port == UDP_MCST ) strcpy(entity, "udp_mcast");
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
// retour de @DROP_UDP_SERVER
|
|
|
|
int SOCKET;
|
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
ip.s_addr = arg->addr;
|
|
|
|
|
2017-04-07 18:25:53 +00:00
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* [1] On démarre le SERVEUR UDP d'écoute globale
|
|
|
|
==========================================================*/
|
2017-04-28 12:54:02 +00:00
|
|
|
/* 1. On crée la socket */
|
2017-04-26 12:25:05 +00:00
|
|
|
if( DROP_UDP_SERVER(arg->addr, arg->port, &SOCKET, &listenInfo, 1) < 0 ){
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&SCK ) printf("{%s} Error creating listener socket\n", entity);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
// On ferme la SOCKET d'écoute globale
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("{%s} Closing listener socket!\n", entity);
|
2017-04-01 15:01:36 +00:00
|
|
|
close(SOCKET);
|
2017-03-28 16:45:18 +00:00
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-28 16:45:18 +00:00
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
/* 2. On définit un timeout d'envoi */
|
|
|
|
setTimeout(SOCKET, SOCK_TIMEOUT, TIMEOUT_SEND);
|
|
|
|
if( DEBUGMOD&SCK ) printf("{%s} SEND timeout set to %d\n", entity, SOCK_TIMEOUT);
|
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
printf("{%s} listen on %s:%d\n", entity, inet_ntoa(ip), arg->port);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Attente de connection
|
|
|
|
============================================================================*/
|
|
|
|
while( 1 ){
|
2017-03-28 16:45:18 +00:00
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
/* On initialise les SOCKET en attendant la connexion d'un client */
|
2017-04-06 11:36:51 +00:00
|
|
|
CLIENT_SOCKET = -1;
|
|
|
|
index = -1;
|
2017-03-28 16:45:18 +00:00
|
|
|
|
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
/* [2] Appel middleware si existe
|
2017-04-07 18:25:53 +00:00
|
|
|
=========================================================*/
|
2017-04-11 17:12:34 +00:00
|
|
|
if( arg->middleware != NULL ){
|
2017-04-06 15:41:34 +00:00
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
/* 1. Construction des arguments */
|
2017-04-28 12:54:02 +00:00
|
|
|
marg.listenSock = SOCKET;
|
|
|
|
marg.comSock = &CLIENT_SOCKET;
|
2017-04-11 17:12:34 +00:00
|
|
|
strcpy(marg.entity, entity);
|
2017-04-06 15:41:34 +00:00
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
/* 2. Exécution middleware */
|
|
|
|
returned = -1;
|
|
|
|
returned = (*arg->middleware)( &marg );
|
2017-04-06 15:41:34 +00:00
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
/* 3. Gestion erreur FATALE */
|
|
|
|
if( returned == -2 )
|
|
|
|
break;
|
2017-04-06 15:41:34 +00:00
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
/* 4. Gestion erreur non FATALE */
|
|
|
|
if( returned == -1 )
|
|
|
|
continue;
|
2017-04-06 15:41:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-07 18:25:53 +00:00
|
|
|
|
2017-04-06 15:41:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-11 17:12:34 +00:00
|
|
|
/* [5] On démarre la tache sur un thread dédié -> si handler
|
2017-04-07 18:25:53 +00:00
|
|
|
=========================================================*/
|
2017-04-11 17:12:34 +00:00
|
|
|
if( arg->handler == NULL )
|
2017-04-06 15:41:34 +00:00
|
|
|
continue;
|
2017-04-06 11:36:51 +00:00
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-07 18:25:53 +00:00
|
|
|
/* 1. On cherche un "manager" libre (inactif) */
|
2017-04-06 11:36:51 +00:00
|
|
|
for( i = 0 ; i < MAX_UDP_THR ; i++ )
|
|
|
|
if( activeUDPManagers[i] == 0 ){ index = i; break; }
|
|
|
|
|
2017-04-07 18:25:53 +00:00
|
|
|
/* 2. si on a trouvé un "manager" libre */
|
2017-04-06 11:36:51 +00:00
|
|
|
if( index != -1 ){
|
|
|
|
|
2017-04-09 17:33:27 +00:00
|
|
|
// Construction arguments thread
|
2017-05-03 13:45:19 +00:00
|
|
|
|
|
|
|
struct handler_arg thread_args = { TCPManagers, UDPManagers, activeTCPManagers, activeUDPManagers, CLIENT_SOCKET, &sgca };
|
2017-04-09 17:33:27 +00:00
|
|
|
|
2017-04-07 18:25:53 +00:00
|
|
|
/* 2.1. On lance un thread pour le traitement de ce client */
|
2017-04-09 17:33:27 +00:00
|
|
|
pthread_create(&UDPManagers[index], NULL, arg->handler, (void*) &thread_args);
|
2017-04-06 11:36:51 +00:00
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&THR ) printf("{%s}{com}(%d) started\n", entity, index);
|
2017-04-06 11:36:51 +00:00
|
|
|
|
2017-04-07 18:25:53 +00:00
|
|
|
/* 2.2. On signale que ce "manager" est maintenant actif */
|
2017-04-06 11:36:51 +00:00
|
|
|
activeUDPManagers[index] = 1;
|
|
|
|
|
|
|
|
}else
|
2017-04-28 12:54:02 +00:00
|
|
|
if( DEBUGMOD&THR ) printf("{%s} No available thread\n", entity);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [n] On ferme la SOCKET d'écoute globale
|
|
|
|
==========================================================*/
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("{%s} Closing listener socket\n", entity);
|
2017-04-01 15:01:36 +00:00
|
|
|
close(SOCKET);
|
|
|
|
|
2017-04-18 18:06:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Publication de la socket TCP sur un groupe multicast
|
|
|
|
*
|
|
|
|
* @history
|
|
|
|
* [0] Initialisation des variables
|
|
|
|
* [1] On initialise la socket CLIENT UDP
|
|
|
|
* [2] On construit la requête à envoyer
|
|
|
|
* @loop
|
|
|
|
* [3] On attends un délai fixé (2s par défaut)
|
|
|
|
* [4] On envoie les information de la socket TCP
|
|
|
|
* [N] On ferme la SOCKET
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void* MCAST_PUBLISH(void* THREADABLE_ARGS){
|
|
|
|
/* [0] Initialisation des variables
|
|
|
|
==========================================================*/
|
|
|
|
/* 1. On initialise les variables */
|
|
|
|
struct sockaddr_in targetInfo; // contiendra les infos de la cible de la socket client
|
|
|
|
char buffer[MAX_BUF_LEN]; // buffer de requêtes (envois)
|
|
|
|
struct bind_header request; // requête brute
|
2017-04-26 12:25:05 +00:00
|
|
|
struct in_addr ip; // Permet d'afficher les IP en dot notation
|
2017-04-18 18:06:14 +00:00
|
|
|
|
|
|
|
/* 2. On parse les arguments */
|
|
|
|
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
|
|
|
|
|
|
|
// retour de @UDP_SOCKET
|
|
|
|
int SOCKET;
|
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
ip.s_addr = arg->addr;
|
|
|
|
|
2017-04-18 18:06:14 +00:00
|
|
|
|
|
|
|
/* [1] On initialise la socket CLIENT UDP
|
|
|
|
==========================================================*/
|
|
|
|
if( UDP_SOCKET(&SOCKET, arg->addr, arg->port, &targetInfo) < 0 ){
|
|
|
|
|
|
|
|
if( DEBUGMOD&SCK ) printf("{udp_mcast} Erreur de création socket UDP client\n");
|
|
|
|
|
|
|
|
// On ferme la SOCKET d'écoute globale
|
|
|
|
printf("{udp_mcast} FERMETURE SOCKET CLIENT UDP!\n");
|
|
|
|
close(SOCKET);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
printf("{udp_mcast} bound to %s:%d\n", inet_ntoa(ip), arg->port);
|
2017-04-18 18:06:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] On construit la requête à envoyer
|
|
|
|
=========================================================*/
|
|
|
|
/* 1. Initialisation du buffer */
|
|
|
|
bzero(buffer, MAX_BUF_LEN*sizeof(char));
|
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
/* 2. On récupère l'IP locale */
|
|
|
|
struct in_addr* servip = GET_LOCAL_IP();
|
|
|
|
if( servip == NULL ){
|
|
|
|
printf("{udp_mcast} /!\\ Cannot fetch local ip address. Aborting!\n");// On ferme la SOCKET d'écoute globale
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("{udp_mcast} Closing publisher socket!\n");
|
2017-04-26 12:25:05 +00:00
|
|
|
close(SOCKET);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3. Remplissage de la requête brute */
|
2017-04-18 18:06:14 +00:00
|
|
|
request.flags = BINDHEAD_TCP;
|
2017-04-26 12:25:05 +00:00
|
|
|
request.addr = htonl(servip->s_addr);
|
2017-04-18 18:06:14 +00:00
|
|
|
request.port = htons(TCP_LIST);
|
|
|
|
|
2017-04-26 12:25:05 +00:00
|
|
|
/* 4. Copie dans le buffer */
|
|
|
|
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) );
|
2017-04-18 18:06:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
while( 1 ){
|
|
|
|
|
|
|
|
/* [3] On attends un délai fixé (2s par défaut)
|
|
|
|
=========================================================*/
|
|
|
|
sleep(PUBL_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] On envoie les information de la socket TCP
|
|
|
|
=========================================================*/
|
|
|
|
if( sendto(SOCKET, buffer, BINDHDR_LEN / sizeof(char) + 1, 0, (struct sockaddr*) &targetInfo, sizeof(struct sockaddr_in)) < 0 ){
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("{udp_mcast} Cannot publish credentials\n");
|
2017-04-18 18:06:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [n] On ferme la SOCKET CLIENT UDP
|
|
|
|
==========================================================*/
|
2017-04-28 12:54:02 +00:00
|
|
|
printf("{udp_mcast} Closing publisher socket\n");
|
2017-04-18 18:06:14 +00:00
|
|
|
close(SOCKET);
|
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
return NULL;
|
2017-04-09 17:33:27 +00:00
|
|
|
}
|