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