2019-11-10 15:52:14 +05:30
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2019 Joachim Nilsson <troglobit@gmail.com>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2019-11-12 13:51:25 +05:30
|
|
|
#include <sys/stat.h>
|
2019-11-10 15:52:14 +05:30
|
|
|
|
|
|
|
#include "queue.h"
|
2019-11-12 13:26:06 +05:30
|
|
|
#include "syslogd.h"
|
2019-11-10 15:52:14 +05:30
|
|
|
|
|
|
|
struct sock {
|
|
|
|
LIST_ENTRY(sock) link;
|
|
|
|
|
2019-11-12 13:51:25 +05:30
|
|
|
struct addrinfo ai;
|
2019-11-10 15:52:14 +05:30
|
|
|
int sd;
|
|
|
|
|
|
|
|
void (*cb)(int, void *arg);
|
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int max_fdnum = -1;
|
|
|
|
LIST_HEAD(, sock) sl = LIST_HEAD_INITIALIZER();
|
|
|
|
|
|
|
|
|
|
|
|
int nfds(void)
|
|
|
|
{
|
|
|
|
return max_fdnum + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* register socket/fd/pipe created elsewhere, optional callback
|
|
|
|
*/
|
2019-11-12 13:51:25 +05:30
|
|
|
int socket_register(int sd, struct addrinfo *ai, void (*cb)(int, void *), void *arg)
|
2019-11-10 15:52:14 +05:30
|
|
|
{
|
2019-11-12 13:51:25 +05:30
|
|
|
struct sock *entry = NULL;
|
2019-11-10 15:52:14 +05:30
|
|
|
|
2019-11-12 20:21:33 +05:30
|
|
|
entry = calloc(1, sizeof(*entry));
|
2019-11-10 15:52:14 +05:30
|
|
|
if (!entry)
|
2019-11-12 13:51:25 +05:30
|
|
|
goto err;
|
|
|
|
|
2019-11-12 20:21:33 +05:30
|
|
|
if (ai) {
|
|
|
|
entry->ai.ai_addr = calloc(1, sizeof(struct sockaddr_un));
|
|
|
|
if (!entry->ai.ai_addr)
|
|
|
|
goto eaddr;
|
2019-11-12 13:51:25 +05:30
|
|
|
|
2019-11-12 20:21:33 +05:30
|
|
|
entry->ai = *ai;
|
|
|
|
*entry->ai.ai_addr = *ai->ai_addr;
|
|
|
|
}
|
2019-11-10 15:52:14 +05:30
|
|
|
|
|
|
|
entry->sd = sd;
|
|
|
|
entry->cb = cb;
|
|
|
|
entry->arg = arg;
|
|
|
|
LIST_INSERT_HEAD(&sl, entry, link);
|
|
|
|
|
|
|
|
/* Keep track for select() */
|
|
|
|
if (sd > max_fdnum)
|
|
|
|
max_fdnum = sd;
|
|
|
|
|
|
|
|
return sd;
|
2019-11-12 13:51:25 +05:30
|
|
|
eaddr: free(entry);
|
|
|
|
err: return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-12 17:23:21 +05:30
|
|
|
static int socket_opts(int sd, int family, int secure)
|
2019-11-12 13:51:25 +05:30
|
|
|
{
|
|
|
|
socklen_t len, slen;
|
|
|
|
int on = 1;
|
|
|
|
|
2019-11-12 17:23:21 +05:30
|
|
|
if (secure)
|
|
|
|
goto skip;
|
|
|
|
|
2019-11-12 13:51:25 +05:30
|
|
|
/*
|
|
|
|
* This first one is best-effort only, try to increase receive
|
|
|
|
* buffer size. Alert user on failure and proceed.
|
|
|
|
*/
|
|
|
|
slen = sizeof(len);
|
|
|
|
if (getsockopt(sd, SOL_SOCKET, SO_RCVBUF, &len, &slen) == 0 && len < RCVBUF_MINSIZE) {
|
|
|
|
len = RCVBUF_MINSIZE;
|
|
|
|
if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)))
|
|
|
|
ERR("Failed increasing size of socket receive buffer");
|
|
|
|
}
|
|
|
|
|
2019-11-12 17:23:21 +05:30
|
|
|
skip: switch (family) {
|
2019-11-12 13:51:25 +05:30
|
|
|
case AF_INET6:
|
|
|
|
if (setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
|
|
|
|
ERR("setsockopt (IPV6_ONLY), suspending IPv6");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* fallthrough */
|
|
|
|
case AF_INET:
|
|
|
|
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
|
|
|
|
ERR("setsockopt(REUSEADDR), suspending inet");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-11-12 17:55:21 +05:30
|
|
|
#ifdef SO_REUSEPORT
|
|
|
|
if (setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0) {
|
|
|
|
ERR("setsockopt(REUSEPORT), suspending inet");
|
|
|
|
}
|
|
|
|
#endif
|
2019-11-12 13:51:25 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-11-10 15:52:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create socket, with optional callback for reading inbound data
|
|
|
|
*/
|
2019-11-12 13:51:25 +05:30
|
|
|
int socket_create(struct addrinfo *ai, void (*cb)(int, void *), void *arg)
|
2019-11-10 15:52:14 +05:30
|
|
|
{
|
2019-11-12 13:51:25 +05:30
|
|
|
struct sockaddr_un *sun = (struct sockaddr_un *)ai->ai_addr;
|
|
|
|
mode_t mode = ai->ai_protocol;
|
2019-11-12 17:23:21 +05:30
|
|
|
int secure = ai->ai_flags & AI_SECURE;
|
2019-11-12 13:51:25 +05:30
|
|
|
int type = ai->ai_socktype | SOCK_CLOEXEC | SOCK_NONBLOCK;
|
2019-11-10 15:52:14 +05:30
|
|
|
int sd;
|
|
|
|
|
2019-11-12 13:51:25 +05:30
|
|
|
if (ai->ai_family == AF_UNIX) {
|
|
|
|
(void)unlink(sun->sun_path);
|
|
|
|
ai->ai_protocol = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sd = socket(ai->ai_family, type, ai->ai_protocol);
|
2019-11-10 15:52:14 +05:30
|
|
|
if (sd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2019-11-12 17:23:21 +05:30
|
|
|
if (socket_opts(sd, ai->ai_family, secure))
|
2019-11-12 13:51:25 +05:30
|
|
|
goto err;
|
|
|
|
|
2019-11-12 17:23:21 +05:30
|
|
|
if (secure)
|
|
|
|
goto skip;
|
|
|
|
|
2019-11-12 13:51:25 +05:30
|
|
|
if (bind(sd, ai->ai_addr, ai->ai_addrlen) < 0)
|
|
|
|
goto err;
|
|
|
|
|
2019-11-12 17:23:21 +05:30
|
|
|
skip: if (ai->ai_family == AF_UNIX) {
|
2019-11-12 13:51:25 +05:30
|
|
|
if (chmod(sun->sun_path, mode) < 0)
|
|
|
|
goto err;
|
2019-11-10 15:52:14 +05:30
|
|
|
}
|
|
|
|
|
2019-11-12 13:51:25 +05:30
|
|
|
if (socket_register(sd, ai, cb, arg) < 0)
|
|
|
|
goto err;
|
|
|
|
|
2019-11-10 15:52:14 +05:30
|
|
|
return sd;
|
2019-11-12 13:51:25 +05:30
|
|
|
err: close(sd);
|
|
|
|
return -1;
|
2019-11-10 15:52:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int socket_close(int sd)
|
|
|
|
{
|
|
|
|
struct sock *entry, *tmp;
|
|
|
|
|
|
|
|
LIST_FOREACH_SAFE(entry, &sl, link, tmp) {
|
|
|
|
if (entry->sd == sd) {
|
|
|
|
LIST_REMOVE(entry, link);
|
|
|
|
close(entry->sd);
|
2019-11-12 13:51:25 +05:30
|
|
|
if (entry->ai.ai_family == AF_UNIX)
|
|
|
|
free(entry->ai.ai_addr);
|
2019-11-10 15:52:14 +05:30
|
|
|
free(entry);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-12 13:51:25 +05:30
|
|
|
int socket_ffs(int family)
|
|
|
|
{
|
|
|
|
struct sock *entry;
|
|
|
|
|
|
|
|
LIST_FOREACH(entry, &sl, link) {
|
|
|
|
if (entry->ai.ai_family == family)
|
|
|
|
return entry->sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-10 15:52:14 +05:30
|
|
|
int socket_poll(struct timeval *timeout)
|
|
|
|
{
|
|
|
|
int num;
|
|
|
|
fd_set fds;
|
|
|
|
struct sock *entry;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
LIST_FOREACH(entry, &sl, link)
|
|
|
|
FD_SET(entry->sd, &fds);
|
|
|
|
|
|
|
|
num = select(nfds(), &fds, NULL, NULL, timeout);
|
|
|
|
if (num <= 0) {
|
|
|
|
/* Log all errors, except when signalled, ignore failures. */
|
|
|
|
if (num < 0 && EINTR != errno)
|
2019-11-12 13:26:06 +05:30
|
|
|
WARN("Failed select(): %s", strerror(errno));
|
2019-11-10 15:52:14 +05:30
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_FOREACH(entry, &sl, link) {
|
|
|
|
if (!FD_ISSET(entry->sd, &fds))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (entry->cb)
|
|
|
|
entry->cb(entry->sd, entry->arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Local Variables:
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* c-file-style: "linux"
|
|
|
|
* End:
|
|
|
|
*/
|