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