2017-04-09 16:56:11 +00:00
|
|
|
/**************************
|
|
|
|
* UDP Server Dependency *
|
|
|
|
***************************
|
|
|
|
* Designed & Developed by *
|
|
|
|
* Adrien Marquès *
|
|
|
|
* <xdrm-brackets> *
|
|
|
|
***************************
|
|
|
|
* doowap31@gmail.com *
|
|
|
|
**************************/
|
2017-04-01 15:01:36 +00:00
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-09 14:06:31 +00:00
|
|
|
int DROP_UDP_SERVER(const char* 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);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
/* [0] Initialisation des variables
|
|
|
|
=========================================================*/
|
2017-04-09 14:06:31 +00:00
|
|
|
int STATUS; // status
|
|
|
|
uint yes = 1;
|
|
|
|
struct ip_mreq mcastReq;
|
2017-04-01 15:01:36 +00:00
|
|
|
*pListenSock = -1;
|
2017-04-09 16:41:44 +00:00
|
|
|
struct timeval timeout;
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [1] Création de la socket
|
|
|
|
=======================================================*/
|
2017-04-09 14:06:31 +00:00
|
|
|
/* 1. Création socket */
|
2017-04-01 15:01:36 +00:00
|
|
|
*pListenSock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
2017-04-09 14:06:31 +00:00
|
|
|
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] socket: %d\n", *pListenSock);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-09 14:06:31 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 4. MULTICAST -> Autorisation plusieurs sockets sur port */
|
2017-04-09 14:06:31 +00:00
|
|
|
if( pMcast ){
|
|
|
|
|
|
|
|
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] set port to be reused\n");
|
|
|
|
|
|
|
|
if( setsockopt(*pListenSock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(uint) ) < 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 */
|
2017-04-07 16:14:21 +00:00
|
|
|
bzero(pInfo, sizeof(struct sockaddr_in));
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
/* (2) On définit les infos */
|
2017-04-07 16:14:21 +00:00
|
|
|
pInfo->sin_family = AF_INET;
|
|
|
|
pInfo->sin_port = htons(pPort);
|
|
|
|
pInfo->sin_addr.s_addr = htonl(INADDR_ANY);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [3] On publie la SOCKET
|
|
|
|
=======================================================*/
|
2017-04-09 14:06:31 +00:00
|
|
|
/* 1. Publication socket */
|
2017-04-07 16:14:21 +00:00
|
|
|
STATUS = bind(*pListenSock, (struct sockaddr*) pInfo, sizeof(struct sockaddr_in));
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-09 14:06:31 +00:00
|
|
|
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] bind: %d\n", STATUS);
|
2017-04-01 15:01:36 +00:00
|
|
|
|
2017-04-09 14:06:31 +00:00
|
|
|
/* 2. Gestion erreur */
|
2017-04-01 15:01:36 +00:00
|
|
|
if( STATUS < 0 ) return -1;
|
|
|
|
|
2017-04-09 14:06:31 +00:00
|
|
|
/* 3. Demande groupe multicast (si demandé) */
|
|
|
|
if( pMcast ){
|
|
|
|
|
|
|
|
if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] join mcast group: %s\n", pAddr);
|
|
|
|
mcastReq.imr_multiaddr.s_addr = inet_addr(pAddr);
|
|
|
|
mcastReq.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
if( setsockopt(*pListenSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcastReq, sizeof(mcastReq)) < 0 ){
|
|
|
|
close(*pListenSock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-04-01 15:01:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [n] Code succès
|
|
|
|
=========================================================*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|