xref: /freebsd/lib/libnetgraph/netgraph.3 (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1.\" Copyright (c) 1996-1999 Whistle Communications, Inc.
2.\" All rights reserved.
3.\"
4.\" Subject to the following obligations and disclaimer of warranty, use and
5.\" redistribution of this software, in source or object code forms, with or
6.\" without modifications are expressly permitted by Whistle Communications;
7.\" provided, however, that:
8.\" 1. Any and all reproductions of the source or object code must include the
9.\"    copyright notice above and the following disclaimer of warranties; and
10.\" 2. No rights are granted, in any manner or form, to use Whistle
11.\"    Communications, Inc. trademarks, including the mark "WHISTLE
12.\"    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
13.\"    such appears in the above copyright notice or in the software.
14.\"
15.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
16.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
17.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
18.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
19.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
20.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
21.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
22.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
23.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
24.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
25.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
27.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
28.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
31.\" OF SUCH DAMAGE.
32.\"
33.\" Author: Archie Cobbs <archie@whistle.com>
34.\"
35.\" $FreeBSD$
36.\" $Whistle: netgraph.3,v 1.7 1999/01/25 07:14:06 archie Exp $
37.\"
38.Dd January 19, 1999
39.Dt NETGRAPH 3
40.Os FreeBSD
41.Sh NAME
42.Nm NgMkSockNode ,
43.Nm NgNameNode ,
44.Nm NgSendMsg ,
45.Nm NgRecvMsg ,
46.Nm NgSendData ,
47.Nm NgRecvData ,
48.Nm NgSetDebug ,
49.Nm NgSetErrLog
50.Nd netgraph user library
51.Sh LIBRARY
52.Lb libnetgraph
53.Sh SYNOPSIS
54.Fd #include <netgraph.h>
55.Ft int
56.Fn NgMkSockNode "const char *name" "int *csp" "int *dsp"
57.Ft int
58.Fn NgNameNode "int cs" "const char *path" "const char *fmt" "..."
59.Ft int
60.Fn NgSendMsg "int cs" "const char *path" "int cookie" "int cmd" "const void *arg" "size_t arglen"
61.Ft int
62.Fn NgSendAsciiMsg "int cs" "const char *path" "const char *fmt" "..."
63.Ft int
64.Fn NgSendMsgReply "int cs" "const char *path" "struct ng_mesg *msg" "const void *arg" "size_t arglen"
65.Ft int
66.Fn NgRecvMsg "int cs" "struct ng_mesg *rep" "size_t replen" "char *path"
67.Ft int
68.Fn NgRecvAsciiMsg "int cs" "struct ng_mesg *rep" "size_t replen" "char *path"
69.Ft int
70.Fn NgSendData "int ds" "const char *hook" "const u_char *buf" "size_t len"
71.Ft int
72.Fn NgRecvData "int ds" "u_char *buf" "size_t len" "char *hook"
73.Ft int
74.Fn NgSetDebug "int level"
75.Ft void
76.Fn NgSetErrLog "void (*log)(const char *fmt, ...)" "void (*logx)(const char *fmt, ...)"
77.Sh DESCRIPTION
78These functions facilitate user-mode program participation in the kernel
79.Xr netgraph 4
80graph-based networking system, by utilizing the netgraph
81.Em socket
82node type (see
83.Xr ng_socket 4 ) .
84.Pp
85.Fn NgMkSockNode
86should be called first, to create a new
87.Em socket
88type netgraph node with associated control and data sockets.  If
89.Fa name
90is non-NULL, the node will have that global name assigned to it.
91.Fa "*csp"
92and
93.Fa "*dsp"
94will be set to the newly opened control and data sockets
95associated with the node; either
96.Fa "csp"
97or
98.Fa "dsp"
99may be NULL if only one socket is desired.
100.Fn NgMkSockNode
101loads the socket node type KLD if it's not already loaded.
102.Pp
103.Fn NgNameNode
104assigns a global name to the node addressed by
105.Fa path .
106.Pp
107.Fn NgSendMsg
108sends a binary control message from the socket node associated
109with control socket
110.Fa cs
111to the node addressed by
112.Fa path .
113The
114.Fa cookie
115indicates how to interpret
116.Fa cmd ,
117which indicates a specific command.
118Extra argument data (if any) is specified by
119.Fa arg
120and
121.Fa arglen .
122The
123.Fa cookie ,
124.Fa cmd ,
125and argument data are defined by the header file corresponding
126to the type of the node being addressed.
127The unique, non-negative token value chosen for use in the message
128header is returned.  This value is typically used to associate replies.
129.Pp
130Use
131.Fn NgSendMsgReply
132to send reply to a previously received control message.
133The original message header should be pointed to by
134.Fa msg .
135.Pp
136.Fn NgSendAsciiMsg
137performs the same function as
138.Fn NgSendMsg ,
139but adds support for
140.Tn ASCII
141encoding of control messages.
142.Fn NgSendAsciiMsg
143formats its input a la
144.Xr printf 3
145and then sends the resulting
146.Tn ASCII
147string to the node in a
148.Dv NGM_ASCII2BINARY
149control message.  The node returns a binary version of the
150message, which is then sent back to the node just as with
151.Fn NgSendMsg .
152As with
153.Fn NgSendMsg ,
154the message token value is returned.
155Note that
156.Tn ASCII
157conversion may not be supported by all node types.
158.Pp
159.Fn NgRecvMsg
160reads the next control message received by the node associated with
161control socket
162.Fa cs .
163The message and any extra argument data must fit in
164.Fa replen
165bytes.
166If
167.Fa "path"
168is non-NULL, it must point to a buffer of at least
169.Dv "NG_PATHLEN + 1"
170bytes, which will be filled in (and NUL terminated) with the path to
171the node from which the message was received.
172.Pp
173.Fn NgRecvAsciiMsg
174works exactly like
175.Fn NgRecvMsg ,
176except that after the message is received, any binary arguments
177are converted to
178.Tn ASCII
179by sending a
180.Dv NGM_BINARY2ASCII
181request back to the originating node.  The result is the same as
182.Fn NgRecvAsciiMsg ,
183with the exception that the reply arguments field will contain
184a NUL-terminated
185.Tn ASCII
186version of the arguments (and the reply
187header argument length field will be adjusted).
188.Pp
189.Fn NgSendData
190writes a data packet out on the specified hook of the node corresponding
191to data socket
192.Fa ds .
193The node must already be connected to some other node via that hook.
194.Pp
195.Fn NgRecvData
196reads the next data packet (of up to
197.Fa len
198bytes) received by the node corresponding to data socket
199.Fa ds
200and stores it in
201.Fa buf ,
202which must be large enough to hold the entire packet.  If
203.Fa "hook"
204is non-NULL, it must point to a buffer of at least
205.Dv "NG_HOOKLEN + 1"
206bytes, which will be filled in (and NUL terminated) with the name of
207the hook on which the data was received.
208.Pp
209.Fn NgSetDebug
210and
211.Fn NgSetErrLog
212are used for debugging.
213.Fn NgSetDebug
214sets the debug level (if non-negative), and returns the old setting.
215Higher debug levels result in more verbosity.  The default is zero.
216All debug and error messages are logged via the functions
217specified in the most recent call to
218.Fn NgSetErrLog .
219The default logging functions are
220.Xr vwarn 3
221and
222.Xr vwarnx 3 .
223.Pp
224At debug level 3, the library attempts to display control message arguments
225in
226.Tn ASCII
227format; however, this results in additional messages being
228sent which may interfere with debugging.  At even higher levels,
229even these additional messagages will be displayed, etc.
230.Pp
231Note that
232.Xr select 2
233can be used on the data and the control sockets to detect the presence of
234incoming data and control messages, respectively.
235Data and control packets are always written and read atomically, i.e.,
236in one whole piece.
237.Pp
238User mode programs must be linked with the
239.Dv -lnetgraph
240flag to link in this library.
241.Sh INITIALIZATION
242To enable Netgraph in your kernel, either your kernel must be
243compiled with
244.Dq options NETGRAPH
245in the kernel configuration
246file, or else the
247.Xr netgraph 4
248and
249.Xr ng_socket 4
250KLD modules must have been loaded via
251.Xr kldload 8 .
252.Sh DIAGNOSTICS
253All functions except
254.Fn NgSetDebug
255and
256.Fn NgSetErrLog
257return -1 if there was an error and set errno accordingly.
258.Pp
259For
260.Fn NgSendAsciiMsg
261and
262.Fn NgRecvAsciiMsg ,
263the following additional errors are possible:
264.Bl -tag -width Er
265.It Bq Er ENOSYS
266The node type does not know how to encode or decode the control message.
267.It Bq Er ERANGE
268The encoded or decoded arguments were too long for the supplied buffer.
269.It Bq Er ENOENT
270An unknown structure field was seen in an
271.Tn ASCII
272control message.
273.It Bq Er EALREADY
274The same structure field was specified twice in an
275.Tn ASCII
276control message.
277.It Bq Er EINVAL
278.Tn ASCII
279control message parse error or illegal value.
280.It Bq Er E2BIG
281ASCII control message array or fixed width string buffer overflow.
282.El
283.Sh SEE ALSO
284.Xr socket 2 ,
285.Xr select 2 ,
286.Xr warnx 3 ,
287.Xr netgraph 4 ,
288.Xr ng_socket 4 ,
289.Xr kld 4
290.Sh HISTORY
291The
292.Nm netgraph
293system was designed and first implemented at Whistle Communications, Inc. in
294a version of
295.Fx 2.2
296customized for the Whistle InterJet.
297.Sh AUTHORS
298.An Archie Cobbs Aq archie@whistle.com
299