4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * Subject to the following obligations and disclaimer of warranty, use and
8 * redistribution of this software, in source or object code forms, with or
9 * without modifications are expressly permitted by Whistle Communications;
10 * provided, however, that:
11 * 1. Any and all reproductions of the source or object code must include the
12 * copyright notice above and the following disclaimer of warranties; and
13 * 2. No rights are granted, in any manner or form, to use Whistle
14 * Communications, Inc. trademarks, including the mark "WHISTLE
15 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16 * such appears in the above copyright notice or in the software.
18 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * Author: Archie Cobbs <archie@whistle.com>
38 * $FreeBSD: src/lib/libnetgraph/debug.c,v 1.9 2005/10/25 20:58:30 ru Exp $
39 * $DragonFly: src/lib/libnetgraph/debug.c,v 1.4 2007/06/17 20:33:14 swildner Exp $
40 * $Whistle: debug.c,v 1.24 1999/01/24 01:15:33 archie Exp $
43 #include <sys/types.h>
45 #include <sys/ioctl.h>
49 #include <netinet/in.h>
50 #include <net/ethernet.h>
53 #include <netgraph7/ng_message.h>
54 #include <netgraph7/socket/ng_socket.h>
59 #include <netgraph7/UI/ng_UI.h>
60 #include <netgraph7/async/ng_async.h>
61 #include <netgraph7/atmllc/ng_atmllc.h>
62 #include <netgraph7/bpf/ng_bpf.h>
63 #include <netgraph/bridge/ng_bridge.h>
64 #include <netgraph7/cisco/ng_cisco.h>
65 #include <netgraph7/deflate/ng_deflate.h>
66 #include <netgraph7/echo/ng_echo.h>
68 #include <netgraph7/eiface/ng_eiface.h>
70 #include <netgraph7/etf/ng_etf.h>
71 #include <netgraph7/ether/ng_ether.h>
73 #include <netgraph7/fec/ng_fec.h>
75 #include <netgraph7/frame_relay/ng_frame_relay.h>
76 #include <netgraph7/hole/ng_hole.h>
77 #include <netgraph7/hub/ng_hub.h>
78 #include <netgraph7/iface/ng_iface.h>
80 #include <netgraph7/ip_input/ng_ip_input.h>
82 #include <netgraph7/ksocket/ng_ksocket.h>
83 #include <netgraph7/l2tp/ng_l2tp.h>
84 #include <netgraph7/lmi/ng_lmi.h>
85 #include <netgraph7/mppc/ng_mppc.h>
86 #include <netgraph7/one2many/ng_one2many.h>
87 #include <netgraph7/ppp/ng_ppp.h>
88 #include <netgraph7/pppoe/ng_pppoe.h>
89 #include <netgraph7/pptpgre/ng_pptpgre.h>
90 #include <netgraph7/rfc1490/ng_rfc1490.h>
92 #include <netgraph7/socket/ng_socket.h>
93 #include <netgraph7/source/ng_source.h>
94 #include <netgraph7/split/ng_split.h>
96 #include <netgraph7/tcpmss/ng_tcpmss.h>
97 #include <netgraph7/tee/ng_tee.h>
99 #include <netgraph7/tty/ng_tty.h>
101 #include <netgraph7/vjc/ng_vjc.h>
103 #include <machine/../isa/df_def.h>
104 #include <machine/../isa/if_wfra.h>
105 #include <machine/../isa/ipac.h>
106 #include <netgraph/ng_df.h>
107 #include <netgraph/ng_ipac.h>
108 #include <netgraph/ng_tn.h>
111 /* Global debug level */
112 int _gNgDebugLevel = 0;
114 /* Debug printing functions */
115 void (*_NgLog) (const char *fmt,...) = warn;
116 void (*_NgLogx) (const char *fmt,...) = warnx;
118 /* Internal functions */
119 static const char *NgCookie(int cookie);
121 /* Known typecookie list */
127 #define COOKIE(c) { NGM_ ## c ## _COOKIE, #c }
129 /* List of known cookies */
130 static const struct ng_cookie cookies[] = {
185 * Set debug level, ie, verbosity, if "level" is non-negative.
186 * Returns old debug level.
189 NgSetDebug(int level)
191 int old = _gNgDebugLevel;
195 _gNgDebugLevel = level;
200 * Set debug logging functions.
203 NgSetErrLog(void (*log) (const char *fmt,...),
204 void (*logx) (const char *fmt,...))
211 * Display a netgraph sockaddr
214 _NgDebugSockaddr(const struct sockaddr_ng *sg)
216 NGLOGX("SOCKADDR: { fam=%d len=%d addr=\"%s\" }",
217 sg->sg_family, sg->sg_len, sg->sg_data);
220 #define ARGS_BUFSIZE 2048
221 #define RECURSIVE_DEBUG_ADJUST 4
224 * Display a negraph message
227 _NgDebugMsg(const struct ng_mesg *msg, const char *path)
229 u_char buf[2 * sizeof(struct ng_mesg) + ARGS_BUFSIZE];
230 struct ng_mesg *const req = (struct ng_mesg *)buf;
231 struct ng_mesg *const bin = (struct ng_mesg *)req->data;
232 int arglen, csock = -1;
234 /* Display header stuff */
236 NGLOGX(" vers %d", msg->header.version);
237 NGLOGX(" arglen %d", msg->header.arglen);
238 NGLOGX(" flags %ld", msg->header.flags);
239 NGLOGX(" token %lu", (u_long)msg->header.token);
240 NGLOGX(" cookie %s (%d)",
241 NgCookie(msg->header.typecookie), msg->header.typecookie);
243 /* At lower debugging levels, skip ASCII translation */
244 if (_gNgDebugLevel <= 2)
247 /* If path is not absolute, don't bother trying to use relative
248 address on a different socket for the ASCII translation */
249 if (strchr(path, ':') == NULL)
252 /* Get a temporary socket */
253 if (NgMkSockNode(NULL, &csock, NULL) < 0)
256 /* Copy binary message into request message payload */
257 arglen = msg->header.arglen;
258 if (arglen > ARGS_BUFSIZE)
259 arglen = ARGS_BUFSIZE;
260 memcpy(bin, msg, sizeof(*msg) + arglen);
261 bin->header.arglen = arglen;
263 /* Lower debugging to avoid infinite recursion */
264 _gNgDebugLevel -= RECURSIVE_DEBUG_ADJUST;
266 /* Ask the node to translate the binary message to ASCII for us */
267 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
268 NGM_BINARY2ASCII, bin, sizeof(*bin) + bin->header.arglen) < 0) {
269 _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
272 if (NgRecvMsg(csock, req, sizeof(buf), NULL) < 0) {
273 _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
277 /* Restore debugging level */
278 _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
280 /* Display command string and arguments */
281 NGLOGX(" cmd %s (%d)", bin->header.cmdstr, bin->header.cmd);
282 NGLOGX(" args %s", bin->data);
286 /* Just display binary version */
287 NGLOGX(" [error decoding message: %s]", strerror(errno));
289 NGLOGX(" cmd %d", msg->header.cmd);
290 NGLOGX(" args (%d bytes)", msg->header.arglen);
291 _NgDebugBytes(msg->data, msg->header.arglen);
299 * Return the name of the node type corresponding to the cookie
306 for (k = 0; cookies[k].cookie != 0; k++) {
307 if (cookies[k].cookie == cookie)
308 return cookies[k].type;
317 _NgDebugBytes(const u_char *ptr, int len)
324 for (count = 0; count < len; ptr += BYPERLINE, count += BYPERLINE) {
327 snprintf(buf, sizeof(buf), "%04x: ", count);
328 for (k = 0; k < BYPERLINE; k++, count++)
330 snprintf(buf + strlen(buf),
331 sizeof(buf) - strlen(buf), "%02x ", ptr[k]);
333 snprintf(buf + strlen(buf),
334 sizeof(buf) - strlen(buf), " ");
335 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
339 for (k = 0; k < BYPERLINE; k++, count++)
341 snprintf(buf + strlen(buf),
342 sizeof(buf) - strlen(buf),
343 "%c", isprint(ptr[k]) ? ptr[k] : '.');
345 snprintf(buf + strlen(buf),
346 sizeof(buf) - strlen(buf), " ");