util.h revision 370006
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
35#include <netgraph.h>
36
37void
38_ng_connect(char const *path1, char const *hook1,
39	    char const *path2, char const *hook2,
40	    char const *file, size_t line);
41#define ng_connect(p1,h1,p2,h2)	\
42   _ng_connect(p1,h1,p2,h2,__FILE__,__LINE__)
43
44void
45_ng_mkpeer(char const *path1, char const *hook1,
46	   char const *type, char const *hook2,
47	   char const *file, size_t line);
48#define ng_mkpeer(p1,h1,t,h2)	\
49   _ng_mkpeer(p1,h1,t,h2,__FILE__,__LINE__)
50
51void
52_ng_shutdown(char const *path,
53	     char const *file, size_t line);
54#define ng_shutdown(p)	\
55   _ng_shutdown(p,__FILE__,__LINE__)
56
57void
58_ng_rmhook(char const *path, char const *hook,
59	   char const *file, size_t line);
60#define ng_rmhook(p,h)	\
61   _ng_rmhook(p,h,__FILE__,__LINE__)
62
63void
64_ng_name(char const *path, char const *name,
65	 char const *file, size_t line);
66#define ng_name(p,n)	\
67   _ng_name(p,n,__FILE__,__LINE__)
68
69
70typedef void (*ng_data_handler_t)(void *, size_t, void *ctx);
71void		ng_register_data(char const *hook, ng_data_handler_t proc);
72void
73_ng_send_data(char const *hook, void const *, size_t,
74	      char const *file, size_t line);
75#define ng_send_data(h,d,l)	\
76   _ng_send_data(h,d,l,__FILE__,__LINE__)
77
78typedef void (*ng_msg_handler_t)(char const *, struct ng_mesg *, void *);
79void		ng_register_msg(ng_msg_handler_t proc);
80int
81_ng_send_msg(char const *path, char const *msg,
82	     char const *file, size_t line);
83#define ng_send_msg(p,m)	\
84   _ng_send_msg(p,m,__FILE__,__LINE__)
85
86int		ng_handle_event(unsigned int ms, void *ctx);
87void		ng_handle_events(unsigned int ms, void *ctx);
88
89typedef enum
90{
91	FAIL, PASS
92} ng_error_t;
93ng_error_t	ng_errors(ng_error_t);
94
95void		_ng_init(char const *file, size_t line);
96#define ng_init()	\
97   _ng_init(__FILE__,__LINE__)
98
99/* Helper function to count received data */
100
101typedef int ng_counter_t[10];
102#define ng_counter_clear(x)\
103   bzero((x), sizeof(x))
104
105void		get_data0(void *data, size_t len, void *ctx);
106void		get_data1(void *data, size_t len, void *ctx);
107void		get_data2(void *data, size_t len, void *ctx);
108void		get_data3(void *data, size_t len, void *ctx);
109void		get_data4(void *data, size_t len, void *ctx);
110void		get_data5(void *data, size_t len, void *ctx);
111void		get_data6(void *data, size_t len, void *ctx);
112void		get_data7(void *data, size_t len, void *ctx);
113void		get_data8(void *data, size_t len, void *ctx);
114void		get_data9(void *data, size_t len, void *ctx);
115