mirror of
https://github.com/berlin-open-wireless-lab/DAWN.git
synced 2025-03-09 15:40:12 +00:00
Provide multi-priority syslog() based logging to improve user and developer experience. Add dawnlog_* functions and macros to convert printf() family and perror() logging to syslog() family. Removed unnecessary sprintf() for building log strings (embed format directly). Add local config settings for log level. Add command line parameters for log level and destination. Set default log level to suppress a lot of previously noisy messages. Restore some previously removed noisy messages as DEBUG level in case they help in future. Eliminate DAWN_NO_OUTPUT static code checks which are no longer used. Signed-off-by: Ian Clowes <clowes_ian@hotmail.com>
77 lines
No EOL
2.1 KiB
C
77 lines
No EOL
2.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "utils.h"
|
|
#include "multicastsocket.h"
|
|
|
|
// based on: http://openbook.rheinwerk-verlag.de/linux_unix_programmierung/Kap11-018.htm
|
|
|
|
static struct ip_mreq command;
|
|
|
|
int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_port, struct sockaddr_in *addr) {
|
|
int loop = 1;
|
|
int sock;
|
|
|
|
memset(addr, 0, sizeof(*addr));
|
|
addr->sin_family = AF_INET;
|
|
addr->sin_addr.s_addr = inet_addr(_multicast_ip);
|
|
addr->sin_port = htons (_multicast_port);
|
|
|
|
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
|
|
dawnlog_perror("socket()");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// allow multiple processes to use the same port
|
|
loop = 1;
|
|
if (setsockopt(sock,
|
|
SOL_SOCKET,
|
|
SO_REUSEADDR,
|
|
&loop, sizeof(loop)) < 0) {
|
|
dawnlog_perror("setsockopt:SO_REUSEADDR");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (bind(sock,
|
|
(struct sockaddr *) addr,
|
|
sizeof(*addr)) < 0) {
|
|
dawnlog_perror("bind");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// allow broadcast
|
|
loop = 1;
|
|
if (setsockopt(sock,
|
|
IPPROTO_IP,
|
|
IP_MULTICAST_LOOP,
|
|
&loop, sizeof(loop)) < 0) {
|
|
dawnlog_perror("setsockopt:IP_MULTICAST_LOOP");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// join broadcast group
|
|
command.imr_multiaddr.s_addr = inet_addr(_multicast_ip);
|
|
command.imr_interface.s_addr = htonl (INADDR_ANY);
|
|
if (command.imr_multiaddr.s_addr == -1) {
|
|
dawnlog_perror("Wrong multicast address!\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (setsockopt(sock,
|
|
IPPROTO_IP,
|
|
IP_ADD_MEMBERSHIP,
|
|
&command, sizeof(command)) < 0) {
|
|
dawnlog_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) {
|
|
dawnlog_perror("setsockopt:IP_DROP_MEMBERSHIP");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
} |