rewrite multicast setup

This commit is contained in:
PolynomialDivision 2017-12-07 19:34:31 +01:00
parent b39d310c00
commit 3dd4fc30ec
5 changed files with 30 additions and 8 deletions

View file

@ -11,7 +11,7 @@
#include "multicastsocket.h"
static struct ip_mreq command; /* static ?! */
static struct ip_mreq command;
int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_port, struct sockaddr_in *addr) {
int loop = 1;
@ -57,7 +57,7 @@ int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_
command.imr_multiaddr.s_addr = inet_addr(_multicast_ip);
command.imr_interface.s_addr = htonl (INADDR_ANY);
if (command.imr_multiaddr.s_addr == -1) {
perror("224.0.0.1 ist keine Multicast-Adresse\n");
perror("Wrong multicast address!\n");
exit(EXIT_FAILURE);
}
if (setsockopt(sock,
@ -67,4 +67,16 @@ int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_
perror("setsockopt:IP_ADD_MEMBERSHIP");
}
return sock;
}
int remove_multicast_socket(int socket)
{
if (setsockopt ( socket,
IPPROTO_IP,
IP_DROP_MEMBERSHIP,
&command, sizeof (command)) < 0 ) {
perror ("setsockopt:IP_DROP_MEMBERSHIP");
return -1;
}
return 0;
}