1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright 2021 Lutz Donnerhacke
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34 #include <atf-c.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <sys/select.h>
40 #include <sys/queue.h>
41
42 #include "util.h"
43
44
45 static int cs = -1, ds = -1;
46 static ng_error_t error_handling = FAIL;
47
48 #define CHECK(r, x) do { \
49 if (!(x)) { \
50 if (error_handling == PASS) \
51 return r; \
52 atf_tc_fail_requirement(file, line, "%s (%s)", \
53 #x " not met", strerror(errno));\
54 } \
55 } while(0)
56
57 struct data_handler
58 {
59 char const *hook;
60 ng_data_handler_t handler;
61 SLIST_ENTRY(data_handler) next;
62 };
63 static SLIST_HEAD(, data_handler) data_head = SLIST_HEAD_INITIALIZER(data_head);
64 static ng_msg_handler_t msg_handler = NULL;
65
66 static void handle_data(void *ctx);
67 static void handle_msg(void *ctx);
68
69 void
_ng_connect(char const * path1,char const * hook1,char const * path2,char const * hook2,char const * file,size_t line)70 _ng_connect(char const *path1, char const *hook1,
71 char const *path2, char const *hook2,
72 char const *file, size_t line)
73 {
74 struct ngm_connect c;
75
76 strncpy(c.ourhook, hook1, sizeof(c.ourhook));
77 strncpy(c.peerhook, hook2, sizeof(c.peerhook));
78 strncpy(c.path, path2, sizeof(c.path));
79
80 CHECK(, -1 != NgSendMsg(cs, path1,
81 NGM_GENERIC_COOKIE, NGM_CONNECT,
82 &c, sizeof(c)));
83 }
84
85 void
_ng_mkpeer(char const * path1,char const * hook1,char const * type,char const * hook2,char const * file,size_t line)86 _ng_mkpeer(char const *path1, char const *hook1,
87 char const *type, char const *hook2,
88 char const *file, size_t line)
89 {
90 struct ngm_mkpeer p;
91
92 strncpy(p.ourhook, hook1, sizeof(p.ourhook));
93 strncpy(p.peerhook, hook2, sizeof(p.peerhook));
94 strncpy(p.type, type, sizeof(p.type));
95
96 CHECK(, -1 != NgSendMsg(cs, path1,
97 NGM_GENERIC_COOKIE, NGM_MKPEER,
98 &p, sizeof(p)));
99 }
100
101 void
_ng_rmhook(char const * path,char const * hook,char const * file,size_t line)102 _ng_rmhook(char const *path, char const *hook,
103 char const *file, size_t line)
104 {
105 struct ngm_rmhook h;
106
107 strncpy(h.ourhook, hook, sizeof(h.ourhook));
108
109 CHECK(, -1 != NgSendMsg(cs, path,
110 NGM_GENERIC_COOKIE, NGM_RMHOOK,
111 &h, sizeof(h)));
112 }
113
114 void
_ng_name(char const * path,char const * name,char const * file,size_t line)115 _ng_name(char const *path, char const *name,
116 char const *file, size_t line)
117 {
118 struct ngm_name n;
119
120 strncpy(n.name, name, sizeof(n.name));
121
122 CHECK(, -1 != NgSendMsg(cs, path,
123 NGM_GENERIC_COOKIE, NGM_NAME,
124 &n, sizeof(n)));
125 }
126
127 void
_ng_shutdown(char const * path,char const * file,size_t line)128 _ng_shutdown(char const *path,
129 char const *file, size_t line)
130 {
131 CHECK(, -1 != NgSendMsg(cs, path,
132 NGM_GENERIC_COOKIE, NGM_SHUTDOWN,
133 NULL, 0));
134 }
135
136 void
ng_register_data(char const * hook,ng_data_handler_t proc)137 ng_register_data(char const *hook, ng_data_handler_t proc)
138 {
139 struct data_handler *p;
140
141 ATF_REQUIRE(NULL != (p = calloc(1, sizeof(struct data_handler))));
142 ATF_REQUIRE(NULL != (p->hook = strdup(hook)));
143 ATF_REQUIRE(NULL != (p->handler = proc));
144 SLIST_INSERT_HEAD(&data_head, p, next);
145 }
146
147 void
_ng_send_data(char const * hook,void const * data,size_t len,char const * file,size_t line)148 _ng_send_data(char const *hook,
149 void const *data, size_t len,
150 char const *file, size_t line)
151 {
152 CHECK(, -1 != NgSendData(ds, hook, data, len));
153 }
154
155 void
ng_register_msg(ng_msg_handler_t proc)156 ng_register_msg(ng_msg_handler_t proc)
157 {
158 msg_handler = proc;
159 }
160
161 static void
handle_msg(void * ctx)162 handle_msg(void *ctx)
163 {
164 struct ng_mesg *m;
165 char path[NG_PATHSIZ];
166
167 ATF_REQUIRE(-1 != NgAllocRecvMsg(cs, &m, path));
168
169 if (msg_handler != NULL)
170 (*msg_handler) (path, m, ctx);
171
172 free(m);
173 }
174
175 static void
handle_data(void * ctx)176 handle_data(void *ctx)
177 {
178 char hook[NG_HOOKSIZ];
179 struct data_handler *hnd;
180 u_char *data;
181 int len;
182
183 ATF_REQUIRE(0 < (len = NgAllocRecvData(ds, &data, hook)));
184 SLIST_FOREACH(hnd, &data_head, next)
185 {
186 if (0 == strcmp(hnd->hook, hook))
187 break;
188 }
189
190 if (hnd != NULL)
191 (*(hnd->handler)) (data, len, ctx);
192
193 free(data);
194 }
195
196 int
ng_handle_event(unsigned int ms,void * context)197 ng_handle_event(unsigned int ms, void *context)
198 {
199 fd_set fds;
200 int maxfd = (ds < cs) ? cs : ds;
201 struct timeval timeout = {0, ms * 1000lu};
202
203 FD_ZERO(&fds);
204 FD_SET(cs, &fds);
205 FD_SET(ds, &fds);
206 retry:
207 switch (select(maxfd + 1, &fds, NULL, NULL, &timeout))
208 {
209 case -1:
210 ATF_REQUIRE_ERRNO(EINTR, 1);
211 goto retry;
212 case 0: /* timeout */
213 return 0;
214 default: /* something to do */
215 if (FD_ISSET(cs, &fds))
216 handle_msg(context);
217 if (FD_ISSET(ds, &fds))
218 handle_data(context);
219 return 1;
220 }
221 }
222
223 void
ng_handle_events(unsigned int ms,void * context)224 ng_handle_events(unsigned int ms, void *context)
225 {
226 while (ng_handle_event(ms, context))
227 ;
228 }
229
230 int
_ng_send_msg(char const * path,char const * msg,char const * file,size_t line)231 _ng_send_msg(char const *path, char const *msg,
232 char const *file, size_t line)
233 {
234 int res;
235
236 CHECK(-1, -1 != (res = NgSendAsciiMsg(cs, path, "%s", msg)));
237 return (res);
238 }
239
240 ng_error_t
ng_errors(ng_error_t n)241 ng_errors(ng_error_t n)
242 {
243 ng_error_t o = error_handling;
244
245 error_handling = n;
246 return (o);
247 }
248
249 void
_ng_init(char const * file,size_t line)250 _ng_init(char const *file, size_t line)
251 {
252 if (cs >= 0) /* prevent reinit */
253 return;
254
255 CHECK(, 0 == NgMkSockNode(NULL, &cs, &ds));
256 NgSetDebug(3);
257 }
258
259 #define GD(x) void \
260 get_data##x(void *data, size_t len, void *ctx) {\
261 int *cnt = ctx; \
262 \
263 (void)data; \
264 (void)len; \
265 cnt[x]++; \
266 }
267
268 GD(0)
269 GD(1)
270 GD(2)
271 GD(3)
272 GD(4)
273 GD(5)
274 GD(6)
275 GD(7)
276 GD(8)
277 GD(9)
278