2017-04-18 08:36:32 +00:00
|
|
|
/**************************int DROP_MULTICAST_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo){
|
|
|
|
* UDP Server Dependency *
|
|
|
|
***************************
|
|
|
|
* Designed & Developed by *
|
|
|
|
* Adrien Marquès *
|
|
|
|
* <xdrm-brackets> *
|
|
|
|
***************************
|
|
|
|
* doowap31@gmail.com *
|
|
|
|
**************************/
|
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int DROP_MULTICAST_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct sockaddr_in* pInfo){
|
|
|
|
|
|
|
|
/* [0] Initialisation des variables
|
|
|
|
=========================================================*/
|
|
|
|
int STATUS; // status
|
|
|
|
struct ip_mreq mcastReq;
|
|
|
|
*pListenSock = -1;
|
|
|
|
struct timeval timeout;
|
2017-04-18 18:14:50 +00:00
|
|
|
u_int reuse = 1;
|
2017-04-18 08:36:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [1] Création de la socket
|
|
|
|
=======================================================*/
|
|
|
|
/* 1. Création socket */
|
|
|
|
*pListenSock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
/* 2. Gestion erreur */
|
|
|
|
if( *pListenSock < 0 ) return -1;
|
|
|
|
|
|
|
|
/* 3. Timeout */
|
|
|
|
timeout.tv_sec = SOCK_TIMEOUT;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
2017-04-28 12:54:02 +00:00
|
|
|
if( setsockopt(*pListenSock, SOL_SOCKET, SO_RCVTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){
|
|
|
|
close(*pListenSock);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( setsockopt(*pListenSock, SOL_SOCKET, SO_SNDTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){
|
2017-04-18 08:36:32 +00:00
|
|
|
close(*pListenSock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-18 18:14:50 +00:00
|
|
|
/* 4. Notification d'utilisation multiple du même port */
|
|
|
|
if( setsockopt(*pListenSock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(u_int)) < 0 ){
|
|
|
|
close(*pListenSock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 08:36:32 +00:00
|
|
|
|
|
|
|
/* [2] On définit les infos de la socket
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Reset des valeurs */
|
|
|
|
bzero(pInfo, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
/* (2) On définit les infos */
|
|
|
|
pInfo->sin_family = AF_INET;
|
|
|
|
pInfo->sin_port = htons(pPort);
|
|
|
|
pInfo->sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On publie la SOCKET
|
|
|
|
=======================================================*/
|
|
|
|
/* 1. Publication socket */
|
|
|
|
STATUS = bind(*pListenSock, (struct sockaddr*) pInfo, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
/* 2. Gestion erreur */
|
|
|
|
if( STATUS < 0 ) return -1;
|
|
|
|
|
|
|
|
/* 3. Demande groupe multicast */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [n] Code succès
|
|
|
|
=========================================================*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|