2017-04-01 15:01:36 +00:00
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int DROP_TCP_SERVER(const int pPort, int* pListenSock){
|
|
|
|
if( DEBUGMOD&HDR ) printf("====== DROP_TCP_SERVER(%d, %p) ======\n\n", pPort, (void*) pListenSock);
|
|
|
|
|
|
|
|
/* [0] Initialisation des variables
|
|
|
|
=========================================================*/
|
|
|
|
static struct sockaddr_in addr; // contiendra les infos du serveur
|
|
|
|
int STATUS; // status
|
2017-04-09 16:41:44 +00:00
|
|
|
struct timeval timeout;
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
*pListenSock = -1;
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] Création de la socket
|
|
|
|
=======================================================*/
|
2017-04-09 16:41:44 +00:00
|
|
|
/* 1. Création socket */
|
2017-04-01 15:01:36 +00:00
|
|
|
*pListenSock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
2017-04-09 14:06:31 +00:00
|
|
|
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] socket: %d\n", *pListenSock);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-09 16:41:44 +00:00
|
|
|
/* 2. Gestion erreur */
|
2017-04-01 15:01:36 +00:00
|
|
|
if( *pListenSock < 0 ) return -1;
|
|
|
|
|
2017-04-09 16:41:44 +00:00
|
|
|
/* 3. Timeout */
|
|
|
|
timeout.tv_sec = SOCK_TIMEOUT;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
if( setsockopt(*pListenSock, SOL_SOCKET, SO_RCVTIMEO|SO_SNDTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){
|
|
|
|
close(*pListenSock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
/* [2] On définit les infos de la socket
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Reset des valeurs */
|
|
|
|
bzero(&addr, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
/* (2) On définit les infos */
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(pPort);
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On publie la SOCKET
|
|
|
|
=======================================================*/
|
2017-04-09 16:41:44 +00:00
|
|
|
/* 1. Publication socket */
|
2017-04-01 15:01:36 +00:00
|
|
|
STATUS = bind(*pListenSock, (struct sockaddr*) &addr, sizeof(addr));
|
|
|
|
|
2017-04-09 14:06:31 +00:00
|
|
|
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] bind: %d\n", STATUS);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-09 16:41:44 +00:00
|
|
|
/* 2. Gestion erreur */
|
2017-04-01 15:01:36 +00:00
|
|
|
if( STATUS < 0 ) return -1;
|
|
|
|
|
|
|
|
|
2017-04-09 16:41:44 +00:00
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* [4] On met la socket sur écoute
|
|
|
|
=======================================================*/
|
2017-04-09 16:41:44 +00:00
|
|
|
/* 1. Démarrage écoute */
|
2017-04-01 15:01:36 +00:00
|
|
|
STATUS = listen(*pListenSock, MAX_TCP_THR);
|
|
|
|
|
2017-04-09 14:06:31 +00:00
|
|
|
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] listen: %d\n", STATUS);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-09 16:41:44 +00:00
|
|
|
/* 2. Gestion erreur */
|
2017-04-01 15:01:36 +00:00
|
|
|
if( STATUS < 0 ) return -1;
|
|
|
|
|
|
|
|
|
2017-04-09 16:41:44 +00:00
|
|
|
|
2017-04-01 15:01:36 +00:00
|
|
|
/* [n] Code succès
|
|
|
|
=========================================================*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|