Commit Diff


commit - 18505d9be569512a298330f59bc56948e7e29297
commit + f7da1d2a5961471e94f95cdffaf2671804b604af
blob - 1510d8ebf06ef9db965f0d59bf6bc60bc18bd448
blob + 8ed04eb1c1bdaeb2ba0a7bb37fe4bf59f1e5f9d7
--- thingsctl/thingsctl.c	
+++ thingsctl/thingsctl.c	
@@ -235,8 +235,9 @@ main(int argc, char *argv[])
 					done = 1;
 					break;
 				}
-				if ((ctl_pkt = calloc(IMSG_DATA_SIZE(&imsg),
-				    sizeof(*ctl_pkt))) == NULL)
+				ctl_pkt = calloc(IMSG_DATA_SIZE(&imsg),
+				    sizeof(*ctl_pkt));
+				if (ctl_pkt == NULL)
 					err(1, "%s: calloc", __func__);
 				if ((ctl_pkt = strndup(imsg.data,
 				    IMSG_DATA_SIZE(&imsg))) == NULL) {
blob - 6d6d70e05c639284417c71d64076daed2e061b9d
blob + 6758d04c9f3c7d43e60792827bf7cf080cfa823f
--- thingsd/client.c	
+++ thingsd/client.c	
@@ -63,8 +63,9 @@ accept_reserve(int sockfd, struct sockaddr *addr, sock
 void
 client_accept_paused(int fd, short events, void *arg)
 {
-	struct thingsd		*env = arg;
-	event_add(env->ev, NULL);
+	struct socket		*sock = arg;
+
+	event_add(sock->ev, NULL);
 }
 
 void
@@ -81,6 +82,12 @@ client_conn(int fd, short event, void *arg)
 	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,
 	    &cgi_inflight);
 	if (client_fd == -1) {
@@ -91,8 +98,8 @@ client_conn(int fd, short event, void *arg)
 			return;
 		case EMFILE:
 		case ENFILE:
-			event_del(env->ev);
-			evtimer_add(&env->pause, &backoff);
+			event_del(sock->ev);
+			evtimer_add(&sock->pause, &backoff);
 			return;
 		default:
 			log_warnx("client accept failed");
@@ -107,10 +114,6 @@ client_conn(int fd, short event, void *arg)
 	if (client->ev == NULL)
 		goto err;
 
-	sock = get_socket(env, fd);
-	if (sock == NULL)
-		goto err;
-
 	if (sock->tls) {
 		if (tls_accept_socket(sock->tls_ctx, &client->tls_ctx,
 		    client_fd) == -1) {
@@ -120,19 +123,18 @@ client_conn(int fd, short event, void *arg)
 		}
 	}
 
-	*client->sub_names = (char *) calloc(env->max_subs,
-	    sizeof(client->sub_names));
-	if (*client->sub_names == NULL)
+	client->subscriptions = calloc(1, sizeof(*client->subscriptions));
+	if (client->subscriptions == NULL)
 		goto err;
 
-	/*  check for unlimited clients */
+	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--;
-		free(*client->sub_names);
 		goto err;
 	}
 
@@ -144,7 +146,6 @@ client_conn(int fd, short event, void *arg)
 	if (client->evb == NULL) {
 		sock->client_cnt--;
 		env->client_cnt--;
-		free(*client->sub_names);
 		goto err;
 	}
 
@@ -157,10 +158,7 @@ client_conn(int fd, short event, void *arg)
 		event_del(client->ev);
 		event_set(client->ev, client->fd, EV_READ | EV_PERSIST,
 		    socket_tls_handshake, env);
-		if (event_add(client->ev, NULL)) {
-			free(*client->sub_names);
-			goto err;
-		}
+		event_add(client->ev, NULL);
 		return;
 	}
 
@@ -174,8 +172,12 @@ err:
 		close(client_fd);
 	if (sock->tls)
 		tls_free(client->tls_ctx);
-	free(client->ev);
-	free(client);
+	if (client->subscriptions != NULL)
+		free(client->subscriptions);
+	if (client != NULL) {
+		free(client->ev);
+		free(client);
+	}
 }
 
 void
@@ -222,18 +224,24 @@ client_del(struct thingsd *env, struct client *client)
 {
 	struct thing		*thing;
 	struct client		*pclient, *tclient;
-	size_t			 n;
+	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);
 
-			for (n = 0; n < pclient->le; n++)
-				TAILQ_FOREACH(thing, env->things, entry)
-					if (strcmp(pclient->sub_names[n],
+
+			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--;
@@ -250,7 +258,7 @@ client_del(struct thingsd *env, struct client *client)
 			close(pclient->fd);
 			TAILQ_REMOVE(env->clients, pclient, entry);
 			free(pclient->ev);
-			free(*pclient->sub_names);
+			free(pclient->subscriptions);
 			free(pclient);
 			break;
 		}
@@ -263,7 +271,8 @@ client_rd(struct bufferevent *bev, void *arg)
 	struct thingsd		*env = (struct thingsd *)arg;
 	struct thing		*thing = NULL;
 	struct client		*client = NULL, *tclient;
-	size_t			 len, n;
+	struct subscription	*sub;
+	size_t			 len;
 	int			 fd = bev->ev_read.ev_fd;
 	char			*pkt = NULL, *npkt = NULL;
 
@@ -286,8 +295,10 @@ client_rd(struct bufferevent *bev, void *arg)
 			return;
 
 		npkt = calloc(len, sizeof(*npkt));
-		if (npkt == NULL)
+		if (npkt == NULL) {
+			free(pkt);
 			return;
+		}
 
 		bufferevent_disable(client->bev, EV_READ);
 		evbuffer_remove(client->evb, pkt, len);
@@ -302,19 +313,16 @@ client_rd(struct bufferevent *bev, void *arg)
 		}
 	} else if (client->subscribed) {
 		/* write to things */
-		for (n = 0; n < client->le; n++) {
+		TAILQ_FOREACH(sub, client->subscriptions, entry) {
 			TAILQ_FOREACH(thing, env->things, entry) {
-				if (strcmp(client->sub_names[n],
-				    thing->name) == 0 &&
+				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);
-
 				}
 			}
 		}
@@ -403,6 +411,7 @@ client_wr_things(struct client *client, struct thing *
 			if (thing->fd == -1) {
 				log_warnx("%s: temporary ipaddr connection"
 				    " failed", __func__);
+				free(pkt);
 				return;
 			}
 
blob - d741d2a36d7bd88c792c0c20a638f121ace62d92
blob + 4edebd007d9eb41edf61af59e71cd07319ceba3d
--- thingsd/events.c	
+++ thingsd/events.c	
@@ -32,9 +32,9 @@ udp_event(int fd, short event, void *arg)
 	struct thingsd		*env = (struct thingsd *)arg;
 	struct thing		*thing = NULL, *tthing;
 	struct client		*client;
+	struct subscription	*sub;
 	char			 pkt[PKT_BUFF];
 	int			 len;
-	size_t			 n;
 	socklen_t		*addrlen = NULL;
 	struct sockaddr		*addr = NULL;
 
@@ -52,9 +52,8 @@ udp_event(int fd, short event, void *arg)
 	if (len > 0) {
 		/*  write to clients */
 		TAILQ_FOREACH(client, env->clients, entry) {
-			for (n = 0; n < client->le; n++) {
-				if (strcmp(client->sub_names[n],
-				    thing->name) == 0)
+			TAILQ_FOREACH(sub, client->subscriptions, entry) {
+				if (strcmp(sub->thing_name, thing->name) == 0)
 					bufferevent_write(client->bev, pkt,
 					    len);
 			}
blob - 8e92284b388844e6c3ffaa7a7efd8eaba58efb67
blob + 7fa8b55aa3c8ddfa8e2c2fdfd131757bfe3fed4c
--- thingsd/parse.y	
+++ thingsd/parse.y	
@@ -90,7 +90,8 @@ void	 clear_config(struct thingsd *xconf);
 
 static int		 errors;
 
-static struct thing	*new_thing;
+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,
@@ -416,9 +417,9 @@ subopts		: {
 		;
 
 subthings	: THING '{' STRING optcomma STRING '}' optcomma {
-			struct thing	*thing;
-			bool		 fail = false;
-			size_t		 n;
+			struct thing		*thing;
+			struct subscription	*sub, *nsub;
+			bool			 fail = false;
 
 			/* check for duplicate name and subscriptions */
 			TAILQ_FOREACH(client, thingsd_env->clients, entry) {
@@ -430,49 +431,52 @@ subthings	: THING '{' STRING optcomma STRING '}' optco
 						fail = true;
 						log_warnx("client exists");
 					}
-					for (n = 0; n < client->le; n++)
-						if (strcmp(client->sub_names[n],
+					TAILQ_FOREACH(sub,
+					    client->subscriptions, entry)
+						if (strcmp(sub->thing_name,
 						    $3) == 0)
 							fail = true;
 					break;
 				}
 			}
-			/*
-			 * fail from previous tests
-			 * check correct port
-			 * test max subscriptions
-			 * subscribe
-			 */
+
+			if (fail)
+				goto done;
+
 			TAILQ_FOREACH(thing, thingsd_env->things, entry) {
-				if (fail)
+				if (thing->port != client->port) {
+					fail = true;
 					continue;
-				if (thing->port != client->port)
-					continue;
-				if (strcmp(thing->name, $3) == 0)
-					if (strcmp(thing->password, $5) == 0) {
-						if (client->subs++ >=
-						    thingsd_env->max_subs) {
-						    	log_warn("max "
-							    "subscriptions "
-							    "reached");
-							continue;
-						}
-						client->subscribed = true;
-						n = strlcpy(client->
-						    sub_names[client->le],
-						    thing->name,
-						    sizeof(client->sub_names));
-						client->le++;
-						thing->client_cnt++;
-						log_info("client %s subscribed "
-						    "to %s", client->name,
-						    thing->name);
-						continue;
-					}
+				}
+				if (strcmp(thing->name, $3) == 0) {
+					fail = false;
+					break;
+				}
 			}
+
+			if (fail)
+				goto done;
+
+			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);
+				}
+			}
+
+done:
 			free($3);
 			free($5);
-		};
+		}
+		;
 
 subthings2	: subthings2 subthings nl
 		| subthings optnl
@@ -1295,6 +1299,23 @@ parse_buf(struct client *pclient, u_char *pkt, int len
 	return (errors ? -1 : 0);
 }
 
+struct subscription *
+new_sub(char *name)
+{
+	struct subscription	*sub;
+	size_t			 n;
+
+	sub = calloc(1, sizeof(*sub));
+	if (sub == NULL)
+		fatal("%s: calloc", __func__);
+	memset(&sub->thing_name, 0, sizeof(sub->thing_name));
+	n = strlcpy(sub->thing_name, name, sizeof(sub->thing_name));
+	if (n >= sizeof(sub->thing_name))
+		fatalx("%s: sub->thing_name too long", __func__);
+
+	return (sub);
+}
+
 struct thing *
 conf_new_thing(char *name)
 {
blob - a90f5d61ffeaaabfe7202785f126b406c842fc87
blob + e598a18fa02a0a644f47076f5377462f5c7f48ef
--- thingsd/sockets.c	
+++ thingsd/sockets.c	
@@ -192,7 +192,7 @@ recreate:
 			    client_conn, env);
 			if (event_add(sock->ev, NULL))
 				fatalx("event add sock");
-			evtimer_set(&env->pause, client_accept_paused, env);
+			evtimer_set(&sock->pause, client_accept_paused, sock);
 		}
 
 		if (conn_socket != NULL && conn_socket->fd > 0) {
@@ -419,9 +419,9 @@ socket_rd(struct bufferevent *bev, void *arg)
 	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;
-	size_t			 n;
 	char			*pkt = NULL;
 
 	TAILQ_FOREACH(tthing, env->things, entry) {
@@ -437,10 +437,10 @@ socket_rd(struct bufferevent *bev, void *arg)
 				return;
 
 			evbuffer_remove(thing->evb, pkt, len);
-
 			TAILQ_FOREACH(client, env->clients, entry) {
-				for (n = 0; n < client->le; n++) {
-					if (strcmp(client->sub_names[n],
+				TAILQ_FOREACH(sub, client->subscriptions,
+				    entry) {
+					if (strcmp(sub->thing_name,
 					    thing->name) == 0)
 						bufferevent_write(client->bev,
 						    pkt, len);
blob - e06b255c5ae89652e46c9d97a1dcd62384fa61d0
blob + 5cf3b4bd8a5c88ab5b676ce8653427b23f705dec
--- thingsd/things.c	
+++ thingsd/things.c	
@@ -257,6 +257,7 @@ things_shutdown(void)
 	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,
@@ -295,8 +296,9 @@ things_shutdown(void)
 	/* clean up clients */
 	TAILQ_FOREACH_SAFE(client, thingsd_env->clients, entry, tclient) {
 		close(client->fd);
-		free(*client->sub_names);
-		*client->sub_names = NULL;
+		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);
 	}
blob - 4cf6eaa079ab6e1fcbca96ee38222cb389276c4d
blob + 34c01e279ea7157a00dea4f3a4a9eadd3b47efe7
--- thingsd/thingsd.h	
+++ thingsd/thingsd.h	
@@ -100,15 +100,21 @@ struct thingsd_parent_info {
 	char		 text[THINGSD_MAXTEXT];
 };
 
+struct subscription {
+	TAILQ_ENTRY(subscription)	 entry;
+	char				 thing_name[THINGSD_MAXNAME];
+};
+TAILQ_HEAD(subscriptionlist, subscription);
+
 struct client {
 	TAILQ_ENTRY(client)	 entry;
+	struct subscriptionlist	*subscriptions;
 	struct evbuffer		*evb;
 	struct event		*ev;
 	struct bufferevent	*bev;
 	struct socket		*socket;
 	bool			 subscribed;
 	char			 name[THINGSD_MAXNAME];
-	char			*sub_names[THINGSD_MAXTEXT];
 	int			 fd;
 	int			 port;
 	time_t			 join_time;
@@ -123,6 +129,7 @@ TAILQ_HEAD(clientlist, client);
 struct socket {
 	TAILQ_ENTRY(socket)	 entry;
 	struct event		*ev;
+	struct event		 pause;
 	char			 name[THINGSD_MAXNAME];
 	int			 fd;
 	int			 port;
@@ -250,10 +257,6 @@ struct thingsd {
 	/* control packets */
 	struct packetclientlist	*packet_clients;
 	int			 packet_client_count;
-
-	/* client accept */
-	struct event		*ev;
-	struct event		 pause;
 };
 
 extern struct thingsd	*thingsd_env;