[BIG UPDATE] Dynamic local ip
This commit is contained in:
parent
48f2b33728
commit
79a1258a53
|
@ -21,10 +21,13 @@ lib/local/middleware.o: lib/header.h lib/local/middleware.h lib/local/middleware
|
||||||
lib/local/handler.o: lib/header.h lib/local/handler.h lib/local/handler.c
|
lib/local/handler.o: lib/header.h lib/local/handler.h lib/local/handler.c
|
||||||
gcc $(CFLAGS) -c -o lib/local/handler.o lib/local/handler.c
|
gcc $(CFLAGS) -c -o lib/local/handler.o lib/local/handler.c
|
||||||
|
|
||||||
|
lib/network/common.o: lib/header.h lib/network/common.h lib/network/common.c
|
||||||
|
gcc $(CFLAGS) -c -o lib/network/common.o lib/network/common.c
|
||||||
|
|
||||||
|
|
||||||
# Compiles the SGCA
|
# Compiles the SGCA
|
||||||
boot: lib/network/tcp/server.o lib/network/udp/server.o lib/network/udp/client.o lib/local/middleware.o lib/local/handler.o central-manager.h central-manager.c
|
boot: lib/network/common.o lib/network/tcp/server.o lib/network/udp/server.o lib/network/udp/client.o lib/local/middleware.o lib/local/handler.o central-manager.h central-manager.c
|
||||||
gcc $(CFLAGS) -o boot lib/network/udp/server.o lib/network/udp/client.o lib/network/tcp/server.o lib/local/middleware.o lib/local/handler.o central-manager.c
|
gcc $(CFLAGS) -o boot lib/network/udp/server.o lib/network/udp/client.o lib/network/common.o lib/network/tcp/server.o lib/local/middleware.o lib/local/handler.o central-manager.c
|
||||||
|
|
||||||
|
|
||||||
# Run full compilation
|
# Run full compilation
|
||||||
|
@ -32,6 +35,8 @@ all: boot
|
||||||
|
|
||||||
# cleans the compiled files
|
# cleans the compiled files
|
||||||
clean:
|
clean:
|
||||||
rm boot;
|
rm lib/network/*.o;
|
||||||
|
rm lib/network/**/*.o;
|
||||||
rm lib/network/**/*.o;
|
rm lib/network/**/*.o;
|
||||||
rm lib/local/*.o;
|
rm lib/local/*.o;
|
||||||
|
rm boot;
|
||||||
|
|
|
@ -17,7 +17,8 @@
|
||||||
* [0] Initialisation des variables
|
* [0] Initialisation des variables
|
||||||
* [1] Lancement des THREADS d'écoute
|
* [1] Lancement des THREADS d'écoute
|
||||||
* 1. On démarre le SERVEUR TCP d'écoute globale
|
* 1. On démarre le SERVEUR TCP d'écoute globale
|
||||||
* 2. On démarre le SERVEUR UDP d'écoute globale
|
* 2. On récupère l'IP locale
|
||||||
|
* 3. On démarre le SERVEUR UDP d'écoute globale
|
||||||
* [2] On attends la fin de tous les THREADS
|
* [2] On attends la fin de tous les THREADS
|
||||||
* [3] On libère les variables globale
|
* [3] On libère les variables globale
|
||||||
*
|
*
|
||||||
|
@ -35,12 +36,22 @@ int main(int argc, char* argv[]){
|
||||||
/* 1. Variables globales */
|
/* 1. Variables globales */
|
||||||
sgca.n = 0;
|
sgca.n = 0;
|
||||||
sgca.data = (struct plane*) malloc( sizeof(struct plane) );
|
sgca.data = (struct plane*) malloc( sizeof(struct plane) );
|
||||||
|
struct in_addr* SERV_HOST;
|
||||||
|
|
||||||
/* 2. Variables locales */
|
/* 2. On récupère l'IP locale' */
|
||||||
struct listen_arg tcp_listn_arg = { SERV_HOST, TCP_LIST, NULL, &managePlane };
|
SERV_HOST = GET_LOCAL_IP();
|
||||||
struct listen_arg udp_mcast_arg = { MCST_HOST, UDP_MCST, NULL, NULL };
|
if( SERV_HOST == NULL ){
|
||||||
struct listen_arg udp_vterm_arg = { MCST_VTER, UDP_VTER, &multicastTerminal, &manageViewTerm };
|
printf("/!\\ Cannot fetch local ip address. Aborting!\n");
|
||||||
struct listen_arg udp_cterm_arg = { MCST_CTER, UDP_CTER, &multicastTerminal, &manageCtrlTerm };
|
free(sgca.data);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
printf("LOCAL IP: %s\n", inet_ntoa(*SERV_HOST));
|
||||||
|
|
||||||
|
/* 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 };
|
||||||
|
|
||||||
|
|
||||||
/* [1] Lancement des THREADS d'écoute
|
/* [1] Lancement des THREADS d'écoute
|
||||||
|
@ -71,6 +82,8 @@ int main(int argc, char* argv[]){
|
||||||
/* [3] On libère les variables globales
|
/* [3] On libère les variables globales
|
||||||
==========================================================*/
|
==========================================================*/
|
||||||
free(sgca.data);
|
free(sgca.data);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -96,7 +109,9 @@ void* LISTEN_TCP(void* THREADABLE_ARGS){
|
||||||
socklen_t len; // taille de la socket
|
socklen_t len; // taille de la socket
|
||||||
int index, i; // compteurs
|
int index, i; // compteurs
|
||||||
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
||||||
|
struct in_addr ip; // Pour afficher l'IP en dot notation
|
||||||
|
|
||||||
|
ip.s_addr = arg->addr;
|
||||||
|
|
||||||
// retour de @DROP_TCP_SERVER
|
// retour de @DROP_TCP_SERVER
|
||||||
int LISTENSOCK; // contiendra la socket d'écoute TCP
|
int LISTENSOCK; // contiendra la socket d'écoute TCP
|
||||||
|
@ -117,7 +132,7 @@ void* LISTEN_TCP(void* THREADABLE_ARGS){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
printf("{tcp_listn} listen on %s:%d\n", arg->addr, arg->port);
|
printf("{tcp_listn} listen on %s:%d\n", inet_ntoa(ip), arg->port);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -212,22 +227,24 @@ void* LISTEN_UDP(void* THREADABLE_ARGS){
|
||||||
char entity[10]; // identifiant pour debug
|
char entity[10]; // identifiant pour debug
|
||||||
int returned; // Contrôle d'exécution middleware
|
int returned; // Contrôle d'exécution middleware
|
||||||
struct middleware_arg marg; // paramètres pour middleware
|
struct middleware_arg marg; // paramètres pour middleware
|
||||||
|
struct in_addr ip; // Permet d'afficher les IP en dot notation
|
||||||
|
|
||||||
/* 2. On parse les arguments */
|
/* 2. On parse les arguments */
|
||||||
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
||||||
|
|
||||||
if( strcmp(arg->addr, MCST_VTER) == 0 && arg->port == UDP_VTER ) strcpy(entity, "udp_vterm");
|
if( arg->port == UDP_VTER ) strcpy(entity, "udp_vterm");
|
||||||
if( strcmp(arg->addr, MCST_CTER) == 0 && arg->port == UDP_CTER ) strcpy(entity, "udp_cterm");
|
if( arg->port == UDP_CTER ) strcpy(entity, "udp_cterm");
|
||||||
if( strcmp(arg->addr, MCST_HOST) == 0 && arg->port == UDP_MCST ) strcpy(entity, "udp_mcast");
|
if( arg->port == UDP_MCST ) strcpy(entity, "udp_mcast");
|
||||||
|
|
||||||
// retour de @DROP_UDP_SERVER
|
// retour de @DROP_UDP_SERVER
|
||||||
int SOCKET;
|
int SOCKET;
|
||||||
|
|
||||||
|
ip.s_addr = arg->addr;
|
||||||
|
|
||||||
|
|
||||||
/* [1] On démarre le SERVEUR UDP d'écoute globale
|
/* [1] On démarre le SERVEUR UDP d'écoute globale
|
||||||
==========================================================*/
|
==========================================================*/
|
||||||
if( DROP_UDP_SERVER(arg->addr, arg->port, &SOCKET, &listenInfo, strcmp(arg->addr, SERV_HOST) != 0) < 0 ){
|
if( DROP_UDP_SERVER(arg->addr, arg->port, &SOCKET, &listenInfo, 1) < 0 ){
|
||||||
|
|
||||||
if( DEBUGMOD&SCK ) printf("{%s} Erreur de création socket d'écoute\n", entity);
|
if( DEBUGMOD&SCK ) printf("{%s} Erreur de création socket d'écoute\n", entity);
|
||||||
|
|
||||||
|
@ -238,7 +255,7 @@ void* LISTEN_UDP(void* THREADABLE_ARGS){
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("{%s} listen on %s:%d\n", entity, arg->addr, arg->port);
|
printf("{%s} listen on %s:%d\n", entity, inet_ntoa(ip), arg->port);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -342,6 +359,7 @@ void* MCAST_PUBLISH(void* THREADABLE_ARGS){
|
||||||
struct sockaddr_in targetInfo; // contiendra les infos de la cible de la socket client
|
struct sockaddr_in targetInfo; // contiendra les infos de la cible de la socket client
|
||||||
char buffer[MAX_BUF_LEN]; // buffer de requêtes (envois)
|
char buffer[MAX_BUF_LEN]; // buffer de requêtes (envois)
|
||||||
struct bind_header request; // requête brute
|
struct bind_header request; // requête brute
|
||||||
|
struct in_addr ip; // Permet d'afficher les IP en dot notation
|
||||||
|
|
||||||
/* 2. On parse les arguments */
|
/* 2. On parse les arguments */
|
||||||
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
||||||
|
@ -349,6 +367,8 @@ void* MCAST_PUBLISH(void* THREADABLE_ARGS){
|
||||||
// retour de @UDP_SOCKET
|
// retour de @UDP_SOCKET
|
||||||
int SOCKET;
|
int SOCKET;
|
||||||
|
|
||||||
|
ip.s_addr = arg->addr;
|
||||||
|
|
||||||
|
|
||||||
/* [1] On initialise la socket CLIENT UDP
|
/* [1] On initialise la socket CLIENT UDP
|
||||||
==========================================================*/
|
==========================================================*/
|
||||||
|
@ -363,7 +383,7 @@ void* MCAST_PUBLISH(void* THREADABLE_ARGS){
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("{udp_mcast} bound to %s:%d\n", arg->addr, arg->port);
|
printf("{udp_mcast} bound to %s:%d\n", inet_ntoa(ip), arg->port);
|
||||||
|
|
||||||
|
|
||||||
/* [2] On construit la requête à envoyer
|
/* [2] On construit la requête à envoyer
|
||||||
|
@ -371,15 +391,24 @@ void* MCAST_PUBLISH(void* THREADABLE_ARGS){
|
||||||
/* 1. Initialisation du buffer */
|
/* 1. Initialisation du buffer */
|
||||||
bzero(buffer, MAX_BUF_LEN*sizeof(char));
|
bzero(buffer, MAX_BUF_LEN*sizeof(char));
|
||||||
|
|
||||||
/* 2. Remplissage de la requête brute */
|
/* 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
|
||||||
|
printf("{udp_mcast} FERMETURE SOCKET CLIENT UDP!\n");
|
||||||
|
close(SOCKET);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. Remplissage de la requête brute */
|
||||||
request.flags = BINDHEAD_TCP;
|
request.flags = BINDHEAD_TCP;
|
||||||
strcpy(request.addr, SERV_HOST);
|
request.addr = htonl(servip->s_addr);
|
||||||
request.port = htons(TCP_LIST);
|
request.port = htons(TCP_LIST);
|
||||||
|
|
||||||
/* 3. Copie dans le buffer */
|
/* 4. Copie dans le buffer */
|
||||||
memcpy(buffer, &request.flags, sizeof(char));
|
memcpy(buffer, &request.flags, sizeof(char) );
|
||||||
memcpy(buffer+sizeof(char), &request.addr, sizeof(char)*INET_ADDRSTRLEN);
|
memcpy(buffer+sizeof(char), &request.addr, sizeof(in_addr_t) );
|
||||||
memcpy(buffer+sizeof(char)*(INET_ADDRSTRLEN+1), &request.port, sizeof(unsigned short));
|
memcpy(buffer+sizeof(char)+sizeof(in_addr_t), &request.port, sizeof(unsigned short) );
|
||||||
|
|
||||||
|
|
||||||
while( 1 ){
|
while( 1 ){
|
||||||
|
|
|
@ -8,10 +8,12 @@
|
||||||
* doowap31@gmail.com *
|
* doowap31@gmail.com *
|
||||||
**************************/
|
**************************/
|
||||||
#include "lib/header.h"
|
#include "lib/header.h"
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* local dependencies */
|
/* local dependencies */
|
||||||
|
#include "lib/network/common.h"
|
||||||
#include "lib/network/tcp/server.h"
|
#include "lib/network/tcp/server.h"
|
||||||
#include "lib/network/udp/server.h"
|
#include "lib/network/udp/server.h"
|
||||||
#include "lib/network/udp/client.h"
|
#include "lib/network/udp/client.h"
|
||||||
|
|
|
@ -40,14 +40,14 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) )
|
#define BINDHDR_LEN ( sizeof(char)+sizeof(in_addr_t)+sizeof(unsigned short) )
|
||||||
#define BINDHEAD_UDP 0x01 // is terminal (ask for UDP socket)
|
#define BINDHEAD_UDP 0x01 // is terminal (ask for UDP socket)
|
||||||
#define BINDHEAD_TCP 0x02 // is plane (ask for TCP socket)
|
#define BINDHEAD_TCP 0x02 // is plane (ask for TCP socket)
|
||||||
#define BINDHEAD_CTL 0x04 // is ctrlTerm (else: viewTerm)
|
#define BINDHEAD_CTL 0x04 // is ctrlTerm (else: viewTerm)
|
||||||
|
|
||||||
struct bind_header{
|
struct bind_header{
|
||||||
char flags;
|
char flags;
|
||||||
char addr[INET_ADDRSTRLEN];
|
in_addr_t addr;
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
#define BUF 0x04 // FILTRE pour BUFFERS
|
#define BUF 0x04 // FILTRE pour BUFFERS
|
||||||
#define HDR 0x08 // FILTRE pour HEADERS
|
#define HDR 0x08 // FILTRE pour HEADERS
|
||||||
#define THR 0x10 // FILTRE pour THREADS
|
#define THR 0x10 // FILTRE pour THREADS
|
||||||
#define COM 0x20 // debug des COMMUNICATIONS
|
#define COM 0x20 // FILTRE pour COMMUNICATIONS
|
||||||
|
|
||||||
// possibilité de cumuler les DEBUGMODES
|
// possibilité de cumuler les DEBUGMODES
|
||||||
#define DEBUGMOD ( SOCKETS | THREADS | BUFFERS ) // REVEALS + HEADER + THREADS
|
#define DEBUGMOD ( SOCKETS | THREADS | BUFFERS ) // REVEALS + HEADER + THREADS
|
|
@ -36,7 +36,6 @@
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
|
|
||||||
/* vars */
|
/* vars */
|
||||||
#define SERV_HOST "127.0.0.1" // adresse serveur
|
|
||||||
#define MCST_HOST "224.0.0.1" // adresse groupe multicast UDP -> plane
|
#define MCST_HOST "224.0.0.1" // adresse groupe multicast UDP -> plane
|
||||||
#define MCST_VTER "224.0.0.2" // adresse groupe multicast UDP -> viewTerm
|
#define MCST_VTER "224.0.0.2" // adresse groupe multicast UDP -> viewTerm
|
||||||
#define MCST_CTER "224.0.0.3" // adresse groupe multicast UDP -> ctrlTerm
|
#define MCST_CTER "224.0.0.3" // adresse groupe multicast UDP -> ctrlTerm
|
||||||
|
@ -75,7 +74,7 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
struct listen_arg{
|
struct listen_arg{
|
||||||
const char addr[16]; // socket address
|
in_addr_t addr; // socket address
|
||||||
const unsigned short port; // socket port
|
const unsigned short port; // socket port
|
||||||
int (*middleware)(struct middleware_arg*); // middleware function
|
int (*middleware)(struct middleware_arg*); // middleware function
|
||||||
void* (*handler)(void*); // management function (thread)
|
void* (*handler)(void*); // management function (thread)
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
int multicastTerminal(struct middleware_arg* arg){
|
int multicastTerminal(struct middleware_arg* arg){
|
||||||
/* [0] Initialisation
|
/* [0] Initialisation
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
/* 1. Initialisation des variables */
|
||||||
struct sockaddr_in comInfo; // contiendra les infos de la socket COM
|
struct sockaddr_in comInfo; // contiendra les infos de la socket COM
|
||||||
struct sockaddr_in clientInfo; // contiendra les infos client
|
struct sockaddr_in clientInfo; // contiendra les infos client
|
||||||
socklen_t len; // taille de la socket
|
socklen_t len; // taille de la socket
|
||||||
|
@ -23,19 +24,24 @@ int multicastTerminal(struct middleware_arg* arg){
|
||||||
char buffer[MAX_BUF_LEN+1]; // buffer requête
|
char buffer[MAX_BUF_LEN+1]; // buffer requête
|
||||||
struct bind_header request; // requête parsée
|
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 */
|
||||||
/* 1. On attends une connection UDP */
|
|
||||||
len = sizeof(struct sockaddr_in);
|
len = sizeof(struct sockaddr_in);
|
||||||
read = recvfrom(arg->listenSock, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
|
read = recvfrom(arg->listenSock, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
|
||||||
|
|
||||||
/* 2. Si erreur reception ou taille incorrecte -> retour à l'écoute */
|
/* 4. Si erreur reception ou taille incorrecte -> retour à l'écoute */
|
||||||
if( read < BINDHDR_LEN ){
|
if( read < BINDHDR_LEN ){
|
||||||
if( DEBUGMOD&BUF ) printf("{%s} read('%s') = %d bytes (expected: %d)\n", arg->entity, buffer, read, (int) (BINDHDR_LEN) );
|
if( DEBUGMOD&BUF ) printf("{%s} read('%s') = %d bytes (expected: %d)\n", arg->entity, buffer, read, (int) (BINDHDR_LEN) );
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 3. On récupère l'adresse IP du client */
|
/* 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));
|
if( DEBUGMOD&SCK ) printf("{%s} %s:%d connecté\n", arg->entity, inet_ntoa(clientInfo.sin_addr), ntohs(clientInfo.sin_port));
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,7 +52,7 @@ int multicastTerminal(struct middleware_arg* arg){
|
||||||
memcpy(&request.flags, buffer, sizeof(char));
|
memcpy(&request.flags, buffer, sizeof(char));
|
||||||
memcpy(&request.addr, buffer+sizeof(char), sizeof(char)*15);
|
memcpy(&request.addr, buffer+sizeof(char), sizeof(char)*15);
|
||||||
memcpy(&request.port, buffer+sizeof(char)*16, sizeof(unsigned short));
|
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);
|
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) */
|
/* 2. Si mauvais flag (avion) */
|
||||||
if( request.flags&BINDHEAD_TCP || !(request.flags&BINDHEAD_UDP) ){
|
if( request.flags&BINDHEAD_TCP || !(request.flags&BINDHEAD_UDP) ){
|
||||||
|
@ -75,7 +81,7 @@ int multicastTerminal(struct middleware_arg* arg){
|
||||||
/* (1) Création socket de communication (si demandé)
|
/* (1) Création socket de communication (si demandé)
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
/* 1. On bind une socket sur un port random */
|
/* 1. On bind une socket sur un port random */
|
||||||
if( DROP_UDP_SERVER(SERV_HOST, 0, arg->comSock, &comInfo, 0) < 0 ){
|
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);
|
if( DEBUGMOD&SCK ) printf("{%s} Erreur de création de la socket COM\n", arg->entity);
|
||||||
|
|
||||||
|
@ -100,9 +106,9 @@ int multicastTerminal(struct middleware_arg* arg){
|
||||||
|
|
||||||
/* 3. Si on a le port -> on le met dans la reponse */
|
/* 3. Si on a le port -> on le met dans la reponse */
|
||||||
}else{
|
}else{
|
||||||
strcpy(request.addr, SERV_HOST);
|
request.addr = SERV_IP->s_addr;
|
||||||
request.port = comInfo.sin_port;
|
request.port = comInfo.sin_port;
|
||||||
if( DEBUGMOD&SCK ) printf("{%s}{com} socket opened on %s:%d\n", arg->entity, request.addr, ntohs(request.port));
|
if( DEBUGMOD&SCK ) printf("{%s}{com} socket opened on %s:%d\n", arg->entity, inet_ntoa(*SERV_IP), ntohs(request.port));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,7 +118,7 @@ int multicastTerminal(struct middleware_arg* arg){
|
||||||
/* 1. On sérialise la réponse */
|
/* 1. On sérialise la réponse */
|
||||||
bzero(buffer, MAX_BUF_LEN*sizeof(char));
|
bzero(buffer, MAX_BUF_LEN*sizeof(char));
|
||||||
memcpy(buffer, &request.flags, sizeof(char));
|
memcpy(buffer, &request.flags, sizeof(char));
|
||||||
memcpy(buffer+sizeof(char), &request.addr, sizeof(char)*15);
|
memcpy(buffer+sizeof(char), &request.addr, sizeof(in_addr_t));
|
||||||
memcpy(buffer+sizeof(char)*16, &request.port, sizeof(unsigned short));
|
memcpy(buffer+sizeof(char)*16, &request.port, sizeof(unsigned short));
|
||||||
|
|
||||||
/* 2. On envoie la réponse*/
|
/* 2. On envoie la réponse*/
|
||||||
|
@ -122,7 +128,7 @@ int multicastTerminal(struct middleware_arg* arg){
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("{%s} sent: bind_header{flag = %d; addr = '%s'; port = %d}\n", arg->entity, (int) request.flags, request.addr, ntohs(request.port));
|
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;
|
return 0;
|
||||||
|
|
|
@ -15,8 +15,8 @@
|
||||||
|
|
||||||
#include "../header.h"
|
#include "../header.h"
|
||||||
#include "../network/udp/server.h"
|
#include "../network/udp/server.h"
|
||||||
|
#include "../network/common.h"
|
||||||
|
|
||||||
// int multicastPlanes(struct middleware_arg* ARGS);
|
|
||||||
int multicastTerminal(struct middleware_arg* ARGS);
|
int multicastTerminal(struct middleware_arg* ARGS);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
/**************************
|
||||||
|
* Network Common Depend. *
|
||||||
|
***************************
|
||||||
|
* Designed & Developed by *
|
||||||
|
* Adrien Marquès *
|
||||||
|
* <xdrm-brackets> *
|
||||||
|
***************************
|
||||||
|
* doowap31@gmail.com *
|
||||||
|
**************************/
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Retourne l'adresse IP du serveur (première interface excepté "lo")
|
||||||
|
*
|
||||||
|
* @history
|
||||||
|
* [0] Initialisation des variables
|
||||||
|
* [1] On récupère la liste des interfaces
|
||||||
|
* [2] Parcourt des interfaces
|
||||||
|
* 1. Si "lo" -> suivant
|
||||||
|
* 2. Si pas IPV4 -> suivant
|
||||||
|
* 3. Si ok, on retourne l'ip
|
||||||
|
* [3] Cas échéant -> aucun résultat (NULL)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
struct in_addr* GET_LOCAL_IP(){
|
||||||
|
/* [0] Initialisation des variables
|
||||||
|
=========================================================*/
|
||||||
|
struct ifaddrs *list; // list of interfaces
|
||||||
|
struct ifaddrs *it; // interface iterator
|
||||||
|
struct sockaddr_in *info; // interface info
|
||||||
|
struct in_addr *result; // ip trouvée
|
||||||
|
result = malloc( sizeof(struct in_addr) );
|
||||||
|
|
||||||
|
|
||||||
|
/* [1] On récupère la liste des interfaces
|
||||||
|
=========================================================*/
|
||||||
|
getifaddrs(&list);
|
||||||
|
|
||||||
|
|
||||||
|
/* [2] Parcourt des interfaces
|
||||||
|
=========================================================*/
|
||||||
|
for( it = list ; it ; it = it->ifa_next ){
|
||||||
|
|
||||||
|
/* 1. Si "lo" -> suivant */
|
||||||
|
if( strcmp(it->ifa_name, "lo") == 0 )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* 2. Si pas IPV4 -> suivant */
|
||||||
|
if( it->ifa_addr->sa_family != AF_INET )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* 3. Si ok, on retourne l'ip */
|
||||||
|
info = (struct sockaddr_in*) it->ifa_addr;
|
||||||
|
memcpy(result, &info->sin_addr, sizeof(struct in_addr));
|
||||||
|
freeifaddrs(list);
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* [3] Cas échéant -> aucun résultat
|
||||||
|
=========================================================*/
|
||||||
|
freeifaddrs(list);
|
||||||
|
return NULL;
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
/**************************
|
||||||
|
* Network Common Depend. *
|
||||||
|
***************************
|
||||||
|
* Designed & Developed by *
|
||||||
|
* Adrien Marquès *
|
||||||
|
* <xdrm-brackets> *
|
||||||
|
***************************
|
||||||
|
* doowap31@gmail.com *
|
||||||
|
**************************/
|
||||||
|
|
||||||
|
#ifndef _LIB_NETWORK_COMMON_H_
|
||||||
|
#define _LIB_NETWORK_COMMON_H_
|
||||||
|
|
||||||
|
#include "../header.h"
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct in_addr* GET_LOCAL_IP();
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int UDP_SOCKET(int* pSocket, const char* pAddr, const int pPort, struct sockaddr_in* pInfo){
|
int UDP_SOCKET(int* pSocket, in_addr_t pAddr, const int pPort, struct sockaddr_in* pInfo){
|
||||||
|
|
||||||
/* [0] Initialisation des variables
|
/* [0] Initialisation des variables
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -37,7 +37,7 @@ int UDP_SOCKET(int* pSocket, const char* pAddr, const int pPort, struct sockaddr
|
||||||
/* (2) On définit les infos */
|
/* (2) On définit les infos */
|
||||||
pInfo->sin_family = AF_INET;
|
pInfo->sin_family = AF_INET;
|
||||||
pInfo->sin_port = htons(pPort);
|
pInfo->sin_port = htons(pPort);
|
||||||
pInfo->sin_addr.s_addr = inet_addr(pAddr);
|
pInfo->sin_addr.s_addr = pAddr;
|
||||||
|
|
||||||
|
|
||||||
/* [n] Code succès
|
/* [n] Code succès
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
/* Créée une socket UDP + crée le sockaddr_in pour la suite
|
/* Créée une socket UDP + crée le sockaddr_in pour la suite
|
||||||
*
|
*
|
||||||
* ==IN==
|
* ==IN==
|
||||||
* @pAddr<const char*> Adresse du groupe multicast UDP
|
* @pAddr<in_addr_t> Adresse du groupe multicast UDP
|
||||||
* @pPort<const int> Port d'écoute UDP
|
* @pPort<const int> Port d'écoute UDP
|
||||||
*
|
*
|
||||||
* ==OUT==
|
* ==OUT==
|
||||||
|
@ -43,7 +43,7 @@
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int UDP_SOCKET(int* pSocket, const char* pAddr, const int pPort, struct sockaddr_in* pInfo);
|
int UDP_SOCKET(int* pSocket, in_addr_t pAddr, const int pPort, struct sockaddr_in* pInfo);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,8 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo, const char pMcast){
|
int DROP_UDP_SERVER(in_addr_t pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo, const char pMcast){
|
||||||
if( DEBUGMOD&HDR ) printf("====== DROP_UDP_MCAST(%s, %d, %p, %p, %d) ======\n\n", pAddr, pPort, (void*) pListenSock, (void*) pInfo, (int) pMcast);
|
if( DEBUGMOD&HDR ) printf("====== DROP_UDP_MCAST(%x, %d, %p, %p, %d) ======\n\n", pAddr, pPort, (void*) pListenSock, (void*) pInfo, (int) pMcast);
|
||||||
|
|
||||||
/* [0] Initialisation des variables
|
/* [0] Initialisation des variables
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -21,6 +21,7 @@ int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct
|
||||||
struct ip_mreq mcastReq;
|
struct ip_mreq mcastReq;
|
||||||
*pListenSock = -1;
|
*pListenSock = -1;
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
|
struct in_addr tmp;
|
||||||
|
|
||||||
|
|
||||||
/* [1] Création de la socket
|
/* [1] Création de la socket
|
||||||
|
@ -67,8 +68,10 @@ int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct
|
||||||
/* 3. Demande groupe multicast (si demandé) */
|
/* 3. Demande groupe multicast (si demandé) */
|
||||||
if( pMcast ){
|
if( pMcast ){
|
||||||
|
|
||||||
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] join mcast group: %s\n", pAddr);
|
tmp.s_addr = pAddr;
|
||||||
mcastReq.imr_multiaddr.s_addr = inet_addr(pAddr);
|
|
||||||
|
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] join mcast group: %s\n", inet_ntoa(tmp));
|
||||||
|
mcastReq.imr_multiaddr.s_addr = pAddr;
|
||||||
mcastReq.imr_interface.s_addr = htonl(INADDR_ANY);
|
mcastReq.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||||
|
|
||||||
if( setsockopt(*pListenSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcastReq, sizeof(mcastReq)) < 0 ){
|
if( setsockopt(*pListenSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcastReq, sizeof(mcastReq)) < 0 ){
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
/* Créé et met un serveur UDP d'écoute MULTICAST ou CLASSIQUE
|
/* Créé et met un serveur UDP d'écoute MULTICAST ou CLASSIQUE
|
||||||
*
|
*
|
||||||
* ==IN==
|
* ==IN==
|
||||||
* @pAddr<const char*> Adresse du groupe multicast UDP
|
* @pAddr<in_addr_t> Adresse du groupe multicast UDP
|
||||||
* @pPort<const int> Port d'écoute UDP
|
* @pPort<const int> Port d'écoute UDP
|
||||||
* @pMcast<const char> Si multicast ou non
|
* @pMcast<const char> Si multicast ou non
|
||||||
*
|
*
|
||||||
|
@ -38,7 +38,7 @@
|
||||||
* [n] On renvoie la socket par référence
|
* [n] On renvoie la socket par référence
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo, const char pMcast);
|
int DROP_UDP_SERVER(in_addr_t pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo, const char pMcast);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
#include "client.h"
|
#include "client.h"
|
||||||
|
|
||||||
|
|
||||||
int TCP_CONNECT(int* pSocket, const char* pAddr, const int pPort, struct sockaddr_in* pInfo){
|
int TCP_CONNECT(int* pSocket, const in_addr_t pAddr, const int pPort, struct sockaddr_in* pInfo){
|
||||||
|
|
||||||
/* [0] Initialisation des variables
|
/* [0] Initialisation des variables
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -35,7 +35,7 @@ int TCP_CONNECT(int* pSocket, const char* pAddr, const int pPort, struct sockadd
|
||||||
/* (2) On définit les infos */
|
/* (2) On définit les infos */
|
||||||
pInfo->sin_family = AF_INET;
|
pInfo->sin_family = AF_INET;
|
||||||
pInfo->sin_port = htons(pPort);
|
pInfo->sin_port = htons(pPort);
|
||||||
pInfo->sin_addr.s_addr = inet_addr(pAddr);
|
pInfo->sin_addr.s_addr = pAddr;
|
||||||
|
|
||||||
|
|
||||||
/* [3] On se connecte au serveur
|
/* [3] On se connecte au serveur
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
/* Créée une socket TCP et la connecte + crée le sockaddr_in pour la suite
|
/* Créée une socket TCP et la connecte + crée le sockaddr_in pour la suite
|
||||||
*
|
*
|
||||||
* ==IN==
|
* ==IN==
|
||||||
* @pAddr<const char*> Adresse du serveur TCP
|
* @pAddr<const in_addr_t> Adresse du serveur TCP
|
||||||
* @pPort<const int> Port du serveur TCP
|
* @pPort<const int> Port du serveur TCP
|
||||||
*
|
*
|
||||||
* ==OUT==
|
* ==OUT==
|
||||||
|
@ -34,6 +34,6 @@
|
||||||
* [3] On se connecte au serveur
|
* [3] On se connecte au serveur
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int TCP_CONNECT(int* pSocket, const char* pAddr, const int pPort, struct sockaddr_in* pInfo);
|
int TCP_CONNECT(int* pSocket, const in_addr_t pAddr, const int pPort, struct sockaddr_in* pInfo);
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -31,6 +31,7 @@ int open_communication(){
|
||||||
char buffer[MAX_BUF_LEN] = {0}; // buffer
|
char buffer[MAX_BUF_LEN] = {0}; // buffer
|
||||||
int status;
|
int status;
|
||||||
struct bind_header request;
|
struct bind_header request;
|
||||||
|
struct in_addr ip; // pour afficher l'ip en dot notation
|
||||||
|
|
||||||
/* [1] Socket Multicast UDP
|
/* [1] Socket Multicast UDP
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -65,10 +66,14 @@ int open_communication(){
|
||||||
bzero(&request, sizeof(struct bind_header));
|
bzero(&request, sizeof(struct bind_header));
|
||||||
|
|
||||||
memcpy(&request.flags, buffer, sizeof(char) );
|
memcpy(&request.flags, buffer, sizeof(char) );
|
||||||
memcpy(&request.addr, buffer+sizeof(char), sizeof(char)*INET_ADDRSTRLEN );
|
memcpy(&request.addr, buffer+sizeof(char), sizeof(in_addr_t) );
|
||||||
memcpy(&request.port, buffer+sizeof(char)*(1+INET_ADDRSTRLEN), sizeof(unsigned short) );
|
memcpy(&request.port, buffer+sizeof(char)+sizeof(in_addr_t), sizeof(unsigned short) );
|
||||||
|
|
||||||
// on indian-switch
|
// on indian-switch
|
||||||
|
printf("received : %x\n", request.addr);
|
||||||
|
request.addr = ntohl(request.addr);
|
||||||
request.port = ntohs(request.port);
|
request.port = ntohs(request.port);
|
||||||
|
ip.s_addr = request.addr;
|
||||||
|
|
||||||
/* 5. On vérifie les flags */
|
/* 5. On vérifie les flags */
|
||||||
if( !(request.flags&BINDHEAD_TCP) || request.flags&BINDHEAD_UDP ){
|
if( !(request.flags&BINDHEAD_TCP) || request.flags&BINDHEAD_UDP ){
|
||||||
|
@ -78,7 +83,7 @@ int open_communication(){
|
||||||
|
|
||||||
|
|
||||||
// printf("* hex("); for( int i = 0 ; i < BINDHDR_LEN/sizeof(char) ; i++ ) printf("\\x%02X", buffer[i]); printf(")\n");
|
// printf("* hex("); for( int i = 0 ; i < BINDHDR_LEN/sizeof(char) ; i++ ) printf("\\x%02X", buffer[i]); printf(")\n");
|
||||||
printf("* received bind_header{flags = %d; addr = %s; port = %d;}\n\n", request.flags, request.addr, request.port);
|
printf("* received bind_header{flags = %d; addr = %x/%s; port = %d;}\n\n", request.flags, request.addr, inet_ntoa(ip), request.port);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -49,14 +49,14 @@
|
||||||
int spd;
|
int spd;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) )
|
#define BINDHDR_LEN ( sizeof(char)+sizeof(in_addr_t)+sizeof(unsigned short) )
|
||||||
#define BINDHEAD_UDP 0x01 // is terminal (ask for UDP socket)
|
#define BINDHEAD_UDP 0x01 // is terminal (ask for UDP socket)
|
||||||
#define BINDHEAD_TCP 0x02 // is plane (ask for TCP socket)
|
#define BINDHEAD_TCP 0x02 // is plane (ask for TCP socket)
|
||||||
#define BINDHEAD_CTL 0x04 // is ctrlTerm (else: viewTerm)
|
#define BINDHEAD_CTL 0x04 // is ctrlTerm (else: viewTerm)
|
||||||
|
|
||||||
struct bind_header{
|
struct bind_header{
|
||||||
char flags;
|
char flags;
|
||||||
char addr[INET_ADDRSTRLEN];
|
in_addr_t addr;
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue