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

@ -3,4 +3,6 @@
int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_port, struct sockaddr_in *addr);
int remove_multicast_socket(int socket);
#endif

View file

@ -5,7 +5,7 @@
pthread_mutex_t send_mutex;
int init_socket_runopts(char *_ip, char *_port, int broadcast_socket);
int init_socket_runopts(char *_ip, char *_port, int _multicast_socket);
int send_string(char *msg);

View file

@ -21,6 +21,7 @@ struct sigaction newSigAction;
void daemon_shutdown() {
// kill threads
close_socket();
printf("Cancelling Threads!\n");
uloop_cancelled = true;

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;
}

View file

@ -29,21 +29,23 @@ const char *ip;
unsigned short port;
char recv_string[MAX_RECV_STRING + 1];
int recv_string_len;
int multicast_socket;
void *receive_msg(void *args);
void *receive_msg_enc(void *args);
int init_socket_runopts(char *_ip, char *_port, int broadcast_socket) {
int init_socket_runopts(char *_ip, char *_port, int _multicast_socket) {
port = atoi(_port);
ip = _ip;
multicast_socket = _multicast_socket;
if (broadcast_socket) {
sock = setup_broadcast_socket(ip, port, &addr);
} else {
if (multicast_socket) {
printf("Settingup multicastsocket!\n");
sock = setup_multicast_socket(ip, port, &addr);
} else {
sock = setup_broadcast_socket(ip, port, &addr);
}
pthread_t sniffer_thread;
@ -235,4 +237,9 @@ int send_string_enc(char *msg) {
return 0;
}
void close_socket() { close(sock); }
void close_socket() {
if(multicast_socket){
remove_multicast_socket(sock);
}
close(sock);
}