Refactor of SLPD - moved most of the SLPD related code into slpd.{c,h}

This commit is contained in:
thorkill 2016-05-25 18:23:42 +02:00
parent 299b223bba
commit 11b8eb81b9
6 changed files with 239 additions and 177 deletions

View file

@ -301,98 +301,6 @@ int setup_vpn_in_socket(const sockaddr_t *sa) {
return nfd;
} /* int setup_vpn_in_socket */
int setup_slpd_in_socket() {
int nfd;
char *my_slpd_port;
char *my_slpd_group;
struct addrinfo *res;
struct addrinfo hints;
struct ipv6_mreq group;
logger(DEBUG_ALWAYS, LOG_ERR, "Prepare SLPD mutlicast socket");
if(!get_config_string(lookup_config(config_tree, "SLPDPort"), &my_slpd_port))
my_slpd_port = xstrdup(DEFAULT_SLPD_PORT);
if(!get_config_string(lookup_config(config_tree, "SLPDGroup"), &my_slpd_group))
my_slpd_group = xstrdup(DEFAULT_SLPD_GROUP);
bzero(&hints, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
//hints.ai_flags = AI_PASSIVE;
int status;
if ((status = getaddrinfo(NULL, my_slpd_port, &hints, &res)) != 0 ) {
logger(DEBUG_ALWAYS, LOG_INFO, "getaddrinfo() error: [%s:%d]", strerror(errno), errno);
return -1;
}
nfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(nfd < 0) {
logger(DEBUG_ALWAYS, LOG_ERR, "Can not create socket for SLPD %s", sockstrerror(sockerrno));
return -1;
}
#ifdef FD_CLOEXEC
fcntl(nfd, F_SETFD, FD_CLOEXEC);
#endif
#ifdef O_NONBLOCK
{
int flags = fcntl(nfd, F_GETFL);
if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
closesocket(nfd);
logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "fcntl",
strerror(errno));
return -1;
}
}
#endif
int reuse = 1;
if(setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
logger(DEBUG_ALWAYS, LOG_ERR, "Can not set SO_REUSEADDR for SLPD %s", sockstrerror(sockerrno));
closesocket(nfd);
return -1;
}
if (bind(nfd, res->ai_addr, res->ai_addrlen) < 0) {
logger(DEBUG_ALWAYS, LOG_ERR, "Can not bind() socket for SLPD %s", sockstrerror(sockerrno));
closesocket(nfd);
return -1;
}
config_t *c_iface;
c_iface = lookup_config(config_tree, "SLPDInterface");
while(c_iface) {
struct sockaddr_in6 group_addr;
inet_pton(AF_INET6, my_slpd_group, &group_addr.sin6_addr);
group.ipv6mr_multiaddr = group_addr.sin6_addr;
group.ipv6mr_interface = if_nametoindex(c_iface->value);
if (setsockopt(nfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &group, sizeof(group)) < 0) {
logger(DEBUG_ALWAYS, LOG_ERR, "Can not join group for SLPD %s", sockstrerror(sockerrno));
closesocket(nfd);
return -1;
}
logger(DEBUG_STATUS, LOG_INFO, "SLPD multicast group joined on %s ready", c_iface->value);
c_iface = lookup_config_next(config_tree, c_iface);
}
logger(DEBUG_STATUS, LOG_INFO, "SLPD socket ready (%d)", nfd);
return nfd;
} /* int setup_slpd_in_socket */
static void retry_outgoing_handler(void *data) {
setup_outgoing_connection(data);
}