Commit Diff


commit - 67eaf295cf443d53a0eb90008e0f4dc61dd9bd60
commit + f62f088a360047e04fe68b45a49f197be74f18c2
blob - 5e584a560d3aac3d61171c7e440552b7cd5dad6f
blob + 28a54ea56f7d60965a2f1844dcc18046820f15b8
--- thingsctl/parser.c	
+++ thingsctl/parser.c	
@@ -1,4 +1,5 @@
 /*
+ * Copyright (c) 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  *
@@ -87,7 +88,7 @@ static const struct token t_list[] = {
 static const struct token t_show[] = {
 	{KEYWORD,	"client",	NONE,		t_client_name},
 	{KEYWORD,	"control",	SHOW_CONTROL,	NULL},
-	{KEYWORD,	"parent",	SHOW_PARENT,	NULL},
+	{KEYWORD,	"parent",	SHOW_THINGSD,	NULL},
 	{KEYWORD,	"socket",	NONE,		t_socket_name},
 	{KEYWORD,	"thing",	NONE,		t_thing_name},
 	{ENDTOKEN,	"",		NONE,		NULL}
blob - 4a71ae5857fad0e1bae5314e2de781e9dedeaa45
blob + 9653c711f3fe8d54f5ff0c3bbc65424e2710e0e5
--- thingsctl/parser.h	
+++ thingsctl/parser.h	
@@ -1,4 +1,5 @@
 /*
+ * Copyright (c) 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  *
@@ -29,7 +30,7 @@ enum actions {
 	SHOW,
 	SHOW_CONTROL,
 	SHOW_PACKETS,
-	SHOW_PARENT,
+	SHOW_THINGSD,
 };
 
 struct parse_result {
blob - 0b0d17cb584882cf34a2457c3e97546716b1f1dd
blob + ff1ea9bc41b9dfdcf6a0c439fc85b5a43d4832d6
--- thingsctl/thingsctl.c	
+++ thingsctl/thingsctl.c	
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
  * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
  * Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
@@ -68,7 +68,7 @@ main(int argc, char *argv[])
 	int			 ctl_sock;
 	int			 done = 0;
 	int			 n, verbose = 0;
-	int			 ch, v = 0;
+	int			 ch;
 	char			*sockname;
 
 	sockname = THINGSD_SOCKET;
@@ -172,23 +172,14 @@ main(int argc, char *argv[])
 		}
 		printf("\n");
 		break;
-	case SHOW_PARENT:
-		imsg_compose(ibuf, IMSG_GET_INFO_PARENT_REQUEST, 0,
+	case SHOW_THINGSD:
+		imsg_compose(ibuf, IMSG_GET_INFO_THINGSD_REQUEST, 0,
 		    0, -1, NULL, 0);
 		break;
 	case RELOAD:
-		printf("\nAll things and sockets will be reset.\n");
-		printf("This can have unintended consequences!\n");
-		printf("For example, all clients will have to reconnect.\n\n");
-		printf("Are you sure you want to reload the config? (y|n) ");
-		ch = getchar();
-		if (ch == 'y' || ch == 'Y') {
-			imsg_compose(ibuf, IMSG_CTL_RESET, 0, 0, -1, &v,
-			    sizeof(v));
-			printf("Reload request sent\n");
-		} else
-			printf("Reload request ignored\n");
-		printf("\n");
+		printf("\nReload is not supported.\n");
+		printf("Use `rcctl restart thingsd` instead.\n");
+		printf("Naturally, this will disconnect all clients.\n");
 		done = 1;
 		break;
 	default:
@@ -237,7 +228,7 @@ main(int argc, char *argv[])
 				}
 				printf("%s\n", imsg.data);
 				break;
-			case SHOW_PARENT:
+			case SHOW_THINGSD:
 				done = show_parent_msg(&imsg);
 				break;
 			default:
@@ -256,10 +247,10 @@ main(int argc, char *argv[])
 int
 show_parent_msg(struct imsg *imsg)
 {
-	struct thingsd_parent_info *npi;
+	struct thingsd_thingsd_info *npi;
 
 	switch (imsg->hdr.type) {
-	case IMSG_GET_INFO_PARENT_DATA:
+	case IMSG_GET_INFO_THINGSD_DATA:
 		npi = imsg->data;
 		printf("\nParent says: Logging level is ");
 		if (npi->verbose == 2)
@@ -270,7 +261,7 @@ show_parent_msg(struct imsg *imsg)
 			printf("brief");
 		printf(" (%d).\n", npi->verbose);
 		break;
-	case IMSG_GET_INFO_PARENT_END_DATA:
+	case IMSG_GET_INFO_THINGSD_END_DATA:
 		return (1);
 	default:
 		break;
@@ -314,47 +305,38 @@ list_things_msg(struct imsg *imsg)
 	case IMSG_GET_INFO_THINGS_DATA:
 		nti = (struct thing *) imsg->data;
 
-		printf("\nThing Name:\t\t\t%s\n", nti->name);
-		switch(nti->type) {
-		case TCP:
-			printf("\tIP Addr:\t\t%s\n", nti->ipaddr);
-			printf("\tConnect Port:\t\t%d\n", nti->conn_port);
-			printf("\tPersists:\t\t%d\n", nti->persist);
+		printf("\nThing Name:\t\t\t%s\n", nti->conf.name);
+		switch(nti->conf.type) {
+		case S_TCP:
+			printf("\tIP Addr:\t\t%s\n", nti->conf.ipaddr);
+			printf("\tConnect Port:\t\t%d\n",
+			    ntohs(nti->conf.tcp_conn_port.val[0]));
+			printf("\tPersists:\t\t%d\n", nti->conf.persist);
 			break;
-		case UDP:
-			printf("\tUDP Listener:\t\t%s\n", nti->udp);
-			printf("\tConnect Port:\t\t%d\n", nti->rcv_port);
+		case S_UDP:
+			printf("\tUDP Listener:\t\t%s\n", nti->conf.udp);
+			printf("\tConnect Port:\t\t%d\n",
+			    ntohs(nti->conf.udp_rcv_port.val[0]));
 			break;
-		case DEV:
-			printf("\tDevice:\t\t\t%s\n", nti->location);
-			printf("\tBaud:\t\t\t%d\n", nti->baud);
-			printf("\tData:\t\t\t%d\n", nti->data_bits);
-			printf("\tStop:\t\t\t%d\n", nti->stop_bits);
-			printf("\tHardware:\t\t%d\n", nti->hw_ctl);
-			printf("\tSoftware:\t\t%d\n", nti->sw_ctl);
-			printf("\tParity:\t\t\t%s\n", nti->parity);
+		case S_DEV:
+			printf("\tDevice:\t\t\t%s\n", nti->conf.location);
+			printf("\tBaud:\t\t\t%d\n", nti->conf.baud);
+			printf("\tData:\t\t\t%d\n", nti->conf.data_bits);
+			printf("\tStop:\t\t\t%d\n", nti->conf.stop_bits);
+			printf("\tHardware:\t\t%d\n", nti->conf.hw_ctl);
+			printf("\tSoftware:\t\t%d\n", nti->conf.sw_ctl);
+			printf("\tParity:\t\t\t%s\n", nti->conf.parity);
 			break;
 	}
-		if (strncmp(nti->iface, "", PKT_BUFF) == 0)
+		if (strncmp(nti->conf.tcp_iface, "", PKT_BUFF) == 0)
 			printf("\tBind Interface:\t\tall\n");
 		else
-			printf("\tBind Interface:\t\t%s\n", nti->iface);
-		printf("\tListen Port:\t\t%d\n", nti->port);
-		if (nti->max_clients == 0)
-			printf("\tMax Clients:\t\tunlimited\n");
-		else
-			printf("\tMax Clients:\t\t%d\n", nti->max_clients);
-		printf("\tPassword:\t\t%s\n", nti->password);
-		printf("\tClient Count:\t\t%zu\n", nti->client_cnt);
-		if (nti->tls == false)
-			break;
-		printf("\tTLS:\t\t\t%d\n", nti->tls);
-		printf("\tCert:\t\t\t%s\n", nti->tls_cert_file);
-		printf("\tKey:\t\t\t%s\n", nti->tls_key_file);
-		printf("\tCA:\t\t\t%s\n", nti->tls_ca_file);
-		printf("\tCRL:\t\t\t%s\n", nti->tls_crl_file);
-		printf("\tOCSP:\t\t\t%s\n", nti->tls_ocsp_staple_file);
-
+			printf("\tBind Interface:\t\t%s\n",
+			    nti->conf.tcp_iface);
+		printf("\tListen Port:\t\t%d\n",
+		    ntohs(nti->conf.tcp_listen_port.val[0]));
+		printf("\tPassword:\t\t%s\n", nti->conf.password);
+		printf("\tTLS:\t\t\t%s\n", nti->conf.tls ? "Yes" : "No");
 		break;
 	case IMSG_GET_INFO_THINGS_END_DATA:
 		return (1);
@@ -379,10 +361,9 @@ list_clients_msg(struct imsg *imsg)
 
 		printf("\nClient Name:\t\t\t%s\n", nci->name);
 		printf("\tfd:\t\t\t%d\n", nci->fd);
-		printf("\tPort:\t\t\t%d\n", nci->port);
-		if (nci->tls == true)
-			printf("\tTLS:\t\t\tyes\n");
-		printf("\tSubscriptions:\t\t%zu\n", nci->subs);
+		printf("\tPort:\t\t\t%d\n", ntohs(nci->port.val[0]));
+		printf("\tSubscribed:\t\t%s\n", nci->subscribed ? "Yes" : "No");
+		printf("\tTLS:\t\t\t%s\n", nci->tls ? "Yes" : "No");
 		break;
 	case IMSG_GET_INFO_CLIENTS_END_DATA:
 		return (1);
@@ -403,19 +384,16 @@ list_sockets_msg(struct imsg *imsg)
 		nsi = (struct socket *) imsg->data;
 
 		printf("\n");
-		if (strcmp(nsi->name, "") == 0)
-			printf("Socket Name:\t\t\tReceive socket\n");
-		else
-			printf("Socket Name:\t\t\t%s\n", nsi->name);
+		printf("Socket Name:\t\t\t%s\n", nsi->conf.name);
 		printf("\tfd:\t\t\t%d\n", nsi->fd);
-		printf("\tPort:\t\t\t%d\n", nsi->port);
-		if (nsi->tls == true)
-			printf("\tTLS:\t\t\tyes\n\n");
+		printf("\tPort:\t\t\t%d\n", ntohs(nsi->conf.port.val[0]));
 		printf("\tClient Count:\t\t%zu\n", nsi->client_cnt);
-		if (nsi->max_clients == 0)
+		if (nsi->conf.max_clients == 0)
 			printf("\tMax Clients:\t\tunlimited\n");
 		else
-			printf("\tMax Clients:\t\t%zu\n", nsi->max_clients);
+			printf("\tMax Clients:\t\t%zu\n",
+			    nsi->conf.max_clients);
+		printf("\tTLS:\t\t\t%s\n", nsi->conf.tls ? "Yes" : "No");
 		break;
 	case IMSG_GET_INFO_SOCKETS_END_DATA:
 		return (1);
blob - 565c7b5ca63fa2cbc56aa0201b6c4b0b863cd19c
blob + 26e70062578727e59ff8485a44299160f1384e3b
--- thingsd/Makefile	
+++ thingsd/Makefile	
@@ -11,7 +11,7 @@ MANDIR?=	${PREFIX}/man/man
 
 LDADD =		-levent -ltls -lssl -lcrypto -lutil
 DPADD =		${LIBEVENT} ${LIBTLS} ${LIBSSL} ${LIBCRYPTO} ${LIBUTIL}
-DEBUG=		-g -DDEBUG=0 -O0
+#DEBUG=		-g -DDEBUG=0 -O0
 CFLAGS +=	-Wall -I${.CURDIR}
 CFLAGS +=	-Wstrict-prototypes -Wmissing-prototypes
 CFLAGS +=	-Wmissing-declarations
blob - c19416b00d16f78d8fa81b51aa9bb91067b90789
blob + 3eb69c906092319da83d8b9071507faaa6bda80b
--- thingsd/client.c	
+++ thingsd/client.c	
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2019, 2020 Tracey Emery <tracey@traceyemery.net>
+ * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -33,8 +33,8 @@
 extern volatile int client_inflight;
 extern enum privsep_procid privsep_process;
 
-void	 bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t,
-	    void *);
+/* void	 bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t, */
+/* 	    void *); */
 void	 client_write_to_things(struct packages *);
 
 void
@@ -331,49 +331,3 @@ client_err(struct bufferevent *bev, short error, void 
 
 	client_del(client);
 }
-
-/* 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: */
-
-/* 		memcpy(filter, imsg->data, sizeof(filter)); */
-
-/* 		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 - 19118da89fd2306aa44a910bc38e30615d0eeae2
blob + a1ea3ecef65fb0f17c25e9e9007a8e1897a4886b
--- thingsd/config.c	
+++ thingsd/config.c	
@@ -67,14 +67,14 @@ config_init(struct thingsd *env)
 		if ((env->dead_things = calloc(1,
 		    sizeof(*env->dead_things))) == 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->packages);
 		TAILQ_INIT(env->dead_things);
-		/* TAILQ_INIT(env->packet_clients); */
+		TAILQ_INIT(env->packet_clients);
 	}
 	return (0);
 }
blob - a02ca3676cd2742caa5e2425933a615437a2f5bf
blob + 0262a2e9c400f007defc806b1a8107c19cc28e49
--- thingsd/control.c	
+++ thingsd/control.c	
@@ -40,6 +40,8 @@
 
 #define CONTROL_BACKLOG 5
 
+int	 sockets_proc, clients_proc;
+
 struct ctl_connlist ctl_conns;
 
 struct ctl_conn *control_connbyfd(int);
@@ -48,11 +50,13 @@ void	 control_close(struct privsep *, struct imsg *, i
 	    struct control_sock *);
 void	 control_dispatch_imsg(int, short, void *);
 int	 control_dispatch_thingsd(int, struct privsep_proc *, struct imsg *);
+int	 control_dispatch_sockets(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[] = {
 	{ "thingsd",	PROC_PARENT,	control_dispatch_thingsd },
+	{ "sockets",	PROC_SOCKS,	control_dispatch_sockets },
 };
 
 void
@@ -69,6 +73,73 @@ control_run(struct privsep *ps, struct privsep_proc *p
 }
 
 int
+control_dispatch_sockets(int fd, struct privsep_proc *p, struct imsg *imsg)
+{
+	struct ctl_conn		*c;
+	struct privsep		*ps = p->p_ps;
+	struct control_sock	 cs = ps->ps_csock;
+	int			 bad = 0;
+
+	switch (imsg->hdr.type) {
+	case IMSG_BAD_THING:
+		bad = 1;
+	case IMSG_GET_INFO_SOCKETS_END_DATA:
+	case IMSG_GET_INFO_CLIENTS_END_DATA:
+		if (imsg->hdr.type == IMSG_GET_INFO_SOCKETS_END_DATA)
+			sockets_proc++;
+		if (imsg->hdr.type == IMSG_GET_INFO_CLIENTS_END_DATA)
+			clients_proc++;
+
+		if (sockets_proc == thingsd_env->prefork_socks) {
+			sockets_proc = 0;
+			goto dispatch;
+		}
+
+		if (clients_proc == thingsd_env->prefork_socks) {
+			clients_proc = 0;
+			goto dispatch;
+		}
+		break;
+dispatch:
+		c = control_connbyfd(imsg->hdr.peerid);
+		if (c == NULL) {
+			log_warnx("%s: fd %d: not found",
+			    __func__, imsg->hdr.peerid);
+			return (-1);
+		}
+
+		if (bad) {
+			control_close(ps, imsg, imsg->hdr.peerid, &cs);
+			break;
+		}
+
+		imsg_compose_event(&c->iev, imsg->hdr.type,
+		    0, 0, -1, imsg->data, IMSG_DATA_SIZE(imsg));
+		break;
+	case IMSG_GET_INFO_SOCKETS_DATA:
+	case IMSG_GET_INFO_CLIENTS_DATA:
+		c = control_connbyfd(imsg->hdr.peerid);
+		if (c == NULL) {
+			log_warnx("%s: fd %d: not found",
+			    __func__, imsg->hdr.peerid);
+			return (-1);
+		}
+
+		if (bad) {
+			control_close(ps, imsg, imsg->hdr.peerid, &cs);
+			break;
+		}
+
+		imsg_compose_event(&c->iev, imsg->hdr.type,
+		    0, 0, -1, imsg->data, IMSG_DATA_SIZE(imsg));
+		break;
+	default:
+		return (-1);
+	}
+	return (0);
+}
+
+int
 control_dispatch_thingsd(int fd, struct privsep_proc *p, struct imsg *imsg)
 {
 	struct ctl_conn		*c;
@@ -85,10 +156,6 @@ control_dispatch_thingsd(int fd, struct privsep_proc *
 	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:
-	case IMSG_GET_INFO_CLIENTS_END_DATA:
-	case IMSG_GET_INFO_SOCKETS_DATA:
-	case IMSG_GET_INFO_SOCKETS_END_DATA:
 		c = control_connbyfd(imsg->hdr.peerid);
 		if (c == NULL) {
 			log_warnx("%s: fd %d: not found",
blob - ebc380249113146b5fa64a0053704158a15ba211 (mode 644)
blob + /dev/null
--- thingsd/events.c	
+++ /dev/null	
@@ -1,71 +0,0 @@
-/*
- * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
- *
- * 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/queue.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-
-#include <event.h>
-#include <imsg.h>
-#include <stdbool.h>
-#include <string.h>
-
-#include "proc.h"
-#include "thingsd.h"
-
-void
-udp_event(int fd, short event, void *arg)
-{
-	struct thingsd		*env = (struct thingsd *)arg;
-	struct thing		*thing = NULL, *tthing = NULL;
-	struct client		*client = NULL;
-	struct subscription	*sub = NULL;
-	char			 pkt[PKT_BUFF];
-	int			 len;
-	socklen_t		*addrlen = NULL;
-	struct sockaddr		*addr = NULL;
-	bool			 fail = true;
-
-	memset(pkt, 0, sizeof(pkt));
-
-	TAILQ_FOREACH(tthing, env->things, entry) {
-		if (tthing->fd == fd) {
-			thing = tthing;
-			fail = false;
-			break;
-		}
-	}
-
-	if (fail)
-		return;
-
-	len = recvfrom(fd, pkt, sizeof(pkt), 0, addr, addrlen);
-
-	if (len > 0) {
-		/*  write to clients */
-		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);
-			}
-		}
-
-		if (env->packet_client_count > 0)
-			send_to_packet_client(env, thing->name, pkt, len);
-
-	}
-}
blob - 0eb330a50f15d0cbe7921bec854f9f9b98e6e320
blob + dc46c01d7a49348ac01e72593071b042c21c2e88
--- thingsd/parse.y	
+++ thingsd/parse.y	
@@ -1375,14 +1375,6 @@ parse_config(const char *filename, struct thingsd *env
 	/* 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);
 }
 
blob - c18821cd0929bd600bddda742eb9512565be25f0
blob + 9dac64172ce767e32c067439f0dd48911dc4cd8e
--- thingsd/proc.c	
+++ thingsd/proc.c	
@@ -1,5 +1,3 @@
-/*	$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>
@@ -685,7 +683,11 @@ proc_dispatch(int fd, short event, void *arg)
 			    pf.pf_instance);
 			break;
 		default:
-			fatalx("%s: %s %d got invalid imsg %d peerid %d "
+			/*
+			 * change from fatalx
+			 * just because it's bad doesn't mean we should crash
+			 */
+			log_warnx("%s: %s %d got invalid imsg %d peerid %d "
 			    "from %s %d",
 			    __func__, title, ps->ps_instance + 1,
 			    imsg.hdr.type, imsg.hdr.peerid,
blob - 953b34a7ef182e2d9616a934f411d2dc3c378104 (mode 644)
blob + /dev/null
--- thingsd/serial.c	
+++ /dev/null	
@@ -1,248 +0,0 @@
-/*
- * Copyright (c) 2016, 2019, 2020 Tracey Emery <tracey@traceyemery.net>
- *
- * 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/queue.h>
-#include <sys/socket.h>
-#include <sys/time.h>
-#include <sys/types.h>
-
-#include <event.h>
-#include <fcntl.h>
-#include <imsg.h>
-#include <stdbool.h>
-#include <string.h>
-#include <termios.h>
-
-#include "proc.h"
-#include "thingsd.h"
-
-void
-open_things(struct thingsd *env, bool reconn)
-{
-	struct thing		*thing;
-	struct dead_thing	*dead_thing;
-	struct termios		 s_opts;
-	int			 fd;
-	int			 baudrate = 0, stop = 0;
-	evbuffercb		 socketrd = socket_rd;
-	evbuffercb		 socketwr = socket_wr;
-
-	TAILQ_FOREACH(thing, env->things, entry) {
-		if (thing->exists)
-			continue;
-
-		if (strlen(thing->location) != 0) {
-			thing->type = 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.
-			 */
-			fd = open(thing->location, O_RDWR | O_NONBLOCK |
-			    O_NOCTTY | O_NDELAY);
-
-			if (fd == -1) {
-				log_warnx("failed to open %s", thing->location);
-
-				if (reconn)
-					return;
-
-				dead_thing = new_dead_thing(thing);
-
-				env->exists = true;
-				env->dcount++;
-				thing->fd = -1;
-				thing->exists = false;
-
-				TAILQ_INSERT_TAIL(env->dead_things->
-				    dead_things_list, dead_thing, entry);
-
-				return;
-			} else {
-				/* load current s_opts */
-				tcgetattr(fd, &s_opts);
-
-				/* set baud */
-				switch (thing->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->data_bits != -1) {
-					s_opts.c_cflag &= ~CSIZE;
-					switch(thing->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->parity) != 0) {
-					s_opts.c_cflag &= ~PARENB;
-
-					/* enable parity checking */
-					if (strcmp(thing->parity, "odd") == 0) {
-						s_opts.c_cflag |= PARENB;
-						s_opts.c_cflag |= PARODD;
-						s_opts.c_iflag |= (INPCK |
-						    ISTRIP);
-					} else if (strcmp(thing->parity,
-					    "even") == 0) {
-						s_opts.c_cflag |= PARENB;
-						s_opts.c_cflag &= ~PARODD;
-						s_opts.c_iflag |= (INPCK |
-						    ISTRIP);
-					}
-
-				}
-
-				/* set stop bits */
-				if (thing->stop_bits != -1) {
-					if (thing->stop_bits == 2)
-						s_opts.c_cflag |= CSTOPB;
-					else
-						s_opts.c_cflag &= ~CSTOPB;
-				}
-
-				/* set hardware control */
-				if (thing->hw_ctl == false) {
-					s_opts.c_cflag &= ~CRTSCTS;
-				} else {
-					s_opts.c_cflag |= CRTSCTS;
-				}
-
-				/* set software control */
-				if (thing->sw_ctl == false) {
-					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(fd, TCSANOW, &s_opts);
-
-				thing->fd = fd;
-				if (thing->fd == '\0') {
-					log_warnx("serial device not opened");
-					if (reconn)
-						return;
-					thing->exists = false;
-					add_reconn(thing);
-					return;
-				}
-
-				thing->bev = bufferevent_new(thing->fd,
-				    socketrd, socketwr, socket_err, env);
-
-				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);
-			}
-
-			if (reconn) {
-				thing->exists = true;
-				log_info("reconnected: %s", thing->name);
-			}
-
-		}
-	}
-}
blob - 756b40607b9a231def58df1588956a422f71737f
blob + 85c6c39ab1e6d1004dca46029c5c57a37ded528a
--- thingsd/sockets.c	
+++ thingsd/sockets.c	
@@ -50,6 +50,7 @@ int	 sockets_dispatch_thingsd(int, struct privsep_proc
 int	 sockets_socket_af(struct sockaddr_storage *, struct portrange);
 int	 sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int,
 	    volatile int *);
+int	 sockets_dispatch_control(int, struct privsep_proc *, struct imsg *);
 
 void	 sockets_dup_new_socket(struct socket *, struct socket *);
 void	 sockets_run(struct privsep *, struct privsep_proc *, void *);
@@ -58,6 +59,9 @@ 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 *);
+void	 sockets_show_info(struct privsep *, struct imsg *);
+void	 sockets_show_clients_info(struct privsep *, struct imsg *);
+void	 sockets_kill_client(struct privsep *, struct imsg *);
 
 struct socket
 	*sockets_conf_new_socket(struct thingsd *, struct thing *, int, int);
@@ -66,6 +70,7 @@ struct socket
 extern enum privsep_procid privsep_process;
 
 static struct privsep_proc procs[] = {
+	{ "control",	PROC_CONTROL,	sockets_dispatch_control, control },
 	{ "thingsd",	PROC_PARENT,	sockets_dispatch_thingsd  },
 };
 
@@ -136,6 +141,30 @@ sockets_privinit(struct socket *sock)
 	return (0);
 }
 
+int
+sockets_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg)
+{
+	struct privsep	*ps = p->p_ps;
+	int		 res = 0, cmd = 0;
+
+	switch (imsg->hdr.type) {
+	default:
+		return (-1);
+	}
+
+	switch (cmd) {
+	case 0:
+		break;
+	default:
+		if (proc_compose_imsg(ps, PROC_CONTROL, -1, cmd,
+		    imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
+			return (-1);
+		break;
+	}
+
+	return (0);
+}
+
 void
 sockets_parse_sockets(struct thingsd *env)
 {
@@ -200,6 +229,9 @@ sockets_dup_new_socket(struct socket *p_sock, struct s
 	memcpy(&sock->conf.thing_name, p_sock->conf.thing_name,
 	    sizeof(sock->conf.thing_name));
 
+	snprintf(sock->conf.name, THINGSD_MAXTEXT, "%s_child",
+	    p_sock->conf.thing_name);
+
 	TAILQ_FOREACH(a, p_sock->conf.al, entry) {
 		if (a->ss.ss_family == AF_INET)
 			continue;
@@ -279,6 +311,9 @@ sockets_conf_new_socket(struct thingsd *env, struct th
 	if (is_dup)
 		goto done;
 
+	snprintf(sock->conf.name, THINGSD_MAXTEXT, "%s_parent",
+	    thing->conf.name);
+
 	if (strlcpy(sock->conf.thing_name, thing->conf.name,
 	    sizeof(sock->conf.thing_name)) >= sizeof(sock->conf.thing_name)) {
 		free(sock->conf.al);
@@ -530,48 +565,79 @@ sockets_write_to_clients(struct package *package)
 }
 
 void
+sockets_show_clients_info(struct privsep *ps, struct imsg *imsg)
+{
+	char filter[THINGSD_MAXNAME];
+	struct client	*client, nci;
+	struct socket	*sock;
+
+	memcpy(filter, imsg->data, sizeof(filter));
+
+	TAILQ_FOREACH(sock, thingsd_env->sockets, entry) {
+		if (sock->client_cnt == 0)
+			continue;
+		TAILQ_FOREACH(client, sock->clients, entry) {
+			if (filter[0] == '\0' || strcmp(filter,
+			    client->name) == 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;
+
+				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;
+}
+
+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));
 
-	/* 	memcpy(filter, imsg->data, sizeof(filter)); */
+	TAILQ_FOREACH(socket, thingsd_env->sockets, entry) {
+		if (filter[0] == '\0' || strcmp(filter,
+		    socket->conf.name) == 0) {
 
-	/* 	TAILQ_FOREACH(socket, thingsd_env->sockets, entry) { */
-	/* 		if (filter[0] == '\0' || memcmp(filter, */
-	/* 		    socket->name, sizeof(filter)) == 0) { */
+			memcpy(&nsi.conf.name, socket->conf.name,
+			    sizeof(nsi.conf.name));
 
-	/* 			memcpy(&nsi.name, socket->name, */
-	/* 			    sizeof(nsi.name)); */
+			nsi.fd = socket->fd;
+			nsi.conf.port = socket->conf.port;
+			nsi.conf.tls = socket->conf.tls;
+			nsi.client_cnt = socket->client_cnt;
+			nsi.conf.max_clients = socket->conf.max_clients;
 
-	/* 			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_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; */
-	/* } */
+	if (proc_compose_imsg(ps, PROC_CONTROL, -1,
+	    IMSG_GET_INFO_SOCKETS_END_DATA, imsg->hdr.peerid,
+		    -1, &nsi, sizeof(nsi)) == -1)
+			return;
 }
 
 void
@@ -596,6 +662,27 @@ sockets_socket_rlimit(int maxfd)
 		fatal("%s: failed to set resource limit", __func__);
 }
 
+void
+sockets_kill_client(struct privsep *ps, struct imsg *imsg)
+{
+	struct socket		*sock;
+	struct client		*client;
+	char			 client_name[THINGSD_MAXTEXT];
+
+	memcpy(client_name, imsg->data, sizeof(client_name));
+
+	TAILQ_FOREACH(sock, thingsd_env->sockets, entry) {
+		TAILQ_FOREACH(client, sock->clients, entry) {
+			if (strcmp(client->name, client_name) == 0) {
+				log_debug("Control killed client: %s",
+				    client_name);
+				client_del(client);
+				break;
+			}
+		}
+	}
+}
+
 int
 sockets_dispatch_thingsd(int fd, struct privsep_proc *p, struct imsg *imsg)
 {
@@ -605,6 +692,9 @@ sockets_dispatch_thingsd(int fd, struct privsep_proc *
 	unsigned int		 mode;
 
 	switch (imsg->hdr.type) {
+	case IMSG_KILL_CLIENT:
+		sockets_kill_client(ps, imsg);
+		break;
 	case IMSG_DIST_THING_PACKAGE:
 		IMSG_SIZE_CHECK(imsg, package);
 		package = (struct package *)imsg->data;
@@ -630,6 +720,12 @@ sockets_dispatch_thingsd(int fd, struct privsep_proc *
 	case IMSG_CTL_START:
 		sockets_launch();
 		break;
+	case IMSG_GET_INFO_CLIENTS_REQUEST:
+		sockets_show_clients_info(ps, imsg);
+		break;
+	case IMSG_GET_INFO_SOCKETS_REQUEST:
+		sockets_show_info(ps, imsg);
+		break;
 	default:
 		return (-1);
 	}
@@ -637,11 +733,6 @@ sockets_dispatch_thingsd(int fd, struct privsep_proc *
 	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)
blob - 926e9290d5bf4516a0b5ee08d318e63b3861e5bf
blob + 9c8892f2be29b70641620c857ebfe3fc865470cc
--- thingsd/thingsd.c	
+++ thingsd/thingsd.c	
@@ -57,10 +57,10 @@ 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_show_thing_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 *);
@@ -69,14 +69,20 @@ 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 *);
+void	 thingsd_echo_pkt(struct privsep *, struct imsg *);
+void	 thingsd_stop_pkt(struct privsep *, struct imsg *);
 
-struct	 dead_thing *thingsd_new_dead_thing(struct thing *);
+struct thing
+	 thingsd_compose_thing(struct thing *, enum imsg_type);
+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 },
 	{ "sockets",	PROC_SOCKS,	thingsd_dispatch_sockets, sockets,
@@ -101,7 +107,7 @@ thingsd_dispatch_control(int fd, struct privsep_proc *
 		IMSG_SIZE_CHECK(imsg, &v);
 		if (imsg->data == NULL)
 			break;
-		/* things_stop_pkt(ps, imsg); */
+		thingsd_stop_pkt(ps, imsg);
 		break;
 	case IMSG_SHOW_PACKETS_REQUEST:
 		IMSG_SIZE_CHECK(imsg, &v);
@@ -110,7 +116,7 @@ thingsd_dispatch_control(int fd, struct privsep_proc *
 		memcpy(thing_name, imsg->data, sizeof(thing_name));
 		TAILQ_FOREACH(thing, thingsd_env->things, entry) {
 			if (strcmp(thing->conf.name, thing_name) == 0) {
-				/* things_echo_pkt(ps, imsg); */
+				thingsd_echo_pkt(ps, imsg);
 				exists = 1;
 				break;
 			}
@@ -125,40 +131,26 @@ 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; */
-		/* 	} */
-		/* } */
+		proc_forward_imsg(ps, imsg, PROC_SOCKS, -1);
 		break;
 	case IMSG_GET_INFO_CLIENTS_REQUEST:
-		/* clients_show_info(ps, imsg); */
+		proc_forward_imsg(ps, imsg, PROC_SOCKS, -1);
 		break;
 	case IMSG_GET_INFO_SOCKETS_REQUEST:
-		/* sockets_show_info(ps, imsg); */
+		proc_forward_imsg(ps, imsg, PROC_SOCKS, -1);
 		break;
 	case IMSG_GET_INFO_THINGS_REQUEST:
 	case IMSG_GET_INFO_THINGS_REQUEST_ROOT:
-		/* things_show_info(ps, imsg); */
+		thingsd_show_thing_info(ps, imsg);
 		break;
 	case IMSG_GET_INFO_THINGSD_REQUEST:
-		/* thingsd_show_info(ps, imsg); */
+		thingsd_show_info(ps, imsg);
 		break;
-	case IMSG_CTL_RESET:
-		IMSG_SIZE_CHECK(imsg, &v);
-		memcpy(&v, imsg->data, sizeof(v));
-		thingsd_reload(v);
-		break;
 	case IMSG_CTL_VERBOSE:
 		IMSG_SIZE_CHECK(imsg, &verbose);
 		memcpy(&verbose, imsg->data, sizeof(verbose));
 		log_setverbose(verbose);
-
-		/* proc_forward_imsg(ps, imsg, PROC_SOCKS, -1); */
+		proc_forward_imsg(ps, imsg, PROC_SOCKS, -1);
 		break;
 	default:
 		return (-1);
@@ -210,13 +202,7 @@ thingsd_sighdlr(int sig, short event, void *arg)
 
 	switch (sig) {
 	case SIGHUP:
-		log_info("%s: reload requested with SIGHUP", __func__);
-
-		/*
-		 * This is safe because libevent uses async signal handlers
-		 * that run in the event loop and not in signal context.
-		 */
-		thingsd_reload(0);
+		log_info("%s: ignoring SIGHUP", __func__);
 		break;
 	case SIGPIPE:
 		log_info("%s: ignoring SIGPIPE", __func__);
@@ -274,6 +260,7 @@ main(int argc, char **argv)
 			env->thingsd_verbose++;
 			break;
 		case 'n':
+			env->thingsd_debug = 2;
 			env->thingsd_noaction = 1;
 			break;
 		case 'P':
@@ -461,27 +448,6 @@ thingsd_configure_done(struct thingsd *env)
 }
 
 void
-thingsd_reload(int reset)
-{
-	const char *filename = thingsd_env->thingsd_conffile;
-
-	log_debug("%s: reload config file %s", __func__, filename);
-
-	/* 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); */
-	/* } */
-
-	/* thingsd_configure_things(thingsd_env->thingsd_ps); */
-}
-
-void
 thingsd_shutdown(struct thingsd *env)
 {
 	proc_kill(env->thingsd_ps);
@@ -523,9 +489,16 @@ thingsd_parent_shutdown(struct thingsd *env)
 	struct socket		*sock, *tsock;
 	struct package		*package, *tpkg;
 	struct dead_thing	*dead_thing, *tdead_thing;
+	struct packet_client	*pclt, *tpclt;
 
 	log_debug("thingsd parent shutting down");
 
+	/* clean up packet clients */
+	TAILQ_FOREACH_SAFE(pclt, env->packet_clients, entry, tpclt) {
+		TAILQ_REMOVE(env->packet_clients, pclt, entry);
+		free(pclt);
+	}
+
 	/* clean up dead things */
 	TAILQ_FOREACH_SAFE(dead_thing, env->dead_things, entry, tdead_thing) {
 		TAILQ_REMOVE(env->dead_things, dead_thing, entry);
@@ -896,7 +869,9 @@ thingsd_write_to_things(struct package *package)
 			}
 		}
 		break;
+	case S_UDP:
 	default:
+		log_info("write %d bytes: %s", package->len, package->pkt);
 		write(thing->fd, package->pkt, package->len);
 		break;
 	}
@@ -907,6 +882,8 @@ thingsd_write_to_socks(struct packages *packages)
 {
 	struct privsep		*ps = thingsd_env->thingsd_ps;
 	struct package		*package, *tpkg, p;
+	struct packet_client	*packet_client;
+	struct thing		*thing = NULL;
 	unsigned int		 id, what;
 	int			 fd = -1, n, m;
 	struct iovec		 iov[6];
@@ -949,6 +926,30 @@ thingsd_write_to_socks(struct packages *packages)
 				}
 			}
 		}
+
+		TAILQ_FOREACH(thing, thingsd_env->things, entry) {
+			if (thing->conf.id == package->thing_id)
+				break;
+		}
+
+		if (thing == NULL)
+			goto free;
+
+		TAILQ_FOREACH(packet_client, thingsd_env->packet_clients,
+		    entry) {
+			if (strlen(packet_client->name) != 0) {
+				if (strcmp(thing->conf.name,
+				    packet_client->name) == 0)
+					if (proc_compose_imsg(
+					    &packet_client->ps,
+					    PROC_CONTROL, -1,
+					    IMSG_SHOW_PACKETS_DATA,
+					    packet_client->imsg.hdr.peerid, -1,
+					    package->pkt, package->len) == -1)
+					break;
+			}
+		}
+free:
 		TAILQ_REMOVE(packages, package, entry);
 		free(package);
 	}
@@ -1107,3 +1108,124 @@ thingsd_do_reconn(struct thingsd *env)
 	if (env->dcount == 0)
 		env->exists = 0;
 }
+
+void
+thingsd_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
+thingsd_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
+thingsd_show_thing_info(struct privsep *ps, struct imsg *imsg)
+{
+	char filter[THINGSD_MAXNAME];
+	struct thing *thing, nti;
+
+	memcpy(filter, imsg->data, sizeof(filter));
+
+	TAILQ_FOREACH(thing, thingsd_env->things, entry) {
+		if (filter[0] == '\0' || strcmp(filter,
+		    thing->conf.name) == 0) {
+			nti = thingsd_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;
+}
+
+struct thing
+thingsd_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.conf.hw_ctl = thing->conf.hw_ctl;
+	nti.conf.persist = thing->conf.persist;
+
+	memcpy(&nti.conf.tcp_iface, thing->conf.tcp_iface,
+	    sizeof(nti.conf.tcp_iface));
+
+	memcpy(&nti.conf.ipaddr, thing->conf.ipaddr, sizeof(nti.conf.ipaddr));
+
+	memcpy(&nti.conf.parity, thing->conf.parity, sizeof(nti.conf.parity));
+
+	memcpy(&nti.conf.name, thing->conf.name, sizeof(nti.conf.name));
+
+	nti.conf.password[0] = '\0';
+	if (type == IMSG_GET_INFO_THINGS_REQUEST)
+		memcpy(&nti.conf.password, blank, sizeof(nti.conf.password));
+	else
+		memcpy(&nti.conf.password, thing->conf.password,
+		    sizeof(nti.conf.password));
+
+	memcpy(&nti.conf.location, thing->conf.location,
+	    sizeof(nti.conf.location));
+
+	memcpy(&nti.conf.udp, thing->conf.udp, sizeof(nti.conf.udp));
+	memcpy(&nti.conf.udp_iface, thing->conf.udp_iface,
+	    sizeof(nti.conf.udp_iface));
+
+	nti.fd = thing->fd;
+	nti.conf.baud = thing->conf.baud;
+	nti.conf.tcp_conn_port = thing->conf.tcp_conn_port;
+	nti.conf.udp_rcv_port = thing->conf.udp_rcv_port;
+	nti.conf.data_bits = thing->conf.data_bits;
+	nti.conf.max_clients = thing->conf.max_clients;
+	nti.conf.tcp_listen_port = thing->conf.tcp_listen_port;
+	nti.conf.stop_bits = thing->conf.stop_bits;
+	nti.conf.type = thing->conf.type;
+	nti.conf.tls = thing->conf.tls;
+
+	return nti;
+}
+
blob - f712cdc0213322235d1d25ab9b17c5f95546be99
blob + 80d2d2ca61cda90faec7101093c25fc2d09345e7
--- thingsd/thingsd.h	
+++ thingsd/thingsd.h	
@@ -177,6 +177,7 @@ TAILQ_HEAD(clientlist, client);
 struct socket_config {
 	struct addresslist	*al;
 	char			 iface[THINGSD_MAXTEXT];
+	char			 name[THINGSD_MAXTEXT];
 	char			 thing_name[THINGSD_MAXTEXT];
 	int			 id;
 	int			 child_id;
@@ -277,8 +278,6 @@ struct thing_config {
 	int			 stop_bits;
 	int			 type;
 
-	size_t			 client_cnt;
-
 	int			 tls;
 	uint8_t			 tls_flags;
 
@@ -314,8 +313,6 @@ struct thing {
 	struct thing_config	 conf;
 	int			 exists;
 
-	size_t			 client_cnt;
-
 	/* either DEV, TCP conn, or UDP rcv fd */
 	int			 fd;
 
@@ -336,16 +333,15 @@ struct dead_thing {
 };
 TAILQ_HEAD(deadthinglist, dead_thing);
 
+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 socketlist	*sockets;
@@ -379,7 +375,7 @@ struct thingsd {
 	int			 socks_reload;
 
 	/* control packets */
-	/* struct packetclientlist	*packet_clients; */
+	struct packetclientlist	*packet_clients;
 	int			 packet_client_count;
 };
 
@@ -393,7 +389,6 @@ void	 client_wr(struct bufferevent *, void *);
 void	 client_err(struct bufferevent *, short, void *);
 void	 client_tls_readcb(int, short, void *);
 void	 client_tls_writecb(int, short, void *);
-/* void	 clients_show_info(struct privsep *, struct imsg *); */
 
 /* sockets.c */
 struct socket
@@ -407,7 +402,6 @@ 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);
@@ -416,21 +410,7 @@ void	 sockets(struct privsep *, struct privsep_proc *)
 /* thingsd.c */
 struct thing	*thingsd_conf_new_thing(struct thingsd *, struct thing *,
 		    char *, int);
-/* things.c */
-/* 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 *); */
 
-/* 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);