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: src/lib/libnetgraph/netgraph.3,v 1.4.2.10 2002/12/29 16:35:36 schweikh Exp $ 36.\" $DragonFly: src/lib/libnetgraph/netgraph.3,v 1.4 2006/10/27 10:53:59 swildner Exp $ 37.\" $Whistle: netgraph.3,v 1.7 1999/01/25 07:14:06 archie Exp $ 38.\" 39.Dd January 19, 1999 40.Dt NETGRAPH 3 41.Os 42.Sh NAME 43.Nm NgMkSockNode , 44.Nm NgNameNode , 45.Nm NgSendMsg , 46.Nm NgSendAsciiMsg , 47.Nm NgSendReplyMsg , 48.Nm NgRecvMsg , 49.Nm NgRecvAsciiMsg , 50.Nm NgSendData , 51.Nm NgRecvData , 52.Nm NgSetDebug , 53.Nm NgSetErrLog 54.Nd netgraph user library 55.Sh LIBRARY 56.Lb libnetgraph 57.Sh SYNOPSIS 58.In netgraph.h 59.Ft int 60.Fn NgMkSockNode "const char *name" "int *csp" "int *dsp" 61.Ft int 62.Fn NgNameNode "int cs" "const char *path" "const char *fmt" "..." 63.Ft int 64.Fn NgSendMsg "int cs" "const char *path" "int cookie" "int cmd" "const void *arg" "size_t arglen" 65.Ft int 66.Fn NgSendAsciiMsg "int cs" "const char *path" "const char *fmt" "..." 67.Ft int 68.Fn NgSendReplyMsg "int cs" "const char *path" "struct ng_mesg *msg" "const void *arg" "size_t arglen" 69.Ft int 70.Fn NgRecvMsg "int cs" "struct ng_mesg *rep" "size_t replen" "char *path" 71.Ft int 72.Fn NgRecvAsciiMsg "int cs" "struct ng_mesg *rep" "size_t replen" "char *path" 73.Ft int 74.Fn NgSendData "int ds" "const char *hook" "const u_char *buf" "size_t len" 75.Ft int 76.Fn NgRecvData "int ds" "u_char *buf" "size_t len" "char *hook" 77.Ft int 78.Fn NgSetDebug "int level" 79.Ft void 80.Fn NgSetErrLog "void (*log)(const char *fmt, ...)" "void (*logx)(const char *fmt, ...)" 81.Sh DESCRIPTION 82These functions facilitate user-mode program participation in the kernel 83.Xr netgraph 4 84graph-based networking system, by utilizing the netgraph 85.Em socket 86node type (see 87.Xr ng_socket 4 ) . 88.Pp 89.Fn NgMkSockNode 90should be called first, to create a new 91.Em socket 92type netgraph node with associated control and data sockets. If 93.Fa name 94is non-NULL, the node will have that global name assigned to it. 95.Fa "*csp" 96and 97.Fa "*dsp" 98will be set to the newly opened control and data sockets 99associated with the node; either 100.Fa "csp" 101or 102.Fa "dsp" 103may be NULL if only one socket is desired. 104.Fn NgMkSockNode 105loads the socket node type KLD if it's not already loaded. 106.Pp 107.Fn NgNameNode 108assigns a global name to the node addressed by 109.Fa path . 110.Pp 111.Fn NgSendMsg 112sends a binary control message from the socket node associated 113with control socket 114.Fa cs 115to the node addressed by 116.Fa path . 117The 118.Fa cookie 119indicates how to interpret 120.Fa cmd , 121which indicates a specific command. 122Extra argument data (if any) is specified by 123.Fa arg 124and 125.Fa arglen . 126The 127.Fa cookie , 128.Fa cmd , 129and argument data are defined by the header file corresponding 130to the type of the node being addressed. 131The unique, non-negative token value chosen for use in the message 132header is returned. This value is typically used to associate replies. 133.Pp 134Use 135.Fn NgSendReplyMsg 136to send reply to a previously received control message. 137The original message header should be pointed to by 138.Fa msg . 139.Pp 140.Fn NgSendAsciiMsg 141performs the same function as 142.Fn NgSendMsg , 143but adds support for 144.Tn ASCII 145encoding of control messages. 146.Fn NgSendAsciiMsg 147formats its input a la 148.Xr printf 3 149and then sends the resulting 150.Tn ASCII 151string to the node in a 152.Dv NGM_ASCII2BINARY 153control message. The node returns a binary version of the 154message, which is then sent back to the node just as with 155.Fn NgSendMsg . 156As with 157.Fn NgSendMsg , 158the message token value is returned. 159Note that 160.Tn ASCII 161conversion may not be supported by all node types. 162.Pp 163.Fn NgRecvMsg 164reads the next control message received by the node associated with 165control socket 166.Fa cs . 167The message and any extra argument data must fit in 168.Fa replen 169bytes. 170If 171.Fa "path" 172is non-NULL, it must point to a buffer of at least 173.Dv "NG_PATHLEN + 1" 174bytes, which will be filled in (and NUL terminated) with the path to 175the node from which the message was received. 176.Pp 177The length of the control message is returned. 178A return value of zero indicates that the socket was closed. 179.Pp 180.Fn NgRecvAsciiMsg 181works exactly like 182.Fn NgRecvMsg , 183except that after the message is received, any binary arguments 184are converted to 185.Tn ASCII 186by sending a 187.Dv NGM_BINARY2ASCII 188request back to the originating node. The result is the same as 189.Fn NgRecvAsciiMsg , 190with the exception that the reply arguments field will contain 191a NUL-terminated 192.Tn ASCII 193version of the arguments (and the reply 194header argument length field will be adjusted). 195.Pp 196.Fn NgSendData 197writes a data packet out on the specified hook of the node corresponding 198to data socket 199.Fa ds . 200The node must already be connected to some other node via that hook. 201.Pp 202.Fn NgRecvData 203reads the next data packet (of up to 204.Fa len 205bytes) received by the node corresponding to data socket 206.Fa ds 207and stores it in 208.Fa buf , 209which must be large enough to hold the entire packet. If 210.Fa "hook" 211is non-NULL, it must point to a buffer of at least 212.Dv "NG_HOOKLEN + 1" 213bytes, which will be filled in (and NUL terminated) with the name of 214the hook on which the data was received. 215.Pp 216The length of the packet is returned. 217A return value of zero indicates that the socket was closed. 218.Pp 219.Fn NgSetDebug 220and 221.Fn NgSetErrLog 222are used for debugging. 223.Fn NgSetDebug 224sets the debug level (if non-negative), and returns the old setting. 225Higher debug levels result in more verbosity. The default is zero. 226All debug and error messages are logged via the functions 227specified in the most recent call to 228.Fn NgSetErrLog . 229The default logging functions are 230.Xr vwarn 3 231and 232.Xr vwarnx 3 . 233.Pp 234At debug level 3, the library attempts to display control message arguments 235in 236.Tn ASCII 237format; however, this results in additional messages being 238sent which may interfere with debugging. At even higher levels, 239even these additional messages will be displayed, etc. 240.Pp 241Note that 242.Xr select 2 243can be used on the data and the control sockets to detect the presence of 244incoming data and control messages, respectively. 245Data and control packets are always written and read atomically, i.e., 246in one whole piece. 247.Pp 248User mode programs must be linked with the 249.Dv -lnetgraph 250flag to link in this library. 251.Sh INITIALIZATION 252To enable Netgraph in your kernel, either your kernel must be 253compiled with 254.Cd options NETGRAPH 255in the kernel configuration 256file, or else the 257.Xr netgraph 4 258and 259.Xr ng_socket 4 260KLD modules must have been loaded via 261.Xr kldload 8 . 262.Sh RETURN VALUES 263.Fn NgSetDebug 264returns the previous debug setting. 265.Fn NgSetErrLog 266has no return value. 267All other functions return \-1 if there was an error and set 268.Va errno 269accordingly. 270A return value of zero from 271.Fn NgRecvMsg 272or 273.Fn NgRecvData 274indicates that the netgraph socket has been closed. 275.Pp 276For 277.Fn NgSendAsciiMsg 278and 279.Fn NgRecvAsciiMsg , 280the following additional errors are possible: 281.Bl -tag -width Er 282.It Bq Er ENOSYS 283The node type does not know how to encode or decode the control message. 284.It Bq Er ERANGE 285The encoded or decoded arguments were too long for the supplied buffer. 286.It Bq Er ENOENT 287An unknown structure field was seen in an 288.Tn ASCII 289control message. 290.It Bq Er EALREADY 291The same structure field was specified twice in an 292.Tn ASCII 293control message. 294.It Bq Er EINVAL 295.Tn ASCII 296control message parse error or illegal value. 297.It Bq Er E2BIG 298ASCII control message array or fixed width string buffer overflow. 299.El 300.Sh SEE ALSO 301.Xr select 2 , 302.Xr socket 2 , 303.Xr warnx 3 , 304.Xr kld 4 , 305.Xr netgraph 4 , 306.Xr ng_socket 4 307.Sh HISTORY 308The 309.Nm netgraph 310system was designed and first implemented at Whistle Communications, Inc. in 311a version of 312.Fx 2.2 313customized for the Whistle InterJet. 314.Sh AUTHORS 315.An Archie Cobbs Aq archie@whistle.com 316