Commit Diff


commit - ef3dc4ff7ec2baebbb88f95b42d54f0899055be6
commit + 4d3e8f2435b25161c51e0a23bbe8985d3a0564dd
blob - 1a8e1b5d5c75495eb0dce4e6fce1fb30a6928171
blob + f94389c887236315b572d4ab51c23d8c21ae1e73
--- thingsctl/Makefile	
+++ thingsctl/Makefile	
@@ -11,7 +11,7 @@ CFLAGS+=	-Wall -I${.CURDIR}
 CFLAGS+=	-Wstrict-prototypes -Wmissing-prototypes
 CFLAGS+=	-Wmissing-declarations
 CFLAGS+=	-Wshadow -Wpointer-arith
-CFLAGS+=	-Wsign-compare -pthread
+CFLAGS+=	-Wsign-compare -fno-common
 CFLAGS+=	-I${.CURDIR} -I${.CURDIR}/../thingsd
 
 MAN=		thingsctl.8
blob - 13a00af9490060efbaae2c1bf92db95c8efeac3a
blob + 565c7b5ca63fa2cbc56aa0201b6c4b0b863cd19c
--- thingsd/Makefile	
+++ thingsd/Makefile	
@@ -1,6 +1,7 @@
 PROG =		thingsd
-SRCS =		config.c control.c things.c log.c thingsd.c parse.y proc.c \
-		tls.c serial.c sockets.c events.c client.c
+SRCS =		parse.y
+SRCS +=		config.c control.c log.c thingsd.c proc.c
+SRCS +=		sockets.c tls.c client.c
 
 MAN =		thingsd.8 thingsd.conf.5
 
@@ -8,17 +9,16 @@ PREFIX=		/usr/local
 BINDIR?=	${PREFIX}/sbin
 MANDIR?=	${PREFIX}/man/man
 
+LDADD =		-levent -ltls -lssl -lcrypto -lutil
+DPADD =		${LIBEVENT} ${LIBTLS} ${LIBSSL} ${LIBCRYPTO} ${LIBUTIL}
+DEBUG=		-g -DDEBUG=0 -O0
 CFLAGS +=	-Wall -I${.CURDIR}
 CFLAGS +=	-Wstrict-prototypes -Wmissing-prototypes
 CFLAGS +=	-Wmissing-declarations
 CFLAGS +=	-Wshadow -Wpointer-arith -fno-common
-CFLAGS +=	-Wsign-compare -pthread
+CFLAGS +=	-Wsign-compare
 YFLAGS =
-LDADD =		-levent -ltls -lssl -lcrypto -lutil -pthread
-DPADD =		${LIBEVENT} ${LIBTLS} ${LIBSSL} ${LIBCRYPTO} ${LIBUTIL}
 
 CLEANFILES+=	y.tab.h
 
-DEBUG=		-g -O2
-
 .include <bsd.prog.mk>
blob - 3a5af70f06da6d9221f1d75e6386da8568d2a8b1
blob + c19416b00d16f78d8fa81b51aa9bb91067b90789
--- thingsd/client.c	
+++ thingsd/client.c	
@@ -17,12 +17,11 @@
 #include <sys/queue.h>
 #include <sys/time.h>
 #include <sys/socket.h>
+#include <sys/tree.h>
 
 #include <errno.h>
 #include <event.h>
 #include <imsg.h>
-#include <pthread.h>
-#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <tls.h>
@@ -31,266 +30,82 @@
 #include "proc.h"
 #include "thingsd.h"
 
-#define FD_RESERVE		5
-#define FD_NEEDED		6
+extern volatile int client_inflight;
+extern enum privsep_procid privsep_process;
 
-volatile int client_inflight = 0;
+void	 bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t,
+	    void *);
+void	 client_write_to_things(struct packages *);
 
-int		 accept_reserve(int, struct sockaddr *, socklen_t *, int,
-		    volatile int *);
-void bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t, void *);
-
-int
-accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
-    int reserve, volatile int *counter)
-{
-	int ret;
-	if (getdtablecount() + reserve +
-	    ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
-		log_debug("inflight fds exceeded");
-		errno = EMFILE;
-		return -1;
-	}
-
-	if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC))
-	    > -1) {
-		(*counter)++;
-		log_debug("inflight incremented, now %d", *counter);
-	}
-	return ret;
-}
-
 void
-client_accept_paused(int fd, short events, void *arg)
+client_del(struct client *client)
 {
-	struct socket		*sock = arg;
+	struct socket		*sock = client->sock;
+	struct socket		*psock = NULL, *csock = NULL;
+	struct client		*tclient, *uclient;
 
-	event_add(sock->ev, NULL);
-}
-
-void
-client_conn(int fd, short event, void *arg)
-{
-	struct sockaddr_storage	 ss;
-	struct timeval		 backoff;
-	struct client		*client = NULL;
-	struct thingsd		*env = (struct thingsd *)arg;
-	struct socket		*sock = NULL;
-	int			 client_fd;
-	socklen_t		 len = sizeof(ss);
-
-	backoff.tv_sec = 1;
-	backoff.tv_usec = 0;
-
-	sock = get_socket(env, fd);
-	if (sock == NULL)
-		return;
-
-	event_add(sock->ev, NULL);
-
-	client_fd = accept_reserve(fd, (struct sockaddr *)&ss, &len, FD_RESERVE,
-	    &client_inflight);
-	if (client_fd == -1) {
-		switch (errno) {
-		case EINTR:
-		case EWOULDBLOCK:
-		case ECONNABORTED:
-			return;
-		case EMFILE:
-		case ENFILE:
-			event_del(sock->ev);
-			evtimer_add(&sock->pause, &backoff);
-			return;
-		default:
-			log_warnx("client accept failed");
-		}
+	TAILQ_FOREACH_SAFE(tclient, sock->clients, entry, uclient) {
+		if (client->id == tclient->id)
+			TAILQ_REMOVE(sock->clients, client, entry);
 	}
+	log_debug("%s: disconnecting client %s (%d)", __func__, client->name,
+	    client->fd);
+	/* if (client->tls) */
+	/* 	tls_free(client->tls_ctx); */
 
-	client = calloc(1, sizeof(*client));
-	if (client == NULL)
-		goto err;
+	if (evtimer_initialized(&client->timeout))
+		evtimer_del(&client->timeout);
 
-	client->ev = calloc(1, sizeof(*client->ev));
-	if (client->ev == NULL)
-		goto err;
+	if (client->subscribed == 0)
+		log_debug("%s: client disconnected", __func__);
+	else
+		log_debug("client disconnected: %s", client->name);
 
-	if (sock->tls) {
-		if (tls_accept_socket(sock->tls_ctx, &client->tls_ctx,
-		    client_fd) == -1) {
-			log_warnx("tls accept failed: %s",
-			    tls_error(sock->tls_ctx));
-			goto err;
-		}
-	}
+	if (client->bev != NULL)
+		bufferevent_free(client->bev);
 
-	client->subscriptions = calloc(1, sizeof(*client->subscriptions));
-	if (client->subscriptions == NULL)
-		goto err;
-
-	TAILQ_INIT(client->subscriptions);
-
-	sock->client_cnt++;
-	env->client_cnt++;
-	if (sock->max_clients > 0 && sock->client_cnt > sock->max_clients) {
-		log_debug("%s: %s max clients reached", __func__, sock->name);
-		sock->client_cnt--;
-		env->client_cnt--;
-		goto err;
-	}
-
-	client->port = sock->port;
-	client->socket = sock;
-	client->subscribed = false;
-	client->evb = evbuffer_new();
-
-	if (client->evb == NULL) {
-		sock->client_cnt--;
-		env->client_cnt--;
-		goto err;
-	}
-
-	client->fd = client_fd;
-	client->join_time = time(NULL);
-
-	TAILQ_INSERT_TAIL(env->clients, client, entry);
-	if (sock->tls) {
-		client->tls = true;
-		event_del(client->ev);
-		event_set(client->ev, client->fd, EV_READ | EV_PERSIST,
-		    socket_tls_handshake, env);
-		event_add(client->ev, NULL);
-		return;
-	}
-
-	client_add(env, client);
-
-	return;
-err:
-	log_debug("%s: client error", __func__);
 	client_inflight--;
-	if (client_fd != -1)
-		close(client_fd);
-	if (client != NULL) {
-		if (sock->tls)
-			tls_free(client->tls_ctx);
-		if (client->subscriptions != NULL)
-			free(client->subscriptions);
-		free(client->ev);
-		free(client);
-	}
-}
 
-void
-client_add(struct thingsd *env, struct client *client)
-{
-	struct socket 		*sock = client->socket;
-	evbuffercb		 clientrd = client_rd;
-	evbuffercb		 clientwr = client_wr;
+	if (client->subscribed == 0)
+		goto done;
 
-	log_debug("%s: client connected, %d", __func__, client->fd);
-
-	client->bev = bufferevent_new(client->fd, clientrd, clientwr,
-	    client_err, env);
-
-	if (client->bev == NULL) {
-		sock->client_cnt--;
-		env->client_cnt--;
-		goto err;
+	/* account for parent/child client counts */
+	if (sock->conf.child_id) {
+		TAILQ_FOREACH(csock, thingsd_env->sockets, entry)
+			if (csock->conf.id == sock->conf.child_id)
+				break;
 	}
-
-	if (client->tls) {
-		event_set(&client->bev->ev_read, client->fd, EV_READ,
-		    client_tls_readcb, client->bev);
-		event_set(&client->bev->ev_write, client->fd, EV_WRITE,
-		    client_tls_writecb, client->bev);
+	if (sock->conf.parent_id) {
+		TAILQ_FOREACH(psock, thingsd_env->sockets, entry)
+			if (psock->conf.id == sock->conf.parent_id)
+				break;
 	}
-
-	bufferevent_setwatermark(client->bev, EV_READ, 0, PKT_BUFF);
-	bufferevent_enable(client->bev, EV_READ);
-
-	return;
-err:
-	log_debug("%s: client error", __func__);
-	if (client->fd != -1)
-		close(client->fd);
-	if (sock->tls)
-		tls_free(client->tls_ctx);
-	free(client->ev);
+	sock->client_cnt--;
+	if (csock != NULL)
+		csock->client_cnt--;
+	if (psock != NULL)
+		psock->client_cnt--;
+done:
+	close(client->fd);
 	free(client);
 }
 
 void
-client_del(struct thingsd *env, struct client *client)
-{
-	struct thing		*thing;
-	struct client		*pclient, *tclient;
-	struct subscription	*sub, *tsub;
-
-	TAILQ_FOREACH_SAFE(pclient, env->clients, entry, tclient) {
-		if (pclient->fd == client->fd) {
-			if (pclient->tls)
-				tls_free(pclient->tls_ctx);
-
-
-			TAILQ_FOREACH_SAFE(sub, pclient->subscriptions, entry,
-			    tsub) {
-				TAILQ_FOREACH(thing, env->things, entry) {
-					if (strcmp(sub->thing_name,
-					    thing->name) == 0)
-						thing->client_cnt--;
-				}
-				TAILQ_REMOVE(pclient->subscriptions, sub,
-				    entry);
-			}
-
-			env->client_cnt--;
-			pclient->socket->client_cnt--;
-
-			if (pclient->subscribed == false)
-				log_debug("%s: client disconnected", __func__);
-			else
-				log_info("client disconnected: %s",
-				    pclient->name);
-
-			if (pclient->bev != NULL)
-				bufferevent_free(pclient->bev);
-
-			client_inflight--;
-			close(pclient->fd);
-			TAILQ_REMOVE(env->clients, pclient, entry);
-			free(pclient->ev);
-			free(pclient->subscriptions);
-			free(pclient);
-			break;
-		}
-	}
-}
-
-void
 client_rd(struct bufferevent *bev, void *arg)
 {
-	struct thingsd		*env = (struct thingsd *)arg;
-	struct thing		*thing = NULL;
-	struct client		*client = NULL, *tclient;
-	struct subscription	*sub;
+	struct client		*client = (struct client *)arg;
 	size_t			 len;
-	int			 fd = bev->ev_read.ev_fd;
 	char			*pkt = NULL, *npkt = NULL;
+	struct package		*package = NULL;
 
-	TAILQ_FOREACH(tclient, env->clients, entry) {
-		if (tclient->fd == fd) {
-			client = tclient;
-			break;
-		}
-	}
 
-	if (client == NULL)
-		return;
-
 	client->evb = EVBUFFER_INPUT(bev);
 	len = EVBUFFER_LENGTH(client->evb);
 
-	if (client->subscribed == false) {
+	if (client->subscribed == 0) {
+		log_debug("%s: client connection established (%d)", __func__,
+		    client->fd);
+
 		/* allow one shot at subscription so we don't get hammered */
 		pkt = calloc(len, sizeof(*pkt));
 		if (pkt == NULL)
@@ -307,27 +122,26 @@ client_rd(struct bufferevent *bev, void *arg)
 
 		/* peak into packet and ensure it's a subscription packet */
 		if (pkt[0] == 0x7E && pkt[1] == 0x7E && pkt[2] == 0x7E) {
+			log_debug("%s: subscription packet received", __func__);
 			memmove(npkt, pkt+3, len-3);
 			parse_buf(client, npkt, len-3);
-			if (client->subscribed)
+			if (client->subscribed) {
 				bufferevent_enable(client->bev,
 				    EV_READ | EV_WRITE);
+				if (evtimer_initialized(&client->timeout))
+				       evtimer_del(&client->timeout);
+			}
 		}
 	} else if (client->subscribed) {
 		/* write to things */
-		TAILQ_FOREACH(sub, client->subscriptions, entry) {
-			TAILQ_FOREACH(thing, env->things, entry) {
-				if (strcmp(sub->thing_name, thing->name) == 0 &&
-				    client->port == thing->port) {
-					if (thing->exists)
-						client_wr_things(client, thing,
-						    len);
-					else
-						evbuffer_drain(client->evb,
-						    len);
-				}
-			}
-		}
+		if ((package = calloc(1, sizeof(*package))) == NULL)
+			return;
+		evbuffer_remove(client->evb, package->pkt, len);
+		package->thing_id = client->thing_id;
+		package->len = len;
+		TAILQ_INSERT_TAIL(thingsd_env->packages, package, entry);
+		client_write_to_things(thingsd_env->packages);
+		evbuffer_drain(client->evb, len);
 	}
 	free(pkt);
 	free(npkt);
@@ -336,108 +150,117 @@ client_rd(struct bufferevent *bev, void *arg)
 void
 client_tls_readcb(int fd, short event, void *arg)
 {
-	struct bufferevent	*bufev = (struct bufferevent *)arg;
-	struct thingsd		*env = bufev->cbarg;
-	struct client		*client = NULL, *tclient;
-	char			 pkt[PKT_BUFF];
-	ssize_t			 ret;
-	size_t			 len;
-	int			 toread = EVBUFFER_READ;
+	/* struct bufferevent	*bufev = (struct bufferevent *)arg; */
+	/* struct thingsd		*env = bufev->cbarg; */
+	/* struct client		*client = NULL, *tclient; */
+	/* char			 pkt[PKT_BUFF]; */
+	/* ssize_t			 ret; */
+	/* size_t			 len; */
+	/* int			 toread = EVBUFFER_READ; */
 
-	TAILQ_FOREACH(tclient, env->clients, entry) {
-		if (tclient->fd == fd)
-			client = tclient;
-	}
+	/* TAILQ_FOREACH(tclient, env->clients, entry) { */
+	/* 	if (tclient->fd == fd) */
+	/* 		client = tclient; */
+	/* } */
 
-	if (client == NULL)
-		return;
+	/* if (client == NULL) */
+	/* 	return; */
 
-	memset(pkt, 0, sizeof(pkt));
+	/* memset(pkt, 0, sizeof(pkt)); */
 
-	ret = tls_read(client->tls_ctx, pkt, PKT_BUFF);
-	if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
-		goto retry;
-	} else if (ret < 0) {
-		toread |= EVBUFFER_ERROR;
-		goto err;
-	}
+	/* ret = tls_read(client->tls_ctx, pkt, PKT_BUFF); */
+	/* if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) { */
+	/* 	goto retry; */
+	/* } else if (ret < 0) { */
+	/* 	toread |= EVBUFFER_ERROR; */
+	/* 	goto err; */
+	/* } */
 
-	len = ret;
-	if (len == 0) {
-		toread |= EVBUFFER_EOF;
-		goto err;
-	}
+	/* len = ret; */
+	/* if (len == 0) { */
+	/* 	toread |= EVBUFFER_EOF; */
+	/* 	goto err; */
+	/* } */
 
-	if (evbuffer_add(bufev->input, pkt, len) == -1) {
-		toread |= EVBUFFER_ERROR;
-		goto err;
-	}
+	/* if (evbuffer_add(bufev->input, pkt, len) == -1) { */
+	/* 	toread |= EVBUFFER_ERROR; */
+	/* 	goto err; */
+	/* } */
 
-	event_add(&bufev->ev_read, NULL);
+	/* event_add(&bufev->ev_read, NULL); */
 
-	len = EVBUFFER_LENGTH(bufev->input);
-	if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
-		return;
+	/* len = EVBUFFER_LENGTH(bufev->input); */
+	/* if (bufev->wm_read.low != 0 && len < bufev->wm_read.low) */
+	/* 	return; */
 
-	if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
-		struct evbuffer *buf = bufev->input;
-		event_del(&bufev->ev_read);
-		evbuffer_setcb(buf, bufferevent_read_pressure_cb, bufev);
-		return;
-	}
+	/* if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) { */
+	/* 	struct evbuffer *buf = bufev->input; */
+	/* 	event_del(&bufev->ev_read); */
+	/* 	evbuffer_setcb(buf, bufferevent_read_pressure_cb, bufev); */
+	/* 	return; */
+	/* } */
 
-	if (bufev->readcb != NULL)
-		(*bufev->readcb)(bufev, bufev->cbarg);
+	/* if (bufev->readcb != NULL) */
+	/* 	(*bufev->readcb)(bufev, bufev->cbarg); */
 
-	return;
-retry:
-	event_del(&bufev->ev_read);
-	event_add(&bufev->ev_read, NULL);
-	return;
-err:
-	(*bufev->errorcb)(bufev, toread, bufev->cbarg);
+	/* return; */
+/* retry: */
+	/* event_del(&bufev->ev_read); */
+	/* event_add(&bufev->ev_read, NULL); */
+	/* return; */
+/* err: */
+	/* (*bufev->errorcb)(bufev, toread, bufev->cbarg); */
 }
 
 void
-client_wr_things(struct client *client, struct thing *thing, size_t len)
+client_write_to_things(struct packages *packages)
 {
-	char			*pkt;
+	struct privsep		*ps = thingsd_env->thingsd_ps;
+	struct package		*package, *tpkg, p;
+	unsigned int		 id, what;
+	int			 fd = -1, n, m;
+	struct iovec		 iov[6];
+	size_t			 c;
 
-	pkt = calloc(len, sizeof(*pkt));
-	if (pkt == NULL)
-		return;
+	TAILQ_FOREACH_SAFE(package, packages, entry, tpkg) {
+		for (id = 0; id < PROC_MAX; id++) {
+			what = ps->ps_what[id];
 
-	switch (thing->type) {
-	case TCP:
-	case DEV:
-		if (thing->persist == false) {
-			thing->fd = open_client_socket(thing->ipaddr,
-			    thing->conn_port);
-			if (thing->fd == -1) {
-				log_warnx("%s: temporary ipaddr connection"
-				    " failed", __func__);
-				free(pkt);
-				return;
-			}
+			if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
+				continue;
 
-			evbuffer_remove(client->evb, pkt, len);
-			write(thing->fd, pkt, len);
-			close(thing->fd);
-			thing->fd = -1;
-		} else {
-			if (thing->fd != -1) {
-				bufferevent_write_buffer(thing->bev,
-				    client->evb);
-				evbuffer_drain(client->evb, len);
+			memcpy(&p, package, sizeof(p));
+
+			c = 0;
+			iov[c].iov_base = &p;
+			iov[c++].iov_len = sizeof(p);
+			if (id == PROC_PARENT) {
+			/* XXX imsg code will close the fd after 1st call */
+				n = -1;
+				proc_range(ps, id, &n, &m);
+				for (n = 0; n < m; n++) {
+					/* send thing fd */
+					if (proc_composev_imsg(ps, id, n,
+					    IMSG_DIST_CLIENT_PACKAGE, -1, fd,
+					    iov, c) != 0) {
+						log_warn("%s: failed to compose"
+						    " IMSG_DIST_THING_PACKAGE"
+						    " imsg",
+					    __func__);
+					return;
+					}
+					if (proc_flush_imsg(ps, id, n) == -1) {
+						log_warn("%s: failed to flush "
+						    "IMSG_DIST_CLIENT_PACKAGE "
+						    "imsg",
+						    __func__);
+						return;
+					}
+				}
 			}
 		}
-		free(pkt);
-		break;
-	default:
-		evbuffer_remove(client->evb, pkt, len);
-		write(thing->fd, pkt, len);
-		free(pkt);
-		break;
+		TAILQ_REMOVE(packages, package, entry);
+		free(package);
 	}
 }
 
@@ -449,160 +272,108 @@ client_wr(struct bufferevent *bev, void *arg)
 void
 client_tls_writecb(int fd, short event, void *arg)
 {
-	struct bufferevent	*bufev = (struct bufferevent *)arg;
-	struct thingsd		*env = bufev->cbarg;
-	struct client		*client = NULL, *tclient;
-	ssize_t			 ret;
-	size_t			 len;
-	int			 towrite = EVBUFFER_WRITE;
+	/* struct bufferevent	*bufev = (struct bufferevent *)arg; */
+	/* struct thingsd		*env = bufev->cbarg; */
+	/* struct client		*client = NULL, *tclient; */
+	/* ssize_t			 ret; */
+	/* size_t			 len; */
+	/* int			 towrite = EVBUFFER_WRITE; */
 
-	TAILQ_FOREACH(tclient, env->clients, entry) {
-		if (tclient->fd == fd)
-			client = tclient;
-	}
+	/* TAILQ_FOREACH(tclient, env->clients, entry) { */
+	/* 	if (tclient->fd == fd) */
+	/* 		client = tclient; */
+	/* } */
 
-	if (EVBUFFER_LENGTH(bufev->output)) {
-		if (client == NULL)
-			return;
+	/* if (EVBUFFER_LENGTH(bufev->output)) { */
+	/* 	if (client == NULL) */
+	/* 		return; */
 
-		ret = tls_write(client->tls_ctx,
-		    EVBUFFER_DATA(bufev->output),
-		    EVBUFFER_LENGTH(bufev->output));
+	/* 	ret = tls_write(client->tls_ctx, */
+	/* 	    EVBUFFER_DATA(bufev->output), */
+	/* 	    EVBUFFER_LENGTH(bufev->output)); */
 
-		if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
-			goto retry;
-		} else if (ret < 0) {
-			towrite |= EVBUFFER_ERROR;
-			goto err;
-		}
+	/* 	if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) { */
+	/* 		goto retry; */
+	/* 	} else if (ret < 0) { */
+	/* 		towrite |= EVBUFFER_ERROR; */
+	/* 		goto err; */
+	/* 	} */
 
-		len = ret;
-		evbuffer_drain(bufev->output, len);
-	}
+	/* 	len = ret; */
+	/* 	evbuffer_drain(bufev->output, len); */
+	/* } */
 
-	if (EVBUFFER_LENGTH(bufev->output) != 0) {
-		event_del(&bufev->ev_write);
-		event_add(&bufev->ev_write, NULL);
-	}
+	/* if (EVBUFFER_LENGTH(bufev->output) != 0) { */
+	/* 	event_del(&bufev->ev_write); */
+	/* 	event_add(&bufev->ev_write, NULL); */
+	/* } */
 
-	if (bufev->writecb != NULL && EVBUFFER_LENGTH(bufev->output) <=
-	    bufev->wm_write.low)
-		(*bufev->writecb)(bufev, bufev->cbarg);
+	/* if (bufev->writecb != NULL && EVBUFFER_LENGTH(bufev->output) <= */
+	/*     bufev->wm_write.low) */
+	/* 	(*bufev->writecb)(bufev, bufev->cbarg); */
 
-	return;
-retry:
-	event_del(&bufev->ev_write);
-	event_add(&bufev->ev_write, NULL);
-	return;
-err:
-	(*bufev->errorcb)(bufev, towrite, bufev->cbarg);
+	/* return; */
+/* retry: */
+	/* event_del(&bufev->ev_write); */
+	/* event_add(&bufev->ev_write, NULL); */
+	/* return; */
+/* err: */
+	/* (*bufev->errorcb)(bufev, towrite, bufev->cbarg); */
 }
 
 void
 client_err(struct bufferevent *bev, short error, void *arg)
 {
-	struct thingsd		*env = (struct thingsd *)arg;
-	struct client		*client;
-	int			 fd = bev->ev_read.ev_fd;
+	struct client		*client = (struct client *)arg;
 
 	if ((error & EVBUFFER_EOF) == 0)
 		log_warnx("%s: client socket error, disconnecting", __func__);
 
-	/* client disconnect */
-	TAILQ_FOREACH(client, env->clients, entry) {
-		if (client->fd == fd) {
-			client_del(env, client);
-			break;
-		}
-	}
+	client_del(client);
 }
 
-void
-client_do_chk(struct thingsd *env)
-{
-	struct client		*client;
-	time_t			 ctime = time(NULL);
+/* void */
+/* clients_show_info(struct privsep *ps, struct imsg *imsg) */
+/* { */
+/* 	char filter[THINGSD_MAXNAME]; */
+/* 	struct client	*client, nci; */
 
-	TAILQ_FOREACH(client, env->clients, entry) {
-		if (client->subscribed == false) {
-			if ((ctime - client->join_time) >= CLIENT_SUB_TIME) {
-				client_del(env, client);
-				break;
-			}
-		}
-	}
-}
+/* 	switch (imsg->hdr.type) { */
+/* 	case IMSG_GET_INFO_CLIENTS_REQUEST: */
 
-void
-*client_chk(void *arg)
-{
-	struct thingsd		*env = (struct thingsd *)arg;
-	void			(*tfptr)(struct thingsd *);
+/* 		memcpy(filter, imsg->data, sizeof(filter)); */
 
-	tfptr = env->client_fptr;
-	while(1) {
-		sleep(CLIENT_SUB_CHK);
-		(void)(*tfptr)(env);
-	}
-	pthread_exit(NULL);
-}
+/* 		TAILQ_FOREACH(client, thingsd_env->clients, entry) { */
+/* 			if (filter[0] == '\0' || memcmp(filter, */
+/* 			    client->name, sizeof(filter)) == 0) { */
 
-void
-start_client_chk(struct thingsd *env)
-{
-	pthread_t		 tclient_chk;
-	int			 rclient_chk;
+/* 				memcpy(&nci.name, client->name, */
+/* 				    sizeof(nci.name)); */
 
-	rclient_chk = pthread_create(&tclient_chk, NULL, client_chk,
-	    (void *)env);
+/* 				nci.subscribed = client->subscribed; */
+/* 				nci.fd = client->fd; */
+/* 				nci.port = client->port; */
+/* 				nci.tls = client->tls; */
+/* 				nci.subs = client->subs; */
 
-	if (rclient_chk) {
-		log_warnx("%s: thread creation failed", __func__);
-		client_chk((void *) env);
-	}
-}
+/* 				if (proc_compose_imsg(ps, PROC_CONTROL, -1, */
+/* 				    IMSG_GET_INFO_CLIENTS_DATA, */
+/* 				    imsg->hdr.peerid, -1, &nci, */
+/* 				    sizeof(nci)) == -1) */
+/* 					return; */
 
-void
-clients_show_info(struct privsep *ps, struct imsg *imsg)
-{
-	char filter[THINGSD_MAXNAME];
-	struct client	*client, nci;
+/* 			} */
+/* 		} */
 
-	switch (imsg->hdr.type) {
-	case IMSG_GET_INFO_CLIENTS_REQUEST:
+/* 		if (proc_compose_imsg(ps, PROC_CONTROL, -1, */
+/* 		    IMSG_GET_INFO_CLIENTS_END_DATA, imsg->hdr.peerid, */
+/* 			    -1, &nci, sizeof(nci)) == -1) */
+/* 				return; */
 
-		memcpy(filter, imsg->data, sizeof(filter));
+/* 		break; */
+/* 	default: */
+/* 		log_debug("%s: error handling imsg", __func__); */
+/* 		break; */
+/* 	} */
+/* } */
 
-		TAILQ_FOREACH(client, thingsd_env->clients, entry) {
-			if (filter[0] == '\0' || memcmp(filter,
-			    client->name, sizeof(filter)) == 0) {
-
-				memcpy(&nci.name, client->name,
-				    sizeof(nci.name));
-
-				nci.subscribed = client->subscribed;
-				nci.fd = client->fd;
-				nci.port = client->port;
-				nci.tls = client->tls;
-				nci.subs = client->subs;
-
-				if (proc_compose_imsg(ps, PROC_CONTROL, -1,
-				    IMSG_GET_INFO_CLIENTS_DATA,
-				    imsg->hdr.peerid, -1, &nci,
-				    sizeof(nci)) == -1)
-					return;
-
-			}
-		}
-
-		if (proc_compose_imsg(ps, PROC_CONTROL, -1,
-		    IMSG_GET_INFO_CLIENTS_END_DATA, imsg->hdr.peerid,
-			    -1, &nci, sizeof(nci)) == -1)
-				return;
-
-		break;
-	default:
-		log_debug("%s: error handling imsg", __func__);
-		break;
-	}
-}
blob - 00671e197709093d56ca09d3a767a9ac91ca0a18
blob + 19118da89fd2306aa44a910bc38e30615d0eeae2
--- thingsd/config.c	
+++ thingsd/config.c	
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2020-2021 Tracey Emery <tracey@traceyemery.net>
  * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -20,11 +20,11 @@
 #include <sys/time.h>
 #include <sys/uio.h>
 #include <sys/socket.h>
+#include <sys/tree.h>
 
 #include <net/if.h>
 #include <netinet/in.h>
 
-#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <termios.h>
@@ -45,56 +45,151 @@ extern enum privsep_procid privsep_process;
 int
 config_init(struct thingsd *env)
 {
-	struct privsep *ps = &env->thingsd_ps;
-	unsigned int	 what;
+	struct privsep		*ps = env->thingsd_ps;
+	unsigned int		 what;
 
 	/* Global configuration. */
 	if (privsep_process == PROC_PARENT)
-		env->prefork_things = THINGS_NUMPROC;
+		env->prefork_socks = SOCKS_NUMPROC;
 
 	ps->ps_what[PROC_PARENT] = CONFIG_ALL;
-	ps->ps_what[PROC_THINGS] = CONFIG_THINGS;
+	ps->ps_what[PROC_SOCKS] = CONFIG_SOCKS;
 
 	/* Other configuration. */
 	what = ps->ps_what[privsep_process];
-	if (what & CONFIG_THINGS) {
-		env->things = calloc(1, sizeof(*env->things));
-		if (env->things == NULL)
+	if (what & CONFIG_SOCKS) {
+		if ((env->things = calloc(1, sizeof(*env->things))) == NULL)
 			return (-1);
-		env->sockets = calloc(1, sizeof(*env->sockets));
-		if (env->sockets == NULL)
+		if ((env->sockets = calloc(1, sizeof(*env->sockets))) == NULL)
 			return (-1);
-		env->clients = calloc(1, sizeof(*env->clients));
-		if (env->clients == NULL)
+		if ((env->packages = calloc(1, sizeof(*env->packages))) == NULL)
 			return (-1);
-		env->dead_things = calloc(1, sizeof(*env->dead_things));
-		if (env->dead_things == NULL)
+		if ((env->dead_things = calloc(1,
+		    sizeof(*env->dead_things))) == NULL)
 			return (-1);
-		env->dead_things->dead_things_list = calloc(1,
-		    sizeof(*env->dead_things->dead_things_list));
-		if (env->dead_things->dead_things_list == NULL)
-			return (-1);
-		env->packet_clients = calloc(1, sizeof(*env->packet_clients));
-		if (env->packet_clients == NULL)
-			return (-1);
+		/* env->packet_clients = calloc(1, sizeof(*env->packet_clients)); */
+		/* if (env->packet_clients == NULL) */
+		/* 	return (-1); */
 		TAILQ_INIT(env->things);
 		TAILQ_INIT(env->sockets);
-		TAILQ_INIT(env->clients);
-		TAILQ_INIT(env->dead_things->dead_things_list);
-		TAILQ_INIT(env->packet_clients);
+		TAILQ_INIT(env->packages);
+		TAILQ_INIT(env->dead_things);
+		/* TAILQ_INIT(env->packet_clients); */
 	}
 	return (0);
 }
 
+int
+config_getcfg(struct thingsd *env, struct imsg *imsg)
+{
+	/* nothing to do but tell parent configuration is done */
+	if (privsep_process != PROC_PARENT)
+		proc_compose(env->thingsd_ps, PROC_PARENT,
+		    IMSG_CFG_DONE, NULL, 0);
+
+	return (0);
+}
+
+int
+config_getsocks(struct thingsd *env, struct imsg *imsg)
+{
+	struct socket		*sock = NULL;
+	struct socket_config	 sock_conf;
+	uint8_t			*p = imsg->data;
+
+	IMSG_SIZE_CHECK(imsg, &sock_conf);
+	memcpy(&sock_conf, p, sizeof(sock_conf));
+
+	if (IMSG_DATA_SIZE(imsg) != sizeof(sock_conf)) {
+		log_debug("%s: imsg size error", __func__);
+		return (-1);
+	}
+
+	/* create a new socket */
+	if ((sock = calloc(1, sizeof(*sock))) == NULL) {
+		if (imsg->fd != -1)
+			close(imsg->fd);
+		return (-1);
+	}
+
+	memcpy(&sock->conf, &sock_conf, sizeof(sock->conf));
+	sock->fd = imsg->fd;
+
+	TAILQ_INSERT_TAIL(env->sockets, sock, entry);
+
+	return (0);
+}
+
+int
+config_setsocks(struct thingsd *env, struct socket *ts)
+{
+	struct privsep		*ps = env->thingsd_ps;
+	struct socket_config	 u;
+	int			 fd = -1, n, m;
+	struct iovec		 iov[6];
+	size_t			 c;
+	unsigned int		 id, what;
+
+	/* setup socket in priv process */
+	if (sockets_privinit(ts) == -1)
+		return (-1);
+
+	for (id = 0; id < PROC_MAX; id++) {
+		what = ps->ps_what[id];
+
+		if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
+			continue;
+
+		memcpy(&u, &ts->conf, sizeof(u));
+
+		c = 0;
+		iov[c].iov_base = &u;
+		iov[c++].iov_len = sizeof(u);
+		if (id == PROC_SOCKS) {
+			/* XXX imsg code will close the fd after 1st call */
+			n = -1;
+			proc_range(ps, id, &n, &m);
+			for (n = 0; n < m; n++) {
+				/* send thing fd */
+				if (ts->fd == -1)
+					fd = -1;
+				else if ((fd = dup(ts->fd)) == -1)
+					return (-1);
+				if (proc_composev_imsg(ps, id, n,
+				    IMSG_CFG_SOCKS, -1, fd, iov,
+				    c) != 0) {
+					log_warn("%s: failed to compose "
+					    "IMSG_CFG_SOCKS imsg",
+					    __func__);
+					return (-1);
+				}
+				if (proc_flush_imsg(ps, id, n) == -1) {
+					log_warn("%s: failed to flush "
+					    "IMSG_CFG_SOCKS imsg",
+					    __func__);
+					return (-1);
+				}
+			}
+		}
+	}
+
+	/* Close thing socket early to prevent fd exhaustion in thingsd. */
+	if (ts->fd != -1) {
+		close(ts->fd);
+		ts->fd = -1;
+	}
+	return (0);
+}
+
 void
 config_purge(struct thingsd *env, unsigned int reset)
 {
-	struct privsep		*ps = &env->thingsd_ps;
+	struct privsep		*ps = env->thingsd_ps;
 	struct thing		*thing;
 	unsigned int		 what;
 
 	what = ps->ps_what[privsep_process];
-	if (what & CONFIG_THINGS) {
+	if (what & CONFIG_SOCKS) {
 		while ((thing = TAILQ_FIRST(env->things)) != NULL) {
 			TAILQ_REMOVE(env->things, thing, entry);
 			free(thing);
@@ -105,7 +200,7 @@ config_purge(struct thingsd *env, unsigned int reset)
 int
 config_setreset(struct thingsd *env, unsigned int reset)
 {
-	struct privsep	*ps = &env->thingsd_ps;
+	struct privsep	*ps = env->thingsd_ps;
 	unsigned int	 id;
 
 	for (id = 0; id < PROC_MAX; id++) {
blob - 9d98aede1721a3938abbb8d79c3928d8327cb68e
blob + a02ca3676cd2742caa5e2425933a615437a2f5bf
--- thingsd/control.c	
+++ thingsd/control.c	
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2020-2021 Tracey Emery <tracey@traceyemery.net>
  * Copyright (c) 2010-2015 Reyk Floeter <reyk@openbsd.org>
  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  *
@@ -30,7 +30,6 @@
 #include <event.h>
 #include <fcntl.h>
 #include <imsg.h>
-#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -48,12 +47,12 @@ void	 control_accept(int, short, void *);
 void	 control_close(struct privsep *, struct imsg *, int,
 	    struct control_sock *);
 void	 control_dispatch_imsg(int, short, void *);
-int	 control_dispatch_parent(int, struct privsep_proc *, struct imsg *);
+int	 control_dispatch_thingsd(int, struct privsep_proc *, struct imsg *);
 void	 control_imsg_forward(struct imsg *);
 void	 control_run(struct privsep *, struct privsep_proc *, void *);
 
 static struct privsep_proc procs[] = {
-	{ "parent",	PROC_PARENT,	control_dispatch_parent },
+	{ "thingsd",	PROC_PARENT,	control_dispatch_thingsd },
 };
 
 void
@@ -70,7 +69,7 @@ control_run(struct privsep *ps, struct privsep_proc *p
 }
 
 int
-control_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
+control_dispatch_thingsd(int fd, struct privsep_proc *p, struct imsg *imsg)
 {
 	struct ctl_conn		*c;
 	struct privsep		*ps = p->p_ps;
@@ -82,8 +81,8 @@ control_dispatch_parent(int fd, struct privsep_proc *p
 		bad = 1;
 	case IMSG_SHOW_PACKETS_DATA:
 	case IMSG_SHOW_PACKETS_END_DATA:
-	case IMSG_GET_INFO_PARENT_DATA:
-	case IMSG_GET_INFO_PARENT_END_DATA:
+	case IMSG_GET_INFO_THINGSD_DATA:
+	case IMSG_GET_INFO_THINGSD_END_DATA:
 	case IMSG_GET_INFO_THINGS_DATA:
 	case IMSG_GET_INFO_THINGS_END_DATA:
 	case IMSG_GET_INFO_CLIENTS_DATA:
@@ -106,7 +105,9 @@ control_dispatch_parent(int fd, struct privsep_proc *p
 		    0, 0, -1, imsg->data, IMSG_DATA_SIZE(imsg));
 		break;
 	case IMSG_CTL_RESET:
-		/* We don't have anything to do to reset. */
+	case IMSG_CFG_DONE:
+	case IMSG_CTL_START:
+		/* don't do anything */
 		break;
 	default:
 		return (-1);
@@ -288,7 +289,7 @@ control_close(struct privsep *ps, struct imsg *imsg, i
 	if (proc_compose_imsg(ps, PROC_PARENT, -1,
 	    IMSG_SHOW_PACKETS_END_DATA, 0, -1,
 	    &fd, sizeof(fd)) == -1)
-		log_warn("fd not sent to parent to close");
+		log_warn("fd not sent to thingsd to close");
 
 	msgbuf_clear(&c->iev.ibuf.w);
 	TAILQ_REMOVE(&ctl_conns, c, entry);
@@ -354,7 +355,7 @@ control_dispatch_imsg(int fd, short event, void *arg)
 			break;
 
 		switch (imsg.hdr.type) {
-		case IMSG_GET_INFO_PARENT_REQUEST:
+		case IMSG_GET_INFO_THINGSD_REQUEST:
 		case IMSG_GET_INFO_THINGS_REQUEST:
 		case IMSG_GET_INFO_CLIENTS_REQUEST:
 		case IMSG_GET_INFO_SOCKETS_REQUEST:
@@ -400,7 +401,7 @@ control_dispatch_imsg(int fd, short event, void *arg)
 			break;
 		case IMSG_KILL_CLIENT:
 		case IMSG_SHOW_PACKETS_REQUEST:
-		case IMSG_GET_INFO_PARENT_REQUEST:
+		case IMSG_GET_INFO_THINGSD_REQUEST:
 		case IMSG_GET_INFO_THINGS_REQUEST:
 		case IMSG_GET_INFO_CLIENTS_REQUEST:
 		case IMSG_GET_INFO_SOCKETS_REQUEST:
blob - 8958a4552140b6c9e7791f518c326da9a024d3ac
blob + 0eb330a50f15d0cbe7921bec854f9f9b98e6e320
--- thingsd/parse.y	
+++ thingsd/parse.y	
@@ -1,5 +1,9 @@
 /*
- * Copyright (c) 2016-2019, 2020 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2016-2021 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2020 Matthias Pressfreund <mpfr@fn.de>
+ * Copyright (c) 2007 - 2015 Reyk Floeter <reyk@openbsd.org>
+ * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
+ * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org>
  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
  * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
  * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -21,16 +25,16 @@
  */
 
 %{
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/queue.h>
 #include <sys/socket.h>
+#include <sys/sockio.h>
 #include <sys/stat.h>
 
-#include <net/if.h>
-#include <netinet/in.h>
-
-#include <arpa/inet.h>
-
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
@@ -38,7 +42,7 @@
 #include <ifaddrs.h>
 #include <imsg.h>
 #include <limits.h>
-#include <stdbool.h>
+#include <netdb.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
@@ -72,7 +76,6 @@ int		 lookup(char *);
 int		 lgetc(int);
 int		 lungetc(int);
 int		 findeol(void);
-int		 load_tls(struct thing *);
 
 TAILQ_HEAD(symhead, sym)	 symhead = TAILQ_HEAD_INITIALIZER(symhead);
 struct sym {
@@ -83,6 +86,7 @@ struct sym {
 	char			*val;
 };
 
+int	 getservice(char *);
 int	 symset(const char *, const char *, int);
 char	*symget(const char *);
 
@@ -90,23 +94,25 @@ void	 clear_config(struct thingsd *xconf);
 
 static int		 errors;
 
+int		 get_addrs(const char *, struct addresslist *,
+		    struct portrange *);
+
+static struct thingsd		*thingsd;
 static struct thing		*new_thing;
-static struct subscription	*new_sub(char *);
 
 const int		 baudrates[18] = {50, 75, 110, 134, 150, 200,
 			    300, 600, 1200, 1800, 2400, 4800, 9600,
 			    38400, 57600, 76800, 115200};
 const char		*parity[4] = {"none", "odd", "even", "space"};
-struct client		*client, *tclient;
+struct client		*pclient, *tclient;
 char			 my_name[THINGSD_MAXTEXT];
 int			 my_fd, pkt_len;
 
-struct thing		*conf_new_thing(char *);
-
 typedef struct {
 	union {
-		int64_t		 number;
-		char		*string;
+		long long		 number;
+		char			*string;
+		struct portrange	 port;
 	} v;
 	int lineno;
 } YYSTYPE;
@@ -114,15 +120,16 @@ typedef struct {
 %}
 
 %token	BAUD BIND CA CERTIFICATE CIPHERS CLIENT CLIENTS CONNECT CONNECTION CRL
-%token	DATA DEFAULT DHE ECDHE ERROR HARDWARE INCLUDE INTERFACE IPADDR KEY
+%token	DATA DHE ECDHE ERROR HARDWARE INCLUDE INTERFACE IPADDR KEY
 %token	LISTEN LOCATION MAX NAME OCSP ON OPTIONAL PARITY PASSWORD PERSISTENT
 %token	PORT PREFORK PROTOCOLS RECEIVE RETRY SOFTWARE STOP SUBSCRIBE
-%token	SUBSCRIPTIONS THING THINGS TLS UDP VERBOSE
+%token	THING THINGS TCP TLS UDP VERBOSE
 
 %token	<v.string>	STRING
 %token	<v.number>	NUMBER
 %type	<v.string>	string
 %type	<v.number>	opttls
+%type	<v.port>	port
 
 %%
 
@@ -137,34 +144,90 @@ grammar		: /* empty */
 		;
 
 conf_main	: bindopts1
-		| DEFAULT PORT NUMBER {
-			thingsd_env->port = $3;
-		}
 		| maxclients
-		| maxsubs
 		| PREFORK NUMBER {
-			thingsd_env->prefork_things = $2;
+			thingsd->prefork_socks = $2;
 			if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
 				yyerror("invalid number of preforked "
-				    "servers: %lld", $2);
+				    "sockets: %lld", $2);
 				YYERROR;
 			}
 		}
 		| thingretry
 		;
 
-bindopts1	: BIND INTERFACE STRING {
-			memcpy(&thingsd_env->iface, $3,
-			    sizeof(thingsd_env->iface));
-			free($3);
+bindopts1	: BIND TCP INTERFACE STRING {
+			if (strlcpy(thingsd->tcp_iface, $4,
+			    sizeof(thingsd->tcp_iface)) >=
+			    sizeof(thingsd->tcp_iface)) {
+				yyerror("%s: thingsd tcp_iface truncated",
+				    __func__);
+				free($4);
+				free(thingsd);
+				YYERROR;
+			}
+			if (strlcpy(thingsd->udp_iface, $4,
+			    sizeof(thingsd->udp_iface)) >=
+			    sizeof(thingsd->udp_iface)) {
+				yyerror("%s: thingsd udp_iface truncated",
+				    __func__);
+				free($4);
+				free(thingsd);
+				YYERROR;
+			}
+			free($4);
 		}
+		| BIND UDP INTERFACE STRING {
+			memset(&thingsd->udp_iface, 0,
+			    sizeof(thingsd->udp_iface));
+			if (strlcpy(thingsd->udp_iface, $4,
+			    sizeof(thingsd->udp_iface)) >=
+			    sizeof(thingsd->udp_iface)) {
+				yyerror("%s: thingsd udp_iface truncated",
+				    __func__);
+				free($4);
+				free(thingsd);
+				YYERROR;
+			}
+			free($4);
+		}
 		;
 
-bindopts2	: BIND INTERFACE STRING {
-			memcpy(&new_thing->iface, $3,
-			    sizeof(new_thing->iface));
-			free($3);
+bindopts2	: BIND TCP INTERFACE STRING {
+			if (strlcpy(new_thing->conf.tcp_iface, $4,
+			    sizeof(new_thing->conf.tcp_iface)) >=
+			    sizeof(new_thing->conf.tcp_iface)) {
+				yyerror("%s: new_thing tcp_iface truncated",
+				    __func__);
+				free($4);
+				free(new_thing);
+				YYERROR;
+			}
+			if (strlen(new_thing->conf.udp_iface) == 0) {
+				if (strlcpy(new_thing->conf.udp_iface, $4,
+				    sizeof(new_thing->conf.udp_iface)) >=
+				    sizeof(new_thing->conf.udp_iface)) {
+					yyerror("%s: new_thing udp_iface "
+					    "truncated", __func__);
+					free($4);
+					free(new_thing);
+					YYERROR;
+				}
+			}
+			free($4);
 		}
+		| BIND UDP INTERFACE STRING {
+			if (strlcpy(new_thing->conf.udp_iface, $4,
+			    sizeof(new_thing->conf.udp_iface)) >=
+			    sizeof(new_thing->conf.udp_iface)) {
+				yyerror("%s: new_thing udp_iface truncated",
+				    __func__);
+				free($4);
+				free(new_thing);
+				YYERROR;
+			}
+			free($4);
+		}
 		;
 
 dosub		: SUBSCRIBE '{' optnl subopts '}'
@@ -173,8 +236,7 @@ dosub		: SUBSCRIBE '{' optnl subopts '}'
 include		: INCLUDE STRING		{
 			struct file	*nfile;
 
-			nfile = newfile($2, 1);
-			if (nfile == NULL) {
+			if ((nfile = newfile($2, 1)) == NULL) {
 				yyerror("failed to include file %s", $2);
 				free($2);
 				YYERROR;
@@ -186,6 +248,29 @@ include		: INCLUDE STRING		{
 		}
 		;
 
+port		: NUMBER {
+			if ($1 <= 0 || $1 > (int)USHRT_MAX) {
+				yyerror("invalid port: %lld", $1);
+				YYERROR;
+			}
+			$$.val[0] = htons($1);
+			$$.op = 1;
+		}
+		| STRING {
+			int	 val;
+
+			if ((val = getservice($1)) == -1) {
+				yyerror("invalid port: %s", $1);
+				free($1);
+				YYERROR;
+			}
+			free($1);
+
+			$$.val[0] = val;
+			$$.op = 1;
+		}
+		;
+
 locationopts	: /* empty */
 		| '{' optnl locationopts2 '}'
 		;
@@ -196,14 +281,14 @@ locationopts1	: bindopts2
 			const int	 bauds = (sizeof(baudrates) /
 					    sizeof(const int));
 
-			new_thing->baud = -1;
+			new_thing->conf.baud = -1;
 			for (bc = 0; bc < bauds; bc++) {
 				if ($2 == baudrates[bc]) {
-					new_thing->baud = $2;
+					new_thing->conf.baud = $2;
 					continue;
 				}
 			}
-			if (new_thing->baud == -1) {
+			if (new_thing->conf.baud == -1) {
 				yyerror("baud rate syntax error");
 				YYERROR;
 			}
@@ -213,30 +298,40 @@ locationopts1	: bindopts2
 				yyerror("data bits syntax error");
 				YYERROR;
 			} else
-				new_thing->data_bits = $2;
+				new_thing->conf.data_bits = $2;
 		}
 		| HARDWARE NUMBER {
 			if ($2 > 1 || $2 < 0) {
 				yyerror("hardware syntax error");
 				YYERROR;
 			} else if ($2 > 0)
-				new_thing->hw_ctl = true;
+				new_thing->conf.hw_ctl = 1;
 		}
-		| LISTEN ON opttls PORT NUMBER {
+		| LISTEN ON opttls PORT port {
 			struct thing	*thing;
 
-			TAILQ_FOREACH(thing, thingsd_env->things, entry) {
-				if (thing->port == $5 && (thing->tls || $3)) {
-					yyerror("tls port already used");
+			TAILQ_FOREACH(thing, thingsd->things, entry) {
+				if (thing->conf.tcp_listen_port.val[0] ==
+				    $5.val[0]) {
+					yyerror("tls port already assigned");
 					YYERROR;
 				}
 			}
-			new_thing->port = $5;
+
+			new_thing->conf.tcp_listen_port.val[0] = $5.val[0];
+			new_thing->conf.tcp_listen_port.op = $5.op;
 		}
 		| maxclientssub
 		| PASSWORD STRING {
-			memcpy(&new_thing->password, $2,
-			    sizeof(new_thing->password));
+			if (strlcpy(new_thing->conf.password, $2,
+			    sizeof(new_thing->conf.password)) >=
+			    sizeof(new_thing->conf.password)) {
+				yyerror("%s: new_thing password truncated",
+				    __func__);
+				free($2);
+				free(new_thing);
+				YYERROR;
+			}
 			free($2);
 		}
 		| PARITY STRING {
@@ -246,12 +341,19 @@ locationopts1	: bindopts2
 
 			for (pc = 0; pc < parities; pc++) {
 				if (strcmp($2, parity[pc]) == 0) {
-					memcpy(&new_thing->parity, $2,
-					    sizeof(new_thing->parity));
+					if (strlcpy(new_thing->conf.parity, $2,
+					    sizeof(new_thing->conf.parity)) >=
+					    sizeof(new_thing->conf.parity)) {
+						yyerror("%s: new_thing parity "
+						    "truncated", __func__);
+						free($2);
+						free(new_thing);
+						YYERROR;
+					}
 					continue;
 				}
 			}
-			if (strlen(new_thing->parity) == 0) {
+			if (strlen(new_thing->conf.parity) == 0) {
 				free($2);
 				yyerror("parity syntax error");
 				YYERROR;
@@ -263,17 +365,17 @@ locationopts1	: bindopts2
 				yyerror("software syntax error");
 				YYERROR;
 			} else
-				new_thing->sw_ctl = true;
+				new_thing->conf.sw_ctl = 1;
 		}
 		| STOP NUMBER {
 			if ($2 > 2 || $2 < 1) {
 				yyerror("stop bits syntax error");
 				YYERROR;
 			} else if ($2 > 0)
-				new_thing->stop_bits = $2;
+				new_thing->conf.stop_bits = $2;
 		}
 		| TLS tlsopts {
-			if (new_thing->tls == false) {
+			if (new_thing->conf.tls == 0) {
 				yyerror("tls options without tls listener");
 				YYERROR;
 			}
@@ -285,31 +387,19 @@ locationopts2	: locationopts2 locationopts1 nl
 		;
 
 maxclients	: MAX CLIENTS NUMBER {
-			thingsd_env->max_clients = $3;
+			thingsd->max_clients = $3;
 		}
 		;
 
 maxclientssub	: MAX CLIENTS NUMBER {
-			new_thing->max_clients = $3;
+			new_thing->conf.max_clients = $3;
 		}
 		;
 
-maxsubs		: MAX SUBSCRIPTIONS NUMBER {
-			thingsd_env->max_subs = $3;
-		}
-		;
-
 name		: NAME optcomma STRING {
-			/* test we're us and set name */
-			TAILQ_FOREACH(client, thingsd_env->clients, entry) {
-				if (client == tclient) {
-					memset(&my_name, 0, sizeof(my_name));
-					memcpy(&client->name, $3,
-					    sizeof(client->name));
-					memcpy(&my_name, $3, sizeof(my_name));
-					break;
-				}
-			}
+			memset(&my_name, 0, sizeof(my_name));
+			memcpy(&pclient->name, $3, sizeof(pclient->name));
+			memcpy(&my_name, $3, sizeof(my_name));
 			free($3);
 		}
 		;
@@ -327,43 +417,72 @@ optnl		: '\n' optnl		/* zero or more newlines */
 
 opttls		: /* empty */ {
 			$$ = 0;
-			new_thing->tls = false;
+			new_thing->conf.tls = 0;
 		}
 		| TLS {
 			$$ = 1;
-			new_thing->tls = true;
+			new_thing->conf.tls = 1;
 		}
 		;
 
-socketopts1	: CONNECT ON PORT NUMBER {
-			new_thing->conn_port = $4;
+socketopts1	: CONNECT ON PORT port {
+			new_thing->conf.tcp_conn_port.val[0] = $4.val[0];
+			new_thing->conf.tcp_conn_port.op = $4.op;
 		}
-		| LISTEN ON opttls PORT NUMBER {
+		| LISTEN ON opttls PORT port {
 			struct thing	*thing;
-			TAILQ_FOREACH(thing, thingsd_env->things, entry) {
-				if (thing->port == $5 && (thing->tls || $3)) {
-					yyerror("tls port already used");
+			TAILQ_FOREACH(thing, thingsd->things, entry) {
+				if (thing->conf.tcp_listen_port.val[0] ==
+				    $5.val[0]) {
+					yyerror("port already assigned");
 					YYERROR;
 				}
 			}
-			new_thing->port = $5;
+
+			new_thing->conf.tcp_listen_port.val[0] = $5.val[0];
+			new_thing->conf.tcp_listen_port.op = $5.op;
 		}
-		| RECEIVE ON PORT NUMBER {
-			new_thing->rcv_port = $4;
+		| RECEIVE ON PORT port {
+			struct thing *thing;
+			TAILQ_FOREACH(thing, thingsd_env->things, entry) {
+				if (thing->conf.udp_rcv_port.val[0] == 0 ||
+				    $4.val[0] == 0)
+					continue;
+				if (thing->conf.udp_rcv_port.val[0] ==
+				    $4.val[0]) {
+					yyerror("UDP thing receive ports must "
+					   "be unique");
+					   YYERROR;
+				}
+			}
+			new_thing->conf.udp_rcv_port.val[0] = $4.val[0];
+			new_thing->conf.udp_rcv_port.op = $4.op;
+
+			/* if((new_thing->conf.udp_rcv_port = */
+			/*     strdup($4)) == NULL) */
+			/* 	fatal("%s: strdup", __func__); */
+			/* free($4); */
 		}
 		| PASSWORD STRING {
-			memcpy(&new_thing->password, $2,
-			    sizeof(new_thing->password));
+			if (strlcpy(new_thing->conf.password, $2,
+			    sizeof(new_thing->conf.password)) >=
+			    sizeof(new_thing->conf.password)) {
+				yyerror("%s: new_thing password truncated",
+				    __func__);
+				free($2);
+				free(new_thing);
+				YYERROR;
+			}
 			free($2);
 		}
 		| PERSISTENT NUMBER {
 			if ($2)
-				new_thing->persist = true;
+				new_thing->conf.persist = 1;
 			else
-				new_thing->persist = false;
+				new_thing->conf.persist = 0;
 		}
 		| TLS tlsopts {
-			if (new_thing->tls == false) {
+			if (new_thing->conf.tls == 0) {
 				yyerror("tls options without tls listener");
 				YYERROR;
 			}
@@ -393,60 +512,75 @@ subopts		: {
 		} '{' name '}' optcomma '{' things '}'
 		;
 
-subthings	: THING '{' STRING optcomma STRING '}' optcomma {
-			struct thing		*thing = NULL;
-			struct subscription	*sub, *nsub;
-			bool			 fail = false;
+subthings	: THING '{' STRING optcomma STRING '}' {
+			struct socket	*sock = pclient->sock, *tsock;
+			struct socket	*psock = NULL, *csock = NULL;
+			struct client	*clt;
 
-			/* check for duplicate name and subscriptions */
-			TAILQ_FOREACH(client, thingsd_env->clients, entry) {
-				if (strlen(client->name) == 0 ||
-				    strlen(my_name) == 0)
-					continue;
-				if (strcmp(client->name, my_name) == 0) {
-					if (my_fd != client->fd) {
-						fail = true;
+			/* check thing name */
+			if ((strcmp(sock->conf.thing_name, $3)) != 0)
+				goto done;
+
+			/* check we have the password */
+			if (strcmp(sock->conf.password, $5) != 0)
+				goto done;
+
+			/* check that ports match */
+			if (sock->conf.port.val[0] != pclient->port.val[0])
+				goto done;
+
+			/*
+			 * XXX: check from here down don't actually work
+			 * when prefork is > 1
+			 * come back and figure this out later
+			 * probably a way to loop through the processes and
+			 * check all
+			 */
+
+			/* check for duplicate name on socket */
+			TAILQ_FOREACH(tsock, thingsd_env->sockets, entry) {
+				TAILQ_FOREACH(clt, tsock->clients, entry) {
+					if (strcmp(clt->name, my_name) == 0 &&
+					    clt->subscribed) {
 						log_warnx("client exists");
+						goto done;
 					}
-					TAILQ_FOREACH(sub,
-					    client->subscriptions, entry)
-						if (strcmp(sub->thing_name,
-						    $3) == 0)
-							fail = true;
-					break;
 				}
 			}
 
-			if (fail)
-				goto done;
-
-			TAILQ_FOREACH(thing, thingsd_env->things, entry) {
-				if (thing->port != client->port) {
-					fail = true;
-					continue;
-				}
-				if (strcmp(thing->name, $3) == 0) {
-					fail = false;
-					break;
-				}
+			/* check parent/child counts */
+			if (sock->conf.child_id) {
+				TAILQ_FOREACH(csock, thingsd_env->sockets,
+				    entry)
+					if (csock->conf.id ==
+					    sock->conf.child_id)
+						break;
 			}
+			if (sock->conf.parent_id) {
+				TAILQ_FOREACH(psock, thingsd_env->sockets,
+				    entry)
+					if (psock->conf.id ==
+					    sock->conf.parent_id)
+						break;
+			}
 
-			if (thing == NULL || fail)
-				goto done;
+			/* if counts are fine, subscribe client */
+			if (sock->conf.max_clients > 0 &&
+			    (sock->client_cnt + 1 >
+			    sock->conf.max_clients)) {
+				log_debug("max clients reached for listener %d",
+				    sock->conf.id);
+			} else {
+				pclient->subscribed = 1;
 
-			if (strcmp(thing->password, $5) == 0) {
-				if (client->subs++ >= thingsd_env->max_subs)
-					log_warn("max subscriptions reached");
-				else {
-					client->subscribed = true;
-					nsub = new_sub(thing->name);
-					TAILQ_INSERT_TAIL(
-					    client->subscriptions, nsub, entry);
-					client->le++;
-					thing->client_cnt++;
-					log_info("client %s subscribed to %s",
-					    client->name, thing->name);
-				}
+				sock->client_cnt++;
+				if (csock != NULL)
+					csock->client_cnt++;
+				if (psock != NULL)
+					psock->client_cnt++;
+
+				log_debug("client %s subscribed to %s",
+				    pclient->name, $3);
 			}
 done:
 			free($3);
@@ -459,80 +593,164 @@ subthings2	: subthings2 subthings
 		;
 
 thing		: THING STRING {
-			new_thing = conf_new_thing($2);
+       			thing_id++;
+			new_thing = thingsd_conf_new_thing(thingsd_env, NULL,
+			    $2, thing_id);
 
-			if (strlen(thingsd_env->iface) != 0) {
-				memcpy(&new_thing->iface, thingsd_env->iface,
-				    sizeof(new_thing->iface));
+			if (strlcpy(new_thing->conf.name, $2,
+			    sizeof(new_thing->conf.name)) >=
+			    sizeof(new_thing->conf.name)) {
+				yyerror("thing name truncated");
+				free($2);
+				free(new_thing);
+				YYERROR;
+			}
+
+			if (strlcpy(new_thing->conf.name, $2,
+			    sizeof(new_thing->conf.name)) >=
+			    sizeof(new_thing->conf.name)) {
+				yyerror("thing name truncated");
+				free($2);
+				free(new_thing);
+				YYERROR;
+			}
+
+			new_thing->exists = 0;
+			new_thing->fd = -1;
+			new_thing->conf.id = thing_id;
+
+			if (strlen(thingsd->tcp_iface) != 0) {
+				if (strlcpy(new_thing->conf.tcp_iface,
+				    thingsd->tcp_iface,
+				    sizeof(new_thing->conf.tcp_iface)) >=
+				    sizeof(new_thing->conf.tcp_iface)) {
+					yyerror("%s: new_thing tcp_iface "
+					    "truncated",
+					    __func__);
+					free(new_thing);
+					YYERROR;
+				}
 			} else
-				memset(new_thing->iface, 0,
-				    sizeof(new_thing->iface));
+				memset(new_thing->conf.tcp_iface, 0,
+				    sizeof(new_thing->conf.tcp_iface));
 
-			memset(new_thing->location, 0,
-			    sizeof(new_thing->location));
-			memset(new_thing->ipaddr, 0, sizeof(new_thing->ipaddr));
-			memset(new_thing->udp, 0, sizeof(new_thing->udp));
-			memset(new_thing->password, 0,
-			    sizeof(new_thing->password));
-			memset(new_thing->parity, 0, sizeof(new_thing->parity));
+			if ((new_thing->conf.tcp_al = calloc(1,
+			    sizeof(*new_thing->conf.tcp_al))) == NULL)
+				fatalx("%s: calloc", __func__);
 
-			new_thing->max_clients = thingsd_env->max_clients;
-			new_thing->port = thingsd_env->port;
-			new_thing->baud = DEFAULT_BAUD;
-			new_thing->conn_port = -1;
-			new_thing->data_bits = -1;
-			new_thing->stop_bits = -1;
-			new_thing->hw_ctl = false;
-			new_thing->sw_ctl = false;
-			new_thing->persist = true;
+			TAILQ_INIT(new_thing->conf.tcp_al);
 
-			new_thing->tls_protocols = TLS_PROTOCOLS_DEFAULT;
-			new_thing->tls_flags = 0;
+			if ((new_thing->conf.udp_al = calloc(1,
+			    sizeof(*new_thing->conf.udp_al))) == NULL)
+				fatalx("%s: calloc", __func__);
 
-			memset(new_thing->tls_cert_file, 0,
-			    sizeof(new_thing->tls_cert_file));
-			memcpy(&new_thing->tls_cert_file, TLS_CERT,
-			    sizeof(new_thing->tls_cert_file));
-			memset(new_thing->tls_key_file, 0,
-			    sizeof(new_thing->tls_key_file));
-			memcpy(&new_thing->tls_key_file, TLS_KEY,
-			    sizeof(new_thing->tls_key_file));
-			memcpy(&new_thing->tls_ciphers, TLS_CIPHERS,
-			    sizeof(new_thing->tls_ciphers));
-			memcpy(&new_thing->tls_dhe_params, TLS_DHE_PARAMS,
-			    sizeof(new_thing->tls_dhe_params));
-			memcpy(&new_thing->tls_ecdhe_curves,
+			TAILQ_INIT(new_thing->conf.udp_al);
+
+			if (strlen(thingsd->udp_iface) != 0) {
+				if (strlcpy(new_thing->conf.udp_iface,
+				    thingsd->udp_iface,
+				    sizeof(new_thing->conf.udp_iface)) >=
+				    sizeof(new_thing->conf.udp_iface)) {
+					yyerror("%s: new_thing udp_iface "
+					    "truncated",
+					    __func__);
+					free(new_thing);
+					YYERROR;
+				}
+			} else
+				memset(new_thing->conf.udp_iface, 0,
+				    sizeof(new_thing->conf.udp_iface));
+
+			memset(new_thing->conf.location, 0,
+			    sizeof(new_thing->conf.location));
+			memset(new_thing->conf.ipaddr, 0,
+			    sizeof(new_thing->conf.ipaddr));
+			memset(new_thing->conf.udp, 0,
+			    sizeof(new_thing->conf.udp));
+			memset(new_thing->conf.password, 0,
+			    sizeof(new_thing->conf.password));
+			memset(new_thing->conf.parity, 0,
+			    sizeof(new_thing->conf.parity));
+
+			new_thing->conf.max_clients = thingsd->max_clients;
+
+			new_thing->conf.baud = DEFAULT_BAUD;
+			new_thing->conf.data_bits = -1;
+			new_thing->conf.stop_bits = -1;
+			new_thing->conf.hw_ctl = 0;
+			new_thing->conf.sw_ctl = 0;
+			new_thing->conf.persist = 1;
+
+			new_thing->conf.tls_protocols = TLS_PROTOCOLS_DEFAULT;
+			new_thing->conf.tls_flags = 0;
+
+			memset(new_thing->conf.tls_ciphers, 0,
+			    sizeof(new_thing->conf.tls_ciphers));
+			memset(new_thing->conf.tls_dhe_params, 0,
+			    sizeof(new_thing->conf.tls_dhe_params));
+			memset(new_thing->conf.tls_ecdhe_curves, 0,
+			    sizeof(new_thing->conf.tls_ecdhe_curves));
+
+			if((new_thing->conf.tls_cert_file =
+			    strdup(TLS_CERT)) == NULL)
+				fatal("%s: strdup", __func__);
+			if((new_thing->conf.tls_key_file =
+			    strdup(TLS_KEY)) == NULL)
+				fatal("%s: strdup", __func__);
+
+			strlcpy(new_thing->conf.tls_ciphers, TLS_CIPHERS,
+			    sizeof(new_thing->conf.tls_ciphers));
+			strlcpy(new_thing->conf.tls_dhe_params, TLS_DHE_PARAMS,
+			    sizeof(new_thing->conf.tls_dhe_params));
+			strlcpy(new_thing->conf.tls_ecdhe_curves,
 			    TLS_ECDHE_CURVES,
-			    sizeof(new_thing->tls_ecdhe_curves));
+			    sizeof(new_thing->conf.tls_ecdhe_curves));
 			free($2);
+
 		} '{' optnl thingopts2 '}' {
-			if (strlen(new_thing->ipaddr) != 0 &&
-			    new_thing->conn_port == -1) {
-				yyerror("ipaddr connect port empty");
+			if (new_thing->conf.tcp_listen_port.val[0] == 0) {
+				yyerror("thing listen port required");
 				YYERROR;
 			}
-			if (strlen(new_thing->ipaddr) != 0 &&
-			    strlen(new_thing->location) != 0) {
+			if (strlen(new_thing->conf.ipaddr) != 0 &&
+			    new_thing->conf.tcp_conn_port.val[0] == 0) {
+				yyerror("ipaddr connect port required");
+				YYERROR;
+			}
+			if (strlen(new_thing->conf.ipaddr) != 0 &&
+			    strlen(new_thing->conf.location) != 0) {
 				yyerror("too many ipaddr device arguments");
 				YYERROR;
 			}
-			if (strlen(new_thing->udp) != 0 &&
-			    new_thing->rcv_port == -1) {
-				yyerror("udp receive port empty");
+			if (strlen(new_thing->conf.udp) != 0 &&
+			    strlen(new_thing->conf.udp_iface) == 0) {
+				yyerror("udp bind interface required");
 				YYERROR;
 			}
-			if (strlen(new_thing->udp) != 0 &&
-			    strlen(new_thing->location) != 0) {
+			if (strlen(new_thing->conf.udp) != 0 &&
+			    new_thing->conf.udp_rcv_port.val[0] == 0) {
+				yyerror("udp receive port required");
+				YYERROR;
+			}
+			if (strlen(new_thing->conf.udp) != 0 &&
+			    strlen(new_thing->conf.location) != 0) {
 				yyerror("too many udp device arguments");
 				YYERROR;
 			}
-			if (thingsd_env->port == 0) {
-				yyerror("could not set default port");
+
+			if (get_addrs(new_thing->conf.tcp_iface,
+			    new_thing->conf.tcp_al,
+			    &new_thing->conf.tcp_listen_port) == -1) {
+				yyerror("could not get tcp iface addrs");
 				YYERROR;
 			}
-			if (new_thing->tls)
-				if (load_tls(new_thing) == -2)
-					YYABORT;
+
+			if (get_addrs(new_thing->conf.udp_iface,
+			    new_thing->conf.udp_al,
+			    &new_thing->conf.udp_rcv_port) == -1) {
+				yyerror("could not get udp iface addrs");
+				YYERROR;
+			}
 		}
 		;
 
@@ -541,17 +759,42 @@ thingopts1	: IPADDR STRING {
 				yyerror("ipaddr string empty");
 				YYERROR;
 			}
-			memcpy(&new_thing->ipaddr, $2,
-			    sizeof(new_thing->ipaddr));
+			if (strlcpy(new_thing->conf.ipaddr, $2,
+			    sizeof(new_thing->conf.ipaddr)) >=
+			    sizeof(new_thing->conf.ipaddr)) {
+				yyerror("%s: new_thing ipaddr truncated",
+				    __func__);
+				free($2);
+				free(new_thing);
+				YYERROR;
+			}
+			new_thing->conf.type = S_TCP;
 			free($2);
 		} '{' optnl socketopts2 '}'
 		| LOCATION STRING {
-			memcpy(&new_thing->location, $2,
-			    sizeof(new_thing->location));
+			if (strlcpy(new_thing->conf.location, $2,
+			    sizeof(new_thing->conf.location)) >=
+			    sizeof(new_thing->conf.location)) {
+				yyerror("%s: new_thing location truncated",
+				    __func__);
+				free($2);
+				free(new_thing);
+				YYERROR;
+			}
+			new_thing->conf.type = S_DEV;
 			free($2);
 		} locationopts
 		| UDP STRING {
-			memcpy(&new_thing->udp, $2, sizeof(new_thing->udp));
+			if (strlcpy(new_thing->conf.udp, $2,
+			    sizeof(new_thing->conf.udp)) >=
+			    sizeof(new_thing->conf.udp)) {
+				yyerror("%s: new_thing udp truncated",
+				    __func__);
+				free($2);
+				free(new_thing);
+				YYERROR;
+			}
+			new_thing->conf.type = S_UDP;
 			free($2);
 		} '{' optnl socketopts2 '}'
 		;
@@ -562,7 +805,7 @@ thingopts2	: thingopts2 thingopts1 nl
 
 thingretry	: CONNECTION RETRY NUMBER {
 			if ($3 >= MIN_RETRY && $3 <= MAX_RETRY)
-				thingsd_env->conn_retry = $3;
+				thingsd->conn_retry = $3;
 		}
 		;
 
@@ -571,55 +814,77 @@ things		: THINGS '{' subthings2 '}'
 
 tlscltopt	: /* empty */
 		| tlscltopt CRL STRING {
-			new_thing->tls_flags = TLSFLAG_CRL;
-			memcpy(&new_thing->tls_crl_file, $3,
-			    sizeof(new_thing->tls_crl_file));
+			new_thing->conf.tls_flags = TLSFLAG_CRL;
+			free(new_thing->conf.tls_crl_file);
+			if ((new_thing->conf.tls_crl_file = strdup($3)) == NULL)
+				fatal("%s: strdup", __func__);
 			free($3);
 		}
 		| tlscltopt OPTIONAL {
-			new_thing->tls_flags |= TLSFLAG_OPTIONAL;
+			new_thing->conf.tls_flags |= TLSFLAG_OPTIONAL;
 		}
 		;
 
 tlsopts		: CERTIFICATE STRING {
-			memcpy(&new_thing->tls_cert_file, $2,
-			    sizeof(new_thing->tls_cert_file));
+			free(new_thing->conf.tls_cert_file);
+			if ((new_thing->conf.tls_cert_file =
+			    strdup($2)) == NULL)
+				fatal("%s: strdup", __func__);
 			free($2);
 		}
 		| CIPHERS STRING {
-			memcpy(&new_thing->tls_ciphers, $2,
-			    sizeof(new_thing->tls_ciphers));
+			if (strlcpy(new_thing->conf.tls_ciphers, $2,
+			    sizeof(new_thing->conf.tls_ciphers)) >=
+			    sizeof(new_thing->conf.tls_ciphers)) {
+				yyerror("ciphers too long");
+				free($2);
+				YYERROR;
+			}
 			free($2);
 		}
 		| CLIENT CA STRING tlscltopt {
-			new_thing->tls_flags |= TLSFLAG_CA;
-			memcpy(&new_thing->tls_ca_file, $3,
-			    sizeof(new_thing->tls_ca_file));
+			new_thing->conf.tls_flags |= TLSFLAG_CA;
+			free(new_thing->conf.tls_ca_file);
+			if ((new_thing->conf.tls_ca_file = strdup($3)) == NULL)
+				fatal("%s: strdup", __func__);
 			free($3);
 		}
 		| DHE STRING {
-			memcpy(&new_thing->tls_dhe_params, $2,
-			    sizeof(new_thing->tls_dhe_params));
+			if (strlcpy(new_thing->conf.tls_dhe_params, $2,
+			    sizeof(new_thing->conf.tls_dhe_params)) >=
+			    sizeof(new_thing->conf.tls_dhe_params)) {
+				yyerror("dhe too long");
+				free($2);
+				YYERROR;
+			}
 			free($2);
 		}
 		| ECDHE STRING {
-			memcpy(&new_thing->tls_ecdhe_curves, $2,
-			    sizeof(new_thing->tls_ecdhe_curves));
+			if (strlcpy(new_thing->conf.tls_ecdhe_curves, $2,
+			    sizeof(new_thing->conf.tls_ecdhe_curves)) >=
+			    sizeof(new_thing->conf.tls_ecdhe_curves)) {
+				yyerror("ecdhe too long");
+				free($2);
+				YYERROR;
+			}
 			free($2);
 		}
 		| KEY STRING {
-			memcpy(&new_thing->tls_key_file, $2,
-			    sizeof(new_thing->tls_key_file));
+			free(new_thing->conf.tls_key_file);
+			if ((new_thing->conf.tls_key_file = strdup($2)) == NULL)
+				fatal("%s: strdup", __func__);
 			free($2);
 		}
 		| OCSP STRING {
-			memcpy(&new_thing->tls_ocsp_staple_file, $2,
-			    sizeof(new_thing->tls_ocsp_staple_file));
+			free(new_thing->conf.tls_ocsp_staple_file);
+			if ((new_thing->conf.tls_ocsp_staple_file =
+			    strdup($2)) == NULL)
+				fatal("%s: strdup", __func__);
 			free($2);
 		}
 		| PROTOCOLS STRING {
 			if (tls_config_parse_protocols(
-			    &new_thing->tls_protocols, $2) != 0) {
+			    &new_thing->conf.tls_protocols, $2) != 0) {
 				yyerror("invalid tls protocols");
 				free($2);
 				YYERROR;
@@ -630,7 +895,7 @@ tlsopts		: CERTIFICATE STRING {
 
 varset		: STRING '=' string		{
 			char *s = $1;
-			if (thingsd_env->thingsd_verbose)
+			if (thingsd->thingsd_verbose)
 				printf("%s = \"%s\"\n", $1, $3);
 			while (*s++) {
 				if (isspace((unsigned char)*s)) {
@@ -691,7 +956,6 @@ lookup(char *s)
 		{ "connection",		CONNECTION },
 		{ "crl",		CRL },
 		{ "data",		DATA },
-		{ "default",		DEFAULT },
 		{ "dhe",		DHE },
 		{ "ecdhe",		ECDHE },
 		{ "hardware",		HARDWARE },
@@ -717,7 +981,7 @@ lookup(char *s)
 		{ "software",		SOFTWARE },
 		{ "stop",		STOP },
 		{ "subscribe",		SUBSCRIBE },
-		{ "subscriptions",	SUBSCRIPTIONS },
+		{ "tcp",		TCP },
 		{ "thing",		THING },
 		{ "things",		THINGS },
 		{ "tls",		TLS },
@@ -751,11 +1015,10 @@ lgetc(int quotec)
 		/* Read character from the parsebuffer instead of input. */
 		if (parseindex >= 0) {
 			if (parsebuf == NULL)
-				return(0);
+				return (0);
 			if (parseindex > pkt_len)
-				return(0);
-			c = parsebuf[parseindex++];
-			if (c != '\0')
+				return (0);
+			if ((c = parsebuf[parseindex++]) != '\0')
 				return (c);
 			parsebuf = NULL;
 		} else
@@ -781,8 +1044,7 @@ lgetc(int quotec)
 
 	if (parseindex == 0) {
 		while ((c = getc(file->stream)) == '\\') {
-			next = getc(file->stream);
-			if (next != '\n') {
+			if ((next = getc(file->stream)) != '\n') {
 				c = next;
 				break;
 			}
@@ -853,8 +1115,7 @@ top:
 			; /* nothing */
 	if (c == '$' && parsebuf == NULL) {
 		while (1) {
-			c = lgetc(0);
-			if (c == EOF)
+			if ((c = lgetc(0)) == EOF)
 				return (0);
 
 			if (p + 1 >= buf + sizeof(buf) - 1) {
@@ -869,8 +1130,7 @@ top:
 			lungetc(c);
 			break;
 		}
-		val = symget(buf);
-		if (val == NULL) {
+		if ((val = symget(buf)) == NULL) {
 			yyerror("macro '%s' not defined", buf);
 			return (findeol());
 		}
@@ -884,15 +1144,13 @@ top:
 	case '"':
 		quotec = c;
 		while (1) {
-			c = lgetc(quotec);
-			if (c == EOF)
+			if ((c = lgetc(quotec)) == EOF)
 				return (0);
 			if (c == '\n') {
 				file->lineno++;
 				continue;
 			} else if (c == '\\') {
-				next = lgetc(quotec);
-				if (next == EOF)
+				if ((next = lgetc(quotec)) == EOF)
 					return (0);
 				if (next == quotec || c == ' ' || c == '\t')
 					c = next;
@@ -914,8 +1172,7 @@ top:
 			}
 			*p++ = c;
 		}
-		yylval.v.string = strdup(buf);
-		if (yylval.v.string == NULL)
+		if ((yylval.v.string = strdup(buf)) == NULL)
 			err(1, "yylex: strdup");
 		return (STRING);
 	}
@@ -972,8 +1229,7 @@ nodigits:
 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
 		lungetc(c);
 		*p = '\0';
-		token = lookup(buf);
-		if (token == STRING) {
+		if ((token = lookup(buf)) == STRING) {
 			yylval.v.string = strdup(buf);
 			if (yylval.v.string == NULL)
 				err(1, "yylex: strdup");
@@ -1014,19 +1270,16 @@ newfile(const char *name, int secret)
 {
 	struct file	*nfile;
 
-	nfile = calloc(1, sizeof(struct file));
-	if (nfile == NULL) {
+	if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
 		log_warn("calloc");
 		return (NULL);
 	}
-	nfile->name = strdup(name);
-	if (nfile->name == NULL) {
+	if ((nfile->name = strdup(name)) == NULL) {
 		log_warn("strdup");
 		free(nfile);
 		return (NULL);
 	}
-	nfile->stream = fopen(nfile->name, "r");
-	if (nfile->stream == NULL) {
+	if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
 		log_warn("%s", nfile->name);
 		free(nfile->name);
 		free(nfile);
@@ -1047,19 +1300,16 @@ newbuff(u_char *pkt)
 {
 	struct file	*bfile;
 
-	bfile = calloc(1, sizeof(struct file));
-	if (bfile == NULL) {
+	if ((bfile = calloc(1, sizeof(struct file))) == NULL) {
 		log_warn("calloc");
 		return (NULL);
 	}
-	bfile->name = strdup("subscribe buffer");
-	if (bfile->name == NULL) {
+	if ((bfile->name = strdup("subscribe buffer")) == NULL) {
 		log_warn("strdup");
 		free(bfile);
 		return (NULL);
 	}
-	parsebuf = pkt;
-	if (parsebuf == NULL) {
+	if ((parsebuf = pkt) == NULL) {
 		log_warn("%s", bfile->name);
 		free(bfile->name);
 		free(bfile);
@@ -1085,27 +1335,30 @@ closebuff(struct file *xfile)
 }
 
 int
-parse_config(const char *filename)
+parse_config(const char *filename, struct thingsd *env)
 {
 	struct sym	*sym, *next;
 
-	file = newfile(filename, 0);
-	if (file == NULL) {
+	if ((file = newfile(filename, 0)) == NULL) {
 		log_warn("failed to open %s", filename);
 		return (0);
 	}
 
-	TAILQ_INIT(thingsd_env->things);
-	thingsd_env->client_fptr = client_do_chk;
-	thingsd_env->conn_retry = CONN_RETRY;
-	thingsd_env->dead_things->run = 1;
+	if (config_init(env) == -1)
+		fatal("failed to initialize configuration");
 
+	thingsd = env;
+
+	thingsd->conn_retry = CONN_RETRY;
+	/* thingsd->dead_things->run = 1; */
+	thingsd->run = 1;
+
 	yyparse();
 	errors = file->errors;
 	closefile(file);
 	/* Free macros and check which have not been used. */
 	TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
-		if ((thingsd_env->thingsd_verbose > 1) && !sym->used)
+		if ((thingsd->thingsd_verbose > 1) && !sym->used)
 			fprintf(stderr, "warning: macro '%s' not used\n",
 			    sym->nam);
 		if (!sym->persist) {
@@ -1119,6 +1372,17 @@ parse_config(const char *filename)
 	if (errors)
 		return (-1);
 
+	/* setup our listening sockets */
+	sockets_parse_sockets(env);
+
+	/* struct socket *sock; */
+	/* TAILQ_FOREACH(sock, env->sockets, entry) { */
+	/* 	log_debug("Sock %d %s: child: %d parent: %d thing: %d", */
+	/* 	    sock->conf.id, sock->conf.iface, */
+	/* 	    sock->conf.child_id, sock->conf.parent_id, */
+	/* 	    sock->conf.thing_id); */
+	/* } */
+
 	return (0);
 }
 
@@ -1142,17 +1406,13 @@ symset(const char *nam, const char *val, int persist)
 			free(sym);
 		}
 	}
-	sym = calloc(1, sizeof(*sym));
-	if (sym == NULL)
+	if ((sym = calloc(1, sizeof(*sym))) == NULL)
 		return (-1);
-
-	sym->nam = strdup(nam);
-	if (sym->nam == NULL) {
+	if ((sym->nam = strdup(nam)) == NULL) {
 		free(sym);
 		return (-1);
 	}
-	sym->val = strdup(val);
-	if (sym->val == NULL) {
+	if ((sym->val = strdup(val)) == NULL) {
 		free(sym->nam);
 		free(sym);
 		return (-1);
@@ -1175,8 +1435,7 @@ cmdline_symset(char *s)
 		return (-1);
 
 	len = strlen(s) - strlen(val) + 1;
-	sym = malloc(len);
-	if (sym == NULL)
+	if ((sym = malloc(len)) == NULL)
 		fatal("%s: malloc", __func__);
 
 	memcpy(&sym, s, len);
@@ -1202,14 +1461,13 @@ symget(const char *nam)
 }
 
 int
-parse_buf(struct client *pclient, u_char *pkt, int len)
+parse_buf(struct client *client, u_char *pkt, int len)
 {
 	pkt_len = len;
-	tclient = pclient;
-	my_fd = tclient->fd;
+	pclient = client;
+	my_fd = client->fd;
 
-	file = newbuff(pkt);
-	if (file == NULL)
+	if ((file = newbuff(pkt)) == NULL)
 		return (-1);
 
 	yyparse();
@@ -1220,40 +1478,6 @@ parse_buf(struct client *pclient, u_char *pkt, int len
 	return (errors ? -1 : 0);
 }
 
-struct subscription *
-new_sub(char *name)
-{
-	struct subscription	*sub;
-
-	sub = calloc(1, sizeof(*sub));
-	if (sub == NULL)
-		fatal("%s: calloc", __func__);
-	memset(&sub->thing_name, 0, sizeof(sub->thing_name));
-	memcpy(&sub->thing_name, name, sizeof(sub->thing_name));
-
-	return (sub);
-}
-
-struct thing *
-conf_new_thing(char *name)
-{
-	struct thing	*thing;
-
-	TAILQ_FOREACH(thing, thingsd_env->things, entry) {
-		if (strcmp(name, thing->name) == 0)
-			return (thing);
-	}
-
-	thing = calloc(1, sizeof(*thing));
-	if (thing == NULL)
-		fatal("%s: calloc", __func__);
-	memcpy(&thing->name, name, sizeof(thing->name));
-
-	TAILQ_INSERT_TAIL(thingsd_env->things, thing, entry);
-
-	return (thing);
-}
-
 void
 clear_config(struct thingsd *xconf)
 {
@@ -1268,26 +1492,326 @@ clear_config(struct thingsd *xconf)
 }
 
 int
-load_tls(struct thing *thing)
+getservice(char *n)
 {
-	if (tls_load_keypair(thing) == -1) {
-		log_warnx("%s:%d: thing \"%s\": failed to load public/private"
-		    " keys", file->name, yylval.lineno, thing->name);
-		return -1;
+	struct servent	*s;
+	const char	*errstr;
+	long long	 llval;
+
+	llval = strtonum(n, 0, UINT16_MAX, &errstr);
+	if (errstr) {
+		s = getservbyname(n, "tcp");
+		if (s == NULL)
+			s = getservbyname(n, "udp");
+		if (s == NULL)
+			return (-1);
+		return (s->s_port);
 	}
-	if (tls_load_ca(thing) == -1) {
-		yyerror("failed to load ca cert(s) for thing %s", thing->name);
-		return -2;
+
+	return (htons((unsigned short)llval));
+}
+
+struct address *
+host_v4(const char *s)
+{
+	struct in_addr		 ina;
+	struct sockaddr_in	*sain;
+	struct address		*h;
+
+	memset(&ina, 0, sizeof(ina));
+	if (inet_pton(AF_INET, s, &ina) != 1)
+		return (NULL);
+
+	if ((h = calloc(1, sizeof(*h))) == NULL)
+		fatal(__func__);
+	sain = (struct sockaddr_in *)&h->ss;
+	sain->sin_len = sizeof(struct sockaddr_in);
+	sain->sin_family = AF_INET;
+	sain->sin_addr.s_addr = ina.s_addr;
+	if (sain->sin_addr.s_addr == INADDR_ANY)
+		h->prefixlen = 0; /* 0.0.0.0 address */
+	else
+		h->prefixlen = -1; /* host address */
+	return (h);
+}
+
+struct address *
+host_v6(const char *s)
+{
+	struct addrinfo		 hints, *res;
+	struct sockaddr_in6	*sa_in6;
+	struct address		*h = NULL;
+
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = AF_INET6;
+	hints.ai_socktype = SOCK_DGRAM; /* dummy */
+	hints.ai_flags = AI_NUMERICHOST;
+	if (getaddrinfo(s, "0", &hints, &res) == 0) {
+		if ((h = calloc(1, sizeof(*h))) == NULL)
+			fatal(__func__);
+		sa_in6 = (struct sockaddr_in6 *)&h->ss;
+		sa_in6->sin6_len = sizeof(struct sockaddr_in6);
+		sa_in6->sin6_family = AF_INET6;
+		memcpy(&sa_in6->sin6_addr,
+		    &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
+		    sizeof(sa_in6->sin6_addr));
+		sa_in6->sin6_scope_id =
+		    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
+		if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
+		    sizeof(sa_in6->sin6_addr)) == 0)
+			h->prefixlen = 0; /* any address */
+		else
+			h->prefixlen = -1; /* host address */
+		freeaddrinfo(res);
 	}
-	if (tls_load_crl(thing) == -1) {
-		yyerror("failed to load crl(s) for thing %s", thing->name);
-		free(thing);
-		return -2;
+
+	return (h);
+}
+
+int
+host_dns(const char *s, struct addresslist *al, int max,
+    struct portrange *port, const char *ifname, int ipproto)
+{
+	struct addrinfo		 hints, *res0, *res;
+	int			 error, cnt = 0;
+	struct sockaddr_in	*sain;
+	struct sockaddr_in6	*sin6;
+	struct address		*h;
+
+	if ((cnt = host_if(s, al, max, port, ifname, ipproto)) != 0)
+		return (cnt);
+
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = PF_UNSPEC;
+	hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
+	hints.ai_flags = AI_ADDRCONFIG;
+	error = getaddrinfo(s, NULL, &hints, &res0);
+	if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
+		return (0);
+	if (error) {
+		log_warnx("%s: could not parse \"%s\": %s", __func__, s,
+		    gai_strerror(error));
+		return (-1);
 	}
-	if (tls_load_ocsp(thing) == -1) {
-		yyerror("failed to load ocsp staple for thing %s", thing->name);
-		free(thing);
-		return -2;
+
+	for (res = res0; res && cnt < max; res = res->ai_next) {
+		if (res->ai_family != AF_INET &&
+		    res->ai_family != AF_INET6)
+			continue;
+		if ((h = calloc(1, sizeof(*h))) == NULL)
+			fatal(__func__);
+
+		if (port != NULL)
+			memcpy(&h->port, port, sizeof(h->port));
+		if (ifname != NULL) {
+			if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
+			    sizeof(h->ifname)) {
+				log_warnx("%s: interface name truncated",
+				    __func__);
+				freeaddrinfo(res0);
+				free(h);
+				return (-1);
+			}
+		}
+		if (ipproto != -1)
+			h->ipproto = ipproto;
+		h->ss.ss_family = res->ai_family;
+		h->prefixlen = -1; /* host address */
+
+		if (res->ai_family == AF_INET) {
+			sain = (struct sockaddr_in *)&h->ss;
+			sain->sin_len = sizeof(struct sockaddr_in);
+			sain->sin_addr.s_addr = ((struct sockaddr_in *)
+			    res->ai_addr)->sin_addr.s_addr;
+		} else {
+			sin6 = (struct sockaddr_in6 *)&h->ss;
+			sin6->sin6_len = sizeof(struct sockaddr_in6);
+			memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
+			    res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
+		}
+
+		TAILQ_INSERT_HEAD(al, h, entry);
+		cnt++;
 	}
-	return 0;
+	if (cnt == max && res) {
+		log_warnx("%s: %s resolves to more than %d hosts", __func__,
+		    s, max);
+	}
+	freeaddrinfo(res0);
+	return (cnt);
 }
+
+int
+host_if(const char *s, struct addresslist *al, int max,
+    struct portrange *port, const char *ifname, int ipproto)
+{
+	struct ifaddrs		*ifap, *p;
+	struct sockaddr_in	*sain;
+	struct sockaddr_in6	*sin6;
+	struct address		*h;
+	int			 cnt = 0, af;
+
+	if (getifaddrs(&ifap) == -1)
+		fatal("getifaddrs");
+
+	/* First search for IPv4 addresses */
+	af = AF_INET;
+
+ nextaf:
+	for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
+		if (p->ifa_addr == NULL ||
+		    p->ifa_addr->sa_family != af ||
+		    (strcmp(s, p->ifa_name) != 0 &&
+		    !is_if_in_group(p->ifa_name, s)))
+			continue;
+		if ((h = calloc(1, sizeof(*h))) == NULL)
+			fatal("calloc");
+
+		if (port != NULL)
+			memcpy(&h->port, port, sizeof(h->port));
+		if (ifname != NULL) {
+			if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
+			    sizeof(h->ifname)) {
+				log_warnx("%s: interface name truncated",
+				    __func__);
+				free(h);
+				freeifaddrs(ifap);
+				return (-1);
+			}
+		}
+		if (ipproto != -1)
+			h->ipproto = ipproto;
+		h->ss.ss_family = af;
+		h->prefixlen = -1; /* host address */
+
+		if (af == AF_INET) {
+			sain = (struct sockaddr_in *)&h->ss;
+			sain->sin_len = sizeof(struct sockaddr_in);
+			sain->sin_addr.s_addr = ((struct sockaddr_in *)
+			    p->ifa_addr)->sin_addr.s_addr;
+		} else {
+			sin6 = (struct sockaddr_in6 *)&h->ss;
+			sin6->sin6_len = sizeof(struct sockaddr_in6);
+			memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
+			    p->ifa_addr)->sin6_addr, sizeof(struct in6_addr));
+			sin6->sin6_scope_id = ((struct sockaddr_in6 *)
+			    p->ifa_addr)->sin6_scope_id;
+		}
+
+		TAILQ_INSERT_HEAD(al, h, entry);
+		cnt++;
+	}
+	if (af == AF_INET) {
+		/* Next search for IPv6 addresses */
+		af = AF_INET6;
+		goto nextaf;
+	}
+
+	if (cnt > max) {
+		log_warnx("%s: %s resolves to more than %d hosts", __func__,
+		    s, max);
+	}
+	freeifaddrs(ifap);
+	return (cnt);
+}
+
+int
+host(const char *s, struct addresslist *al, int max,
+    struct portrange *port, const char *ifname, int ipproto)
+{
+	struct address *h;
+
+	h = host_v4(s);
+
+	/* IPv6 address? */
+	if (h == NULL)
+		h = host_v6(s);
+
+	if (h != NULL) {
+		if (port != NULL)
+			memcpy(&h->port, port, sizeof(h->port));
+		if (ifname != NULL) {
+			if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
+			    sizeof(h->ifname)) {
+				log_warnx("%s: interface name truncated",
+				    __func__);
+				free(h);
+				return (-1);
+			}
+		}
+		if (ipproto != -1)
+			h->ipproto = ipproto;
+
+		TAILQ_INSERT_HEAD(al, h, entry);
+		return (1);
+	}
+
+	return (host_dns(s, al, max, port, ifname, ipproto));
+}
+
+int
+is_if_in_group(const char *ifname, const char *groupname)
+{
+	unsigned int		 len;
+	struct ifgroupreq	 ifgr;
+	struct ifg_req		*ifg;
+	int			 s;
+	int			 ret = 0;
+
+	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+		err(1, "socket");
+
+	memset(&ifgr, 0, sizeof(ifgr));
+	if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
+		err(1, "IFNAMSIZ");
+	if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
+		if (errno == EINVAL || errno == ENOTTY)
+			goto end;
+		err(1, "SIOCGIFGROUP");
+	}
+
+	len = ifgr.ifgr_len;
+	ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
+	    sizeof(struct ifg_req));
+	if (ifgr.ifgr_groups == NULL)
+		err(1, "getifgroups");
+	if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
+		err(1, "SIOCGIFGROUP");
+
+	ifg = ifgr.ifgr_groups;
+	for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
+		len -= sizeof(struct ifg_req);
+		if (strcmp(ifg->ifgrq_group, groupname) == 0) {
+			ret = 1;
+			break;
+		}
+	}
+	free(ifgr.ifgr_groups);
+
+end:
+	close(s);
+	return (ret);
+}
+
+int
+get_addrs(const char *addr, struct addresslist *al, struct portrange *port)
+{
+	if (strcmp("", addr) == 0) {
+		if (host("0.0.0.0", al, 1, port, "0.0.0.0", -1) <= 0) {
+			yyerror("invalid listen ip: %s",
+			    "0.0.0.0");
+			return (-1);
+		}
+		if (host("::", al, 1, port, "::", -1) <= 0) {
+			yyerror("invalid listen ip: %s", "::");
+			return (-1);
+		}
+	} else {
+		if (host(addr, al, THINGSD_MAXIFACE, port, addr,
+		    -1) <= 0) {
+			yyerror("invalid listen ip: %s", addr);
+			return (-1);
+		}
+	}
+	return (0);
+}
blob - 420eae4cbb3c6c0b080fb05f67626d7caa4cdfd4
blob + c18821cd0929bd600bddda742eb9512565be25f0
--- thingsd/proc.c	
+++ thingsd/proc.c	
@@ -1,3 +1,5 @@
+/*	$OpenBSD: proc.c,v 1.39 2020/08/03 10:57:21 benno Exp $	*/
+
 /*
  * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
  * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
@@ -27,13 +29,15 @@
 #include <string.h>
 #include <errno.h>
 #include <signal.h>
+#include <paths.h>
 #include <pwd.h>
 #include <event.h>
 #include <imsg.h>
 
 #include "proc.h"
+#include "thingsd.h"
 
-void	 proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
+void	 proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
 	    int, char **);
 void	 proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
 void	 proc_open(struct privsep *, int, int);
@@ -57,7 +61,6 @@ proc_ispeer(struct privsep_proc *procs, unsigned int n
 	for (i = 0; i < nproc; i++)
 		if (procs[i].p_id == type)
 			return (1);
-
 	return (0);
 }
 
@@ -81,7 +84,7 @@ proc_getid(struct privsep_proc *procs, unsigned int np
 
 void
 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
-    int argc, char **argv)
+    int debug, int argc, char **argv)
 {
 	unsigned int		 proc, nargc, i, proc_i;
 	char			**nargv;
@@ -142,6 +145,16 @@ proc_exec(struct privsep *ps, struct privsep_proc *pro
 				} else if (fcntl(fd, F_SETFD, 0) == -1)
 					fatal("fcntl");
 
+				/* Daemons detach from terminal. */
+				if (!debug && (fd =
+				    open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
+					(void)dup2(fd, STDIN_FILENO);
+					(void)dup2(fd, STDOUT_FILENO);
+					(void)dup2(fd, STDERR_FILENO);
+					if (fd > 2)
+						(void)close(fd);
+				}
+
 				execvp(argv[0], nargv);
 				fatal("%s: execvp", __func__);
 				break;
@@ -152,7 +165,6 @@ proc_exec(struct privsep *ps, struct privsep_proc *pro
 			}
 		}
 	}
-
 	free(nargv);
 }
 
@@ -193,7 +205,7 @@ proc_connect(struct privsep *ps)
 
 void
 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
-    int argc, char **argv, enum privsep_procid proc_id)
+    int debug, int argc, char **argv, enum privsep_procid proc_id)
 {
 	struct privsep_proc	*p = NULL;
 	struct privsep_pipes	*pa, *pb;
@@ -210,7 +222,7 @@ proc_init(struct privsep *ps, struct privsep_proc *pro
 		proc_setup(ps, procs, nproc);
 
 		/*
-		 * Create the children sockets so we can use them 
+		 * Create the children sockets so we can use them
 		 * to distribute the rest of the socketpair()s using
 		 * proc_connect() later.
 		 */
@@ -233,7 +245,7 @@ proc_init(struct privsep *ps, struct privsep_proc *pro
 		}
 
 		/* Engage! */
-		proc_exec(ps, procs, nproc, argc, argv);
+		proc_exec(ps, procs, nproc, debug, argc, argv);
 		return;
 	}
 
@@ -305,9 +317,8 @@ proc_setup(struct privsep *ps, struct privsep_proc *pr
 
 		id = procs[src].p_id;
 		ps->ps_title[id] = procs[src].p_title;
-		ps->ps_ievs[id] = calloc(ps->ps_instances[id],
-		    sizeof(struct imsgev));
-		if (ps->ps_ievs[id] == NULL)
+		if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
+		    sizeof(struct imsgev))) == NULL)
 			fatal("%s: calloc", __func__);
 
 		/* With this set up, we are ready to call imsg_init(). */
@@ -334,9 +345,8 @@ proc_setup(struct privsep *ps, struct privsep_proc *pr
 	 */
 	for (src = 0; src < PROC_MAX; src++) {
 		/* Allocate destination array for each process */
-		ps->ps_pipes[src] = calloc(ps->ps_instances[src],
-		    sizeof(struct privsep_pipes));
-		if (ps->ps_pipes[src] == NULL)
+		if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
+		    sizeof(struct privsep_pipes))) == NULL)
 			fatal("%s: calloc", __func__);
 
 		for (i = 0; i < ps->ps_instances[src]; i++) {
@@ -344,10 +354,9 @@ proc_setup(struct privsep *ps, struct privsep_proc *pr
 
 			for (dst = 0; dst < PROC_MAX; dst++) {
 				/* Allocate maximum fd integers */
-				pp->pp_pipes[dst] =
+				if ((pp->pp_pipes[dst] =
 				    calloc(ps->ps_instances[dst],
-				    sizeof(int));
-				if (pp->pp_pipes[dst] == NULL)
+				    sizeof(int))) == NULL)
 					fatal("%s: calloc", __func__);
 
 				/* Mark fd as unused */
@@ -395,7 +404,7 @@ proc_kill(struct privsep *ps)
 			free(cause);
 		} else
 			log_warnx("lost child: pid %u", pid);
-	} while (pid != -1 || (pid == -1 && errno == EINTR));
+	} while (pid != -1 || errno == EINTR);
 }
 
 void
@@ -488,7 +497,7 @@ proc_shutdown(struct privsep_proc *p)
 
 	proc_close(ps);
 
-	log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
+	log_info("%s exiting, pid %d", p->p_title, getpid());
 
 	exit(0);
 }
@@ -510,7 +519,7 @@ proc_sig_handler(int sig, short event, void *arg)
 		/* ignore */
 		break;
 	default:
-		fatalx("proc_sig_handler: unexpected signal");
+		fatalx("%s: unexpected signal", __func__);
 		/* NOTREACHED */
 	}
 }
@@ -550,9 +559,9 @@ proc_run(struct privsep *ps, struct privsep_proc *p,
 		root = pw->pw_dir;
 
 	if (chroot(root) == -1)
-		fatal("proc_run: chroot");
+		fatal("%s: chroot", __func__);
 	if (chdir("/") == -1)
-		fatal("proc_run: chdir(\"/\")");
+		fatal("%s: chdir(\"/\")", __func__);
 
 	privsep_process = p->p_id;
 
@@ -561,7 +570,7 @@ proc_run(struct privsep *ps, struct privsep_proc *p,
 	if (setgroups(1, &pw->pw_gid) ||
 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
-		fatal("proc_run: cannot drop privileges");
+		fatal("%s: cannot drop privileges", __func__);
 
 	event_init();
 
@@ -618,8 +627,7 @@ proc_dispatch(int fd, short event, void *arg)
 	ibuf = &iev->ibuf;
 
 	if (event & EV_READ) {
-		n = imsg_read(ibuf);
-		if (n == -1 && errno != EAGAIN)
+		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
 			fatal("%s: imsg_read", __func__);
 		if (n == 0) {
 			/* this pipe is dead, so remove the event handler */
@@ -630,8 +638,7 @@ proc_dispatch(int fd, short event, void *arg)
 	}
 
 	if (event & EV_WRITE) {
-		n = msgbuf_write(&ibuf->w);
-		if (n == -1 && errno != EAGAIN)
+		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
 			fatal("%s: msgbuf_write", __func__);
 		if (n == 0) {
 			/* this pipe is dead, so remove the event handler */
@@ -642,8 +649,7 @@ proc_dispatch(int fd, short event, void *arg)
 	}
 
 	for (;;) {
-		n = imsg_get(ibuf, &imsg);
-		if (n == -1)
+		if ((n = imsg_get(ibuf, &imsg)) == -1)
 			fatal("%s: imsg_get", __func__);
 		if (n == 0)
 			break;
@@ -668,7 +674,6 @@ proc_dispatch(int fd, short event, void *arg)
 		 */
 		switch (imsg.hdr.type) {
 		case IMSG_CTL_VERBOSE:
-			log_info("%s", __func__);
 			IMSG_SIZE_CHECK(&imsg, &verbose);
 			memcpy(&verbose, imsg.data, sizeof(verbose));
 			log_setverbose(verbose);
@@ -680,7 +685,7 @@ proc_dispatch(int fd, short event, void *arg)
 			    pf.pf_instance);
 			break;
 		default:
-			log_warnx("%s: %s %d got invalid imsg %d peerid %d "
+			fatalx("%s: %s %d got invalid imsg %d peerid %d "
 			    "from %s %d",
 			    __func__, title, ps->ps_instance + 1,
 			    imsg.hdr.type, imsg.hdr.peerid,
@@ -724,8 +729,8 @@ imsg_compose_event(struct imsgev *iev, uint16_t type, 
 {
 	int	ret;
 
-	ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
-	if (ret == -1)
+	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
+	    pid, fd, data, datalen)) == -1)
 		return (ret);
 	imsg_event_add(iev);
 	return (ret);
@@ -737,8 +742,8 @@ imsg_composev_event(struct imsgev *iev, uint16_t type,
 {
 	int	ret;
 
-	ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
-	if (ret == -1)
+	if ((ret = imsg_composev(&iev->ibuf, type, peerid,
+	    pid, fd, iov, iovcnt)) == -1)
 		return (ret);
 	imsg_event_add(iev);
 	return (ret);
@@ -837,8 +842,7 @@ proc_flush_imsg(struct privsep *ps, enum privsep_proci
 
 	proc_range(ps, id, &n, &m);
 	for (; n < m; n++) {
-		ibuf = proc_ibuf(ps, id, n);
-		if (ibuf == NULL)
+		if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
 			return (-1);
 		do {
 			ret = imsg_flush(ibuf);
blob - 7984e6289b162d786b5ed60c0dcc9d601dfe670a
blob + 99340c3a5cf75f7edec9d22a35af02b89678136e
--- thingsd/proc.h	
+++ thingsd/proc.h	
@@ -54,11 +54,6 @@ struct control_sock {
 };
 TAILQ_HEAD(control_socks, control_sock);
 
-struct cs {
-	struct event	 ev;
-	int		 fd;
-};
-
 struct ctl_conn {
 	TAILQ_ENTRY(ctl_conn)	 entry;
 	uint8_t			 flags;
@@ -72,17 +67,17 @@ TAILQ_HEAD(ctl_connlist, ctl_conn);
 extern  struct ctl_connlist ctl_conns;
 
 /* privsep */
-#define CONFIG_RELOAD		0x00
-#define CONFIG_THINGS		0x01
-#define CONFIG_ALL		0xff
-
 enum privsep_procid {
 	PROC_PARENT	= 0,
 	PROC_CONTROL,
-	PROC_THINGS,
+	PROC_SOCKS,
 	PROC_MAX,
 };
 
+#define CONFIG_RELOAD		0x00
+#define CONFIG_SOCKS		0x01
+#define CONFIG_ALL		0xff
+
 struct privsep_pipes {
 	int				*pp_pipes[PROC_MAX];
 };
@@ -143,8 +138,9 @@ struct privsep_fd {
 #define PROC_MAX_INSTANCES      32
 
 /* proc.c */
+void	 proc_range(struct privsep *, enum privsep_procid, int *, int *);
 void	 proc_init(struct privsep *, struct privsep_proc *, unsigned int,
-	    int, char **, enum privsep_procid);
+	    int, int, char **, enum privsep_procid);
 void	 proc_kill(struct privsep *);
 void	 proc_connect(struct privsep *ps);
 void	 proc_dispatch(int, short event, void *);
blob - 4ff814057a5a25d9f2f569d5b51764496b65b948
blob + 756b40607b9a231def58df1588956a422f71737f
--- thingsd/sockets.c	
+++ thingsd/sockets.c	
@@ -1,5 +1,7 @@
 /*
- * Copyright (c) 2016, 2019, 2020 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2016, 2019-2021 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
+ * Copyright (c) 2007 - 2015 Reyk Floeter <reyk@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -16,15 +18,19 @@
 
 #include <sys/queue.h>
 #include <sys/time.h>
+#include <sys/param.h>
 #include <sys/socket.h>
+#include <sys/resource.h>
 
+#include <arpa/inet.h>
+
+#include <err.h>
 #include <errno.h>
 #include <event.h>
 #include <ifaddrs.h>
 #include <fcntl.h>
 #include <imsg.h>
 #include <netdb.h>
-#include <stdbool.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -33,498 +39,853 @@
 #include "proc.h"
 #include "thingsd.h"
 
-extern struct ctl_pkt	*ctl_pkt;
-extern bool		 ctl_conn;
+#define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
 
+#define FD_RESERVE		5
+#define FD_NEEDED		6
+
+volatile int client_inflight = 0;
+
+int	 sockets_dispatch_thingsd(int, struct privsep_proc *, struct imsg *);
+int	 sockets_socket_af(struct sockaddr_storage *, struct portrange);
+int	 sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int,
+	    volatile int *);
+
+void	 sockets_dup_new_socket(struct socket *, struct socket *);
+void	 sockets_run(struct privsep *, struct privsep_proc *, void *);
+void	 sockets_socket_conn(int, short, void *);
+void	 sockets_launch(void);
+void	 sockets_client_sub_timeout(int, short, void *);
+void	 sockets_client_accept_paused(int, short, void *);
+void	 sockets_write_to_clients(struct package *);
+
+struct socket
+	*sockets_conf_new_socket(struct thingsd *, struct thing *, int, int);
+
+
+extern enum privsep_procid privsep_process;
+
+static struct privsep_proc procs[] = {
+	{ "thingsd",	PROC_PARENT,	sockets_dispatch_thingsd  },
+};
+
 void
-create_sockets(struct thingsd *env, bool reconn)
+sockets(struct privsep *ps, struct privsep_proc *p)
 {
+	proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
+}
+
+void
+sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
+{
+	if (config_init(ps->ps_env) == -1)
+		fatal("failed to initialize configuration");
+
+	p->p_shutdown = sockets_shutdown;
+
+	sockets_socket_rlimit(-1);
+
+	signal_del(&ps->ps_evsigchld);
+	signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
+	signal_add(&ps->ps_evsigchld, NULL);
+
+	if (pledge("stdio inet recvfd", NULL) == -1)
+		fatal("pledge");
+}
+
+void
+sockets_launch(void)
+{
+	struct socket		*sock;
+
+	TAILQ_FOREACH(sock, thingsd_env->sockets, entry) {
+		log_debug("%s: configuring thing listener %d (%d)", __func__,
+		    sock->conf.id, sock->fd);
+
+		/* socket_tls_init(sock); */
+
+		if ((sock->clients = calloc(1, sizeof(*sock->clients))) == NULL)
+			fatalx("%s: calloc", __func__);
+		TAILQ_INIT(sock->clients);
+
+		event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
+		    sockets_socket_conn, sock);
+
+		if (event_add(&sock->ev, NULL))
+			fatalx("event add sock");
+
+		evtimer_set(&sock->pause, sockets_client_accept_paused, sock);
+
+		log_debug("%s: running thing listener %d", __func__,
+		    sock->conf.id);
+	}
+}
+
+int
+sockets_privinit(struct socket *sock)
+{
+	log_debug("%s: initializing thing listener %d", __func__,
+	    sock->conf.id);
+
+	if ((sock->fd = sockets_create_socket(sock->conf.al, sock->conf.port,
+	    S_TCP)) == -1) {
+		log_warnx("%s: create sock socket failed", __func__);
+		return (-1);
+	}
+
+	return (0);
+}
+
+void
+sockets_parse_sockets(struct thingsd *env)
+{
 	struct thing		*thing;
-	struct socket		*sock = NULL, *tsock, *conn_socket = NULL;
-	char			*iface = NULL;
-	int			 csock = 0, conn_csock = 0;
-	bool			 fail = false;
-	evbuffercb		 socketrd = socket_rd;
-	evbuffercb		 socketwr = socket_wr;
+	struct socket		*sock, *new_sock = NULL;
+	struct address		*a;
+	int			 sock_id = 0, ipv4 = 0, ipv6 = 0;
 
 	TAILQ_FOREACH(thing, env->things, entry) {
-		if (reconn) {
-			if (thing->exists)
-				continue;
-			if (thing->type == TCP)
-				goto recreate;
-			else
-				continue;
-		}
+		sock_id++;
+		new_sock = sockets_conf_new_socket(env, thing, sock_id, 0);
+		TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
 
-		sock = new_socket(thing->port);
+		/* add ipv6 children */
+		TAILQ_FOREACH(sock, env->sockets, entry) {
+			ipv4 = ipv6 = 0;
 
-		if (thing->tls)
-			socket_tls_init(sock, thing);
-		else
-			sock->tls = false;
+			TAILQ_FOREACH(a, sock->conf.al, entry) {
+				if (a->ss.ss_family == AF_INET)
+					ipv4 = 1;
+				if (a->ss.ss_family == AF_INET6)
+					ipv6 = 1;
+			}
 
-		sock->max_clients += thing->max_clients;
-
-		memcpy(&sock->name, thing->name, sizeof(sock->name));
-
-		if (strlen(thing->iface) != 0)
-			iface = get_ifaddrs(thing->iface);
-
-		if (thing->type == DEV) {
-			if((csock = sock->fd = create_socket(thing->port, iface,
-			    TCP)) == -1)
-				log_warnx("fd create socket failed");
-			else if (thing->fd != -1)
-				thing->exists = true;
-			else {
-				thing->exists = false;
+			/* create ipv6 sock */
+			if (ipv4 == 1 && ipv6 == 1) {
+				sock_id++;
+				sock->conf.child_id = sock_id;
+				new_sock = sockets_conf_new_socket(env, thing,
+				    sock_id, 1);
+				sockets_dup_new_socket(sock, new_sock);
+				TAILQ_INSERT_TAIL(env->sockets, new_sock,
+				    entry);
+				continue;
 			}
 		}
+	}
+}
 
-		if (strlen(thing->udp) != 0) {
-			/* create fd for udp instead of serial device */
-			thing->type = UDP;
-			conn_socket = new_socket(thing->rcv_port);
+void
+sockets_dup_new_socket(struct socket *p_sock, struct socket *sock)
+{
+	struct address		*a, *acp;
 
-			csock = sock->fd = create_socket(thing->port,
-			    iface, TCP);
-			if (csock == -1)
-				log_warnx("udp create socket failed");
-			else
-				thing->exists = true;
+	sock->conf.parent_id = p_sock->conf.id;
+	sock->conf.thing_id = p_sock->conf.thing_id;
+	sock->conf.ipv4 = 0;
+	sock->conf.ipv6 = 1;
 
-			conn_csock = conn_socket->fd =
-			    create_socket(thing->rcv_port, iface, thing->type);
-			if (conn_csock == -1)
-				log_warnx("udp create socket failed");
-			else
-				thing->exists = true;
+	if (strlcpy(sock->conf.password, p_sock->conf.password,
+	    sizeof(sock->conf.password)) >= sizeof(sock->conf.password))
+		fatalx("%s: strlcpy", __func__);
 
-			thing->fd = conn_socket->fd;
-		}
-recreate:
-		if (strlen(thing->ipaddr) != 0) {
-			thing->type = TCP;
+	sock->conf.port.val[0] = p_sock->conf.port.val[0];
+	sock->conf.port.op = p_sock->conf.port.op;
 
-			if (thing->persist == 1) {
-				thing->fd = open_client_socket(thing->ipaddr,
-				    thing->conn_port);
-				if (thing->fd == -1) {
-					log_warnx("ipaddr connection failed");
-					if (reconn)
-						continue;
-					thing->exists = false;
-					add_reconn(thing);
-				} else
-					thing->exists = true;
+	sock->conf.tls = p_sock->conf.tls;
+	sock->conf.tls_protocols = p_sock->conf.tls_flags;
+	sock->conf.tls_flags = p_sock->conf.tls_flags;
+	sock->conf.max_clients = p_sock->conf.max_clients;
 
-				if (thing->fd != -1) {
-					thing->bev = bufferevent_new(thing->fd,
-					    socketrd, socketwr, socket_err,
-					    env);
+	memcpy(&sock->conf.thing_name, p_sock->conf.thing_name,
+	    sizeof(sock->conf.thing_name));
 
-					if (thing->bev == NULL)
-						fatalx("ipaddr bev error");
-
-					thing->evb = evbuffer_new();
-
-					if (thing->evb == NULL)
-						fatalx("ipaddr evb error");
-
-					bufferevent_enable(thing->bev,
-					    EV_READ | EV_WRITE);
-				}
-			} else
-				thing->fd = -1;
-
-			if (reconn == false) {
-				csock = sock->fd = create_socket(thing->port,
-				    iface, TCP);
-				if (csock == -1) {
-					log_warnx("tcp create socket failed");
-					thing->exists = false;
-					add_reconn(thing);
-				} else {
-					if (thing->fd != -1)
-						thing->exists = true;
-				}
-			}
-
-		}
-		if (reconn && thing->exists) {
-			log_info("reconnected: %s", thing->name);
+	TAILQ_FOREACH(a, p_sock->conf.al, entry) {
+		if (a->ss.ss_family == AF_INET)
 			continue;
+
+		if ((acp = calloc(1, sizeof(*acp))) == NULL)
+			fatal("%s: calloc", __func__);
+		memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
+		acp->ipproto = a->ipproto;
+		acp->prefixlen = a->prefixlen;
+		acp->port.val[0] = a->port.val[0];
+		if (strlen(a->ifname) != 0) {
+			if (strlcpy(acp->ifname, a->ifname,
+			    sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
+				fatalx("%s: interface name truncated",
+				    __func__);
+			}
 		}
 
-		if (csock == -1 || conn_csock == -1)
-			fatalx("socket creation failed");
-		else if (csock == -2 || conn_csock == -2) {
-			fail = true;
+		TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
+	}
 
-			TAILQ_FOREACH(tsock, env->sockets, entry) {
-				if (thing->port == tsock->port) {
-					fail = false;
-					break;
-				}
-			}
+	if(sock->conf.tls == 0)
+		return;
 
-			if (fail)
-				fatalx("can't set socket port");
-			else {
-				log_info("   -socket exists: skipping");
-				fail = true;
-			}
+	if((sock->conf.tls_cert_file =
+	    strdup(p_sock->conf.tls_cert_file)) == NULL)
+		fatal("%s: strdup", __func__);
 
-		}
+	if((sock->conf.tls_key_file =
+	    strdup(p_sock->conf.tls_key_file)) == NULL)
+		fatal("%s: strdup", __func__);
 
-		if (fail == false)
-			TAILQ_INSERT_TAIL(env->sockets, sock, entry);
+	if (strlcpy(sock->conf.tls_ciphers, p_sock->conf.tls_ciphers,
+	    sizeof(sock->conf.tls_ciphers)) >= sizeof(sock->conf.tls_ciphers))
+		fatalx("%s: strlcpy", __func__);
 
-		if (thing->type == UDP)
-			TAILQ_INSERT_TAIL(env->sockets, conn_socket, entry);
+	if (strlcpy(sock->conf.tls_dhe_params, p_sock->conf.tls_dhe_params,
+	    sizeof(sock->conf.tls_dhe_params)) >=
+	    sizeof(sock->conf.tls_dhe_params))
+		fatalx("%s: strlcpy", __func__);
 
-		if (sock->fd > 0) {
-			event_set(sock->ev, sock->fd, EV_READ | EV_PERSIST,
-			    client_conn, env);
-			if (event_add(sock->ev, NULL))
-				fatalx("event add sock");
-			evtimer_set(&sock->pause, client_accept_paused, sock);
-		}
+	if (strlcpy(sock->conf.tls_ecdhe_curves, p_sock->conf.tls_ecdhe_curves,
+	    sizeof(sock->conf.tls_ecdhe_curves)) >=
+	    sizeof(sock->conf.tls_ecdhe_curves))
+		fatalx("%s: strlcpy", __func__);
+}
 
-		if (conn_socket != NULL && conn_socket->fd > 0) {
-			event_set(conn_socket->ev, conn_socket->fd,
-			    EV_READ | EV_PERSIST, udp_event, env);
+struct socket *
+sockets_conf_new_socket(struct thingsd *env, struct thing *thing, int id,
+    int is_dup)
+{
+	struct socket		*sock;
+	struct address		*a, *acp;
 
-			if (event_add(conn_socket->ev, NULL))
-				fatalx("event add conn_socket");
+	if ((sock = calloc(1, sizeof(*sock))) == NULL)
+		fatal("%s: calloc", __func__);
+
+	if ((sock->conf.al = calloc(1,
+	    sizeof(*sock->conf.al))) == NULL)
+		fatal("%s: calloc", __func__);
+
+	TAILQ_INIT(sock->conf.al);
+
+	sock->conf.parent_id = 0;
+	sock->conf.id = id;
+	sock->conf.ipv4 = 1;
+	sock->conf.thing_id = thing->conf.id;
+	sock->conf.max_clients = thing->conf.max_clients;
+
+	memset(sock->conf.tls_ciphers, 0,
+	    sizeof(sock->conf.tls_ciphers));
+	memset(sock->conf.tls_dhe_params, 0,
+	    sizeof(sock->conf.tls_dhe_params));
+	memset(sock->conf.tls_ecdhe_curves, 0,
+	    sizeof(sock->conf.tls_ecdhe_curves));
+
+	if (is_dup)
+		goto done;
+
+	if (strlcpy(sock->conf.thing_name, thing->conf.name,
+	    sizeof(sock->conf.thing_name)) >= sizeof(sock->conf.thing_name)) {
+		free(sock->conf.al);
+		free(sock);
+		fatalx("%s: strlcpy", __func__);
+	}
+
+	TAILQ_FOREACH(a, thing->conf.tcp_al, entry) {
+		if ((acp = calloc(1, sizeof(*acp))) == NULL)
+			fatal("%s: calloc", __func__);
+		memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
+		acp->ipproto = a->ipproto;
+		acp->prefixlen = a->prefixlen;
+		acp->port.val[0] = a->port.val[0];
+		if (strlen(a->ifname) != 0) {
+			if (strlcpy(acp->ifname, a->ifname,
+			    sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
+				fatalx("%s: interface name truncated",
+				    __func__);
+			}
 		}
 
+		TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
 	}
-	free(iface);
+
+	thing->conf.sock_id = id;
+
+	if (strlcpy(sock->conf.password, thing->conf.password,
+	    sizeof(sock->conf.password)) >= sizeof(sock->conf.password))
+		fatalx("%s: strlcpy", __func__);
+
+	sock->conf.port.val[0] = thing->conf.tcp_listen_port.val[0];
+	sock->conf.port.op = thing->conf.tcp_listen_port.op;
+
+	sock->conf.tls = thing->conf.tls;
+	sock->conf.tls_protocols = thing->conf.tls_flags;
+	sock->conf.tls_flags = thing->conf.tls_flags;
+
+	if(sock->conf.tls == 0)
+		goto done;
+
+	if((sock->conf.tls_cert_file =
+	    strdup(thing->conf.tls_cert_file)) == NULL)
+		fatal("%s: strdup", __func__);
+
+	if((sock->conf.tls_key_file =
+	    strdup(thing->conf.tls_key_file)) == NULL)
+		fatal("%s: strdup", __func__);
+
+	if (strlcpy(sock->conf.tls_ciphers, thing->conf.tls_ciphers,
+	    sizeof(sock->conf.tls_ciphers)) >= sizeof(sock->conf.tls_ciphers))
+		fatalx("%s: strlcpy", __func__);
+
+	if (strlcpy(sock->conf.tls_dhe_params, thing->conf.tls_dhe_params,
+	    sizeof(sock->conf.tls_dhe_params)) >=
+	    sizeof(sock->conf.tls_dhe_params))
+		fatalx("%s: strlcpy", __func__);
+
+	if (strlcpy(sock->conf.tls_ecdhe_curves, thing->conf.tls_ecdhe_curves,
+	    sizeof(sock->conf.tls_ecdhe_curves)) >=
+	    sizeof(sock->conf.tls_ecdhe_curves))
+		fatalx("%s: strlcpy", __func__);
+	socket_tls_load(sock);
+done:
+	return (sock);
 }
 
 int
-create_socket(int iport, char *iface, int type)
+sockets_socket_af(struct sockaddr_storage *ss, struct portrange port)
 {
-	struct addrinfo		 addr_hints, *addr_res, *loop_res;
-	char			 port[6];
-	int			 socket_fd, gai, o_val = 1, flags;
+	switch (ss->ss_family) {
+	case AF_INET:
+		((struct sockaddr_in *)ss)->sin_port = port.val[0];
+		((struct sockaddr_in *)ss)->sin_len =
+		    sizeof(struct sockaddr_in);
+		break;
+	case AF_INET6:
+		((struct sockaddr_in6 *)ss)->sin6_port = port.val[0];
+		((struct sockaddr_in6 *)ss)->sin6_len =
+		    sizeof(struct sockaddr_in6);
+		break;
+	default:
+		return (-1);
+	}
 
-	snprintf(port, sizeof(port), "%d", iport);
-	memset(&addr_hints, 0, sizeof(addr_hints));
-	addr_hints.ai_family = AF_UNSPEC;
+	return (0);
+}
+
+int
+sockets_create_socket(struct addresslist *al, struct portrange port, int type)
+{
+	struct addrinfo		 hints;
+	struct address		*a;
+	int			 fd = -1, o_val = 1, flags;
+
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = AF_UNSPEC;
 	switch (type) {
-	case UDP:
-		addr_hints.ai_socktype = SOCK_DGRAM;
-		addr_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
-		addr_hints.ai_protocol = 0;
+	case S_UDP:
+		hints.ai_socktype = SOCK_DGRAM;
+		hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
+		hints.ai_protocol = 0;
 		break;
-	case TCP:
+	case S_TCP:
 	default:
-		addr_hints.ai_socktype = SOCK_STREAM;
-		addr_hints.ai_flags |= AI_PASSIVE;
+		hints.ai_socktype = SOCK_STREAM;
+		hints.ai_flags |= AI_PASSIVE;
 		break;
 	}
-	gai = getaddrinfo(iface, port, &addr_hints, &addr_res);
-	if (gai != 0)
-		fatalx("getaddrinfo failed on %s: %s:%s", gai_strerror(gai),
-		    iface, port);
-	for (loop_res = addr_res; loop_res != NULL;
-	    loop_res = loop_res->ai_next) {
-		socket_fd = socket(loop_res->ai_family, loop_res->ai_socktype,
-		    loop_res->ai_protocol);
-		if (socket_fd == -1)
-			fatalx("unable to create socket");
 
-		if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &o_val,
+	TAILQ_FOREACH(a, al, entry) {
+		if (sockets_socket_af(&a->ss, port) == -1) {
+			log_warnx("%s: sockets_socket_af", __func__);
+			goto fail;
+		}
+
+		fd = socket(a->ss.ss_family, hints.ai_socktype,
+		    a->ipproto);
+			log_debug("%s: opening socket (%d) for %s", __func__,
+			    fd, a->ifname);
+
+		if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
 		    sizeof(int)) == -1) {
-			freeaddrinfo(addr_res);
-			fatalx("setsockopt error");
-
+			log_warn("%s: setsockopt error", __func__);
+			return (-1);
 		}
 
 		/* non-blocking */
-		flags = fcntl(socket_fd, F_GETFL);
+		flags = fcntl(fd, F_GETFL);
 		flags |= O_NONBLOCK;
+		fcntl(fd, F_SETFL, flags);
 
-		fcntl(socket_fd, F_SETFL, flags);
-
-		if (bind(socket_fd, loop_res->ai_addr,
-		    loop_res->ai_addrlen) == -1) {
-			close(socket_fd);
-			log_warnx("%s%s", "bind address busy\n",
-			    " -checking existing sockets");
-			return -2;
+		if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
+			close(fd);
+			log_info("%s: can't bind to port %d", __func__,
+			    ntohs(port.val[0]));
+			goto fail;
 		}
-
-		break;
-	}
-
-	if (loop_res == NULL) {
-		freeaddrinfo(addr_res);
-		fatalx("can't bind to port");
-	}
-
-	freeaddrinfo(addr_res);
-
-	if (type == TCP) {
-		if (listen(socket_fd, SOMAXCONN) == -1) {
-			fatal("unable to listen on socket");
-			return -1;
+		if (type == S_TCP) {
+			if (listen(fd, SOMAXCONN) == -1) {
+				log_warn("%s, unable to listen on socket",
+				    __func__);
+				goto fail;
+			}
 		}
 	}
 
-	return socket_fd;
+	free(a);
+	return (fd);
+fail:
+	free(a);
+	return (-1);
 }
 
 int
-open_client_socket(char *ip_addr, int cport)
+sockets_open_client(char *ip_addr, struct portrange *port)
 {
-	struct addrinfo		 hints, *res, *res0;
-	int			 client_fd, error;
-	char			 port[6];
+	int			 fd = -1;
 
-	memset(&hints, 0, sizeof(hints));
+	struct addresslist	 al;
+	struct address		*a;
 
-	snprintf(port, sizeof(port), "%d", cport);
+	TAILQ_INIT(&al);
+	if (host(ip_addr, &al, 1, port, ip_addr, -1) <= 0) {
+		log_warnx("invalid listen ip: %s", ip_addr);
+		return (-1);
+	}
 
-	hints.ai_family = AF_UNSPEC;
-	hints.ai_socktype = SOCK_STREAM;
+	TAILQ_FOREACH(a, &al, entry) {
+		if (sockets_socket_af(&a->ss, *port) == -1) {
+			log_warnx("%s: sockets_socket_af", __func__);
+			return (-1);
+		}
 
-	error = getaddrinfo(ip_addr, port, &hints, &res0);
-	if (error)
-		fatalx("getaddrinfo failed: %s", gai_strerror(error));
-
-	client_fd = -1;
-
-	for (res = res0; res; res = res->ai_next) {
-		client_fd = socket(res->ai_family, res->ai_socktype,
-		    res->ai_protocol);
-
-		if (client_fd == -1)
+		if ((fd = socket(a->ss.ss_family, SOCK_STREAM,
+		    a->ipproto)) == -1)
 			continue;
 
-		if (connect(client_fd, res->ai_addr, res->ai_addrlen) == -1) {
-			close(client_fd);
-			client_fd = -1;
-			continue;
+		if ((connect(fd, (struct sockaddr *)&a->ss,
+		    a->ss.ss_len)) == -1) {
+			if (errno != EINPROGRESS) {
+				close(fd);
+				return (-1);
+			}
 		}
-
-		break;
 	}
 
-	if (client_fd == -1)
+	if (fd == -1)
 		log_warnx("can't connect ip: %s", ip_addr);
 
-	freeaddrinfo(res0);
-
-	return client_fd;
+	free(a);
+	return fd;
 }
 
-char *
-get_ifaddrs(char *name)
-{
-	struct ifaddrs		*ifaps, *ifap;
-	struct sockaddr		*sa = NULL;
-	char			*addr = NULL;
-	char			 hbuf[NI_MAXHOST];
-
-	if (getifaddrs(&ifaps) == -1)
-		fatalx("getifaddrs error");
-
-	ifap = ifaps;
-
-	while (ifap) {
-		if ((ifap->ifa_addr) &&
-		    ((ifap->ifa_addr->sa_family == AF_INET) ||
-		    (ifap->ifa_addr->sa_family == AF_INET6))) {
-
-			if (ifap->ifa_addr->sa_family == AF_INET) {
-				struct sockaddr_in *in =
-				    (struct sockaddr_in *) ifap->ifa_addr;
-				sa = (struct sockaddr *) in;
-			}
-
-			if (ifap->ifa_addr->sa_family == AF_INET6) {
-				struct sockaddr_in6 *in6 =
-				    (struct sockaddr_in6*) ifap->ifa_addr;
-				sa = (struct sockaddr *) in6;
-			}
-
-			if (getnameinfo(sa, sa->sa_len, hbuf,
-			    sizeof(hbuf), NULL, 0, NI_NAMEREQD |
-			    NI_NUMERICHOST))
-				log_warnx("getnameinfo error");
-
-			free(addr);
-
-			addr = NULL;
-			addr = strdup(hbuf);
-
-			if (addr == NULL)
-				fatalx("out of memory");
-
-			if (strcmp(name, ifap->ifa_name) == 0) {
-				freeifaddrs(ifaps);
-				return addr;
-			}
-
-		}
-
-		ifap = ifap->ifa_next;
-
-	}
-
-	free(addr);
-	freeifaddrs(ifaps);
-	return NULL;
-}
-
 struct socket *
-new_socket(int port)
+sockets_new_socket(char *port)
 {
 	struct socket		*sock;
 
-	sock = calloc(1, sizeof(*sock));
-	if (sock == NULL)
+	if ((sock = calloc(1, sizeof(*sock))) == NULL)
 		fatal("%s: calloc", __func__);
 
-	sock->ev = calloc(1, sizeof(*sock->ev));
-	if (sock->ev == NULL)
-		fatal("%s: calloc", __func__);
-
-	sock->port = port;
-
 	return (sock);
 };
 
 struct socket *
-get_socket(struct thingsd *env, int val)
+sockets_get_socket(struct thingsd *env, int val)
 {
 	struct socket		*sock = NULL;
 
 	TAILQ_FOREACH(sock, env->sockets, entry) {
-		if (sock->fd == val || sock->port == val)
+		if (sock->fd == val)
 			return sock;
 	}
 
 	return NULL;
 }
 
+struct socket *
+sockets_get_socket_byid(struct thingsd *env, int val)
+{
+	struct socket		*sock = NULL;
+
+	TAILQ_FOREACH(sock, env->sockets, entry) {
+		if (sock->conf.id == val)
+			return sock;
+	}
+
+	return NULL;
+}
+
 void
-socket_rd(struct bufferevent *bev, void *arg)
+sockets_write_to_clients(struct package *package)
 {
-	struct thingsd		*env = (struct thingsd *)arg;
-	struct thing		*thing = NULL, *tthing;
-	struct client		*client;
-	struct subscription	*sub;
-	size_t			 len;
-	int			 fd = bev->ev_read.ev_fd;
-	char			*pkt = NULL;
+	struct socket		*sock = NULL;
+	struct client		*client = NULL;
 
-	TAILQ_FOREACH(tthing, env->things, entry) {
-		if (tthing->fd == fd) {
-			thing = tthing;
-
-			thing->evb = EVBUFFER_INPUT(bev);
-
-			len = EVBUFFER_LENGTH(thing->evb);
-
-			pkt = calloc(len, sizeof(*pkt));
-			if (pkt == NULL)
-				return;
-
-			evbuffer_remove(thing->evb, pkt, len);
-			TAILQ_FOREACH(client, env->clients, entry) {
-				TAILQ_FOREACH(sub, client->subscriptions,
-				    entry) {
-					if (strcmp(sub->thing_name,
-					    thing->name) == 0)
-						bufferevent_write(client->bev,
-						    pkt, len);
+	TAILQ_FOREACH(sock, thingsd_env->sockets, entry) {
+		if (sock->conf.thing_id == package->thing_id &&
+		    sock->client_cnt > 0) {
+			TAILQ_FOREACH(client, sock->clients, entry) {
+				if (client->subscribed) {
+					bufferevent_write(client->bev,
+					    package->pkt, package->len);
 				}
 			}
-
-			if (env->packet_client_count > 0)
-				send_to_packet_client(env, thing->name, pkt,
-				    len);
-
-			free(pkt);
-			pkt = NULL;
 		}
 	}
-	free(pkt);
 }
 
 void
-socket_wr(struct bufferevent *bev, void *arg)
-{
-}
-
-void
-socket_err(struct bufferevent *bev, short error, void *arg)
-{
-	struct thingsd		*env = (struct thingsd *)arg;
-	struct thing		*thing = NULL;
-	int			 fd = bev->ev_read.ev_fd;
-
-	if ((error & EVBUFFER_ERROR) == 0 || error & EVBUFFER_TIMEOUT) {
-		TAILQ_FOREACH(thing, env->things, entry) {
-			if (thing->fd == fd && thing->persist) {
-				bufferevent_free(thing->bev);
-				close(thing->fd);
-				thing->fd = -1;
-				thing->exists = false;
-				add_reconn(thing);
-				log_warnx("thing error: %s disconnected",
-				    thing->name);
-			}
-		}
-	}
-}
-
-void
 sockets_show_info(struct privsep *ps, struct imsg *imsg)
 {
-	char filter[THINGSD_MAXNAME];
-	struct socket	*socket, nsi;
+	/* char filter[THINGSD_MAXNAME]; */
+	/* struct socket	*socket, nsi; */
 
+	/* switch (imsg->hdr.type) { */
+	/* case IMSG_GET_INFO_SOCKETS_REQUEST: */
+
+	/* 	memcpy(filter, imsg->data, sizeof(filter)); */
+
+	/* 	TAILQ_FOREACH(socket, thingsd_env->sockets, entry) { */
+	/* 		if (filter[0] == '\0' || memcmp(filter, */
+	/* 		    socket->name, sizeof(filter)) == 0) { */
+
+	/* 			memcpy(&nsi.name, socket->name, */
+	/* 			    sizeof(nsi.name)); */
+
+	/* 			nsi.fd = socket->fd; */
+	/* 			nsi.port = socket->port; */
+	/* 			nsi.tls = socket->tls; */
+	/* 			nsi.client_cnt = socket->client_cnt; */
+	/* 			nsi.max_clients = socket->max_clients; */
+
+	/* 			if (proc_compose_imsg(ps, PROC_CONTROL, -1, */
+	/* 			    IMSG_GET_INFO_SOCKETS_DATA, */
+	/* 			    imsg->hdr.peerid, -1, &nsi, */
+	/* 			    sizeof(nsi)) == -1) */
+	/* 				return; */
+
+	/* 		} */
+	/* 	} */
+
+	/* 	if (proc_compose_imsg(ps, PROC_CONTROL, -1, */
+	/* 	    IMSG_GET_INFO_SOCKETS_END_DATA, imsg->hdr.peerid, */
+	/* 		    -1, &nsi, sizeof(nsi)) == -1) */
+	/* 			return; */
+
+	/* 	break; */
+	/* default: */
+	/* 	log_debug("%s: error handling imsg", __func__); */
+	/* 	break; */
+	/* } */
+}
+
+void
+sockets_socket_rlimit(int maxfd)
+{
+	struct rlimit	 rl;
+
+	if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
+		fatal("%s: failed to get resource limit", __func__);
+
+	log_debug("%s: max open files %llu", __func__, rl.rlim_max);
+
+	/*
+	 * Allow the maximum number of open file descriptors for this
+	 * login class (which should be the class "daemon" by default).
+	 */
+	if (maxfd == -1)
+		rl.rlim_cur = rl.rlim_max;
+	else
+		rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
+	if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
+		fatal("%s: failed to set resource limit", __func__);
+}
+
+int
+sockets_dispatch_thingsd(int fd, struct privsep_proc *p, struct imsg *imsg)
+{
+	struct package		*package = NULL;
+	struct privsep		*ps = p->p_ps;
+	int			 res = 0, cmd = 0, verbose;
+	unsigned int		 mode;
+
 	switch (imsg->hdr.type) {
-	case IMSG_GET_INFO_SOCKETS_REQUEST:
+	case IMSG_DIST_THING_PACKAGE:
+		IMSG_SIZE_CHECK(imsg, package);
+		package = (struct package *)imsg->data;
+		sockets_write_to_clients(package);
+		break;
+	case IMSG_CFG_SOCKS:
+		config_getsocks(thingsd_env, imsg);
+		break;
+	case IMSG_CFG_DONE:
+		config_getcfg(thingsd_env, imsg);
+		break;
+	case IMSG_CTL_RESET:
+		IMSG_SIZE_CHECK(imsg, &mode);
+		memcpy(&mode, imsg->data, sizeof(mode));
 
-		memcpy(filter, imsg->data, sizeof(filter));
+		config_getreset(thingsd_env, imsg);
+		break;
+	case IMSG_CTL_VERBOSE:
+		IMSG_SIZE_CHECK(imsg, &verbose);
+		memcpy(&verbose, imsg->data, sizeof(verbose));
+		log_setverbose(verbose);
+		break;
+	case IMSG_CTL_START:
+		sockets_launch();
+		break;
+	default:
+		return (-1);
+	}
 
-		TAILQ_FOREACH(socket, thingsd_env->sockets, entry) {
-			if (filter[0] == '\0' || memcmp(filter,
-			    socket->name, sizeof(filter)) == 0) {
+	switch (cmd) {
+	case 0:
+		break;
+	case IMSG_GET_INFO_THINGS_END_DATA:
+		if (proc_compose_imsg(ps, PROC_PARENT, -1, cmd,
+		    imsg->hdr.peerid, -1, &mode, sizeof(mode)) == -1)
+			return (-1);
+		break;
+	default:
+		if (proc_compose_imsg(ps, PROC_PARENT, -1, cmd,
+		    imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
+			return (-1);
+		break;
+	}
 
-				memcpy(&nsi.name, socket->name,
-				    sizeof(nsi.name));
+	return (0);
+}
 
-				nsi.fd = socket->fd;
-				nsi.port = socket->port;
-				nsi.tls = socket->tls;
-				nsi.client_cnt = socket->client_cnt;
-				nsi.max_clients = socket->max_clients;
+void
+sockets_sighdlr(int sig, short event, void *arg)
+{
+	switch (sig) {
+	default:
+		fatalx("unexpected signal");
+	}
+}
 
-				if (proc_compose_imsg(ps, PROC_CONTROL, -1,
-				    IMSG_GET_INFO_SOCKETS_DATA,
-				    imsg->hdr.peerid, -1, &nsi,
-				    sizeof(nsi)) == -1)
-					return;
+void
+sockets_shutdown(void)
+{
+	struct socket		*sock = NULL, *tsock;
+	struct client		*client, *tclient;
+	/* struct subscription	*sub, *tsub; */
 
-			}
+
+	/* clean up sockets */
+	TAILQ_FOREACH_SAFE(sock, thingsd_env->sockets, entry, tsock) {
+		/* clean up clients */
+		TAILQ_FOREACH_SAFE(client, sock->clients, entry, tclient) {
+			if (client->bev != NULL)
+				bufferevent_disable(client->bev,
+				    EV_READ | EV_WRITE);
+			if (client->bev != NULL)
+				bufferevent_free(client->bev);
+
+			close(client->fd);
+			TAILQ_REMOVE(sock->clients, client, entry);
+			free(client);
 		}
 
-		if (proc_compose_imsg(ps, PROC_CONTROL, -1,
-		    IMSG_GET_INFO_SOCKETS_END_DATA, imsg->hdr.peerid,
-			    -1, &nsi, sizeof(nsi)) == -1)
-				return;
+		if (sock->conf.tls) {
+			/* tls_config_free(sock->tls_config); */
+			/* tls_free(sock->tls_ctx); */
+			/* free(sock->conf.tls_cert); */
+			/* free(sock->conf.tls_cert_file); */
+			/* free(sock->conf.tls_key); */
+			/* free(sock->conf.tls_key_file); */
+			free(sock->conf.tls_ca);
+			free(sock->conf.tls_ca_file);
+			free(sock->conf.tls_crl);
+			free(sock->conf.tls_crl_file);
+			free(sock->conf.tls_ocsp_staple);
+			free(sock->conf.tls_ocsp_staple_file);
+		}
+		event_del(&sock->ev);
+		evtimer_del(&sock->pause);
+		close(sock->fd);
+		TAILQ_REMOVE(thingsd_env->sockets, sock, entry);
+		free(sock);
+	}
+	free(thingsd_env->sockets);
 
-		break;
-	default:
-		log_debug("%s: error handling imsg", __func__);
-		break;
+}
+
+int
+sockets_client_cmp(struct client *a, struct client *b)
+{
+	return ((int)a->id - b->id);
+}
+
+int
+sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
+    int reserve, volatile int *counter)
+{
+	int ret;
+
+	if (getdtablecount() + reserve +
+	    ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
+		log_debug("inflight fds exceeded");
+		errno = EMFILE;
+		return -1;
 	}
+
+	if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC))
+	    > -1) {
+		(*counter)++;
+		log_debug("inflight incremented, now %d", *counter);
+	}
+	return ret;
 }
+
+void
+sockets_client_accept_paused(int fd, short events, void *arg)
+{
+	struct socket		*sock = arg;
+
+	event_add(&sock->ev, NULL);
+}
+
+void
+sockets_client_sub_timeout(int fd, short type, void *arg)
+{
+	struct client		*client = (struct client *)arg;
+
+	if (client->subscribed == 0)
+		client_del(client);
+}
+
+void
+sockets_socket_conn(int fd, short event, void *arg)
+{
+	struct sockaddr_storage	 ss;
+	struct timeval		 backoff, subtime;
+	struct client		*client = NULL;
+	struct socket		*sock = (struct socket *)arg;
+	int			 client_fd;
+	socklen_t		 len = sizeof(ss);
+	evbuffercb		 clientrd = client_rd;
+	evbuffercb		 clientwr = client_wr;
+
+	backoff.tv_sec = 1;
+	backoff.tv_usec = 0;
+
+	subtime.tv_sec = CLIENT_SUB_TIME_SEC;
+	subtime.tv_usec = CLIENT_SUB_TIME_USEC;
+
+	sock = sockets_get_socket(thingsd_env, fd);
+	if (sock == NULL)
+		return;
+
+	log_debug("%s: client connection started on listener %d", __func__,
+	    sock->conf.id);
+
+	event_add(&sock->ev, NULL);
+
+	client_fd = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
+	    FD_RESERVE, &client_inflight);
+	if (client_fd == -1) {
+		switch (errno) {
+		case EINTR:
+		case EWOULDBLOCK:
+		case ECONNABORTED:
+			return;
+		case EMFILE:
+		case ENFILE:
+			event_del(&sock->ev);
+			evtimer_add(&sock->pause, &backoff);
+			return;
+		default:
+			log_warnx("client accept failed");
+		}
+	}
+
+	client = calloc(1, sizeof(*client));
+	if (client == NULL)
+		goto err2;
+
+	client->port.val[0] = sock->conf.port.val[0];
+	client->port.op = sock->conf.port.op;
+
+	memset(&client->timeout, 0, sizeof(struct event));
+	evtimer_set(&client->timeout, sockets_client_sub_timeout, client);
+
+	/* if (sock->conf.tls) { */
+	/* 	if (tls_accept_socket(sock->tls_ctx, &client->tls_ctx, */
+	/* 	    client_fd) == -1) { */
+	/* 		log_warnx("tls accept failed: %s", */
+	/* 		    tls_error(sock->tls_ctx)); */
+	/* 		goto err; */
+	/* 	} */
+	/* } */
+
+	client->packages = calloc(1, sizeof(*client->packages));
+	if (client->packages == NULL)
+		goto err2;
+
+	TAILQ_INIT(client->packages);
+
+	/* sock->client_cnt++; */
+
+	if (sock->conf.max_clients > 0)
+		if (sock->client_cnt > sock->conf.max_clients) {
+			log_debug("%s: listener %d max clients reached",
+			    __func__, sock->conf.id);
+			goto err;
+		}
+
+	client->sock_id = sock->conf.id;
+	client->thing_id = sock->conf.thing_id;
+	client->sock = sock;
+	client->sock_conf = &sock->conf;
+	client->subscribed = 0;
+	client->evb = evbuffer_new();
+
+	evtimer_pending(&client->timeout, NULL);
+	log_debug("%s: started client timer", __func__);
+	evtimer_add(&client->timeout, &subtime);
+
+	if (client->evb == NULL)
+		goto err;
+
+	client->fd = client_fd;
+	client->join_time = time(NULL);
+
+	/* if (sock->conf.tls) { */
+	/* 	client->tls = 1; */
+	/* 	event_del(&client->ev); */
+	/* 	event_set(&client->ev, client->fd, EV_READ | EV_PERSIST, */
+	/* 	    socket_tls_handshake, env); */
+	/* 	event_add(&client->ev, NULL); */
+	/* 	return; */
+	/* } */
+
+	client->bev = bufferevent_new(client->fd, clientrd, clientwr,
+	    client_err, client);
+
+	if (client->bev == NULL)
+		goto err;
+
+	if (client->tls) {
+		event_set(&client->bev->ev_read, client->fd, EV_READ,
+		    client_tls_readcb, client->bev);
+		event_set(&client->bev->ev_write, client->fd, EV_WRITE,
+		    client_tls_writecb, client->bev);
+	}
+
+	bufferevent_setwatermark(client->bev, EV_READ, 0, PKT_BUFF);
+	bufferevent_enable(client->bev, EV_READ|EV_WRITE);
+	TAILQ_INSERT_TAIL(sock->clients, client, entry);
+
+	log_debug("%s: client connected (%d)", __func__, client->fd);
+	return;
+err:
+	sock->client_cnt--;
+err2:
+	log_debug("%s: client error", __func__);
+	client_inflight--;
+	if (client_fd != -1)
+		close(client_fd);
+	if (client != NULL) {
+		/* if (sock->conf.tls) */
+		/* 	tls_free(client->tls_ctx); */
+		free(client);
+	}
+}
blob - fe7f6d3506c7a8a034367139268f2c0011cd4517 (mode 644)
blob + /dev/null
--- thingsd/things.c	
+++ /dev/null	
@@ -1,521 +0,0 @@
-/*
- * Copyright (c) 2016, 2019, 2020 Tracey Emery <tracey@traceyemery.net>
- * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <sys/param.h>
-#include <sys/ioctl.h>
-#include <sys/queue.h>
-#include <sys/wait.h>
-#include <sys/uio.h>
-#include <sys/socket.h>
-#include <sys/time.h>
-#include <sys/mman.h>
-
-#include <net/if.h>
-#include <netinet/in.h>
-
-#include <errno.h>
-#include <event.h>
-#include <fcntl.h>
-#include <imsg.h>
-#include <limits.h>
-#include <poll.h>
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <tls.h>
-#include <unistd.h>
-#include <util.h>
-
-#include "proc.h"
-#include "thingsd.h"
-
-int things_dispatch_parent(int, struct privsep_proc *, struct imsg *);
-void things_run(struct privsep *, struct privsep_proc *, void *);
-
-struct thing compose_thing(struct thing *, enum imsg_type);
-
-static struct privsep_proc procs[] = {
-	{ "parent",	PROC_PARENT,	things_dispatch_parent  },
-};
-
-void
-things(struct privsep *ps, struct privsep_proc *p)
-{
-	proc_run(ps, p, procs, nitems(procs), things_run, NULL);
-}
-
-void
-things_run(struct privsep *ps, struct privsep_proc *p, void *arg)
-{
-	if (config_init(ps->ps_env) == -1)
-		fatal("failed to initialize configuration");
-
-	signal_del(&ps->ps_evsigchld);
-	signal_set(&ps->ps_evsigchld, SIGCHLD, things_sighdlr, ps);
-	signal_add(&ps->ps_evsigchld, NULL);
-
-	if (pledge("stdio recvfd", NULL) == -1)
-		fatal("pledge");
-}
-
-int
-things_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
-{
-	struct privsep		*ps = p->p_ps;
-	struct thing		*thing, nti;
-	int			 res = 0, cmd = 0, verbose;
-	unsigned int		 mode;
-
-	switch (imsg->hdr.type) {
-	case IMSG_ADD_THING:
-		IMSG_SIZE_CHECK(imsg, &nti);
-
-		memcpy(&nti, imsg->data, sizeof(nti));
-
-		thing = calloc(1, sizeof(*thing));
-		if (thing == NULL)
-			fatal("%s: calloc", __func__);
-
-		thing->exists = nti.exists;
-		thing->hw_ctl = nti.hw_ctl;
-		thing->persist = nti.persist;
-
-		memcpy(&thing->iface, nti.iface, sizeof(thing->iface));
-
-		memcpy(&thing->ipaddr, nti.ipaddr, sizeof(thing->ipaddr));
-
-		memcpy(&thing->parity, nti.parity, sizeof(thing->parity));
-
-		memcpy(&thing->name, nti.name, sizeof(thing->name));
-
-		memcpy(&thing->password, nti.password,
-		    sizeof(thing->password));
-
-		memcpy(&thing->location, nti.location,
-		    sizeof(thing->location));
-
-		memcpy(&thing->udp, nti.udp, sizeof(thing->udp));
-
-		thing->fd = nti.fd;
-		thing->baud = nti.baud;
-		thing->conn_port = nti.conn_port;
-		thing->rcv_port = nti.rcv_port;
-		thing->data_bits = nti.data_bits;
-		thing->max_clients = nti.max_clients;
-		thing->port = nti.port;
-		thing->stop_bits = nti.stop_bits;
-		thing->type = nti.type;
-		thing->client_cnt = nti.client_cnt;
-
-		thing->tls = nti.tls;
-		memcpy(&thing->tls_cert_file, nti.tls_cert_file,
-		    sizeof(thing->tls_cert_file));
-
-		memcpy(&thing->tls_key_file, nti.tls_key_file,
-		    sizeof(thing->tls_key_file));
-
-		memcpy(&thing->tls_ca_file, nti.tls_ca_file,
-		    sizeof(thing->tls_ca_file));
-
-		memcpy(&thing->tls_crl_file, nti.tls_crl_file,
-		    sizeof(thing->tls_crl_file));
-
-		memcpy(&thing->tls_ocsp_staple_file,
-		    nti.tls_ocsp_staple_file,
-		    sizeof(thing->tls_ocsp_staple_file));
-
-		TAILQ_INSERT_TAIL(thingsd_env->things, thing, entry);
-
-		break;
-	case IMSG_CTL_RESET:
-		IMSG_SIZE_CHECK(imsg, &mode);
-		memcpy(&mode, imsg->data, sizeof(mode));
-
-		config_getreset(thingsd_env, imsg);
-		break;
-	case IMSG_CTL_VERBOSE:
-		IMSG_SIZE_CHECK(imsg, &verbose);
-		memcpy(&verbose, imsg->data, sizeof(verbose));
-		log_setverbose(verbose);
-		break;
-	default:
-		return (-1);
-	}
-
-	switch (cmd) {
-	case 0:
-		break;
-	case IMSG_GET_INFO_THINGS_END_DATA:
-		if (proc_compose_imsg(ps, PROC_PARENT, -1, cmd,
-		    imsg->hdr.peerid, -1, &mode, sizeof(mode)) == -1)
-			return (-1);
-		break;
-	default:
-		if (proc_compose_imsg(ps, PROC_PARENT, -1, cmd,
-		    imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
-			return (-1);
-		break;
-	}
-
-	return (0);
-}
-
-void
-things_sighdlr(int sig, short event, void *arg)
-{
-	switch (sig) {
-	default:
-		fatalx("unexpected signal");
-	}
-}
-
-void
-things_reset(void)
-{
-	struct thing		*thing, *tthing;
-	struct socket		*sock, *tsock;
-	struct client		*client, *tclient;
-	struct packet_client	*packet_client, *tpacket_client;
-
-	/* clean up packet clients */
-	TAILQ_FOREACH_SAFE(packet_client, thingsd_env->packet_clients, entry,
-	    tpacket_client) {
-		TAILQ_REMOVE(thingsd_env->packet_clients, packet_client, entry);
-	}
-
-	/* clean up clients */
-	TAILQ_FOREACH_SAFE(client, thingsd_env->clients, entry, tclient) {
-		close(client->fd);
-		TAILQ_REMOVE(thingsd_env->clients, client, entry);
-	}
-
-	/* clean up sockets */
-	TAILQ_FOREACH_SAFE(sock, thingsd_env->sockets, entry, tsock) {
-		if (sock->tls) {
-			tls_config_free(sock->tls_config);
-			tls_free(sock->tls_ctx);
-		}
-		close(sock->fd);
-		TAILQ_REMOVE(thingsd_env->sockets, sock, entry);
-	}
-
-	/* clean up things */
-	TAILQ_FOREACH_SAFE(thing, thingsd_env->things, entry, tthing) {
-		close(thing->fd);
-		TAILQ_REMOVE(thingsd_env->things, thing, entry);
-	}
-
-}
-
-void
-things_shutdown(void)
-{
-	struct thing		*thing, *tthing;
-	struct socket		*sock, *tsock;
-	struct client		*client, *tclient;
-	struct dead_thing	*dead_thing, *tdead_thing;
-	struct packet_client	*packet_client, *tpacket_client;
-	struct subscription	*sub, *tsub;
-
-	/* clean up packet clients */
-	TAILQ_FOREACH_SAFE(packet_client, thingsd_env->packet_clients, entry,
-	    tpacket_client) {
-		TAILQ_REMOVE(thingsd_env->packet_clients, packet_client, entry);
-		free(packet_client);
-	}
-
-	/* clean up things */
-	TAILQ_FOREACH_SAFE(thing, thingsd_env->things, entry, tthing) {
-		close(thing->fd);
-		TAILQ_REMOVE(thingsd_env->things, thing, entry);
-		free(thing);
-	}
-
-	/* clean up dead things */
-	TAILQ_FOREACH_SAFE(dead_thing,
-	    thingsd_env->dead_things->dead_things_list, entry, tdead_thing) {
-		TAILQ_REMOVE(thingsd_env->dead_things->dead_things_list,
-		    dead_thing, entry);
-		free(dead_thing);
-	}
-
-	/* clean up sockets */
-	TAILQ_FOREACH_SAFE(sock, thingsd_env->sockets, entry, tsock) {
-		if (sock->tls) {
-			tls_config_free(sock->tls_config);
-			tls_free(sock->tls_ctx);
-		}
-		close(sock->fd);
-		free(sock->ev);
-		TAILQ_REMOVE(thingsd_env->sockets, sock, entry);
-		free(sock);
-	}
-
-	/* clean up clients */
-	TAILQ_FOREACH_SAFE(client, thingsd_env->clients, entry, tclient) {
-		close(client->fd);
-		TAILQ_FOREACH_SAFE(sub, client->subscriptions, entry, tsub)
-			TAILQ_REMOVE(client->subscriptions, sub, entry);
-		free(client->subscriptions);
-		TAILQ_REMOVE(thingsd_env->clients, client, entry);
-		free(client);
-	}
-	free(thingsd_env);
-}
-
-void
-things_echo_pkt(struct privsep *ps, struct imsg *imsg)
-{
-	struct packet_client	*packet_client;
-
-	packet_client = calloc(1, sizeof(*packet_client));
-	if (packet_client == NULL)
-		fatal("%s: calloc", __func__);
-
-	thingsd_env->packet_client_count++;
-
-	packet_client->ps = *ps;
-	packet_client->imsg = *imsg;
-
-	memcpy(packet_client->name, imsg->data,  sizeof(packet_client->name));
-
-	log_debug("control packet echo request for %s", packet_client->name);
-
-	TAILQ_INSERT_TAIL(thingsd_env->packet_clients, packet_client, entry);
-}
-
-void
-things_stop_pkt(struct privsep *ps, struct imsg *imsg)
-{
-	struct packet_client	*packet_client, *tpacket_client;
-	uint32_t		 fd;
-
-	memcpy(&fd, imsg->data, sizeof(fd));
-
-	TAILQ_FOREACH_SAFE(packet_client, thingsd_env->packet_clients, entry,
-	    tpacket_client) {
-		if (fd == packet_client->imsg.hdr.peerid) {
-			TAILQ_REMOVE(thingsd_env->packet_clients,
-			    packet_client, entry);
-			log_debug("control packet echo request stopping for %s",
-			    packet_client->name);
-			thingsd_env->packet_client_count--;
-			free(packet_client);
-			return;
-		}
-	}
-
-}
-
-void
-things_show_info(struct privsep *ps, struct imsg *imsg)
-{
-	char filter[THINGSD_MAXNAME];
-	struct thing *thing, nti;
-
-	switch (imsg->hdr.type) {
-	case IMSG_GET_INFO_THINGS_REQUEST:
-	case IMSG_GET_INFO_THINGS_REQUEST_ROOT:
-
-		memcpy(filter, imsg->data, sizeof(filter));
-
-		TAILQ_FOREACH(thing, thingsd_env->things, entry) {
-			if (filter[0] == '\0' || memcmp(filter,
-			    thing->name, sizeof(filter)) == 0) {
-				nti = compose_thing(thing, imsg->hdr.type);
-
-				if (proc_compose_imsg(ps, PROC_CONTROL, -1,
-				    IMSG_GET_INFO_THINGS_DATA,
-				    imsg->hdr.peerid, -1, &nti,
-				    sizeof(nti)) == -1)
-					return;
-			}
-		}
-
-		if (proc_compose_imsg(ps, PROC_CONTROL, -1,
-		    IMSG_GET_INFO_THINGS_END_DATA, imsg->hdr.peerid,
-			    -1, &nti, sizeof(nti)) == -1)
-				return;
-
-		break;
-	default:
-		log_debug("%s: error handling imsg", __func__);
-		break;
-	}
-}
-
-struct thing
-compose_thing(struct thing *thing, enum imsg_type type)
-{
-	struct thing		 nti;
-	char			 blank[9] = "********";
-
-	memset(&nti, 0, sizeof(nti));
-	nti.exists = thing->exists;
-	nti.hw_ctl = thing->hw_ctl;
-	nti.persist = thing->persist;
-
-	memcpy(&nti.iface, thing->iface, sizeof(nti.iface));
-
-	memcpy(&nti.ipaddr, thing->ipaddr, sizeof(nti.ipaddr));
-
-	memcpy(&nti.parity, thing->parity, sizeof(nti.parity));
-
-	memcpy(&nti.name, thing->name, sizeof(nti.name));
-
-	nti.password[0] = '\0';
-	if (type == IMSG_GET_INFO_THINGS_REQUEST)
-		memcpy(&nti.password, blank, sizeof(nti.password));
-	else
-		memcpy(&nti.password, thing->password,
-		    sizeof(nti.password));
-
-	memcpy(&nti.location, thing->location, sizeof(nti.location));
-
-	memcpy(&nti.udp, thing->udp, sizeof(nti.udp));
-
-	nti.fd = thing->fd;
-	nti.baud = thing->baud;
-	nti.conn_port = thing->conn_port;
-	nti.rcv_port = thing->rcv_port;
-	nti.data_bits = thing->data_bits;
-	nti.max_clients = thing->max_clients;
-	nti.port = thing->port;
-	nti.stop_bits = thing->stop_bits;
-	nti.type = thing->type;
-	nti.client_cnt = thing->client_cnt;
-
-	nti.tls = thing->tls;
-	memcpy(&nti.tls_cert_file, thing->tls_cert_file,
-	    sizeof(nti.tls_cert_file));
-
-	memcpy(&nti.tls_key_file, thing->tls_key_file,
-	    sizeof(nti.tls_key_file));
-
-	memcpy(&nti.tls_ca_file, thing->tls_ca_file,
-	    sizeof(nti.tls_ca_file));
-
-	memcpy(&nti.tls_crl_file, thing->tls_crl_file,
-	    sizeof(nti.tls_crl_file));
-
-	memcpy(&nti.tls_ocsp_staple_file, thing->tls_ocsp_staple_file,
-	    sizeof(nti.tls_ocsp_staple_file));
-
-	return nti;
-}
-
-void
-add_reconn(struct thing *thing)
-{
-	struct dead_thing	*dead_thing;
-
-	dead_thing = new_dead_thing(thing);
-
-	thingsd_env->exists = true;
-	thingsd_env->dcount++;
-
-	TAILQ_INSERT_TAIL(thingsd_env->dead_things->dead_things_list,
-	    dead_thing, entry);
-
-}
-
-struct dead_thing
-*new_dead_thing(struct thing *thing)
-{
-	struct dead_thing	*dead_thing;
-
-	dead_thing = calloc(1, sizeof(*dead_thing));
-	if (dead_thing == NULL)
-		fatal("%s: calloc", __func__);
-
-	log_debug("%s: adding detached thing, %s", __func__, thing->name);
-
-	memcpy(&dead_thing->name, thing->name, sizeof(dead_thing->name));
-
-	dead_thing->type = thing->type;
-	dead_thing->dtime = time(NULL);
-
-	return dead_thing;
-}
-
-void
-do_reconn(void)
-{
-	struct dead_thing	*dead_thing, *tdead_thing;
-	struct thing		*thing;
-
-	TAILQ_FOREACH_SAFE(dead_thing,
-	    thingsd_env->dead_things->dead_things_list, entry, tdead_thing) {
-		if ((size_t)(time(NULL) - dead_thing->dtime) >
-		    thingsd_env->conn_retry) {
-			dead_thing->dtime = time(NULL);
-
-			log_debug("attempting to reconnect %s",
-			    dead_thing->name);
-
-			switch (dead_thing->type) {
-			case DEV:
-				open_things(thingsd_env, true);
-				break;
-			case TCP:
-				create_sockets(thingsd_env, true);
-				break;
-			}
-		}
-	}
-
-	TAILQ_FOREACH_SAFE(dead_thing,
-	    thingsd_env->dead_things->dead_things_list, entry, tdead_thing) {
-		TAILQ_FOREACH(thing, thingsd_env->things, entry) {
-			if (strcmp(thing->name, dead_thing->name) == 0 &&
-			    thing->exists) {
-				TAILQ_REMOVE(thingsd_env->dead_things->
-				    dead_things_list,
-				    dead_thing, entry);
-				thingsd_env->dcount--;
-
-				free(dead_thing);
-
-				return;
-			}
-		}
-	}
-
-	if (thingsd_env->dcount == 0)
-		thingsd_env->exists = false;
-}
-
-void
-send_to_packet_client(struct thingsd *env, char *name, char *pkt, int len)
-{
-	struct packet_client	*packet_client;
-	int			 snm;
-
-	TAILQ_FOREACH(packet_client, env->packet_clients, entry) {
-		if (strlen(packet_client->name) != 0) {
-			snm = strcmp(name, packet_client->name);
-			if (snm == 0)
-				if (proc_compose_imsg(&packet_client->ps,
-				    PROC_CONTROL, -1, IMSG_SHOW_PACKETS_DATA,
-				    packet_client->imsg.hdr.peerid, -1,
-				    pkt, len) == -1)
-				return;
-		}
-	}
-}
\ No newline at end of file
blob - /dev/null
blob + 36838519876949921b38a0725d6abd22a0b9efaf (mode 644)
--- /dev/null	
+++ thingsd/things.c.orig	
@@ -0,0 +1,1063 @@
+/*
+ * Copyright (c) 2016, 2019-2021 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/queue.h>
+#include <sys/wait.h>
+#include <sys/uio.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+
+#include <net/if.h>
+#include <netinet/in.h>
+
+#include <errno.h>
+#include <event.h>
+#include <fcntl.h>
+#include <imsg.h>
+#include <limits.h>
+#include <poll.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <tls.h>
+#include <unistd.h>
+#include <util.h>
+
+#include "proc.h"
+#include "thingsd.h"
+
+int	 things_dispatch_thingsd(int, struct privsep_proc *, struct imsg *);
+void	 things_run(struct privsep *, struct privsep_proc *, void *);
+void	 thing_serial_open(struct thing *, int);
+int	 thing_setup(struct thing *, int);
+int	 thing_sock_setup(struct socket *, int);
+void	 thing_udp_event(int, short, void *);
+void	 things_launch(void);
+
+extern enum privsep_procid privsep_process;
+/* struct thing compose_thing(struct thing *, enum imsg_type); */
+
+static struct privsep_proc procs[] = {
+	{ "thingsd",	PROC_PARENT,	things_dispatch_thingsd  },
+};
+
+void
+things(struct privsep *ps, struct privsep_proc *p)
+{
+	proc_run(ps, p, procs, nitems(procs), things_run, NULL);
+}
+
+void
+things_run(struct privsep *ps, struct privsep_proc *p, void *arg)
+{
+	if (config_init(ps->ps_env) == -1)
+		fatal("failed to initialize configuration");
+
+	p->p_shutdown = things_shutdown;
+
+	socket_rlimit(-1);
+
+	signal_del(&ps->ps_evsigchld);
+	signal_set(&ps->ps_evsigchld, SIGCHLD, things_sighdlr, ps);
+	signal_add(&ps->ps_evsigchld, NULL);
+
+	if (pledge("stdio inet recvfd", NULL) == -1)
+		fatal("pledge");
+}
+
+void
+things_launch(void)
+{
+	/* struct thing		*thing; */
+	struct socket		*sock;
+	/* evbuffercb		 thingrd = thing_rd; */
+	/* evbuffercb		 thingwr = thing_wr; */
+
+	/* TAILQ_FOREACH(thing, thingsd_env->things, entry) { */
+	/* 	log_debug("%s: configuring thing %s (%d)", __func__, */
+	/* 	    thing->conf.name, thing->fd); */
+
+		/* TAILQ_FOREACH(sock, thing->sockets, entry) { */
+		/* 	if (sock->conf.id == thing->conf.sock_id) { */
+		/* 		log_debug("%s: assign thing %s listener %d", */
+		/* 		    __func__, thing->conf.name, sock->conf.id); */
+		/* 		thing->sock = sock; */
+		/* 		break; */
+		/* 	} */
+		/* } */
+
+		/* if ((thing->clients = calloc(1, */
+		/*     sizeof(*thing->clients))) == NULL) */
+		/* 	fatal("%s: calloc", __func__); */
+		/* SPLAY_INIT(&thing->clients); */
+
+		/* if (thing->conf.type == S_UDP) { */
+		/* 	event_set(&thing->udp_ev, thing->fd, EV_READ | */
+		/* 	    EV_PERSIST, thing_udp_event, thing); */
+		/* 	if (event_add(&thing->udp_ev, NULL)) */
+		/* 		fatalx("%s: udp ev error", __func__); */
+		/* } else { */
+			/* thing->bev = bufferevent_new(thing->fd, */
+			/*     thingrd, thingwr, thing_err, thing); */
+
+			/* if (thing->bev == NULL) */
+			/* 	fatalx("%s: ipaddr bev error", __func__); */
+
+			/* thing->evb = evbuffer_new(); */
+
+			/* if (thing->evb == NULL) */
+			/* 	fatalx("ipaddr evb error"); */
+
+			/* bufferevent_enable(thing->bev, EV_READ | EV_WRITE); */
+		/* } */
+		/* log_debug("%s: running thing %s listening on %d", __func__, */
+		/*     thing->conf.name, thing->conf.sock_id); */
+
+	/* } */
+
+	TAILQ_FOREACH(sock, thingsd_env->sockets, entry) {
+		log_debug("%s: configuring thing listener %d (%d)", __func__,
+		    sock->conf.id, sock->fd);
+
+		socket_tls_init(sock);
+
+		event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
+		    client_conn, thingsd_env);
+
+		if (event_add(&sock->ev, NULL))
+			fatalx("event add sock");
+
+		evtimer_set(&sock->pause, client_accept_paused, sock);
+
+		log_debug("%s: running thing listener %d", __func__,
+		    sock->conf.id);
+	}
+}
+
+int
+things_dispatch_thingsd(int fd, struct privsep_proc *p, struct imsg *imsg)
+{
+	struct privsep		*ps = p->p_ps;
+	int			 res = 0, cmd = 0, verbose;
+	unsigned int		 mode;
+
+	switch (imsg->hdr.type) {
+	case IMSG_CFG_THINGS:
+		config_getthing(thingsd_env, imsg, 0);
+		break;
+	case IMSG_CFG_SOCKS:
+		config_getthing(thingsd_env, imsg, 1);
+		break;
+	case IMSG_CFG_DONE:
+		config_getcfg(thingsd_env, imsg);
+		break;
+	case IMSG_CTL_RESET:
+		IMSG_SIZE_CHECK(imsg, &mode);
+		memcpy(&mode, imsg->data, sizeof(mode));
+
+		config_getreset(thingsd_env, imsg);
+		break;
+	case IMSG_CTL_VERBOSE:
+		IMSG_SIZE_CHECK(imsg, &verbose);
+		memcpy(&verbose, imsg->data, sizeof(verbose));
+		log_setverbose(verbose);
+		break;
+	case IMSG_CTL_START:
+		things_launch();
+		break;
+	default:
+		return (-1);
+	}
+
+	switch (cmd) {
+	case 0:
+		break;
+	case IMSG_GET_INFO_THINGS_END_DATA:
+		if (proc_compose_imsg(ps, PROC_PARENT, -1, cmd,
+		    imsg->hdr.peerid, -1, &mode, sizeof(mode)) == -1)
+			return (-1);
+		break;
+	default:
+		if (proc_compose_imsg(ps, PROC_PARENT, -1, cmd,
+		    imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
+			return (-1);
+		break;
+	}
+
+	return (0);
+}
+
+void
+things_sighdlr(int sig, short event, void *arg)
+{
+	switch (sig) {
+	default:
+		fatalx("unexpected signal");
+	}
+}
+
+void
+thing_serial_open(struct thing *thing, int reconn)
+{
+	struct termios		 s_opts;
+	int			 baudrate = 0, stop = 0;
+
+	if (thing->conf.type == S_DEV) {
+
+		/*
+		 * Just a reminder to set the ownership of your serial
+		 * devices to _thingsd. Otherwise, a thing will not be
+		 * able to successfully open(2) the file descriptor.
+		 */
+		if ((thing->fd = open(thing->conf.location, O_RDWR |
+		    O_NONBLOCK | O_NOCTTY | O_NDELAY)) == -1) {
+			log_warnx("failed to open %s", thing->conf.location);
+
+			if (reconn)
+				return;
+
+			/* dead_thing = new_dead_thing(thing); */
+
+			/* env->exists = 1; */
+			/* env->dcount++; */
+
+			/* TAILQ_INSERT_TAIL(env->dead_things-> */
+			/*     dead_things_list, dead_thing, entry); */
+
+			return;
+		} else {
+			/* load current s_opts */
+			tcgetattr(thing->fd, &s_opts);
+
+			/* set baud */
+			switch (thing->conf.baud) {
+			case 50:
+				baudrate = B50;
+				break;
+			case 75:
+				baudrate = B75;
+				break;
+			case 110:
+				baudrate = B110;
+				break;
+			case 134:
+				baudrate = B134;
+				break;
+			case 150:
+				baudrate = B150;
+				break;
+			case 200:
+				baudrate = B200;
+				break;
+			case 300:
+				baudrate = B300;
+				break;
+			case 600:
+				baudrate = B600;
+				break;
+			case 1200:
+				baudrate = B1200;
+				break;
+			case 1800:
+				baudrate = B1800;
+				break;
+			case 2400:
+				baudrate = B2400;
+				break;
+			case 4800:
+				baudrate = B4800;
+				break;
+			case 9600:
+				baudrate = B9600;
+				break;
+			case 19200:
+				baudrate = B19200;
+				break;
+			case 38400:
+				baudrate = B38400;
+				break;
+			case 57600:
+				baudrate = B57600;
+				break;
+			case 76800:
+				baudrate = B76800;
+				break;
+			case 115200:
+				baudrate = B115200;
+				break;
+			}
+
+			cfsetispeed(&s_opts, baudrate);
+			cfsetospeed(&s_opts, baudrate);
+
+			/* enable and set local */
+			s_opts.c_cflag |= (CLOCAL | CREAD);
+
+			/* set data bits */
+			if (thing->conf.data_bits != -1) {
+				s_opts.c_cflag &= ~CSIZE;
+				switch(thing->conf.data_bits) {
+				case 5:
+					stop = CS5;
+					break;
+				case 6:
+					stop = CS6;
+					break;
+				case 7:
+					stop = CS7;
+					break;
+				case 8:
+					stop = CS8;
+					break;
+				}
+				s_opts.c_cflag |= stop;
+			}
+
+			/* set parity */
+			if (strlen(thing->conf.parity) != 0) {
+				s_opts.c_cflag &= ~PARENB;
+
+				/* enable parity checking */
+				if (strcmp(thing->conf.parity, "odd") == 0) {
+					s_opts.c_cflag |= PARENB;
+					s_opts.c_cflag |= PARODD;
+					s_opts.c_iflag |= (INPCK |
+					    ISTRIP);
+				} else if (strcmp(thing->conf.parity,
+				    "even") == 0) {
+					s_opts.c_cflag |= PARENB;
+					s_opts.c_cflag &= ~PARODD;
+					s_opts.c_iflag |= (INPCK |
+					    ISTRIP);
+				}
+
+			}
+
+			/* set stop bits */
+			if (thing->conf.stop_bits != -1) {
+				if (thing->conf.stop_bits == 2)
+					s_opts.c_cflag |= CSTOPB;
+				else
+					s_opts.c_cflag &= ~CSTOPB;
+			}
+
+			/* set hardware control */
+			if (thing->conf.hw_ctl == 0) {
+				s_opts.c_cflag &= ~CRTSCTS;
+			} else {
+				s_opts.c_cflag |= CRTSCTS;
+			}
+
+			/* set software control */
+			if (thing->conf.sw_ctl == 0) {
+				s_opts.c_iflag &= ~(IXON | IXOFF |
+				    IXANY);
+			} else {
+				s_opts.c_iflag |= (IXON | IXOFF |
+				    IXANY);
+			}
+
+			/* set input/output as raw */
+			s_opts.c_lflag &= ~(ICANON | ECHO | ECHOE |
+			    ISIG);
+
+			s_opts.c_oflag &= ~OPOST;
+
+			/* Set the new options for the port */
+			tcsetattr(thing->fd, TCSANOW, &s_opts);
+
+			if (thing->fd == '\0') {
+				log_warnx("serial device not opened");
+				/* if (reconn) */
+				/* 	return; */
+				/* add_reconn(thing); */
+				return;
+			}
+		}
+
+		/* if (reconn) { */
+		/* 	thing->exists = 1; */
+		/* 	log_info("reconnected: %s", thing->name); */
+		/* } */
+	}
+}
+
+int
+thing_privinit(struct thing *thing)
+{
+	log_debug("%s: initializing thing %s", __func__, thing->conf.name);
+
+	thing_serial_open(thing, 0);
+
+	if (thing_setup(thing, 0) == -1)
+		return (-1);
+
+	return (0);
+}
+
+int
+thing_sock_privinit(struct socket *sock)
+{
+	log_debug("%s: initializing thing listener %d", __func__,
+	    sock->conf.id);
+
+	if (thing_sock_setup(sock, 0) == -1)
+		return (-1);
+
+	return (0);
+}
+
+int
+thing_sock_setup(struct socket *sock, int reconn)
+{
+	if ((sock->fd = create_socket(sock->conf.al, sock->conf.port,
+	    S_TCP)) == -1) {
+		log_warnx("%s: create sock socket failed", __func__);
+		return (-1);
+	}
+	return (0);
+}
+
+int
+thing_setup(struct thing *thing, int reconn)
+{
+	if (thing->conf.type == S_TCP) {
+		if (thing->conf.persist == 1) {
+			if ((thing->fd = open_client_socket(thing->conf.ipaddr,
+			    &thing->conf.tcp_conn_port)) == -1) {
+				log_warnx("%s: ipaddr connection failed",
+				    __func__);
+				if (reconn)
+					goto done;
+				/* add_reconn(thing); */
+				goto done;
+			}
+		} else
+			/* 0 indicates non-persistent socket */
+			thing->fd = 0;
+	}
+
+	if (thing->conf.type == S_UDP) {
+		if ((thing->fd = create_socket(thing->conf.udp_al,
+		    thing->conf.udp_rcv_port, S_UDP)) == -1)
+			fatalx("%s: create udp socket failed", __func__);
+		goto done;
+	}
+
+	/* if (reconn) { */
+	/* 	log_info("reconnected: %s", thing->name); */
+	/* 	goto done; */
+	/* } */
+done:
+	return (0);
+}
+
+void
+things_reset(void)
+{
+	struct thing		*thing, *tthing;
+	/* struct socket		*sock, *tsock; */
+	/* struct client		*client, *tclient; */
+	/* struct packet_client	*packet_client, *tpacket_client; */
+
+	/* clean up packet clients */
+	/* TAILQ_FOREACH_SAFE(packet_client, thingsd_env->packet_clients, entry, */
+	/*     tpacket_client) { */
+	/* 	TAILQ_REMOVE(thingsd_env->packet_clients, packet_client, entry); */
+	/* } */
+
+	/* clean up clients */
+	/* TAILQ_FOREACH_SAFE(client, thingsd_env->clients, entry, tclient) { */
+	/* 	close(client->fd); */
+	/* 	TAILQ_REMOVE(thingsd_env->clients, client, entry); */
+	/* } */
+
+	/* clean up sockets */
+	/* TAILQ_FOREACH_SAFE(sock, thingsd_env->sockets, entry, tsock) { */
+	/* 	if (sock->tls) { */
+	/* 		tls_config_free(sock->tls_config); */
+	/* 		tls_free(sock->tls_ctx); */
+	/* 	} */
+	/* 	close(sock->fd); */
+	/* 	TAILQ_REMOVE(thingsd_env->sockets, sock, entry); */
+	/* } */
+
+	/* clean up things */
+	TAILQ_FOREACH_SAFE(thing, thingsd_env->things, entry, tthing) {
+		close(thing->fd);
+		TAILQ_REMOVE(thingsd_env->things, thing, entry);
+	}
+
+}
+
+void
+things_shutdown(void)
+{
+	/* struct thing		*thing, *tthing; */
+	/* struct socket		*sock = NULL, *tsock; */
+	/* struct client		*client, *tclient; */
+	/* struct subscription	*sub, *tsub; */
+
+	/* struct dead_thing	*dead_thing, *tdead_thing; */
+	/* struct packet_client	*packet_client, *tpacket_client; */
+
+	/* clean up packet clients */
+	/* TAILQ_FOREACH_SAFE(packet_client, thingsd_env->packet_clients, entry, */
+	/*     tpacket_client) { */
+	/* 	TAILQ_REMOVE(thingsd_env->packet_clients, packet_client, entry); */
+	/* 	free(packet_client); */
+	/* } */
+
+	/* clean up dead things */
+	/* TAILQ_FOREACH_SAFE(dead_thing, */
+	/*     thingsd_env->dead_things->dead_things_list, entry, tdead_thing) { */
+	/* 	TAILQ_REMOVE(thingsd_env->dead_things->dead_things_list, */
+	/* 	    dead_thing, entry); */
+	/* 	free(dead_thing); */
+	/* } */
+
+	/* clean up clients */
+	/* TAILQ_FOREACH_SAFE(client, thingsd_env->clients, entry, tclient) { */
+	/* 	close(client->fd); */
+	/* 	TAILQ_FOREACH_SAFE(sub, client->subscriptions, entry, tsub) */
+	/* 		TAILQ_REMOVE(client->subscriptions, sub, entry); */
+	/* 	free(client->subscriptions); */
+	/* 	TAILQ_REMOVE(thingsd_env->clients, client, entry); */
+	/* 	free(client); */
+	/* } */
+	/* free(thingsd_env->clients); */
+
+
+	/* clean up sockets */
+
+	/* TAILQ_FOREACH_SAFE(sock, thingsd_env->sockets, entry, tsock) { */
+		/* TAILQ_FOREACH_SAFE(client, sock->clients, entry, tclient) { */
+		/* 	if (client->bev != NULL) */
+		/* 		bufferevent_disable(client->bev, */
+		/* 		    EV_READ | EV_WRITE); */
+		/* 	if (client->bev != NULL) */
+		/* 		bufferevent_free(client->bev); */
+
+		/* 	close(client->fd); */
+		/* 	TAILQ_FOREACH_SAFE(sub, client->subscriptions, entry, */
+		/* 	    tsub) */
+		/* 		TAILQ_REMOVE(client->subscriptions, sub, entry); */
+		/* 	free(client->subscriptions); */
+		/* 	TAILQ_REMOVE(sock->clients, client, entry); */
+		/* 	free(client); */
+		/* } */
+		/* free(thingsd_env->clients); */
+		/* if (sock->conf.tls) { */
+		/* 	tls_config_free(sock->tls_config); */
+		/* 	tls_free(sock->tls_ctx); */
+		/* 	/1* free(sock->conf.tls_cert); *1/ */
+		/* 	/1* free(sock->conf.tls_cert_file); *1/ */
+		/* 	/1* free(sock->conf.tls_key); *1/ */
+		/* 	/1* free(sock->conf.tls_key_file); *1/ */
+		/* 	free(sock->conf.tls_ca); */
+		/* 	free(sock->conf.tls_ca_file); */
+		/* 	free(sock->conf.tls_crl); */
+		/* 	free(sock->conf.tls_crl_file); */
+		/* 	free(sock->conf.tls_ocsp_staple); */
+		/* 	free(sock->conf.tls_ocsp_staple_file); */
+		/* } */
+		/* event_del(&sock->ev); */
+		/* evtimer_del(&sock->pause); */
+		/* close(sock->fd); */
+		/* TAILQ_REMOVE(thingsd_env->sockets, sock, entry); */
+		/* free(sock); */
+	/* } */
+	/* free(thingsd_env->sockets); */
+
+	/* clean up things */
+	/* TAILQ_FOREACH_SAFE(thing, thingsd_env->things, entry, tthing) { */
+	/* TAILQ_FOREACH_SAFE(sock, thing->sockets, entry, tsock) { */
+
+		/* TAILQ_FOREACH_SAFE(client, sock->clients, entry, tclient) { */
+		/* 	if (client->bev != NULL) */
+		/* 		bufferevent_disable(client->bev, */
+		/* 		    EV_READ | EV_WRITE); */
+		/* 	if (client->bev != NULL) */
+		/* 		bufferevent_free(client->bev); */
+
+		/* 	close(client->fd); */
+		/* 	TAILQ_FOREACH_SAFE(sub, client->subscriptions, entry, */
+		/* 	    tsub) */
+		/* 		TAILQ_REMOVE(client->subscriptions, sub, entry); */
+		/* 	free(client->subscriptions); */
+		/* 	TAILQ_REMOVE(sock->clients, client, entry); */
+		/* 	free(client); */
+		/* } */
+		/* free(thingsd_env->clients); */
+		/* if (sock->conf.tls) { */
+		/* 	tls_config_free(sock->tls_config); */
+		/* 	tls_free(sock->tls_ctx); */
+		/* 	/1* free(sock->conf.tls_cert); *1/ */
+		/* 	/1* free(sock->conf.tls_cert_file); *1/ */
+		/* 	/1* free(sock->conf.tls_key); *1/ */
+		/* 	/1* free(sock->conf.tls_key_file); *1/ */
+		/* 	free(sock->conf.tls_ca); */
+		/* 	free(sock->conf.tls_ca_file); */
+		/* 	free(sock->conf.tls_crl); */
+		/* 	free(sock->conf.tls_crl_file); */
+		/* 	free(sock->conf.tls_ocsp_staple); */
+		/* 	free(sock->conf.tls_ocsp_staple_file); */
+		/* } */
+		/* event_del(&sock->ev); */
+		/* evtimer_del(&sock->pause); */
+		/* close(sock->fd); */
+		/* TAILQ_REMOVE(thing->sockets, sock, entry); */
+		/* free(sock); */
+	/* } */
+	/* free(thing->sockets); */
+		/* event_del(&thing->udp_ev); */
+		/* if (thing->bev != NULL) */
+		/* 	bufferevent_disable(thing->bev, EV_READ | EV_WRITE); */
+		/* if (thing->bev != NULL) */
+		/* 	bufferevent_free(thing->bev); */
+
+		/* close(thing->fd); */
+		/* TAILQ_REMOVE(thingsd_env->things, thing, entry); */
+		/* free(thing); */
+	/* } */
+	free(thingsd_env->things);
+	free(thingsd_env);
+}
+
+void
+things_echo_pkt(struct privsep *ps, struct imsg *imsg)
+{
+	/* struct packet_client	*packet_client; */
+
+	/* packet_client = calloc(1, sizeof(*packet_client)); */
+	/* if (packet_client == NULL) */
+	/* 	fatal("%s: calloc", __func__); */
+
+	/* thingsd_env->packet_client_count++; */
+
+	/* packet_client->ps = *ps; */
+	/* packet_client->imsg = *imsg; */
+
+	/* memcpy(packet_client->name, imsg->data,  sizeof(packet_client->name)); */
+
+	/* log_debug("control packet echo request for %s", packet_client->name); */
+
+	/* TAILQ_INSERT_TAIL(thingsd_env->packet_clients, packet_client, entry); */
+}
+
+/* void */
+/* things_stop_pkt(struct privsep *ps, struct imsg *imsg) */
+/* { */
+/* 	struct packet_client	*packet_client, *tpacket_client; */
+/* 	uint32_t		 fd; */
+
+/* 	memcpy(&fd, imsg->data, sizeof(fd)); */
+
+/* 	TAILQ_FOREACH_SAFE(packet_client, thingsd_env->packet_clients, entry, */
+/* 	    tpacket_client) { */
+/* 		if (fd == packet_client->imsg.hdr.peerid) { */
+/* 			TAILQ_REMOVE(thingsd_env->packet_clients, */
+/* 			    packet_client, entry); */
+/* 			log_debug("control packet echo request stopping for %s", */
+/* 			    packet_client->name); */
+/* 			thingsd_env->packet_client_count--; */
+/* 			free(packet_client); */
+/* 			return; */
+/* 		} */
+/* 	} */
+
+/* } */
+
+void
+things_show_info(struct privsep *ps, struct imsg *imsg)
+{
+	char filter[THINGSD_MAXNAME];
+	struct thing *thing, nti;
+
+	switch (imsg->hdr.type) {
+	case IMSG_GET_INFO_THINGS_REQUEST:
+	case IMSG_GET_INFO_THINGS_REQUEST_ROOT:
+
+		memcpy(filter, imsg->data, sizeof(filter));
+
+		TAILQ_FOREACH(thing, thingsd_env->things, entry) {
+			if (filter[0] == '\0' || memcmp(filter,
+			    thing->conf.name, sizeof(filter)) == 0) {
+				/* nti = compose_thing(thing, imsg->hdr.type); */
+
+				if (proc_compose_imsg(ps, PROC_CONTROL, -1,
+				    IMSG_GET_INFO_THINGS_DATA,
+				    imsg->hdr.peerid, -1, &nti,
+				    sizeof(nti)) == -1)
+					return;
+			}
+		}
+
+		if (proc_compose_imsg(ps, PROC_CONTROL, -1,
+		    IMSG_GET_INFO_THINGS_END_DATA, imsg->hdr.peerid,
+			    -1, &nti, sizeof(nti)) == -1)
+				return;
+
+		break;
+	default:
+		log_debug("%s: error handling imsg", __func__);
+		break;
+	}
+}
+
+/* struct thing */
+/* compose_thing(struct thing *thing, enum imsg_type type) */
+/* { */
+/* 	struct thing		 nti; */
+/* 	char			 blank[9] = "********"; */
+
+/* 	memset(&nti, 0, sizeof(nti)); */
+/* 	nti.exists = thing->exists; */
+/* 	nti.hw_ctl = thing->hw_ctl; */
+/* 	nti.persist = thing->persist; */
+
+/* 	memcpy(&nti.tcp_iface, thing->tcp_iface, sizeof(nti.tcp_iface)); */
+
+/* 	memcpy(&nti.ipaddr, thing->ipaddr, sizeof(nti.ipaddr)); */
+
+/* 	memcpy(&nti.parity, thing->parity, sizeof(nti.parity)); */
+
+/* 	memcpy(&nti.name, thing->name, sizeof(nti.name)); */
+
+/* 	nti.password[0] = '\0'; */
+/* 	if (type == IMSG_GET_INFO_THINGS_REQUEST) */
+/* 		memcpy(&nti.password, blank, sizeof(nti.password)); */
+/* 	else */
+/* 		memcpy(&nti.password, thing->password, */
+/* 		    sizeof(nti.password)); */
+
+/* 	memcpy(&nti.location, thing->location, sizeof(nti.location)); */
+
+/* 	memcpy(&nti.udp, thing->udp, sizeof(nti.udp)); */
+
+/* 	nti.fd = thing->fd; */
+/* 	nti.baud = thing->baud; */
+/* 	nti.conn_port = thing->conn_port; */
+/* 	nti.rcv_port = thing->rcv_port; */
+/* 	nti.data_bits = thing->data_bits; */
+/* 	nti.max_clients = thing->max_clients; */
+/* 	nti.port = thing->port; */
+/* 	nti.stop_bits = thing->stop_bits; */
+/* 	nti.type = thing->type; */
+/* 	nti.client_cnt = thing->client_cnt; */
+
+/* 	nti.tls = thing->tls; */
+/* 	memcpy(&nti.tls_cert_file, thing->tls_cert_file, */
+/* 	    sizeof(nti.tls_cert_file)); */
+
+/* 	memcpy(&nti.tls_key_file, thing->tls_key_file, */
+/* 	    sizeof(nti.tls_key_file)); */
+
+/* 	memcpy(&nti.tls_ca_file, thing->tls_ca_file, */
+/* 	    sizeof(nti.tls_ca_file)); */
+
+/* 	memcpy(&nti.tls_crl_file, thing->tls_crl_file, */
+/* 	    sizeof(nti.tls_crl_file)); */
+
+/* 	memcpy(&nti.tls_ocsp_staple_file, thing->tls_ocsp_staple_file, */
+/* 	    sizeof(nti.tls_ocsp_staple_file)); */
+
+/* 	return nti; */
+/* } */
+
+/* void */
+/* add_reconn(struct thing *thing) */
+/* { */
+/* 	struct dead_thing	*dead_thing; */
+
+/* 	dead_thing = new_dead_thing(thing); */
+
+/* 	thingsd_env->exists = 1; */
+/* 	thingsd_env->dcount++; */
+
+/* 	TAILQ_INSERT_TAIL(thingsd_env->dead_things->dead_things_list, */
+/* 	    dead_thing, entry); */
+
+/* } */
+
+/* struct dead_thing */
+/* *new_dead_thing(struct thing *thing) */
+/* { */
+/* 	struct dead_thing	*dead_thing; */
+
+/* 	dead_thing = calloc(1, sizeof(*dead_thing)); */
+/* 	if (dead_thing == NULL) */
+/* 		fatal("%s: calloc", __func__); */
+
+/* 	log_debug("%s: adding detached thing, %s", __func__, thing->name); */
+
+/* 	memcpy(&dead_thing->name, thing->name, sizeof(dead_thing->name)); */
+
+/* 	dead_thing->type = thing->type; */
+/* 	dead_thing->dtime = time(NULL); */
+
+/* 	return dead_thing; */
+/* } */
+
+/* void */
+/* do_reconn(void) */
+/* { */
+/* 	struct dead_thing	*dead_thing, *tdead_thing; */
+/* 	struct thing		*thing; */
+
+/* 	TAILQ_FOREACH_SAFE(dead_thing, */
+/* 	    thingsd_env->dead_things->dead_things_list, entry, tdead_thing) { */
+/* 		if ((size_t)(time(NULL) - dead_thing->dtime) > */
+/* 		    thingsd_env->conn_retry) { */
+/* 			dead_thing->dtime = time(NULL); */
+
+/* 			log_debug("attempting to reconnect %s", */
+/* 			    dead_thing->name); */
+
+/* 			switch (dead_thing->type) { */
+/* 			case DEV: */
+/* 				/1* open_things(thingsd_env, 1); *1/ */
+/* 				break; */
+/* 			case TCP: */
+/* 				/1* create_sockets(thingsd_env, 1); *1/ */
+/* 				break; */
+/* 			} */
+/* 		} */
+/* 	} */
+
+/* 	TAILQ_FOREACH_SAFE(dead_thing, */
+/* 	    thingsd_env->dead_things->dead_things_list, entry, tdead_thing) { */
+/* 		TAILQ_FOREACH(thing, thingsd_env->things, entry) { */
+/* 			if (strcmp(thing->name, dead_thing->name) == 0 && */
+/* 			    thing->exists) { */
+/* 				TAILQ_REMOVE(thingsd_env->dead_things-> */
+/* 				    dead_things_list, */
+/* 				    dead_thing, entry); */
+/* 				thingsd_env->dcount--; */
+
+/* 				free(dead_thing); */
+
+/* 				return; */
+/* 			} */
+/* 		} */
+/* 	} */
+
+/* 	if (thingsd_env->dcount == 0) */
+/* 		thingsd_env->exists = 0; */
+/* } */
+
+/* void */
+/* send_to_packet_client(struct thingsd *env, char *name, char *pkt, int len) */
+/* { */
+/* 	struct packet_client	*packet_client; */
+/* 	int			 snm; */
+
+/* 	TAILQ_FOREACH(packet_client, env->packet_clients, entry) { */
+/* 		if (strlen(packet_client->name) != 0) { */
+/* 			snm = strcmp(name, packet_client->name); */
+/* 			if (snm == 0) */
+/* 				if (proc_compose_imsg(&packet_client->ps, */
+/* 				    PROC_CONTROL, -1, IMSG_SHOW_PACKETS_DATA, */
+/* 				    packet_client->imsg.hdr.peerid, -1, */
+/* 				    pkt, len) == -1) */
+/* 				return; */
+/* 		} */
+/* 	} */
+/* } */
+
+void
+thing_rd(struct bufferevent *bev, void *arg)
+{
+	/* struct thingsd		*env = (struct thingsd *)arg; */
+	/* struct thing		*thing = (struct thing *) arg; */
+	/* struct socket		*sock, *csock = NULL; */
+	/* struct client		*client; */
+	/* struct subscription	*sub; */
+	/* size_t			 len; */
+	/* int			 fd = bev->ev_read.ev_fd; */
+	/* char			*pkt = NULL; */
+
+	/* log_info("%s", __func__); */
+	/* TAILQ_FOREACH(thing, thingsd_env->things, entry) */
+	/* 	if (thing->fd == fd) */
+	/* 		break; */
+
+	/* sock = thing->sock; */
+	/* TAILQ_FOREACH(csock, thing->sockets, entry) */
+	/* 	if (sock->conf.child_id == csock->conf.id) */
+	/* 		break; */
+
+	/* thing->evb = EVBUFFER_INPUT(bev); */
+
+	/* len = EVBUFFER_LENGTH(thing->evb); */
+
+	/* if ((pkt = calloc(len, sizeof(*pkt))) == NULL) */
+	/* 	return; */
+
+	/* evbuffer_remove(thing->evb, pkt, len); */
+/* log_info("Pkt: %s", pkt); */
+	/* TAILQ_FOREACH(client, thing->clients, entry) */
+	/* 	TAILQ_FOREACH(sub, client->subscriptions, entry) */
+	/* 		if (strcmp(sub->thing_name, thing->conf.name) == 0) */
+	/* 			bufferevent_write(client->bev, pkt, len); */
+
+	/* if (env->packet_client_count > 0) */
+	/* 	send_to_packet_client(env, thing->name, pkt, */
+	/* 	    len); */
+
+	/* if (csock != NULL) */
+	/* 	TAILQ_FOREACH(client, csock->clients, entry) */
+	/* 		TAILQ_FOREACH(sub, client->subscriptions, entry) */
+	/* 			if (strcmp(sub->thing_name, */
+	/* 			    thing->conf.name) == 0) */
+	/* 				bufferevent_write(client->bev, pkt, */
+	/* 				    len); */
+	/* free(pkt); */
+}
+
+void
+thing_wr(struct bufferevent *bev, void *arg)
+{
+}
+
+void
+thing_err(struct bufferevent *bev, short error, void *arg)
+{
+	struct thingsd		*env = (struct thingsd *)arg;
+	struct thing		*thing = NULL;
+	int			 fd = bev->ev_read.ev_fd;
+
+	if ((error & EVBUFFER_ERROR) == 0 || error & EVBUFFER_TIMEOUT) {
+		TAILQ_FOREACH(thing, env->things, entry) {
+			if (thing->fd == fd && thing->conf.persist) {
+				if (thing->bev != NULL)
+					bufferevent_disable(thing->bev,
+					    EV_READ | EV_WRITE);
+				if (thing->bev != NULL)
+					bufferevent_free(thing->bev);
+				thing->evb = NULL;
+				thing->bev = NULL;
+				close(thing->fd);
+				thing->fd = -1;
+				/* add_reconn(thing); */
+				log_warnx("thing error: %s disconnected",
+				    thing->conf.name);
+			}
+		}
+	}
+}
+
+void
+thing_udp_event(int fd, short event, void *arg)
+{
+	/* struct thingsd		*env = (struct thingsd *)arg; */
+	/* struct thing		*thing = (struct thing *) arg; */
+	/* struct client		*client = NULL; */
+	/* struct package		*package = NULL, *pkg; */
+	/* struct subscription	*sub = NULL; */
+	/* struct sockaddr		*addr = NULL; */
+	/* struct socket		*sock, *csock = NULL; */
+	/* char			 pkt[PKT_BUFF]; */
+	/* socklen_t		*addrlen = NULL; */
+
+	/* memset(pkt, 0, sizeof(pkt)); */
+
+	/* TAILQ_FOREACH(thing, env->things, entry) */
+	/* 	if (thing->fd == fd) */
+	/* 		break; */
+
+	/* sock = thing->sock; */
+	/* TAILQ_FOREACH(csock, env->sockets, entry) */
+	/* 	if (sock->conf.child_id == csock->conf.id) */
+	/* 		break; */
+
+	/*
+	 * XXX: I think events race here. Figure that out. But, I think that
+	 * the len variable is getting changed before the write event can
+	 * even happen.
+	 *
+	 * Update, it looks like each privsep process isn'ta  full copy? You
+	 * still don't understand what the fuck you're doing.
+	 *
+	 * write to clients
+	 */
+
+
+	/* if ((package = calloc(1, sizeof(*package))) == NULL) */
+	/* 	fatalx("%s: calloc", __func__); */
+	/* if ((package->pkt = calloc(1, sizeof(*package->pkt))) == NULL) */
+	/* 	fatalx("%s: calloc", __func__); */
+
+	/* package->len = recvfrom(fd, pkt, sizeof(pkt), 0, addr, addrlen); */
+
+	/* if (package->len == -1) */
+	/* 	goto done; */
+
+	/* if (snprintf(package->pkt, package->len, "%s", pkt) < 0) */
+	/* 	fatal("%s: snprintf", __func__); */
+/* log_info("Pkt: %s", package->pkt); */
+	/* while ((client = SPLAY_ROOT(&thing->clients)) != NULL) */
+
+	/* SPLAY_FOREACH(client, clientlist, &thing->clients) */
+	/* 	bufferevent_write(client->bev, package->pkt, package->len); */
+	/* TAILQ_FOREACH(client, thing->clients, entry) { */
+	/* log_info("create package %s", package->pkt); */
+	/* 	TAILQ_FOREACH(sub, client->subscriptions, entry) { */
+	/* 		if (strcmp(sub->thing_name, */
+	/* 		    thing->conf.name) == 0) { */
+	/* 			/1* package->bev = client->bev; *1/ */
+	/* 			log_info("enter package"); */
+	/* 			/1* TAILQ_INSERT_TAIL(client->packages, package, *1/ */
+	/* 			/1*     entry); *1/ */
+	/* 			bufferevent_write(client->bev, package->pkt, */
+	/* 			    package->len); */
+	/* 		} */
+	/* 	} */
+	/* } */
+	/* if (csock != NULL) */
+	/* 	TAILQ_FOREACH(client, csock->clients, entry) */
+	/* 		TAILQ_FOREACH(sub, client->subscriptions, entry) */
+	/* 			if (strcmp(sub->thing_name, */
+	/* 			    thing->conf.name) == 0) { */
+	/* 				/1* package->bev = client->bev; *1/ */
+	/* 			log_info("enter cpackage"); */
+	/* 				/1* TAILQ_INSERT_TAIL(client->packages, *1/ */
+	/* 				/1*     package, entry); *1/ */
+	/* 			} */
+
+
+	/* TAILQ_FOREACH(pkg, client->packages, entry) { */
+	/* 	log_info("write pkg"); */
+	/* 	bufferevent_write(pkg->bev, pkg->pkt, pkg->len); */
+	/* } */
+/* done: */
+/* 	free(package->pkt); */
+/* 	free(package); */
+	/* if (env->packet_client_count > 0) */
+	/* 	send_to_packet_client(env, thing->name, pkt, len); */
+}
+
+int
+thing_client_cmp(struct client *a, struct client *b)
+{
+	return ((int)a->id - b->id);
+}
blob - d4fa01a8b54fbb62f5986f5905c2bb05d4de31bc
blob + 926e9290d5bf4516a0b5ee08d318e63b3861e5bf
--- thingsd/thingsd.c	
+++ thingsd/thingsd.c	
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2019, 2020 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2016, 2019-2021 Tracey Emery <tracey@traceyemery.net>
  * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -17,6 +17,7 @@
 
 #include <sys/param.h>
 #include <sys/queue.h>
+#include <sys/resource.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
 #include <sys/cdefs.h>
@@ -35,7 +36,6 @@
 #include <imsg.h>
 #include <pwd.h>
 #include <signal.h>
-#include <stdbool.h>
 #include <syslog.h>
 #include <unistd.h>
 #include <ctype.h>
@@ -47,39 +47,53 @@
 __dead void usage(void);
 
 int	 main(int, char **);
-int	 thingsd_configure(struct privsep *);
-void	 thingsd_sighdlr(int sig, short event, void *arg);
-void	 thingsd_shutdown(void);
+
+int	 thingsd_configure(struct thingsd *);
 int	 thingsd_control_run(void);
 int	 thingsd_dispatch_control(int, struct privsep_proc *, struct imsg *);
-int	 thingsd_dispatch_things(int, struct privsep_proc *, struct imsg *);
-void	 thingsd_configure_things(struct privsep *);
+int	 thingsd_dispatch_sockets(int, struct privsep_proc *, struct imsg *);
 
+void	 thingsd_sighdlr(int sig, short event, void *arg);
+void	 thingsd_shutdown(struct thingsd *);
+void	 thingsd_parent_shutdown(struct thingsd *);
+void	 thingsd_configure_done(struct thingsd *);
+void	 thingsd_reload(int);
+void	 thingsd_thing_serial_open(struct thing *, int);
+void	 thingsd_thing_setup(struct thingsd *, struct thing *, int);
 void	 thingsd_show_info(struct privsep *, struct imsg *);
+void	 thingsd_thing_udp_event(int, short, void *);
+void	 thingsd_thing_read(struct bufferevent *, void *);
+void	 thingsd_thing_write(struct bufferevent *, void *);
+void	 thingsd_thing_err(struct bufferevent *, short, void *);
+void	 thingsd_add_reconn(struct thing *);
+void	 thingsd_do_reconn(struct thingsd *);
+void	 thingsd_write_to_socks(struct packages *);
+void	 thingsd_write_to_things(struct package *);
 
+struct	 dead_thing *thingsd_new_dead_thing(struct thing *);
+
+extern enum privsep_procid privsep_process;
+
 struct thingsd	*thingsd_env;
+int		 thing_id;
 
 static struct privsep_proc procs[] = {
 	{ "control",	PROC_CONTROL,	thingsd_dispatch_control, control },
-	{ "things",	PROC_THINGS,	thingsd_dispatch_things, things,
-	    things_shutdown },
+	{ "sockets",	PROC_SOCKS,	thingsd_dispatch_sockets, sockets,
+	    sockets_shutdown },
 };
 
 /* For the privileged process */
 static struct privsep_proc *proc_priv = &procs[0];
 static struct passwd proc_privpw;
 
-extern enum privsep_procid privsep_process;
-
 int
 thingsd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg)
 {
 	struct privsep	*ps = p->p_ps;
 	int		 res = 0, cmd = 0, verbose, exists = 0;
 	unsigned int	 v = 0;
-	char		 client_name[THINGSD_MAXNAME];
 	char		 thing_name[THINGSD_MAXNAME];
-	struct client	*client;
 	struct thing	*thing;
 
 	switch (imsg->hdr.type) {
@@ -87,7 +101,7 @@ thingsd_dispatch_control(int fd, struct privsep_proc *
 		IMSG_SIZE_CHECK(imsg, &v);
 		if (imsg->data == NULL)
 			break;
-		things_stop_pkt(ps, imsg);
+		/* things_stop_pkt(ps, imsg); */
 		break;
 	case IMSG_SHOW_PACKETS_REQUEST:
 		IMSG_SIZE_CHECK(imsg, &v);
@@ -95,14 +109,14 @@ thingsd_dispatch_control(int fd, struct privsep_proc *
 			break;
 		memcpy(thing_name, imsg->data, sizeof(thing_name));
 		TAILQ_FOREACH(thing, thingsd_env->things, entry) {
-			if (strcmp(thing->name, thing_name) == 0) {
-				things_echo_pkt(ps, imsg);
+			if (strcmp(thing->conf.name, thing_name) == 0) {
+				/* things_echo_pkt(ps, imsg); */
 				exists = 1;
 				break;
 			}
 		}
 		if (!exists)
-			if (proc_compose_imsg(ps, PROC_CONTROL, -1, 
+			if (proc_compose_imsg(ps, PROC_CONTROL, -1,
 			    IMSG_BAD_THING,
 			    imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
 				return (-1);
@@ -111,28 +125,28 @@ thingsd_dispatch_control(int fd, struct privsep_proc *
 		IMSG_SIZE_CHECK(imsg, &v);
 		if (imsg->data == NULL)
 			break;
-		memcpy(client_name, imsg->data, sizeof(client_name));
-		TAILQ_FOREACH(client, thingsd_env->clients, entry) {
-			if (strcmp(client->name, client_name) == 0) {
-				log_debug("Control killed client: %s",
-				    client_name);
-				client_del(thingsd_env, client);
-				break;
-			}
-		}
+		/* memcpy(client_name, imsg->data, sizeof(client_name)); */
+		/* TAILQ_FOREACH(client, thingsd_env->clients, entry) { */
+		/* 	if (strcmp(client->name, client_name) == 0) { */
+		/* 		log_debug("Control killed client: %s", */
+		/* 		    client_name); */
+		/* 		client_del(thingsd_env, client); */
+		/* 		break; */
+		/* 	} */
+		/* } */
 		break;
 	case IMSG_GET_INFO_CLIENTS_REQUEST:
-		clients_show_info(ps, imsg);
+		/* clients_show_info(ps, imsg); */
 		break;
 	case IMSG_GET_INFO_SOCKETS_REQUEST:
-		sockets_show_info(ps, imsg);
+		/* sockets_show_info(ps, imsg); */
 		break;
 	case IMSG_GET_INFO_THINGS_REQUEST:
 	case IMSG_GET_INFO_THINGS_REQUEST_ROOT:
-		things_show_info(ps, imsg);
+		/* things_show_info(ps, imsg); */
 		break;
-	case IMSG_GET_INFO_PARENT_REQUEST:
-		thingsd_show_info(ps, imsg);
+	case IMSG_GET_INFO_THINGSD_REQUEST:
+		/* thingsd_show_info(ps, imsg); */
 		break;
 	case IMSG_CTL_RESET:
 		IMSG_SIZE_CHECK(imsg, &v);
@@ -144,7 +158,7 @@ thingsd_dispatch_control(int fd, struct privsep_proc *
 		memcpy(&verbose, imsg->data, sizeof(verbose));
 		log_setverbose(verbose);
 
-		proc_forward_imsg(ps, imsg, PROC_THINGS, -1);
+		/* proc_forward_imsg(ps, imsg, PROC_SOCKS, -1); */
 		break;
 	default:
 		return (-1);
@@ -164,14 +178,21 @@ thingsd_dispatch_control(int fd, struct privsep_proc *
 }
 
 int
-thingsd_dispatch_things(int fd, struct privsep_proc *p, struct imsg *imsg)
+thingsd_dispatch_sockets(int fd, struct privsep_proc *p, struct imsg *imsg)
 {
+	struct package		*package = NULL;
 	struct privsep		*ps = p->p_ps;
+	struct thingsd		*env = ps->ps_env;
 
 	switch (imsg->hdr.type) {
-	case IMSG_ADD_THING:
-		proc_forward_imsg(ps, imsg, PROC_CONTROL, -1);
+	case  IMSG_DIST_CLIENT_PACKAGE:
+		IMSG_SIZE_CHECK(imsg, package);
+		package = (struct package *)imsg->data;
+		thingsd_write_to_things(package);
 		break;
+	case IMSG_CFG_DONE:
+		thingsd_configure_done(env);
+		break;
 	default:
 		return (-1);
 	}
@@ -182,6 +203,8 @@ thingsd_dispatch_things(int fd, struct privsep_proc *p
 void
 thingsd_sighdlr(int sig, short event, void *arg)
 {
+	struct privsep	*ps = arg;
+
 	if (privsep_process != PROC_PARENT)
 		return;
 
@@ -203,7 +226,7 @@ thingsd_sighdlr(int sig, short event, void *arg)
 		break;
 	case SIGTERM:
 	case SIGINT:
-		thingsd_shutdown();
+		thingsd_shutdown(ps->ps_env);
 		break;
 	default:
 		fatalx("unexpected signal");
@@ -224,22 +247,16 @@ main(int argc, char **argv)
 	struct thingsd		*env;
 	struct privsep		*ps;
 	struct timeval		 eb_timeout;
-	int			 ch;
+	const char		*errp, *title = NULL;
 	const char		*conffile = THINGSD_CONF;
-	enum privsep_procid	 proc_id = PROC_PARENT;
 	int			 proc_instance = 0;
-	const char		*errp, *title = NULL;
+	int			 ch;
 	int			 argc0 = argc;
+	enum privsep_procid	 proc_id = PROC_PARENT;
 
-	/* log to stderr until daemonized */
-	log_init(1, LOG_DAEMON);
-
-	env = calloc(1, sizeof(*env));
-	if (env == NULL)
+	if ((env = calloc(1, sizeof(*env))) == NULL)
 		fatal("%s: calloc", __func__);
 
-	thingsd_env = env;
-
 	while ((ch = getopt(argc, argv, "D:P:I:df:vn")) != -1) {
 		switch (ch) {
 		case 'D':
@@ -276,10 +293,25 @@ main(int argc, char **argv)
 		}
 	}
 
+	/* log to stderr until daemonized */
+	log_init(env->thingsd_debug ? env->thingsd_debug : 1, LOG_DAEMON);
+
 	argc -= optind;
 	if (argc > 0)
 		usage();
 
+	if ((ps = calloc(1, sizeof(*ps))) == NULL)
+		fatal("%s: calloc", __func__);
+
+	thingsd_env = env;
+	env->thingsd_ps = ps;
+	ps->ps_env = env;
+	TAILQ_INIT(&ps->ps_rcsocks);
+	env->thingsd_conffile = conffile;
+
+	if (parse_config(env->thingsd_conffile, env) == -1)
+		exit(1);
+
 	if (env->thingsd_noaction && !env->thingsd_debug)
 		env->thingsd_debug = 1;
 
@@ -289,28 +321,16 @@ main(int argc, char **argv)
 			fatalx("need root privileges");
 	}
 
-	ps = &env->thingsd_ps;
-	ps->ps_env = env;
-
-	if (config_init(env) == -1)
-		fatal("failed to initialize configuration");
-
-	ps->ps_pw = getpwnam(THINGSD_USER);
-	if (ps->ps_pw == NULL)
+	if ((ps->ps_pw = getpwnam(THINGSD_USER)) == NULL)
 		fatal("unknown user %s", THINGSD_USER);
 
-	env->thingsd_conffile = conffile;
 
-	if (parse_config(env->thingsd_conffile) == -1)
-		exit(1);
-
 	/* First proc runs as root without pledge but in default chroot */
 	proc_priv->p_pw = &proc_privpw; /* initialized to all 0 */
 	proc_priv->p_chroot = ps->ps_pw->pw_dir; /* from THINGSD_USER */
 
 	/* Configure the control socket */
 	ps->ps_csock.cs_name = THINGSD_SOCKET;
-	TAILQ_INIT(&ps->ps_rcsocks);
 
 	log_init(env->thingsd_debug, LOG_DAEMON);
 	log_setverbose(env->thingsd_verbose);
@@ -318,22 +338,23 @@ main(int argc, char **argv)
 	if (env->thingsd_noaction)
 		ps->ps_noaction = 1;
 
-	ps->ps_instances[PROC_THINGS] = env->prefork_things;
+	ps->ps_instances[PROC_SOCKS] = env->prefork_socks;
 	ps->ps_instance = proc_instance;
 	if (title != NULL)
 		ps->ps_title[proc_id] = title;
 
-	/* only the parent returns */
-	proc_init(ps, procs, nitems(procs), argc0, argv, proc_id);
+	/* only the thingsd returns */
+	proc_init(ps, procs, nitems(procs), env->thingsd_debug, argc0,
+	    argv, proc_id);
 
-	log_procinit("parent");
+	log_procinit("thingsd");
 	if (!env->thingsd_debug && daemon(0, 0) == -1)
 		fatal("can't daemonize");
 
 	if (ps->ps_noaction == 0)
 		log_info("%s startup", getprogname());
 
-	env->things_eb = event_init();
+	env->thingsd_eb = event_init();
 
 	signal_set(&ps->ps_evsigint, SIGINT, thingsd_sighdlr, ps);
 	signal_set(&ps->ps_evsigterm, SIGTERM, thingsd_sighdlr, ps);
@@ -350,29 +371,13 @@ main(int argc, char **argv)
 	if (!env->thingsd_noaction)
 		proc_connect(ps);
 
-	if (thingsd_configure(ps) == -1)
+	if (thingsd_configure(env) == -1)
 		fatalx("configuration failed");
 
 	/* begin thing watchdog */
 	eb_timeout.tv_sec = EB_TIMEOUT;
 	eb_timeout.tv_usec = 0;
 
-	while (env->dead_things->run) {
-		if (env->exists) {
-			do_reconn();
-		}
-		event_base_loopexit(env->things_eb, &eb_timeout);
-		event_base_dispatch(env->things_eb);
-	}
-
-	log_debug("%s parent exiting", getprogname());
-
-	return (0);
-}
-
-int
-thingsd_configure(struct privsep *ps)
-{
 	if (unveil(THINGSD_CONF, "r") == -1)
 		err(1, "unveil");
 	if (unveil("/dev", "rw") == -1)
@@ -383,67 +388,125 @@ thingsd_configure(struct privsep *ps)
 	if (pledge("stdio rpath wpath inet proc tty dns", NULL) == -1)
 		err(1, "pledge");
 
-	if (parse_config(thingsd_env->thingsd_conffile) == -1) {
-		proc_kill(&thingsd_env->thingsd_ps);
-		exit(1);
+	env->run = 1;
+	while (env->run) {
+		if (env->exists)
+			thingsd_do_reconn(env);
+		event_base_loopexit(env->thingsd_eb, &eb_timeout);
+		event_base_dispatch(env->thingsd_eb);
 	}
 
-	if (thingsd_env->thingsd_noaction) {
+	log_debug("%s thingsd exiting", getprogname());
+
+	return (0);
+}
+
+int
+thingsd_configure(struct thingsd *env)
+{
+	struct thing		*thing;
+	struct socket		*sock;
+	unsigned int		 id;
+
+	if (env->thingsd_noaction) {
 		fprintf(stderr, "configuration OK\n");
-		proc_kill(&thingsd_env->thingsd_ps);
+		proc_kill(env->thingsd_ps);
 		exit(0);
 	}
 
-	thingsd_configure_things(ps);
+	env->socks_reload = env->prefork_socks;
 
+	/* setup our things, which run privileged */
+	TAILQ_FOREACH(thing, env->things, entry) {
+		/* setup serial things */
+		thingsd_thing_serial_open(thing, 0);
+
+		/* setup udp/tcp things */
+		thingsd_thing_setup(env, thing, 0);
+	}
+
+
+	TAILQ_FOREACH(sock, env->sockets, entry) {
+		if (config_setsocks(env, sock) == -1)
+			fatalx("%s: send socket error", __func__);
+	}
+
+	for (id = 0; id < PROC_MAX; id++) {
+		if (id == privsep_process)
+			continue;
+		proc_compose(env->thingsd_ps, id, IMSG_CFG_DONE, NULL, 0);
+	}
+
 	return (0);
 }
 
 void
+thingsd_configure_done(struct thingsd *env)
+{
+	unsigned int	 id;
+	if (env->socks_reload == 0) {
+		log_warnx("%s: configuration already finished", __func__);
+		return;
+	}
+
+	env->socks_reload--;
+	if (env->socks_reload == 0) {
+		for (id = 0; id < PROC_MAX; id++) {
+			if (id == privsep_process)
+				continue;
+			proc_compose(env->thingsd_ps, id, IMSG_CTL_START,
+			    NULL, 0);
+		}
+	}
+}
+
+void
 thingsd_reload(int reset)
 {
 	const char *filename = thingsd_env->thingsd_conffile;
 
-	log_warnx("%s: reload config file %s", __func__, filename);
+	log_debug("%s: reload config file %s", __func__, filename);
 
-	things_reset();
+	/* things_reset(); */
 
 	/* Purge the existing configuration. */
 	config_purge(thingsd_env, reset);
 	config_setreset(thingsd_env, reset);
 
-	if (parse_config(thingsd_env->thingsd_conffile) == -1) {
-		log_warnx("%s: failed to reload config file %s",
-		    __func__, filename);
-	}
+	/* if (parse_config(thingsd_env->thingsd_conffile) == -1) { */
+	/* 	log_warnx("%s: failed to reload config file %s", */
+	/* 	    __func__, filename); */
+	/* } */
 
-	thingsd_configure_things(&thingsd_env->thingsd_ps);
+	/* thingsd_configure_things(thingsd_env->thingsd_ps); */
 }
 
 void
-thingsd_shutdown(void)
+thingsd_shutdown(struct thingsd *env)
 {
-	proc_kill(&thingsd_env->thingsd_ps);
-	things_shutdown();
+	proc_kill(env->thingsd_ps);
+	control_cleanup(&env->thingsd_ps->ps_csock);
 
-	log_warnx("parent terminating");
+	thingsd_parent_shutdown(env);
+
+	log_warnx("thingsd terminating");
 	exit(0);
 }
 
 void
 thingsd_show_info(struct privsep *ps, struct imsg *imsg)
 {
-	struct thingsd_parent_info	npi;
+	struct thingsd_thingsd_info	npi;
 
 	switch (imsg->hdr.type) {
-	case IMSG_GET_INFO_PARENT_REQUEST:
+	case IMSG_GET_INFO_THINGSD_REQUEST:
 		npi.verbose = log_getverbose();
 		if (proc_compose_imsg(ps, PROC_CONTROL, -1,
-		    IMSG_GET_INFO_PARENT_DATA, imsg->hdr.peerid,
+		    IMSG_GET_INFO_THINGSD_DATA, imsg->hdr.peerid,
 		    -1, &npi, sizeof(npi)) == -1)
 			return;
 		if (proc_compose_imsg(ps, PROC_CONTROL, -1,
-		    IMSG_GET_INFO_PARENT_END_DATA, imsg->hdr.peerid,
+		    IMSG_GET_INFO_THINGSD_END_DATA, imsg->hdr.peerid,
 		    -1, &npi, sizeof(npi)) == -1)
 			return;
 		break;
@@ -454,67 +517,593 @@ thingsd_show_info(struct privsep *ps, struct imsg *ims
 }
 
 void
-thingsd_configure_things(struct privsep *ps)
+thingsd_parent_shutdown(struct thingsd *env)
 {
-	struct thing	*thing, nti;
+	struct thing		*thing, *tthing;
+	struct socket		*sock, *tsock;
+	struct package		*package, *tpkg;
+	struct dead_thing	*dead_thing, *tdead_thing;
 
-	open_things(thingsd_env, false);
-	create_sockets(thingsd_env, false);
+	log_debug("thingsd parent shutting down");
 
-	start_client_chk(thingsd_env);
+	/* clean up dead things */
+	TAILQ_FOREACH_SAFE(dead_thing, env->dead_things, entry, tdead_thing) {
+		TAILQ_REMOVE(env->dead_things, dead_thing, entry);
+		free(dead_thing);
+	}
 
-	/* Send configured things to things. */
-	TAILQ_FOREACH(thing, thingsd_env->things, entry) {
-		nti.exists = thing->exists;
-		nti.hw_ctl = thing->hw_ctl;
-		nti.persist = thing->persist;
+	/*  clean up packages */
+	TAILQ_FOREACH_SAFE(package, env->packages, entry, tpkg) {
+		TAILQ_REMOVE(env->packages, package, entry);
+		free(package);
+	}
 
-		memcpy(&nti.iface, thing->iface, sizeof(nti.iface));
+	/* clean up sockets */
+	TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
+		if (sock->conf.tls) {
+			free(sock->conf.tls_cert);
+			free(sock->conf.tls_cert_file);
+			free(sock->conf.tls_key);
+			free(sock->conf.tls_key_file);
+			free(sock->conf.tls_ca);
+			free(sock->conf.tls_ca_file);
+			free(sock->conf.tls_crl);
+			free(sock->conf.tls_crl_file);
+			free(sock->conf.tls_ocsp_staple);
+			free(sock->conf.tls_ocsp_staple_file);
+		}
+		if (event_initialized(&sock->ev))
+			event_del(&sock->ev);
+		close(sock->fd);
+		TAILQ_REMOVE(env->sockets, sock, entry);
+		free(sock);
+	}
+	free(env->sockets);
 
-		memcpy(&nti.ipaddr, thing->ipaddr, sizeof(nti.ipaddr));
+	/* clean up things */
+	TAILQ_FOREACH_SAFE(thing, env->things, entry, tthing) {
+		if (thing->conf.tls) {
+			free(thing->conf.tls_cert);
+			free(thing->conf.tls_cert_file);
+			free(thing->conf.tls_key);
+			free(thing->conf.tls_key_file);
+			free(thing->conf.tls_ca);
+			free(thing->conf.tls_ca_file);
+			free(thing->conf.tls_crl);
+			free(thing->conf.tls_crl_file);
+			free(thing->conf.tls_ocsp_staple);
+			free(thing->conf.tls_ocsp_staple_file);
+		}
 
-		memcpy(&nti.parity, thing->parity, sizeof(nti.parity));
+		if (event_initialized(&thing->udp_ev))
+			event_del(&thing->udp_ev);
 
-		memcpy(&nti.name, thing->name, sizeof(nti.name));
+		if (thing->bev != NULL)
+			bufferevent_disable(thing->bev, EV_READ | EV_WRITE);
+		if (thing->bev != NULL)
+			bufferevent_free(thing->bev);
 
-		memcpy(&nti.password, thing->password,
-		    sizeof(nti.password));
+		if (thing->fd != -1)
+			close(thing->fd);
+		TAILQ_REMOVE(env->things, thing, entry);
+		free(thing);
+	}
 
-		memcpy(&nti.location, thing->location,
-		    sizeof(nti.location));
+	env->run = 0;
+	free(env->things);
+	free(env);
+}
 
-		memcpy(&nti.udp, thing->udp, sizeof(nti.udp));
+struct thing *
+thingsd_conf_new_thing(struct thingsd *env, struct thing *p_thing, char *name,
+    int id)
+{
+	struct thing	*thing;
 
-		nti.fd = thing->fd;
-		nti.baud = thing->baud;
-		nti.conn_port = thing->conn_port;
-		nti.rcv_port = thing->rcv_port;
-		nti.data_bits = thing->data_bits;
-		nti.max_clients = thing->max_clients;
-		nti.port = thing->port;
-		nti.stop_bits = thing->stop_bits;
-		nti.type = thing->type;
-		nti.client_cnt = thing->client_cnt;
+	/* check if thing already exists */
+	TAILQ_FOREACH(thing, env->things, entry) {
+		if (strcmp(name, thing->conf.name) == 0)
+			return (thing);
+	}
 
-		nti.tls = thing->tls;
+	if ((thing = calloc(1, sizeof(*thing))) == NULL)
+		fatal("%s: calloc", __func__);
 
-		memcpy(&nti.tls_cert_file, thing->tls_cert_file,
-		    sizeof(nti.tls_cert_file));
+	thing->conf.ipv4 = 1;
+	thing->conf.id = id;
 
-		memcpy(&nti.tls_key_file, thing->tls_key_file,
-		    sizeof(nti.tls_key_file));
+	TAILQ_INSERT_TAIL(env->things, thing, entry);
 
-		memcpy(&nti.tls_ca_file, thing->tls_ca_file,
-		    sizeof(nti.tls_ca_file));
+	return (thing);
+}
 
-		memcpy(&nti.tls_crl_file, thing->tls_crl_file,
-		    sizeof(nti.tls_crl_file));
+void
+thingsd_thing_serial_open(struct thing *thing, int reconn)
+{
+	struct dead_thing	*dead_thing;
+	struct termios		 s_opts;
+	int			 baudrate = 0, stop = 0;
 
-		memcpy(&nti.tls_ocsp_staple_file,
-		    thing->tls_ocsp_staple_file,
-		    sizeof(nti.tls_ocsp_staple_file));
+	if (thing->conf.type == S_DEV) {
 
-		proc_compose(ps, PROC_THINGS, IMSG_ADD_THING,
-		    &nti, sizeof(nti));
+		/*
+		 * Just a reminder to set the ownership of your serial
+		 * devices to _thingsd. Otherwise, a thing will not be
+		 * able to successfully open(2) the file descriptor.
+		 */
+		if ((thing->fd = open(thing->conf.location, O_RDWR |
+		    O_NONBLOCK | O_NOCTTY | O_NDELAY)) == -1) {
+			log_warnx("failed to open %s", thing->conf.location);
+
+			if (reconn)
+				return;
+
+			dead_thing = thingsd_new_dead_thing(thing);
+
+			thingsd_env->exists = 1;
+			thingsd_env->dcount++;
+
+			TAILQ_INSERT_TAIL(thingsd_env->dead_things,
+			    dead_thing, entry);
+
+			return;
+		} else {
+			/* load current s_opts */
+			tcgetattr(thing->fd, &s_opts);
+
+			/* set baud */
+			switch (thing->conf.baud) {
+			case 50:
+				baudrate = B50;
+				break;
+			case 75:
+				baudrate = B75;
+				break;
+			case 110:
+				baudrate = B110;
+				break;
+			case 134:
+				baudrate = B134;
+				break;
+			case 150:
+				baudrate = B150;
+				break;
+			case 200:
+				baudrate = B200;
+				break;
+			case 300:
+				baudrate = B300;
+				break;
+			case 600:
+				baudrate = B600;
+				break;
+			case 1200:
+				baudrate = B1200;
+				break;
+			case 1800:
+				baudrate = B1800;
+				break;
+			case 2400:
+				baudrate = B2400;
+				break;
+			case 4800:
+				baudrate = B4800;
+				break;
+			case 9600:
+				baudrate = B9600;
+				break;
+			case 19200:
+				baudrate = B19200;
+				break;
+			case 38400:
+				baudrate = B38400;
+				break;
+			case 57600:
+				baudrate = B57600;
+				break;
+			case 76800:
+				baudrate = B76800;
+				break;
+			case 115200:
+				baudrate = B115200;
+				break;
+			}
+
+			cfsetispeed(&s_opts, baudrate);
+			cfsetospeed(&s_opts, baudrate);
+
+			/* enable and set local */
+			s_opts.c_cflag |= (CLOCAL | CREAD);
+
+			/* set data bits */
+			if (thing->conf.data_bits != -1) {
+				s_opts.c_cflag &= ~CSIZE;
+				switch(thing->conf.data_bits) {
+				case 5:
+					stop = CS5;
+					break;
+				case 6:
+					stop = CS6;
+					break;
+				case 7:
+					stop = CS7;
+					break;
+				case 8:
+					stop = CS8;
+					break;
+				}
+				s_opts.c_cflag |= stop;
+			}
+
+			/* set parity */
+			if (strlen(thing->conf.parity) != 0) {
+				s_opts.c_cflag &= ~PARENB;
+
+				/* enable parity checking */
+				if (strcmp(thing->conf.parity, "odd") == 0) {
+					s_opts.c_cflag |= PARENB;
+					s_opts.c_cflag |= PARODD;
+					s_opts.c_iflag |= (INPCK |
+					    ISTRIP);
+				} else if (strcmp(thing->conf.parity,
+				    "even") == 0) {
+					s_opts.c_cflag |= PARENB;
+					s_opts.c_cflag &= ~PARODD;
+					s_opts.c_iflag |= (INPCK |
+					    ISTRIP);
+				}
+
+			}
+
+			/* set stop bits */
+			if (thing->conf.stop_bits != -1) {
+				if (thing->conf.stop_bits == 2)
+					s_opts.c_cflag |= CSTOPB;
+				else
+					s_opts.c_cflag &= ~CSTOPB;
+			}
+
+			/* set hardware control */
+			if (thing->conf.hw_ctl == 0) {
+				s_opts.c_cflag &= ~CRTSCTS;
+			} else {
+				s_opts.c_cflag |= CRTSCTS;
+			}
+
+			/* set software control */
+			if (thing->conf.sw_ctl == 0) {
+				s_opts.c_iflag &= ~(IXON | IXOFF |
+				    IXANY);
+			} else {
+				s_opts.c_iflag |= (IXON | IXOFF |
+				    IXANY);
+			}
+
+			/* set input/output as raw */
+			s_opts.c_lflag &= ~(ICANON | ECHO | ECHOE |
+			    ISIG);
+
+			s_opts.c_oflag &= ~OPOST;
+
+			/* Set the new options for the port */
+			tcsetattr(thing->fd, TCSANOW, &s_opts);
+
+			if (thing->fd == '\0') {
+				log_warnx("serial device not opened");
+				if (reconn)
+					return;
+				thingsd_add_reconn(thing);
+				return;
+			}
+			thing->exists = 1;
+		}
+
+		if (reconn && thing->exists)
+			log_info("reconnected: %s", thing->conf.name);
 	}
 }
+
+void
+thingsd_thing_setup(struct thingsd *env, struct thing *thing, int reconn)
+{
+	evbuffercb		 thingrd = thingsd_thing_read;
+	evbuffercb		 thingwr = thingsd_thing_write;
+
+	if (thing->conf.type == S_TCP) {
+		if (thing->conf.persist == 1) {
+			if ((thing->fd = sockets_open_client(thing->conf.ipaddr,
+			    &thing->conf.tcp_conn_port)) == -1) {
+				log_warnx("%s: ipaddr connection failed",
+				    __func__);
+				if (reconn)
+					return;
+				thingsd_add_reconn(thing);
+				return;
+			}
+		} else
+			/* 0 indicates non-persistent socket */
+			thing->fd = 0;
+	}
+
+	if (thing->conf.type == S_UDP) {
+		if ((thing->fd = sockets_create_socket(thing->conf.udp_al,
+		    thing->conf.udp_rcv_port, S_UDP)) == -1)
+			fatalx("%s: create udp socket failed", __func__);
+	}
+
+	log_debug("%s: configuring thing %s (%d)", __func__, thing->conf.name,
+	    thing->fd);
+
+	if (thing->conf.type == S_UDP) {
+		event_set(&thing->udp_ev, thing->fd, EV_READ |
+		    EV_PERSIST, thingsd_thing_udp_event, thing);
+
+		if (event_add(&thing->udp_ev, NULL))
+			fatalx("%s: udp ev error", __func__);
+	} else {
+		thing->bev = bufferevent_new(thing->fd,
+		    thingrd, thingwr, thingsd_thing_err, thing);
+
+		if (thing->bev == NULL)
+			fatalx("%s: ipaddr bev error", __func__);
+
+		thing->evb = evbuffer_new();
+
+		if (thing->evb == NULL)
+			fatalx("ipaddr evb error");
+
+		bufferevent_enable(thing->bev, EV_READ | EV_WRITE);
+	}
+
+	log_debug("%s: running thing %s", __func__, thing->conf.name);
+
+	thing->conf.env = env;
+	thing->exists = 1;
+
+	if (reconn)
+		log_info("reconnected: %s", thing->conf.name);
+}
+
+void
+thingsd_write_to_things(struct package *package)
+{
+	struct thing		*thing = NULL;
+
+	TAILQ_FOREACH(thing, thingsd_env->things, entry)
+		if (thing->conf.id == package->thing_id)
+			break;
+
+	if (thing == NULL)
+		return;
+
+	switch (thing->conf.type) {
+	case S_TCP:
+	case S_DEV:
+		if (thing->conf.persist == 0) {
+			if ((thing->fd = sockets_open_client(thing->conf.ipaddr,
+			    &thing->conf.tcp_conn_port)) == -1) {
+				log_warnx("%s: temporary ipaddr connection"
+				    " failed", __func__);
+				return;
+			}
+			write(thing->fd, package->pkt, package->len);
+			close(thing->fd);
+			thing->fd = -1;
+		} else {
+			if (thing->fd != -1) {
+				bufferevent_write(thing->bev, package->pkt,
+				    package->len);
+			}
+		}
+		break;
+	default:
+		write(thing->fd, package->pkt, package->len);
+		break;
+	}
+}
+
+void
+thingsd_write_to_socks(struct packages *packages)
+{
+	struct privsep		*ps = thingsd_env->thingsd_ps;
+	struct package		*package, *tpkg, p;
+	unsigned int		 id, what;
+	int			 fd = -1, n, m;
+	struct iovec		 iov[6];
+	size_t			 c;
+
+	TAILQ_FOREACH_SAFE(package, packages, entry, tpkg) {
+		for (id = 0; id < PROC_MAX; id++) {
+			what = ps->ps_what[id];
+
+			if ((what & CONFIG_SOCKS) == 0 || id == privsep_process)
+				continue;
+
+			memcpy(&p, package, sizeof(p));
+
+			c = 0;
+			iov[c].iov_base = &p;
+			iov[c++].iov_len = sizeof(p);
+			if (id == PROC_SOCKS) {
+			/* XXX imsg code will close the fd after 1st call */
+				n = -1;
+				proc_range(ps, id, &n, &m);
+				for (n = 0; n < m; n++) {
+					/* send thing fd */
+					if (proc_composev_imsg(ps, id, n,
+					    IMSG_DIST_THING_PACKAGE, -1, fd,
+					    iov, c) != 0) {
+						log_warn("%s: failed to compose"
+						    " IMSG_DIST_THING_PACKAGE"
+						    " imsg",
+					    __func__);
+					return;
+					}
+					if (proc_flush_imsg(ps, id, n) == -1) {
+						log_warn("%s: failed to flush "
+						    "IMSG_DIST_THING_PACKAGE "
+						    "imsg",
+						    __func__);
+						return;
+					}
+				}
+			}
+		}
+		TAILQ_REMOVE(packages, package, entry);
+		free(package);
+	}
+}
+
+void
+thingsd_thing_udp_event(int fd, short event, void *arg)
+{
+	struct thing		*thing = (struct thing *)arg;
+	struct package		*package = NULL;
+	struct sockaddr		*addr = NULL;
+	socklen_t		*addrlen = NULL;
+
+	if ((package = calloc(1, sizeof(*package))) == NULL)
+		fatalx("%s: calloc", __func__);
+
+	package->len = recvfrom(fd, package->pkt, sizeof(package->pkt),
+	    0, addr, addrlen);
+	package->thing_id = thing->conf.id;
+
+	if (package->len == -1) {
+		free(package);
+		return;
+	}
+
+	TAILQ_INSERT_TAIL(thingsd_env->packages, package, entry);
+	thingsd_write_to_socks(thingsd_env->packages);
+}
+
+void
+thingsd_thing_read(struct bufferevent *bev, void *arg)
+{
+	struct thing		*thing = (struct thing *)arg;
+	struct package		*package = NULL;
+
+
+	if ((package = calloc(1, sizeof(*package))) == NULL)
+		fatalx("%s: calloc", __func__);
+
+	thing->evb = EVBUFFER_INPUT(bev);
+	package->len = EVBUFFER_LENGTH(thing->evb);
+
+	evbuffer_remove(thing->evb, package->pkt, package->len);
+	package->thing_id = thing->conf.id;
+
+	TAILQ_INSERT_TAIL(thingsd_env->packages, package, entry);
+	thingsd_write_to_socks(thingsd_env->packages);
+}
+
+void
+thingsd_thing_write(struct bufferevent *bev, void *arg)
+{
+}
+
+void
+thingsd_thing_err(struct bufferevent *bev, short error, void *arg)
+{
+	struct thing	*thing = (struct thing *)arg;
+
+	if ((error & EVBUFFER_ERROR) == 0 || error & EVBUFFER_TIMEOUT) {
+		if (thing->fd != -1) {
+			if (thing->bev != NULL)
+				bufferevent_disable(thing->bev,
+				    EV_READ | EV_WRITE);
+			if (thing->bev != NULL)
+				bufferevent_free(thing->bev);
+
+			thing->evb = NULL;
+			thing->bev = NULL;
+
+			close(thing->fd);
+			thing->fd = -1;
+
+			thingsd_add_reconn(thing);
+
+			log_warnx("thing error: %s disconnected",
+			    thing->conf.name);
+		}
+	}
+}
+
+void
+thingsd_add_reconn(struct thing *thing)
+{
+	struct dead_thing	*dead_thing;
+
+	dead_thing = thingsd_new_dead_thing(thing);
+
+	thingsd_env->exists = 1;
+	thingsd_env->dcount++;
+
+	TAILQ_INSERT_TAIL(thingsd_env->dead_things, dead_thing, entry);
+
+}
+
+struct dead_thing
+*thingsd_new_dead_thing(struct thing *thing)
+{
+	struct dead_thing	*dead_thing;
+
+	dead_thing = calloc(1, sizeof(*dead_thing));
+	if (dead_thing == NULL)
+		fatal("%s: calloc", __func__);
+
+	log_debug("%s: adding detached thing, %s", __func__, thing->conf.name);
+
+	if (strlcpy(dead_thing->name, thing->conf.name,
+	    sizeof(dead_thing->name)) >= sizeof(dead_thing->name))
+		fatalx("%s: strlcpy", __func__);
+
+	dead_thing->type = thing->conf.type;
+	dead_thing->dtime = time(NULL);
+
+	thing->exists = 0;
+
+	return dead_thing;
+}
+
+void
+thingsd_do_reconn(struct thingsd *env)
+{
+	struct dead_thing	*dead_thing = NULL;
+	struct thing		*thing = NULL;
+	int			 found = 0;
+
+	TAILQ_FOREACH(thing, env->things, entry) {
+		TAILQ_FOREACH(dead_thing, env->dead_things, entry)
+			if (strcmp(thing->conf.name, dead_thing->name) == 0) {
+				found = 1;
+				break;
+			}
+		if (found)
+			break;
+	}
+
+	if (thing == NULL || dead_thing == NULL)
+		return;
+
+	if ((size_t)(time(NULL) - dead_thing->dtime) > env->conn_retry) {
+		dead_thing->dtime = time(NULL);
+
+		log_debug("attempting to reconnect %s", dead_thing->name);
+
+		if (dead_thing->type == S_DEV)
+			thingsd_thing_serial_open(thing, 1);
+
+		thingsd_thing_setup(env, thing, 1);
+	}
+
+	if (thing->exists) {
+		TAILQ_REMOVE(env->dead_things, dead_thing, entry);
+		env->dcount--;
+		free(dead_thing);
+	}
+
+	if (env->dcount == 0)
+		env->exists = 0;
+}
blob - fc0cc60f0d8d2ea8c4015f86c780a25d52bf76d9
blob + e4722aa14179afb58334fa47f72516170908b851
--- thingsd/thingsd.conf.5	
+++ thingsd/thingsd.conf.5	
@@ -50,23 +50,23 @@ must be quoted.
 .Sh GLOBAL CONFIGURATION
 Here are the settings that can be set globally:
 .Bl -tag -width Ds
-.It Ic bind interface Ar string
-Specify a single interface to create sockets on.
+.It Ic bind tcp interface Ar string
+Specify a single interface to create client listening sockets on.
 If this is not set,
 .Xr thingsd 8
-will attempt to open the default port on all network interfaces.
+will attempt to open thing assigned
+.Ar listen
+ports on all network interfaces.
+.It Ic bind udp interface Ar string
+Specify a single interface to create UDP receive sockets on.
+If this is not set, it is required to set the interface per UDP thing.
 .It Ic connection retry Ar number
 Set the lost connection thing watchdog interval.
 Default is 30 seconds.
 Range is 30-600 seconds.
-.It Ic default port Ar number
-Set the default port for things to communicate on.
-This is a required setting.
 .It Ic max clients Ar number
 Set the default maximum number of clients allowed to connect to each
 .Xr thingsd 8 thing.
-.It Ic max subscriptions Ar number
-Set the maximum number of things a client can subscribe to at a time.
 .It Ic prefork Ar number
 Run the specified number of things processes.
 This increases the performance and prevents delays when connecting to a thing.
@@ -84,12 +84,16 @@ thing.
 .It Ic baud Ar number
 Set the baud rate for a serial thing
 .Ar location .
-.It Ic bind interface Ar string
+.It Ic bind tcp interface Ar string
 Specify a single interface to create a
 .Ar location
 or
 .Ar ipaddr
 thing socket.
+.It Ic bind udp interface Ar string
+Specify a single interface to create a
+.Ar udp
+receive socket for a thing.
 .It Ic connect on port Ar number
 Set the TCP/IP port to connect to for an
 .Ar ipaddr
@@ -113,9 +117,9 @@ Set a local socket port for a
 .Ar udp ,
 or
 .Ar ipaddr
-thing, other than the global default port.
-If TLS is used, the chosen port must be unique from the default port and all
-other things.
+thing.
+This setting is required and must be unique from other thing assigned listen
+ports.
 .It Ic location Ar string Brq ...
 Create a
 .Ar thing
@@ -157,7 +161,7 @@ Set the
 port to listen on for a UDP packet.
 This setting is required for
 .Ar udp
-things.
+things and must be unique from other thing assigned UPD ports.
 .It Ic software Ar number
 Set to 1 to turn on software control for a serial thing
 .Ar location .
@@ -268,8 +272,6 @@ which listens for user datagram protocol packets.
 .El
 .Sh EXAMPLES
 This configuration file would create a serial thing and a TCP/IP host thing.
-The serial thing would communicate on the default port, while the local network
-ipaddr thing would communicate on a separate port.
 .Bd -literal -offset indent
 
 #
@@ -278,8 +280,6 @@ ipaddr thing would communicate on a separate port.
 
 prefork 5
 
-default port 50000
-
 max clients 10
 
 max suscriptions 5
@@ -301,6 +301,7 @@ thing "serial_thing" {
 thing "ipaddr_thing" {
 	ipaddr "10.0.0.1" {
 		connect on port 8080
+		listen on port 50000
 		password "password"
 	}
 }
blob - 34c01e279ea7157a00dea4f3a4a9eadd3b47efe7
blob + f712cdc0213322235d1d25ab9b17c5f95546be99
--- thingsd/thingsd.h	
+++ thingsd/thingsd.h	
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2019, 2020 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2016, 2019-2021 Tracey Emery <tracey@traceyemery.net>
  * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -15,8 +15,11 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <net/if.h>
+#include <netinet/in.h>
+#include <sys/tree.h>
+
 #include <limits.h>
-#include <pthread.h>
 
 #ifdef THINGSD_DEBUG
 #define dprintf(x...)   do { log_debug(x); } while(0)
@@ -32,9 +35,10 @@
 #define MIN_RETRY		 10
 #define MAX_RETRY		 600
 #define DEFAULT_BAUD		 9600
-#define CLIENT_SUB_TIME		 5
+#define CLIENT_SUB_TIME_SEC	 5
+#define CLIENT_SUB_TIME_USEC	 100
 #define CLIENT_SUB_CHK		 1
-#define EB_TIMEOUT		 10
+#define EB_TIMEOUT		 1
 
 #define TLS_CONFIG_MAX		 511
 #define TLS_CERT		 "/etc/ssl/thing.crt"
@@ -49,15 +53,17 @@
 
 #define THINGSD_MAXTEXT		 511
 #define THINGSD_MAXNAME		 16
+#define THINGSD_MAXPORT		 6
+#define THINGSD_MAXIFACE	 16
 
-#define THINGS_NUMPROC		 3
+#define SOCKS_NUMPROC		 3
 
 #define PKT_BUFF		 2048
 
 enum imsg_type {
-	IMSG_GET_INFO_PARENT_REQUEST = IMSG_PROC_MAX,
-	IMSG_GET_INFO_PARENT_DATA,
-	IMSG_GET_INFO_PARENT_END_DATA,
+	IMSG_GET_INFO_THINGSD_REQUEST = IMSG_PROC_MAX,
+	IMSG_GET_INFO_THINGSD_DATA,
+	IMSG_GET_INFO_THINGSD_END_DATA,
 
 	IMSG_GET_INFO_CONTROL_REQUEST,
 	IMSG_GET_INFO_CONTROL_DATA,
@@ -81,21 +87,32 @@ enum imsg_type {
 	IMSG_SHOW_PACKETS_END_DATA,
 	IMSG_BAD_THING,
 
-	IMSG_ADD_THING,
 	IMSG_KILL_CLIENT,
+
+	IMSG_CTL_START,
+	IMSG_CFG_DONE,
+
+	IMSG_CFG_SOCKS,
+	IMSG_DIST_THING_PACKAGE,
+	IMSG_DIST_CLIENT_PACKAGE,
 };
 
+/* 
+ * parse.y contains TCP and UDP
+ * so, these have to be different
+ * to use "socketypes" in parse.y code
+ */
 enum socktypes {
-	TCP,
-	UDP,
-	DEV
+	S_TCP,
+	S_UDP,
+	S_DEV
 };
 
 struct thingsd_control_info {
 	int		 verbose;
 };
 
-struct thingsd_parent_info {
+struct thingsd_thingsd_info {
 	int		 verbose;
 	char		 text[THINGSD_MAXTEXT];
 };
@@ -106,94 +123,184 @@ struct subscription {
 };
 TAILQ_HEAD(subscriptionlist, subscription);
 
+struct portrange {
+	in_port_t		 val[2];
+	uint8_t			 op;
+};
+
+struct address {
+	TAILQ_ENTRY(address)	 entry;
+	struct sockaddr_storage	 ss;
+	int			 ipproto;
+	int			 prefixlen;
+	struct portrange	 port;
+	char			 ifname[IFNAMSIZ];
+};
+TAILQ_HEAD(addresslist, address);
+
+struct package {
+	TAILQ_ENTRY(package)	 entry;
+	char			 pkt[PKT_BUFF];
+	int			 len;
+	int			 thing_id;
+};
+TAILQ_HEAD(packages, package);
+
 struct client {
 	TAILQ_ENTRY(client)	 entry;
-	struct subscriptionlist	*subscriptions;
+	struct packages		*packages;
+	int			 id;
+	int			 thing_id;
+	int			 sock_id;
+	void			*sock;
+	void			*sock_conf;
+
+	struct clt_time		*clt;
 	struct evbuffer		*evb;
-	struct event		*ev;
 	struct bufferevent	*bev;
-	struct socket		*socket;
-	bool			 subscribed;
+	struct event		 ev;
+	struct event		 timeout;
+
+	int			 subscribed;
 	char			 name[THINGSD_MAXNAME];
+
 	int			 fd;
-	int			 port;
+	struct portrange	 port;
 	time_t			 join_time;
-	size_t			 le;
 	size_t			 subs;
 
-	bool			 tls;
+	int			 tls;
 	struct tls		*tls_ctx;
 };
 TAILQ_HEAD(clientlist, client);
 
+struct socket_config {
+	struct addresslist	*al;
+	char			 iface[THINGSD_MAXTEXT];
+	char			 thing_name[THINGSD_MAXTEXT];
+	int			 id;
+	int			 child_id;
+	int			 parent_id;
+	int			 thing_id;
+
+	struct sockaddr_storage	 ss;
+	int			 ipv4;
+	int			 ipv6;
+
+	struct portrange	 port;
+	size_t			 max_clients;
+	char			 password[THINGSD_MAXTEXT];
+
+	int			 tls;
+	uint8_t			 tls_flags;
+
+	uint8_t			*tls_cert;
+	char			*tls_cert_file;
+	size_t			 tls_cert_len;
+
+	uint8_t			*tls_key;
+	char			*tls_key_file;
+	size_t			 tls_key_len;
+
+	uint8_t			*tls_ca;
+	char			*tls_ca_file;
+	size_t			 tls_ca_len;
+
+	uint8_t			*tls_crl;
+	char			*tls_crl_file;
+	size_t			 tls_crl_len;
+
+	uint8_t			*tls_ocsp_staple;
+	char			*tls_ocsp_staple_file;
+	size_t			 tls_ocsp_staple_len;
+
+	char			 tls_ciphers[TLS_CONFIG_MAX];
+	char			 tls_dhe_params[TLS_CONFIG_MAX];
+	char			 tls_ecdhe_curves[TLS_CONFIG_MAX];
+
+	uint32_t		 tls_protocols;
+};
+
 struct socket {
 	TAILQ_ENTRY(socket)	 entry;
-	struct event		*ev;
-	struct event		 pause;
-	char			 name[THINGSD_MAXNAME];
+	struct clientlist	*clients;
+	struct socket_config	 conf;
+
 	int			 fd;
-	int			 port;
+
 	size_t			 client_cnt;
-	size_t			 max_clients;
 
-	bool			 tls;
 	struct tls_config	*tls_config;
 	struct tls		*tls_ctx;
+
+	/* event for client fd */
+	struct event		 ev;
+	struct event		 pause;
 };
 TAILQ_HEAD(socketlist, socket);
 
-struct thing {
-	TAILQ_ENTRY(thing)	 entry;
-	struct bufferevent	*bev;
-	struct evbuffer		*evb;
+struct thing_config {
+	struct thingsd		*env;
+	struct addresslist	*tcp_al;
+	struct addresslist	*udp_al;
 
-	bool			 exists;
-	bool			 hw_ctl;
-	bool			 persist;
-	bool			 sw_ctl;
+	int			 ipv4;
+	int			 ipv6;
 
-	char			 iface[THINGSD_MAXTEXT];
+	char			 name[THINGSD_MAXNAME];
+
+	int			 id;
+	int			 sock_id;
+	int			 child_id;
+	int			 parent_id;
+
+	int			 hw_ctl;
+	int			 persist;
+	int			 sw_ctl;
+
+	char			 tcp_iface[THINGSD_MAXTEXT];
 	char			 ipaddr[THINGSD_MAXTEXT];
-	char			 name[THINGSD_MAXNAME];
 	char			 parity[THINGSD_MAXTEXT];
 	char			 password[THINGSD_MAXTEXT];
 	char			 location[THINGSD_MAXTEXT];
+
 	char			 udp[THINGSD_MAXTEXT];
+	char			 udp_iface[THINGSD_MAXTEXT];
 
-	int			 fd;
+	struct portrange	 tcp_listen_port;
+	struct portrange	 tcp_conn_port;
+	struct portrange	 udp_rcv_port;
+
 	int			 baud;
-	int			 conn_port;
-	int			 rcv_port;
 	int			 data_bits;
-	int			 max_clients;
-	int			 port;
+	size_t			 max_clients;
 	int			 stop_bits;
 	int			 type;
 
 	size_t			 client_cnt;
 
-	bool			 tls;
+	int			 tls;
 	uint8_t			 tls_flags;
 
 	uint8_t			*tls_cert;
+	char			*tls_cert_file;
 	size_t			 tls_cert_len;
-	char			 tls_cert_file[THINGSD_MAXTEXT];
 
 	uint8_t			*tls_key;
+	char			*tls_key_file;
 	size_t			 tls_key_len;
-	char			 tls_key_file[THINGSD_MAXTEXT];
 
 	uint8_t			*tls_ca;
+	char			*tls_ca_file;
 	size_t			 tls_ca_len;
-	char			 tls_ca_file[THINGSD_MAXTEXT];
 
 	uint8_t			*tls_crl;
+	char			*tls_crl_file;
 	size_t			 tls_crl_len;
-	char			 tls_crl_file[THINGSD_MAXTEXT];
 
 	uint8_t			*tls_ocsp_staple;
+	char			*tls_ocsp_staple_file;
 	size_t			 tls_ocsp_staple_len;
-	char			 tls_ocsp_staple_file[THINGSD_MAXTEXT];
 
 	char			 tls_ciphers[TLS_CONFIG_MAX];
 	char			 tls_dhe_params[TLS_CONFIG_MAX];
@@ -201,6 +308,24 @@ struct thing {
 
 	uint32_t		 tls_protocols;
 };
+
+struct thing {
+	TAILQ_ENTRY(thing)	 entry;
+	struct thing_config	 conf;
+	int			 exists;
+
+	size_t			 client_cnt;
+
+	/* either DEV, TCP conn, or UDP rcv fd */
+	int			 fd;
+
+	/* event for TCP thing fd */
+	struct bufferevent	*bev;
+	struct evbuffer		*evb;
+
+	/* event for UDP thing fd */
+	struct event		 udp_ev;
+};
 TAILQ_HEAD(thinglist, thing);
 
 struct dead_thing {
@@ -211,121 +336,129 @@ struct dead_thing {
 };
 TAILQ_HEAD(deadthinglist, dead_thing);
 
-struct dead_things {
-	struct deadthinglist	*dead_things_list;
-	volatile sig_atomic_t	 run;
-};
 
-struct packet_client {
-	TAILQ_ENTRY(packet_client)	 entry;
-	struct privsep		 ps;
-	struct imsg		 imsg;
-	int			 fd;
-	char			 name[THINGSD_MAXNAME];
-};
-TAILQ_HEAD(packetclientlist, packet_client);
+/* struct packet_client { */
+/* 	TAILQ_ENTRY(packet_client)	 entry; */
+/* 	struct privsep		 ps; */
+/* 	struct imsg		 imsg; */
+/* 	int			 fd; */
+/* 	char			 name[THINGSD_MAXNAME]; */
+/* }; */
+/* TAILQ_HEAD(packetclientlist, packet_client); */
 
 struct thingsd {
 	struct thinglist	*things;
-	struct clientlist	*clients;
 	struct socketlist	*sockets;
+	struct packages		*packages;
 
-	struct privsep		 thingsd_ps;
+	struct privsep		*thingsd_ps;
 	const char		*thingsd_conffile;
 
 	int			 thingsd_debug;
 	int			 thingsd_verbose;
 	int			 thingsd_noaction;
 
-	char			 iface[THINGSD_MAXTEXT];
-	int			 max_clients;
-	int			 port;
+	char			 tcp_iface[THINGSD_MAXTEXT];
+	char			 udp_iface[THINGSD_MAXTEXT];
+
+	size_t			 max_clients;
 	size_t			 client_cnt;
 	size_t			 conn_retry;
-	size_t			 max_subs;
-	uint16_t		 prefork_things;
-	void			 (*client_fptr)(struct thingsd *);
+	uint16_t		 prefork_socks;
+	int			 run;
 
 	/* disconnected things parts */
-	struct dead_things	*dead_things;
-	bool			 exists;
+	struct deadthinglist	*dead_things;
+	int			 exists;
 	int			 dcount;
 
 	/* things */
-	struct event_base	*things_eb;
+	struct event_base	*thingsd_eb;
 
+	/* socks */
+	int			 socks_reload;
+
 	/* control packets */
-	struct packetclientlist	*packet_clients;
+	/* struct packetclientlist	*packet_clients; */
 	int			 packet_client_count;
 };
 
 extern struct thingsd	*thingsd_env;
+extern int		 thing_id;
 
-/* events.c */
-void	 udp_event(int, short, void *);
-
 /* client.c */
-void	 client_conn(int, short, void *);
-void	 client_add(struct thingsd *, struct client *);
-void	 client_del(struct thingsd *, struct client *);
+void	 client_del(struct client *);
 void	 client_rd(struct bufferevent *, void *);
 void	 client_wr(struct bufferevent *, void *);
 void	 client_err(struct bufferevent *, short, void *);
-void	 start_client_chk(struct thingsd *);
-void	*client_chk(void *);
-void	 client_do_chk(struct thingsd *);
-void	 client_wr_things(struct client *, struct thing *, size_t);
 void	 client_tls_readcb(int, short, void *);
 void	 client_tls_writecb(int, short, void *);
-void	 clients_show_info(struct privsep *, struct imsg *);
-void	 client_accept_paused(int, short, void *);
+/* void	 clients_show_info(struct privsep *, struct imsg *); */
 
-/* serial.c */
-void	 open_things(struct thingsd *, bool);
-
 /* sockets.c */
-struct socket	*new_socket(int);
-struct socket	*get_socket(struct thingsd *, int);
-char	*get_ifaddrs(char *);
-void	 create_sockets(struct thingsd *, bool);
-void	 socket_rd(struct bufferevent *, void *);
-void	 socket_wr(struct bufferevent *, void *);
-void	 socket_err(struct bufferevent *, short, void *);
-int	 create_socket(int, char *, int);
-int	 open_client_socket(char *, int);
+struct socket
+	*sockets_new_socket(char *);
+struct socket
+	*sockets_get_socket(struct thingsd *, int);
+struct socket
+	*sockets_get_socket_byid(struct thingsd *, int);
+int	 sockets_create_socket(struct addresslist *, struct portrange, int);
+int	 sockets_open_client(char *, struct portrange *);
+int	 sockets_privinit(struct socket *);
+int	 sockets_client_cmp(struct client *, struct client *);
+void	 sockets_parse_sockets(struct thingsd *);
 void	 sockets_show_info(struct privsep *, struct imsg *);
+void	 sockets_socket_rlimit(int);
+void	 sockets_sighdlr(int, short, void *);
+void	 sockets_shutdown(void);
+void	 sockets(struct privsep *, struct privsep_proc *);
 
 /* thingsd.c */
-void	 thingsd_reload(int);
-
+struct thing	*thingsd_conf_new_thing(struct thingsd *, struct thing *,
+		    char *, int);
 /* things.c */
-struct	 dead_thing *new_dead_thing(struct thing *);
-void	 things(struct privsep *, struct privsep_proc *);
-void	 things_reset(void);
-void	 things_shutdown(void);
-void	 add_reconn(struct thing *);
-void	 do_reconn(void);
-void	 things_sighdlr(int, short, void *);
-void	 things_show_info(struct privsep *, struct imsg *);
-void	 things_echo_pkt(struct privsep *, struct imsg *);
-void	 things_stop_pkt(struct privsep *, struct imsg *);
-void	 send_to_packet_client(struct thingsd *, char *, char *, int);
+/* void	 thing_rd(struct bufferevent *, void *); */
+/* void	 thing_wr(struct bufferevent *, void *); */
+/* void	 thing_err(struct bufferevent *, short, void *); */
+/* void	 things(struct privsep *, struct privsep_proc *); */
+/* void	 things_reset(void); */
+/* void	 things_shutdown(void); */
+/* int	 thing_privinit(struct thing *); */
+/* int	 thing_sock_privinit(struct socket *); */
 
-/* control.c */
+/* void	 things_show_info(struct privsep *, struct imsg *); */
+/* void	 things_echo_pkt(struct privsep *, struct imsg *); */
+/* void	 things_stop_pkt(struct privsep *, struct imsg *); */
+/* void	 send_to_packet_client(struct thingsd *, char *, char *, int); */
+
+/* config.c */
 int	 config_init(struct thingsd *);
 void	 config_purge(struct thingsd *, unsigned int);
 int	 config_setreset(struct thingsd *, unsigned int);
 int	 config_getreset(struct thingsd *, struct imsg *);
+int	 config_setsocks(struct thingsd *, struct socket *);
+int	 config_getsocks(struct thingsd *, struct imsg *);
+int	 config_getcfg(struct thingsd *, struct imsg *);
 
 /* parse.y */
-int	 parse_config(const char *);
+int	 parse_config(const char *, struct thingsd *);
 int	 parse_buf(struct client *, u_char *, int);
 int	 cmdline_symset(char *);
+struct address	*host_v4(const char *);
+struct address	*host_v6(const char *);
+int		 host_dns(const char *, struct addresslist *,
+		    int, struct portrange *, const char *, int);
+int		 host_if(const char *, struct addresslist *,
+		    int, struct portrange *, const char *, int);
+int		 host(const char *, struct addresslist *,
+		    int, struct portrange *, const char *, int);
+int		 is_if_in_group(const char *, const char *);
 
 /* tls.c */
-int	 tls_load_keypair(struct thing *);
-int	 tls_load_ca(struct thing *);
-int	 tls_load_crl(struct thing *);
-int	 tls_load_ocsp(struct thing *);
-int	 socket_tls_init(struct socket *, struct thing *);
+int	 tls_load_keypair(struct socket *);
+int	 tls_load_ca(struct socket *);
+int	 tls_load_crl(struct socket *);
+int	 tls_load_ocsp(struct socket *);
+void	 socket_tls_load(struct socket *);
+int	 socket_tls_init(struct socket *);
 void	 socket_tls_handshake(int, short, void *);
blob - 7143a1b701146e1575f922cd0b7eda80e7640bce
blob + 0260b0e56c5d835ba95fb7a3de1805d693e00a11
--- thingsd/tls.c	
+++ thingsd/tls.c	
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2019 - 2021 Tracey Emery <tracey@traceyemery.net>
  * Copyright (c) 2006 - 2015 Reyk Floeter <reyk@openbsd.org>
  * 	(snippets from httpd functions)
  *
@@ -32,263 +32,285 @@
 #include <event.h>
 #include <imsg.h>
 #include <event.h>
-#include <stdbool.h>
 #include <tls.h>
 
 #include "proc.h"
 #include "thingsd.h"
 
 int
-tls_load_keypair(struct thing *thing)
+tls_load_keypair(struct socket *sock)
 {
-	if (thing->tls == false)
+	if (sock->conf.tls == 0)
 		return (0);
 
-	thing->tls_cert = tls_load_file(thing->tls_cert_file,
-	    &thing->tls_cert_len, NULL);
-	if (thing->tls_cert == NULL)
+	sock->conf.tls_cert = tls_load_file(sock->conf.tls_cert_file,
+	    &sock->conf.tls_cert_len, NULL);
+	if (sock->conf.tls_cert == NULL)
 		return (-1);
 
-	log_debug("%s: using certificate %s", __func__, thing->tls_cert_file);
+	log_debug("%s: using certificate %s", __func__,
+	    sock->conf.tls_cert_file);
 
 	/* XXX allow to specify password for encrypted key */
-	thing->tls_key = tls_load_file(thing->tls_key_file,
-	    &thing->tls_key_len, NULL);
-	if (thing->tls_key == NULL)
+	sock->conf.tls_key = tls_load_file(sock->conf.tls_key_file,
+	    &sock->conf.tls_key_len, NULL);
+	if (sock->conf.tls_key == NULL)
 		return (-1);
 
-	log_debug("%s: using private key %s", __func__, thing->tls_key_file);
+	log_debug("%s: using private key %s", __func__,
+	    sock->conf.tls_key_file);
 
 	return (0);
 }
 
 int
-tls_load_ca(struct thing *thing)
+tls_load_ca(struct socket *sock)
 {
-	if ((thing->tls_flags & TLSFLAG_CA) == 0 ||
-	    strlen(thing->tls_ca_file) == 0)
+	if ((sock->conf.tls_flags & TLSFLAG_CA) == 0 ||
+	    sock->conf.tls_ca_file == NULL)
 		return (0);
 
-	thing->tls_ca = tls_load_file(thing->tls_ca_file,
-	    &thing->tls_ca_len, NULL);
-	if (thing->tls_ca == NULL)
+	sock->conf.tls_ca = tls_load_file(sock->conf.tls_ca_file,
+	    &sock->conf.tls_ca_len, NULL);
+	if (sock->conf.tls_ca == NULL)
 		return (-1);
 
-	log_debug("%s: using ca cert(s) from %s", __func__, thing->tls_ca_file);
+	log_debug("%s: using ca cert(s) from %s", __func__,
+	    sock->conf.tls_ca_file);
 
 	return (0);
 }
 
 int
-tls_load_ocsp(struct thing *thing)
+tls_load_ocsp(struct socket *sock)
 {
-	if (thing->tls == false)
+	if (sock->conf.tls == 0)
 		return (0);
 
-	if (strlen(thing->tls_ocsp_staple_file) == 0)
+	if (sock->conf.tls_ocsp_staple_file == NULL)
 		return (0);
 
-	thing->tls_ocsp_staple = tls_load_file(
-	    thing->tls_ocsp_staple_file,
-	    &thing->tls_ocsp_staple_len, NULL);
-	if (thing->tls_ocsp_staple == NULL) {
+	sock->conf.tls_ocsp_staple = tls_load_file(
+	    sock->conf.tls_ocsp_staple_file,
+	    &sock->conf.tls_ocsp_staple_len, NULL);
+	if (sock->conf.tls_ocsp_staple == NULL) {
 		log_warnx("%s: Failed to load ocsp staple from %s", __func__,
-		    thing->tls_ocsp_staple_file);
+		    sock->conf.tls_ocsp_staple_file);
 		return (-1);
 	}
 
-	if (thing->tls_ocsp_staple_len == 0) {
+	if (sock->conf.tls_ocsp_staple_len == 0) {
 		log_warnx("%s: ignoring 0 length ocsp staple from %s", __func__,
-		    thing->tls_ocsp_staple_file);
+		    sock->conf.tls_ocsp_staple_file);
 		return (0);
 	}
 
 	log_debug("%s: using ocsp staple from %s", __func__,
-	    thing->tls_ocsp_staple_file);
+	    sock->conf.tls_ocsp_staple_file);
 
 	return (0);
 }
 
 int
-tls_load_crl(struct thing *thing)
+tls_load_crl(struct socket *sock)
 {
-	if ((thing->tls_flags & TLSFLAG_CA) == 0 ||
-	    strlen(thing->tls_crl_file) == 0)
+	if ((sock->conf.tls_flags & TLSFLAG_CA) == 0 ||
+	    sock->conf.tls_crl_file == NULL)
 		return (0);
 
-	thing->tls_crl = tls_load_file(thing->tls_crl_file,
-	    &thing->tls_crl_len, NULL);
-	if (thing->tls_crl == NULL)
+	sock->conf.tls_crl = tls_load_file(sock->conf.tls_crl_file,
+	    &sock->conf.tls_crl_len, NULL);
+	if (sock->conf.tls_crl == NULL)
 		return (-1);
 
-	log_debug("%s: using crl(s) from %s", __func__, thing->tls_crl_file);
+	log_debug("%s: using crl(s) from %s", __func__,
+	    sock->conf.tls_crl_file);
 
 	return (0);
 }
 
 int
-socket_tls_init(struct socket *socket, struct thing *thing)
+socket_tls_init(struct socket *sock)
 {
-	if (thing->tls == false)
+	if (sock->conf.tls == 0)
 		return 0;
 
-	if (thing->tls_cert == NULL)
+	if (sock->conf.tls_cert == NULL)
 		/*
 		 * hard fail if cert is not there yet
 		 * we don't check again
 		 */
 		return (-1);
 
-	log_debug("%s: setting up tls for %s", __func__, thing->name);
+	log_debug("%s: initializing tls for %d", __func__, sock->conf.id);
 
 	if (tls_init() != 0) {
 		log_warnx("%s: failed to initialise tls", __func__);
 		return (-1);
 	}
 
-	socket->tls_config = tls_config_new();
-	if (socket->tls_config == NULL) {
+	sock->tls_config = tls_config_new();
+	if (sock->tls_config == NULL) {
 		log_warnx("%s: failed to get tls config", __func__);
 		return (-1);
 	}
 
-	socket->tls_ctx = tls_server();
-	if (socket->tls_ctx == NULL) {
+	sock->tls_ctx = tls_server();
+	if (sock->tls_ctx == NULL) {
 		log_warnx("%s: failed to get tls server", __func__);
 		return (-1);
 	}
 
-	if (tls_config_set_protocols(socket->tls_config,
-	    thing->tls_protocols) != 0) {
+	if (tls_config_set_protocols(sock->tls_config,
+	    sock->conf.tls_protocols) != 0) {
 		log_warnx("%s: failed to set tls protocols: %s", __func__,
-		    tls_config_error(socket->tls_config));
+		    tls_config_error(sock->tls_config));
 		return (-1);
 	}
 
-	if (tls_config_set_ciphers(socket->tls_config,
-	    thing->tls_ciphers) != 0) {
+	if (tls_config_set_ciphers(sock->tls_config,
+	    sock->conf.tls_ciphers) != 0) {
 		log_warnx("%s: failed to set tls ciphers: %s", __func__,
-		    tls_config_error(socket->tls_config));
+		    tls_config_error(sock->tls_config));
 		return (-1);
 	}
 
-	if (tls_config_set_dheparams(socket->tls_config,
-	    thing->tls_dhe_params) != 0) {
+	if (tls_config_set_dheparams(sock->tls_config,
+	    sock->conf.tls_dhe_params) != 0) {
 		log_warnx("%s: failed to set tls dhe params: %s", __func__,
-		    tls_config_error(socket->tls_config));
+		    tls_config_error(sock->tls_config));
 		return (-1);
 	}
 
-	if (tls_config_set_ecdhecurves(socket->tls_config,
-	    thing->tls_ecdhe_curves) != 0) {
+	if (tls_config_set_ecdhecurves(sock->tls_config,
+	    sock->conf.tls_ecdhe_curves) != 0) {
 		log_warnx("%s: failed to set tls ecdhe curves: %s", __func__,
-		    tls_config_error(socket->tls_config));
+		    tls_config_error(sock->tls_config));
 		return (-1);
 	}
 
-	if (tls_config_set_keypair_ocsp_mem(socket->tls_config, thing->tls_cert,
-	    thing->tls_cert_len, thing->tls_key, thing->tls_key_len,
-	    thing->tls_ocsp_staple, thing->tls_ocsp_staple_len) != 0) {
+	if (tls_config_set_keypair_ocsp_mem(sock->tls_config,
+	    sock->conf.tls_cert, sock->conf.tls_cert_len,
+	    sock->conf.tls_key, sock->conf.tls_key_len,
+	    sock->conf.tls_ocsp_staple,
+	    sock->conf.tls_ocsp_staple_len) != 0) {
 		log_warnx("%s: failed to set tls certificate/key: %s", __func__,
-		    tls_config_error(socket->tls_config));
+		    tls_config_error(sock->tls_config));
 		return (-1);
 	}
 
-	if (thing->tls_ca != NULL) {
-		if (tls_config_set_ca_mem(socket->tls_config, thing->tls_ca,
-			thing->tls_ca_len) != 0) {
+	if (sock->conf.tls_ca != NULL) {
+		if (tls_config_set_ca_mem(sock->tls_config, sock->conf.tls_ca,
+			sock->conf.tls_ca_len) != 0) {
 			log_warnx("%s: failed to add ca cert(s)", __func__);
 			return (-1);
 		}
 
-		if (tls_config_set_crl_mem(socket->tls_config, thing->tls_crl,
-			thing->tls_crl_len) != 0) {
+		if (tls_config_set_crl_mem(sock->tls_config,
+		    sock->conf.tls_crl, sock->conf.tls_crl_len) != 0) {
 			log_warnx("%s: failed to add crl(s)", __func__);
 			return (-1);
 		}
 
-		if (thing->tls_flags & TLSFLAG_OPTIONAL)
-			tls_config_verify_client_optional(socket->tls_config);
+		if (sock->conf.tls_flags & TLSFLAG_OPTIONAL)
+			tls_config_verify_client_optional(sock->tls_config);
 		else
-			tls_config_verify_client(socket->tls_config);
+			tls_config_verify_client(sock->tls_config);
 	}
 
-	log_debug("%s: adding keypair for server %s", __func__, thing->name);
+	/* log_debug("%s: adding keypair for server %s", __func__, */
+	/*     sock->conf.name); */
 
-	if (tls_config_add_keypair_ocsp_mem(socket->tls_config, thing->tls_cert,
-	    thing->tls_cert_len, thing->tls_key, thing->tls_key_len,
-	    thing->tls_ocsp_staple, thing->tls_ocsp_staple_len) != 0) {
+	if (tls_config_add_keypair_ocsp_mem(sock->tls_config,
+	    sock->conf.tls_cert, sock->conf.tls_cert_len,
+	    sock->conf.tls_key, sock->conf.tls_key_len,
+	    sock->conf.tls_ocsp_staple,
+	    sock->conf.tls_ocsp_staple_len) != 0) {
 		log_warnx("%s: failed to add tls keypair", __func__);
 		return (-1);
 	}
 
-	if (tls_configure(socket->tls_ctx, socket->tls_config) != 0) {
+	if (tls_configure(sock->tls_ctx, sock->tls_config) != 0) {
 		log_warnx("%s: failed to configure tls - %s", __func__,
-		    tls_error(socket->tls_ctx));
+		    tls_error(sock->tls_ctx));
 		return (-1);
 	}
 
 	/* We're now done with the public/private key & ca/crl... */
-	tls_config_clear_keys(socket->tls_config);
+	tls_config_clear_keys(sock->tls_config);
 
-	socket->tls = true;
+	freezero(sock->conf.tls_cert, sock->conf.tls_cert_len);
+	freezero(sock->conf.tls_key, sock->conf.tls_key_len);
+	free(sock->conf.tls_ca);
+	free(sock->conf.tls_crl);
 
-	freezero(thing->tls_cert, thing->tls_cert_len);
-	freezero(thing->tls_key, thing->tls_key_len);
-	free(thing->tls_ca);
-	free(thing->tls_crl);
+	sock->conf.tls_ca = NULL;
+	sock->conf.tls_cert = NULL;
+	sock->conf.tls_crl = NULL;
+	sock->conf.tls_key = NULL;
 
-	thing->tls_ca = NULL;
-	thing->tls_cert = NULL;
-	thing->tls_crl = NULL;
-	thing->tls_key = NULL;
+	sock->conf.tls_ca_len = 0;
+	sock->conf.tls_cert_len = 0;
+	sock->conf.tls_crl_len = 0;
+	sock->conf.tls_key_len = 0;
 
-	thing->tls_ca_len = 0;
-	thing->tls_cert_len = 0;
-	thing->tls_crl_len = 0;
-	thing->tls_key_len = 0;
-
 	return 0;
 }
 
 void
 socket_tls_handshake(int fd, short event, void *arg)
 {
-	struct thingsd		*thingsd = (struct thingsd *)arg;
-	struct client		*client = NULL, *tclient;
-	ssize_t			 ret = 0;
+	/* struct thingsd		*thingsd = (struct thingsd *)arg; */
+	/* struct client		*client = NULL, *tclient; */
+	/* ssize_t			 ret = 0; */
 
-	TAILQ_FOREACH(tclient, thingsd->clients, entry) {
-		if (tclient->fd == fd) {
-			client = tclient;
-			break;
-		}
-	}
+	/* TAILQ_FOREACH(tclient, thingsd->clients, entry) { */
+	/* 	if (tclient->fd == fd) { */
+	/* 		client = tclient; */
+	/* 		break; */
+	/* 	} */
+	/* } */
 
-	if (client == NULL)
-		return;
+	/* if (client == NULL) */
+	/* 	return; */
 
-	ret = tls_handshake(client->tls_ctx);
+	/* ret = tls_handshake(client->tls_ctx); */
 
-	if (ret == 0) {
-		event_del(client->ev);
-		log_debug("%s: tls handshake success", __func__);
-		/* client_add(thingsd, client); */
-	} else if (ret == TLS_WANT_POLLIN) {
-		event_del(client->ev);
-		event_set(client->ev, client->fd, EV_READ|EV_PERSIST,
-			    socket_tls_handshake, thingsd);
-		if (event_add(client->ev, NULL))
-			log_debug("%s: event add client", __func__);
-	} else if (ret == TLS_WANT_POLLOUT) {
-		event_del(client->ev);
-		event_set(client->ev, client->fd, EV_WRITE|EV_PERSIST,
-			    socket_tls_handshake, thingsd);
-		if (event_add(client->ev, NULL))
-			log_debug("%s: event add client", __func__);
-	} else {
-		event_del(client->ev);
-		log_debug("%s: tls handshake failed - %s", __func__,
-		    tls_error(client->tls_ctx));
-	}
+	/* if (ret == 0) { */
+	/* 	event_del(client->ev); */
+	/* 	log_debug("%s: tls handshake success", __func__); */
+	/* 	/1* client_add(thingsd, client); *1/ */
+	/* } else if (ret == TLS_WANT_POLLIN) { */
+	/* 	event_del(client->ev); */
+	/* 	event_set(client->ev, client->fd, EV_READ|EV_PERSIST, */
+	/* 		    socket_tls_handshake, thingsd); */
+	/* 	if (event_add(client->ev, NULL)) */
+	/* 		log_debug("%s: event add client", __func__); */
+	/* } else if (ret == TLS_WANT_POLLOUT) { */
+	/* 	event_del(client->ev); */
+	/* 	event_set(client->ev, client->fd, EV_WRITE|EV_PERSIST, */
+	/* 		    socket_tls_handshake, thingsd); */
+	/* 	if (event_add(client->ev, NULL)) */
+	/* 		log_debug("%s: event add client", __func__); */
+	/* } else { */
+	/* 	event_del(client->ev); */
+	/* 	log_debug("%s: tls handshake failed - %s", __func__, */
+	/* 	    tls_error(client->tls_ctx)); */
+	/* } */
 }
+
+void
+socket_tls_load(struct socket *sock)
+{
+	log_debug("%s: setup TLS for listener %d", __func__, sock->conf.id);
+
+	if (tls_load_keypair(sock) == -1)
+		fatalx("%s: failed to load public/private keys on sock %d",
+		    __func__, sock->conf.id);
+	if (tls_load_ca(sock) == -1)
+		fatalx("failed to load ca cert(s) for sock %d", sock->conf.id);
+	if (tls_load_crl(sock) == -1)
+		fatalx("failed to load crl(s) for sock %d", sock->conf.id);
+	if (tls_load_ocsp(sock) == -1)
+		fatalx("failed to load ocsp staple for sock %d", sock->conf.id);
+}