Refactor flags bind_header

This commit is contained in:
xdrm-brackets 2017-04-25 11:54:15 +02:00
parent b245e6f623
commit a20cc18377
4 changed files with 58 additions and 35 deletions

View File

@ -41,9 +41,9 @@
#define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) ) #define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) )
#define BINDHEAD_CTL 0x01 // is command terminal (else: view terminal) #define BINDHEAD_UDP 0x01 // is terminal (ask for UDP socket)
#define BINDHEAD_SCK 0x02 // ask for com socket (terminal) #define BINDHEAD_TCP 0x02 // is plane (ask for TCP socket)
#define BINDHEAD_TCP 0x04 // ask TCP com socket (plane) #define BINDHEAD_CTL 0x04 // is ctrlTerm (else: viewTerm)
struct bind_header{ struct bind_header{
char flags; char flags;

View File

@ -48,49 +48,65 @@ int multicastTerminal(struct middleware_arg* arg){
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 = '%s'; port = %d}\n", arg->entity, (int) request.flags, 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) Création socket de communication (si demandé)
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* 1. Si on veut un port de communicatin */ /* 1. On bind une socket sur un port random */
if( request.flags&BINDHEAD_SCK ){ if( DROP_UDP_SERVER(SERV_HOST, 0, arg->comSock, &comInfo, 0) < 0 ){
/* 2. On bind une socket sur un port random */ if( DEBUGMOD&SCK ) printf("{%s} Erreur de création de la socket COM\n", arg->entity);
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. 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.2 Si on veut on port de communication */
/* (2) Récupération port random /* (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 ){
/* 1. On récupère le port de la socket de communication */ if( DEBUGMOD&SCK ) printf("{%s} Erreur de recherche du port COM ouvert\n", arg->entity);
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);
/* 2. On ferme la socket et enleve le FLAG pour dire qu'on a pas pu ouvrir le port */ request.flags -= BINDHEAD_UDP;
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 = comInfo.sin_port;
if( DEBUGMOD&SCK ) printf("{%s}{udp_com} socket opened on %s:%d\n", arg->entity, request.addr, ntohs(request.port));
}
/* 3. Si on a le port -> on le met dans la reponse */
}else{
strcpy(request.addr, SERV_HOST);
request.port = comInfo.sin_port;
if( DEBUGMOD&SCK ) printf("{%s}{udp_com} socket opened on %s:%d\n", arg->entity, request.addr, ntohs(request.port));
} }
/* [2] Envoi de la réponse /* [2] Envoi de la réponse
=========================================================*/ =========================================================*/
/* 1. On sérialise la réponse */ /* 1. On sérialise la réponse */

View File

@ -64,12 +64,19 @@ int open_communication(){
/* 4. On récupère les données */ /* 4. On récupère les données */
bzero(&request, sizeof(struct bind_header)); bzero(&request, sizeof(struct bind_header));
memcpy(&request.addr, 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(char)*INET_ADDRSTRLEN );
memcpy(&request.port, buffer+sizeof(char)*(1+INET_ADDRSTRLEN), sizeof(unsigned short) ); memcpy(&request.port, buffer+sizeof(char)*(1+INET_ADDRSTRLEN), sizeof(unsigned short) );
// on indian-switch // on indian-switch
request.port = ntohs(request.port); request.port = ntohs(request.port);
/* 5. On vérifie les flags */
if( !(request.flags&BINDHEAD_TCP) || request.flags&BINDHEAD_UDP ){
close(mcast_socket);
return 0;
}
// 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 = %s; port = %d;}\n\n", request.flags, request.addr, request.port);

View File

@ -50,9 +50,9 @@
}; };
#define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) ) #define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) )
#define BINDHEAD_CTL 0x01 // is command terminal (else: view terminal) #define BINDHEAD_UDP 0x01 // is terminal (ask for UDP socket)
#define BINDHEAD_SCK 0x02 // ask for com socket #define BINDHEAD_TCP 0x02 // is plane (ask for TCP socket)
#define BINDHEAD_TCP 0x04 // ask TCP instead of UDP #define BINDHEAD_CTL 0x04 // is ctrlTerm (else: viewTerm)
struct bind_header{ struct bind_header{
char flags; char flags;