xref: /freebsd/tests/sys/netgraph/util.c (revision 9021c46603bf29b9700f24b8dce8796b434d7c8f)
124ea1dbfSLutz Donnerhacke /*
224ea1dbfSLutz Donnerhacke  * SPDX-License-Identifier: BSD-3-Clause
324ea1dbfSLutz Donnerhacke  *
424ea1dbfSLutz Donnerhacke  * Copyright 2021 Lutz Donnerhacke
524ea1dbfSLutz Donnerhacke  *
624ea1dbfSLutz Donnerhacke  * Redistribution and use in source and binary forms, with or without
724ea1dbfSLutz Donnerhacke  * modification, are permitted provided that the following conditions
824ea1dbfSLutz Donnerhacke  * are met:
924ea1dbfSLutz Donnerhacke  *
1024ea1dbfSLutz Donnerhacke  * 1. Redistributions of source code must retain the above copyright
1124ea1dbfSLutz Donnerhacke  *    notice, this list of conditions and the following disclaimer.
1224ea1dbfSLutz Donnerhacke  * 2. Redistributions in binary form must reproduce the above
1324ea1dbfSLutz Donnerhacke  *    copyright notice, this list of conditions and the following
1424ea1dbfSLutz Donnerhacke  *    disclaimer in the documentation and/or other materials provided
1524ea1dbfSLutz Donnerhacke  *    with the distribution.
1624ea1dbfSLutz Donnerhacke  * 3. Neither the name of the copyright holder nor the names of its
1724ea1dbfSLutz Donnerhacke  *    contributors may be used to endorse or promote products derived
1824ea1dbfSLutz Donnerhacke  *    from this software without specific prior written permission.
1924ea1dbfSLutz Donnerhacke  *
2024ea1dbfSLutz Donnerhacke  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
2124ea1dbfSLutz Donnerhacke  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
2224ea1dbfSLutz Donnerhacke  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2324ea1dbfSLutz Donnerhacke  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2424ea1dbfSLutz Donnerhacke  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
2524ea1dbfSLutz Donnerhacke  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2624ea1dbfSLutz Donnerhacke  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2724ea1dbfSLutz Donnerhacke  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2824ea1dbfSLutz Donnerhacke  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2924ea1dbfSLutz Donnerhacke  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
3024ea1dbfSLutz Donnerhacke  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3124ea1dbfSLutz Donnerhacke  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3224ea1dbfSLutz Donnerhacke  * SUCH DAMAGE.
3324ea1dbfSLutz Donnerhacke  */
3424ea1dbfSLutz Donnerhacke #include <atf-c.h>
3524ea1dbfSLutz Donnerhacke #include <errno.h>
3624ea1dbfSLutz Donnerhacke #include <stdlib.h>
3724ea1dbfSLutz Donnerhacke #include <string.h>
3824ea1dbfSLutz Donnerhacke 
3924ea1dbfSLutz Donnerhacke #include <sys/select.h>
4024ea1dbfSLutz Donnerhacke #include <sys/queue.h>
4124ea1dbfSLutz Donnerhacke 
4224ea1dbfSLutz Donnerhacke #include "util.h"
4324ea1dbfSLutz Donnerhacke 
4424ea1dbfSLutz Donnerhacke 
4524ea1dbfSLutz Donnerhacke static int cs = -1, ds = -1;
4624ea1dbfSLutz Donnerhacke static ng_error_t error_handling = FAIL;
4724ea1dbfSLutz Donnerhacke 
4824ea1dbfSLutz Donnerhacke #define CHECK(r, x)	do {			\
49*9021c466SLutz Donnerhacke 	if (!(x)) {				\
50*9021c466SLutz Donnerhacke 		if (error_handling == PASS)	\
5124ea1dbfSLutz Donnerhacke 		    return r;			\
52*9021c466SLutz Donnerhacke 		atf_tc_fail_requirement(file, line, "%s (%s)", \
53*9021c466SLutz Donnerhacke 		    #x " not met", strerror(errno));\
54*9021c466SLutz Donnerhacke 	}					\
5524ea1dbfSLutz Donnerhacke } while(0)
5624ea1dbfSLutz Donnerhacke 
5724ea1dbfSLutz Donnerhacke struct data_handler {
5824ea1dbfSLutz Donnerhacke 	char const *hook;
5924ea1dbfSLutz Donnerhacke 	ng_data_handler_t handler;
6024ea1dbfSLutz Donnerhacke 	SLIST_ENTRY(data_handler) next;
6124ea1dbfSLutz Donnerhacke };
6224ea1dbfSLutz Donnerhacke static SLIST_HEAD(, data_handler) data_head = SLIST_HEAD_INITIALIZER(data_head);
6309307dbfSLutz Donnerhacke static ng_msg_handler_t msg_handler = NULL;
6424ea1dbfSLutz Donnerhacke 
6524ea1dbfSLutz Donnerhacke static void handle_data(void *ctx);
6609307dbfSLutz Donnerhacke static void handle_msg(void *ctx);
6724ea1dbfSLutz Donnerhacke 
6824ea1dbfSLutz Donnerhacke void
69*9021c466SLutz Donnerhacke _ng_connect(char const *path1, char const *hook1,
70*9021c466SLutz Donnerhacke 	    char const *path2, char const *hook2,
71*9021c466SLutz Donnerhacke 	    char const *file, size_t line)
7224ea1dbfSLutz Donnerhacke {
7324ea1dbfSLutz Donnerhacke 	struct ngm_connect c;
7424ea1dbfSLutz Donnerhacke 
7524ea1dbfSLutz Donnerhacke 	strncpy(c.ourhook,  hook1, sizeof(c.ourhook));
7624ea1dbfSLutz Donnerhacke 	strncpy(c.peerhook, hook2, sizeof(c.peerhook));
7724ea1dbfSLutz Donnerhacke 	strncpy(c.path,     path2, sizeof(c.path));
7824ea1dbfSLutz Donnerhacke 
7924ea1dbfSLutz Donnerhacke 	CHECK(, -1 != NgSendMsg(cs, path1,
8024ea1dbfSLutz Donnerhacke 	    NGM_GENERIC_COOKIE, NGM_CONNECT,
8124ea1dbfSLutz Donnerhacke 	    &c, sizeof(c)));
8224ea1dbfSLutz Donnerhacke }
8324ea1dbfSLutz Donnerhacke 
8424ea1dbfSLutz Donnerhacke void
85*9021c466SLutz Donnerhacke _ng_mkpeer(char const *path1, char const *hook1,
86*9021c466SLutz Donnerhacke 	   char const *type,  char const *hook2,
87*9021c466SLutz Donnerhacke 	   char const *file, size_t line)
8824ea1dbfSLutz Donnerhacke {
8924ea1dbfSLutz Donnerhacke 	struct ngm_mkpeer p;
9024ea1dbfSLutz Donnerhacke 
9124ea1dbfSLutz Donnerhacke 	strncpy(p.ourhook,  hook1, sizeof(p.ourhook));
9224ea1dbfSLutz Donnerhacke 	strncpy(p.peerhook, hook2, sizeof(p.peerhook));
9324ea1dbfSLutz Donnerhacke 	strncpy(p.type,     type,  sizeof(p.type));
9424ea1dbfSLutz Donnerhacke 
9524ea1dbfSLutz Donnerhacke 	CHECK(, -1 != NgSendMsg(cs, path1,
9624ea1dbfSLutz Donnerhacke 	    NGM_GENERIC_COOKIE, NGM_MKPEER,
9724ea1dbfSLutz Donnerhacke 	    &p, sizeof(p)));
9824ea1dbfSLutz Donnerhacke }
9924ea1dbfSLutz Donnerhacke 
10024ea1dbfSLutz Donnerhacke void
101*9021c466SLutz Donnerhacke _ng_rmhook(char const *path, char const *hook,
102*9021c466SLutz Donnerhacke 	   char const *file, size_t line)
10324ea1dbfSLutz Donnerhacke {
10424ea1dbfSLutz Donnerhacke 	struct ngm_rmhook h;
10524ea1dbfSLutz Donnerhacke 
10624ea1dbfSLutz Donnerhacke 	strncpy(h.ourhook, hook, sizeof(h.ourhook));
10724ea1dbfSLutz Donnerhacke 
10824ea1dbfSLutz Donnerhacke 	CHECK(, -1 != NgSendMsg(cs, path,
10924ea1dbfSLutz Donnerhacke 	    NGM_GENERIC_COOKIE, NGM_RMHOOK,
11024ea1dbfSLutz Donnerhacke 	    &h, sizeof(h)));
11124ea1dbfSLutz Donnerhacke }
11224ea1dbfSLutz Donnerhacke 
11324ea1dbfSLutz Donnerhacke void
114*9021c466SLutz Donnerhacke _ng_name(char const *path, char const *name,
115*9021c466SLutz Donnerhacke 	 char const *file, size_t line)
11624ea1dbfSLutz Donnerhacke {
11724ea1dbfSLutz Donnerhacke 	struct ngm_name n;
11824ea1dbfSLutz Donnerhacke 
11924ea1dbfSLutz Donnerhacke 	strncpy(n.name, name, sizeof(n.name));
12024ea1dbfSLutz Donnerhacke 
12124ea1dbfSLutz Donnerhacke 	CHECK(, -1 != NgSendMsg(cs, path,
12224ea1dbfSLutz Donnerhacke 	    NGM_GENERIC_COOKIE, NGM_NAME,
12324ea1dbfSLutz Donnerhacke 	    &n, sizeof(n)));
12424ea1dbfSLutz Donnerhacke }
12524ea1dbfSLutz Donnerhacke 
12624ea1dbfSLutz Donnerhacke void
127*9021c466SLutz Donnerhacke _ng_shutdown(char const *path,
128*9021c466SLutz Donnerhacke 	     char const *file, size_t line)
12924ea1dbfSLutz Donnerhacke {
13024ea1dbfSLutz Donnerhacke 	CHECK(, -1 != NgSendMsg(cs, path,
13124ea1dbfSLutz Donnerhacke 	    NGM_GENERIC_COOKIE, NGM_SHUTDOWN,
13224ea1dbfSLutz Donnerhacke 	    NULL, 0));
13324ea1dbfSLutz Donnerhacke }
13424ea1dbfSLutz Donnerhacke 
13524ea1dbfSLutz Donnerhacke void
13624ea1dbfSLutz Donnerhacke ng_register_data(char const *hook, ng_data_handler_t proc)
13724ea1dbfSLutz Donnerhacke {
13824ea1dbfSLutz Donnerhacke 	struct data_handler *p;
13924ea1dbfSLutz Donnerhacke 
14024ea1dbfSLutz Donnerhacke 	ATF_REQUIRE(NULL != (p = calloc(1, sizeof(struct data_handler))));
14124ea1dbfSLutz Donnerhacke 	ATF_REQUIRE(NULL != (p->hook = strdup(hook)));
14224ea1dbfSLutz Donnerhacke 	ATF_REQUIRE(NULL != (p->handler = proc));
14324ea1dbfSLutz Donnerhacke 	SLIST_INSERT_HEAD(&data_head, p, next);
14424ea1dbfSLutz Donnerhacke }
14524ea1dbfSLutz Donnerhacke 
14624ea1dbfSLutz Donnerhacke void
147*9021c466SLutz Donnerhacke _ng_send_data(char const *hook,
148*9021c466SLutz Donnerhacke 	      void const *data, size_t len,
149*9021c466SLutz Donnerhacke 	      char const *file, size_t line)
15024ea1dbfSLutz Donnerhacke {
15124ea1dbfSLutz Donnerhacke 	CHECK(, -1 != NgSendData(ds, hook, data, len));
15224ea1dbfSLutz Donnerhacke }
15324ea1dbfSLutz Donnerhacke 
15409307dbfSLutz Donnerhacke void
15509307dbfSLutz Donnerhacke ng_register_msg(ng_msg_handler_t proc) {
15609307dbfSLutz Donnerhacke 	msg_handler = proc;
15709307dbfSLutz Donnerhacke }
15809307dbfSLutz Donnerhacke 
15924ea1dbfSLutz Donnerhacke static void
16009307dbfSLutz Donnerhacke handle_msg(void *ctx) {
16124ea1dbfSLutz Donnerhacke 	struct ng_mesg *m;
16224ea1dbfSLutz Donnerhacke 	char path[NG_PATHSIZ];
16324ea1dbfSLutz Donnerhacke 
16424ea1dbfSLutz Donnerhacke 	ATF_REQUIRE(-1 != NgAllocRecvMsg(cs, &m, path));
16524ea1dbfSLutz Donnerhacke 
16609307dbfSLutz Donnerhacke 	if(msg_handler != NULL)
16709307dbfSLutz Donnerhacke 		(*msg_handler)(path, m, ctx);
16809307dbfSLutz Donnerhacke 
16924ea1dbfSLutz Donnerhacke 	free(m);
17024ea1dbfSLutz Donnerhacke }
17124ea1dbfSLutz Donnerhacke 
17224ea1dbfSLutz Donnerhacke static void
17324ea1dbfSLutz Donnerhacke handle_data(void *ctx) {
17424ea1dbfSLutz Donnerhacke 	char hook[NG_HOOKSIZ];
17524ea1dbfSLutz Donnerhacke 	struct data_handler *hnd;
17624ea1dbfSLutz Donnerhacke 	u_char *data;
17724ea1dbfSLutz Donnerhacke 	int len;
17824ea1dbfSLutz Donnerhacke 
17924ea1dbfSLutz Donnerhacke 	ATF_REQUIRE(0 < (len = NgAllocRecvData(ds, &data, hook)));
18024ea1dbfSLutz Donnerhacke 	SLIST_FOREACH(hnd, &data_head, next)
18124ea1dbfSLutz Donnerhacke 		if (0 == strcmp(hnd->hook, hook))
18224ea1dbfSLutz Donnerhacke 			break;
18324ea1dbfSLutz Donnerhacke 
18424ea1dbfSLutz Donnerhacke 	if (hnd != NULL)
18524ea1dbfSLutz Donnerhacke 		(*(hnd->handler))(data, len, ctx);
18624ea1dbfSLutz Donnerhacke 
18724ea1dbfSLutz Donnerhacke 	free(data);
18824ea1dbfSLutz Donnerhacke }
18924ea1dbfSLutz Donnerhacke 
19024ea1dbfSLutz Donnerhacke int
19124ea1dbfSLutz Donnerhacke ng_handle_event(unsigned int ms, void *context)
19224ea1dbfSLutz Donnerhacke {
19324ea1dbfSLutz Donnerhacke 	fd_set fds;
19424ea1dbfSLutz Donnerhacke 	int maxfd = (ds < cs) ? cs : ds;
19524ea1dbfSLutz Donnerhacke 	struct timeval timeout = { 0, ms * 1000lu };
19624ea1dbfSLutz Donnerhacke 
19724ea1dbfSLutz Donnerhacke 	FD_ZERO(&fds);
19824ea1dbfSLutz Donnerhacke 	FD_SET(cs, &fds);
19924ea1dbfSLutz Donnerhacke 	FD_SET(ds, &fds);
20024ea1dbfSLutz Donnerhacke retry:
20124ea1dbfSLutz Donnerhacke 	switch (select(maxfd+1, &fds, NULL, NULL, &timeout)) {
20224ea1dbfSLutz Donnerhacke 	case -1:
20324ea1dbfSLutz Donnerhacke 		ATF_REQUIRE_ERRNO(EINTR, 1);
20424ea1dbfSLutz Donnerhacke 		goto retry;
20524ea1dbfSLutz Donnerhacke 	case 0:			       /* timeout */
20624ea1dbfSLutz Donnerhacke 		return 0;
20724ea1dbfSLutz Donnerhacke 	default:		       /* something to do */
20824ea1dbfSLutz Donnerhacke 		if (FD_ISSET(cs, &fds))
20909307dbfSLutz Donnerhacke 		    handle_msg(context);
21024ea1dbfSLutz Donnerhacke 		if (FD_ISSET(ds, &fds))
21124ea1dbfSLutz Donnerhacke 		    handle_data(context);
21224ea1dbfSLutz Donnerhacke 		return 1;
21324ea1dbfSLutz Donnerhacke 	}
21424ea1dbfSLutz Donnerhacke }
21524ea1dbfSLutz Donnerhacke 
21624ea1dbfSLutz Donnerhacke void
21724ea1dbfSLutz Donnerhacke ng_handle_events(unsigned int ms, void *context)
21824ea1dbfSLutz Donnerhacke {
21924ea1dbfSLutz Donnerhacke 	while(ng_handle_event(ms, context))
22024ea1dbfSLutz Donnerhacke 		;
22124ea1dbfSLutz Donnerhacke }
22224ea1dbfSLutz Donnerhacke 
22324ea1dbfSLutz Donnerhacke int
224*9021c466SLutz Donnerhacke _ng_send_msg(char const *path, char const *msg,
225*9021c466SLutz Donnerhacke 	     char const *file, size_t line)
22624ea1dbfSLutz Donnerhacke {
22724ea1dbfSLutz Donnerhacke 	int res;
22824ea1dbfSLutz Donnerhacke 
22924ea1dbfSLutz Donnerhacke 	CHECK(-1, -1 != (res = NgSendAsciiMsg(cs, path, "%s", msg)));
23024ea1dbfSLutz Donnerhacke 	return (res);
23124ea1dbfSLutz Donnerhacke }
23224ea1dbfSLutz Donnerhacke 
23324ea1dbfSLutz Donnerhacke ng_error_t
23424ea1dbfSLutz Donnerhacke ng_errors(ng_error_t n)
23524ea1dbfSLutz Donnerhacke {
23624ea1dbfSLutz Donnerhacke 	ng_error_t o = error_handling;
23724ea1dbfSLutz Donnerhacke 
23824ea1dbfSLutz Donnerhacke 	error_handling = n;
23924ea1dbfSLutz Donnerhacke 	return (o);
24024ea1dbfSLutz Donnerhacke }
24124ea1dbfSLutz Donnerhacke 
24224ea1dbfSLutz Donnerhacke void
243*9021c466SLutz Donnerhacke _ng_init(char const *file, size_t line) {
24424ea1dbfSLutz Donnerhacke 	if (cs >= 0)		       /* prevent reinit */
24524ea1dbfSLutz Donnerhacke 		return;
24624ea1dbfSLutz Donnerhacke 
247*9021c466SLutz Donnerhacke 	CHECK(, 0 == NgMkSockNode(NULL, &cs, &ds));
24824ea1dbfSLutz Donnerhacke 	NgSetDebug(3);
24924ea1dbfSLutz Donnerhacke }
250