[update] Managing already used ports (so take next, 10 times max)
This commit is contained in:
parent
87ed0fb7c9
commit
6848840638
|
@ -105,12 +105,13 @@ int main(int argc, char* argv[]){
|
|||
void* LISTEN_TCP(void* THREADABLE_ARGS){
|
||||
/* [0] Initialisation des variables
|
||||
==========================================================*/
|
||||
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
|
||||
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
||||
struct in_addr ip; // Pour afficher l'IP en dot notation
|
||||
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
|
||||
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
||||
struct in_addr ip; // Pour afficher l'IP en dot notation
|
||||
int rtnPort; // Port utilisé par le serveur
|
||||
|
||||
ip.s_addr = arg->addr;
|
||||
|
||||
|
@ -120,7 +121,8 @@ void* LISTEN_TCP(void* THREADABLE_ARGS){
|
|||
|
||||
/* [1] On démarre le SERVEUR TCP d'écoute globale
|
||||
==========================================================*/
|
||||
if( DROP_TCP_SERVER(arg->port, &LISTENSOCK) < 0 ){
|
||||
rtnPort = DROP_TCP_SERVER(arg->port, &LISTENSOCK);
|
||||
if( rtnPort < 0 ){
|
||||
|
||||
if( DEBUGMOD&SCK ) printf("{tcp_listn} Error creating listener socket\n");
|
||||
|
||||
|
@ -133,7 +135,9 @@ void* LISTEN_TCP(void* THREADABLE_ARGS){
|
|||
|
||||
|
||||
|
||||
printf("{tcp_listn} listen on %s:%d\n", inet_ntoa(ip), arg->port);
|
||||
printf("{tcp_listn} listen on %s:%d\n", inet_ntoa(ip), rtnPort);
|
||||
if( rtnPort != arg->port )
|
||||
printf("{tcp_listn} /!\\ Another SGCA might be running, port %d is already used\n", arg->port);
|
||||
|
||||
|
||||
|
||||
|
@ -229,6 +233,7 @@ void* LISTEN_UDP(void* THREADABLE_ARGS){
|
|||
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
|
||||
int rtnPort; // Returned port (if already used)
|
||||
|
||||
/* 2. On parse les arguments */
|
||||
struct listen_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
||||
|
@ -246,7 +251,8 @@ void* LISTEN_UDP(void* THREADABLE_ARGS){
|
|||
/* [1] On démarre le SERVEUR UDP d'écoute globale
|
||||
==========================================================*/
|
||||
/* 1. On crée la socket */
|
||||
if( DROP_UDP_SERVER(arg->addr, arg->port, &SOCKET, &listenInfo, 1) < 0 ){
|
||||
rtnPort = DROP_UDP_SERVER(arg->addr, arg->port, &SOCKET, &listenInfo, 1);
|
||||
if( rtnPort < 0 ){
|
||||
|
||||
if( DEBUGMOD&SCK ) printf("{%s} Error creating listener socket\n", entity);
|
||||
|
||||
|
@ -261,7 +267,10 @@ void* LISTEN_UDP(void* THREADABLE_ARGS){
|
|||
setTimeout(SOCKET, SOCK_TIMEOUT, TIMEOUT_SEND);
|
||||
if( DEBUGMOD&SCK ) printf("{%s} SEND timeout set to %d\n", entity, SOCK_TIMEOUT);
|
||||
|
||||
printf("{%s} listen on %s:%d\n", entity, inet_ntoa(ip), arg->port);
|
||||
printf("{%s} listen on %s:%d\n", entity, inet_ntoa(ip), rtnPort);
|
||||
|
||||
if( rtnPort != arg->port )
|
||||
printf("{%s} /!\\ Another SGCA might be running, port %d is already used\n", entity, arg->port);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#define HEADERS 0x08 // debug des HEADERS de fonctions
|
||||
#define THREADS 0x10 // debug des THREADS
|
||||
#define COMMUNI 0x20 // debug des COMMUNICATIONS
|
||||
#define SOCKADV 0x40 // debug des ADVANCED SOCKET
|
||||
|
||||
#define SCK 0x01 // FILTRE pour SOCKET
|
||||
#define RVL 0x02 // FILTRE pour REVEALS
|
||||
|
@ -22,6 +23,7 @@
|
|||
#define HDR 0x08 // FILTRE pour HEADERS
|
||||
#define THR 0x10 // FILTRE pour THREADS
|
||||
#define COM 0x20 // FILTRE pour COMMUNICATIONS
|
||||
#define SAD 0x40 // FILTRE pour ADVANCED SOCKET
|
||||
|
||||
// possibilité de cumuler les DEBUGMODES
|
||||
#define DEBUGMOD ( SOCKETS | THREADS | BUFFERS ) // REVEALS + HEADER + THREADS
|
|
@ -95,4 +95,24 @@ int setTimeout(int pSocket, const time_t pSec, char pFlags){
|
|||
|
||||
/* 3. Résultat de succès */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int checkMulticastGroup(in_addr_t pAddr){
|
||||
// printf("Checking if multicast '%d' is already used\n");
|
||||
|
||||
/* [0] Initialisation des variables
|
||||
=========================================================*/
|
||||
// int sock, nb;
|
||||
|
||||
|
||||
/* [1] Création d'une socket UDP
|
||||
=========================================================*/
|
||||
return -1;
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -53,4 +53,22 @@
|
|||
int setTimeout(int pSocket, const time_t pSec, char pFlags);
|
||||
|
||||
|
||||
|
||||
/* Vérifie si un groupe multicast est déja utilisé
|
||||
*
|
||||
* @in
|
||||
* pAddr<in_addr_t> Adresse du groupe multicast
|
||||
*
|
||||
* @out
|
||||
* -1 en cas d'échec
|
||||
* 0 sinon
|
||||
*
|
||||
* @history
|
||||
* [1] Initialisation des variables
|
||||
* [2] On Vérifie si le groupe a du traffic
|
||||
*
|
||||
*/
|
||||
int checkMulticastGroup(in_addr_t pAddr);
|
||||
|
||||
|
||||
#endif
|
|
@ -51,7 +51,13 @@ int DROP_TCP_SERVER(const int pPort, int* pListenSock){
|
|||
/* [3] On publie la SOCKET
|
||||
=======================================================*/
|
||||
/* 1. Publication socket */
|
||||
STATUS = bind(*pListenSock, (struct sockaddr*) &addr, sizeof(addr));
|
||||
int count = -1;
|
||||
do{
|
||||
count++;
|
||||
addr.sin_port = htons(pPort+count);
|
||||
STATUS = bind(*pListenSock, (struct sockaddr*) &addr, sizeof(struct sockaddr_in));
|
||||
if( DEBUGMOD&SAD ) printf("\tudp:%d used, trying udp:%d\n", pPort+count-1, pPort+count);
|
||||
}while( STATUS < 0 && count < 10 );
|
||||
|
||||
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] bind: %d\n", STATUS);
|
||||
|
||||
|
@ -74,6 +80,6 @@ int DROP_TCP_SERVER(const int pPort, int* pListenSock){
|
|||
|
||||
/* [n] Code succès
|
||||
=========================================================*/
|
||||
return 0;
|
||||
return pPort+count;
|
||||
}
|
||||
|
||||
|
|
|
@ -48,7 +48,13 @@ int DROP_UDP_SERVER(in_addr_t pAddr, const int pPort, int* pListenSock, struct s
|
|||
/* [3] On publie la SOCKET
|
||||
=======================================================*/
|
||||
/* 1. Publication socket */
|
||||
STATUS = bind(*pListenSock, (struct sockaddr*) pInfo, sizeof(struct sockaddr_in));
|
||||
int count = -1;
|
||||
do{
|
||||
count++;
|
||||
pInfo->sin_port = htons(pPort+count);
|
||||
STATUS = bind(*pListenSock, (struct sockaddr*) pInfo, sizeof(struct sockaddr_in));
|
||||
if( DEBUGMOD&SAD ) printf("\tudp:%d used, trying udp:%d\n", pPort+count-1, pPort+count);
|
||||
}while( STATUS < 0 && count < 10 );
|
||||
|
||||
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] bind: %d\n", STATUS);
|
||||
|
||||
|
@ -74,6 +80,6 @@ int DROP_UDP_SERVER(in_addr_t pAddr, const int pPort, int* pListenSock, struct s
|
|||
|
||||
/* [n] Code succès
|
||||
=========================================================*/
|
||||
return 0;
|
||||
return pPort+count;
|
||||
}
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ int DROP_MULTICAST_SERVER(const char* pAddr, const int pPort, int* pListenSock,
|
|||
/* [3] On publie la SOCKET
|
||||
=======================================================*/
|
||||
/* 1. Publication socket */
|
||||
STATUS = bind(*pListenSock, (struct sockaddr*) pInfo, sizeof(struct sockaddr_in));
|
||||
STATUS = bind(*pListenSock, (struct sockaddr*) pInfo, sizeof(struct sockaddr_in));
|
||||
|
||||
/* 2. Gestion erreur */
|
||||
if( STATUS < 0 ) return -1;
|
||||
|
|
Loading…
Reference in New Issue