xref: /freebsd/lib/libnetgraph/debug.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
14cf49a43SJulian Elischer /*
24cf49a43SJulian Elischer  * debug.c
34cf49a43SJulian Elischer  *
44cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
54cf49a43SJulian Elischer  * All rights reserved.
64cf49a43SJulian Elischer  *
74cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
84cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
94cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
104cf49a43SJulian Elischer  * provided, however, that:
114cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
124cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
134cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
144cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
154cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
164cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
174cf49a43SJulian Elischer  *
184cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
194cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
204cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
214cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
224cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
234cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
244cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
254cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
264cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
274cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
284cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
294cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
304cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
314cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
324cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
334cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
344cf49a43SJulian Elischer  * OF SUCH DAMAGE.
354cf49a43SJulian Elischer  *
364cf49a43SJulian Elischer  * Author: Archie Cobbs <archie@whistle.com>
374cf49a43SJulian Elischer  *
384cf49a43SJulian Elischer  * $Whistle: debug.c,v 1.24 1999/01/24 01:15:33 archie Exp $
394cf49a43SJulian Elischer  */
404cf49a43SJulian Elischer 
414cf49a43SJulian Elischer #include <sys/types.h>
42baa60a3bSArchie Cobbs #include <sys/time.h>
43baa60a3bSArchie Cobbs #include <sys/ioctl.h>
44f8307e12SArchie Cobbs 
454cf49a43SJulian Elischer #include <stdarg.h>
46f8307e12SArchie Cobbs 
47f8307e12SArchie Cobbs #include <netinet/in.h>
48f8307e12SArchie Cobbs #include <net/ethernet.h>
49baa60a3bSArchie Cobbs #include <net/bpf.h>
50f8307e12SArchie Cobbs 
514cf49a43SJulian Elischer #include <netgraph/ng_message.h>
524cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
534cf49a43SJulian Elischer 
544cf49a43SJulian Elischer #include "netgraph.h"
554cf49a43SJulian Elischer #include "internal.h"
564cf49a43SJulian Elischer 
57f8307e12SArchie Cobbs #include <netgraph/ng_UI.h>
584cf49a43SJulian Elischer #include <netgraph/ng_async.h>
59baa60a3bSArchie Cobbs #include <netgraph/ng_bpf.h>
6041fa1ea9SRuslan Ermilov #include <netgraph/ng_bridge.h>
61*78a786f9SAlexander Motin #include <netgraph/ng_car.h>
62f8307e12SArchie Cobbs #include <netgraph/ng_cisco.h>
63*78a786f9SAlexander Motin #include <netgraph/ng_deflate.h>
6441fa1ea9SRuslan Ermilov #include <netgraph/ng_device.h>
65f8307e12SArchie Cobbs #include <netgraph/ng_echo.h>
6641fa1ea9SRuslan Ermilov #include <netgraph/ng_eiface.h>
6741fa1ea9SRuslan Ermilov #include <netgraph/ng_etf.h>
68f8307e12SArchie Cobbs #include <netgraph/ng_ether.h>
69*78a786f9SAlexander Motin #include <netgraph/ng_ether_echo.h>
704cf49a43SJulian Elischer #include <netgraph/ng_frame_relay.h>
7141fa1ea9SRuslan Ermilov #include <netgraph/ng_gif.h>
7241fa1ea9SRuslan Ermilov #include <netgraph/ng_gif_demux.h>
73f8307e12SArchie Cobbs #include <netgraph/ng_hole.h>
7441fa1ea9SRuslan Ermilov #include <netgraph/ng_hub.h>
75f8307e12SArchie Cobbs #include <netgraph/ng_iface.h>
7641fa1ea9SRuslan Ermilov #include <netgraph/ng_ip_input.h>
7741fa1ea9SRuslan Ermilov #include <netgraph/ng_ipfw.h>
78f8307e12SArchie Cobbs #include <netgraph/ng_ksocket.h>
7941fa1ea9SRuslan Ermilov #include <netgraph/ng_l2tp.h>
804cf49a43SJulian Elischer #include <netgraph/ng_lmi.h>
8141fa1ea9SRuslan Ermilov #include <netgraph/ng_mppc.h>
8241fa1ea9SRuslan Ermilov #include <netgraph/ng_nat.h>
83*78a786f9SAlexander Motin #include <netgraph/netflow/ng_netflow.h>
8441fa1ea9SRuslan Ermilov #include <netgraph/ng_one2many.h>
85*78a786f9SAlexander Motin #include <netgraph/ng_patch.h>
86*78a786f9SAlexander Motin #include <netgraph/ng_pipe.h>
87f8307e12SArchie Cobbs #include <netgraph/ng_ppp.h>
88f8307e12SArchie Cobbs #include <netgraph/ng_pppoe.h>
8941fa1ea9SRuslan Ermilov #include <netgraph/ng_pptpgre.h>
90*78a786f9SAlexander Motin #include <netgraph/ng_pred1.h>
91f8307e12SArchie Cobbs #include <netgraph/ng_rfc1490.h>
92f8307e12SArchie Cobbs #include <netgraph/ng_socket.h>
9341fa1ea9SRuslan Ermilov #include <netgraph/ng_source.h>
9441fa1ea9SRuslan Ermilov #include <netgraph/ng_split.h>
95*78a786f9SAlexander Motin #include <netgraph/ng_tag.h>
9641fa1ea9SRuslan Ermilov #include <netgraph/ng_tcpmss.h>
97f8307e12SArchie Cobbs #include <netgraph/ng_tee.h>
984cf49a43SJulian Elischer #include <netgraph/ng_tty.h>
99f8307e12SArchie Cobbs #include <netgraph/ng_vjc.h>
10041fa1ea9SRuslan Ermilov #include <netgraph/ng_vlan.h>
101f8307e12SArchie Cobbs #ifdef	WHISTLE
102f8307e12SArchie Cobbs #include <machine/../isa/df_def.h>
103f8307e12SArchie Cobbs #include <machine/../isa/if_wfra.h>
104f8307e12SArchie Cobbs #include <machine/../isa/ipac.h>
105f8307e12SArchie Cobbs #include <netgraph/ng_df.h>
106f8307e12SArchie Cobbs #include <netgraph/ng_ipac.h>
107f8307e12SArchie Cobbs #include <netgraph/ng_tn.h>
108f8307e12SArchie Cobbs #endif
1094cf49a43SJulian Elischer 
1104cf49a43SJulian Elischer /* Global debug level */
1114cf49a43SJulian Elischer int     _gNgDebugLevel = 0;
1124cf49a43SJulian Elischer 
1134cf49a43SJulian Elischer /* Debug printing functions */
1144cf49a43SJulian Elischer void    (*_NgLog) (const char *fmt,...) = warn;
1154cf49a43SJulian Elischer void    (*_NgLogx) (const char *fmt,...) = warnx;
1164cf49a43SJulian Elischer 
1174cf49a43SJulian Elischer /* Internal functions */
1184cf49a43SJulian Elischer static const	char *NgCookie(int cookie);
119f8307e12SArchie Cobbs 
120f8307e12SArchie Cobbs /* Known typecookie list */
121f8307e12SArchie Cobbs struct ng_cookie {
122f8307e12SArchie Cobbs 	int		cookie;
123f8307e12SArchie Cobbs 	const char	*type;
124f8307e12SArchie Cobbs };
125f8307e12SArchie Cobbs 
126f8307e12SArchie Cobbs #define COOKIE(c)	{ NGM_ ## c ## _COOKIE, #c }
127f8307e12SArchie Cobbs 
128f8307e12SArchie Cobbs /* List of known cookies */
129f8307e12SArchie Cobbs static const struct ng_cookie cookies[] = {
130f8307e12SArchie Cobbs 	COOKIE(UI),
131f8307e12SArchie Cobbs 	COOKIE(ASYNC),
132e489a907SArchie Cobbs 	COOKIE(BPF),
13341fa1ea9SRuslan Ermilov 	COOKIE(BRIDGE),
134*78a786f9SAlexander Motin 	COOKIE(CAR),
135f8307e12SArchie Cobbs 	COOKIE(CISCO),
136*78a786f9SAlexander Motin 	COOKIE(DEFLATE),
13741fa1ea9SRuslan Ermilov 	COOKIE(DEVICE),
138f8307e12SArchie Cobbs 	COOKIE(ECHO),
13941fa1ea9SRuslan Ermilov 	COOKIE(EIFACE),
14041fa1ea9SRuslan Ermilov 	COOKIE(ETF),
141f8307e12SArchie Cobbs 	COOKIE(ETHER),
142*78a786f9SAlexander Motin 	COOKIE(ETHER_ECHO),
143f8307e12SArchie Cobbs 	COOKIE(FRAMERELAY),
14441fa1ea9SRuslan Ermilov 	COOKIE(GIF),
14541fa1ea9SRuslan Ermilov 	COOKIE(GIF_DEMUX),
146f8307e12SArchie Cobbs 	COOKIE(GENERIC),
147f8307e12SArchie Cobbs 	COOKIE(HOLE),
14841fa1ea9SRuslan Ermilov 	COOKIE(HUB),
149f8307e12SArchie Cobbs 	COOKIE(IFACE),
15041fa1ea9SRuslan Ermilov 	COOKIE(IP_INPUT),
15141fa1ea9SRuslan Ermilov 	COOKIE(IPFW),
152f8307e12SArchie Cobbs 	COOKIE(KSOCKET),
15341fa1ea9SRuslan Ermilov 	COOKIE(L2TP),
154f8307e12SArchie Cobbs 	COOKIE(LMI),
15541fa1ea9SRuslan Ermilov 	COOKIE(MPPC),
15641fa1ea9SRuslan Ermilov 	COOKIE(NAT),
157*78a786f9SAlexander Motin 	COOKIE(NETFLOW),
15841fa1ea9SRuslan Ermilov 	COOKIE(ONE2MANY),
159*78a786f9SAlexander Motin 	COOKIE(PATCH),
160*78a786f9SAlexander Motin 	COOKIE(PIPE),
161f8307e12SArchie Cobbs 	COOKIE(PPP),
162f8307e12SArchie Cobbs 	COOKIE(PPPOE),
16341fa1ea9SRuslan Ermilov 	COOKIE(PPTPGRE),
164*78a786f9SAlexander Motin 	COOKIE(PRED1),
165f8307e12SArchie Cobbs 	COOKIE(RFC1490),
166f8307e12SArchie Cobbs 	COOKIE(SOCKET),
16741fa1ea9SRuslan Ermilov 	COOKIE(SOURCE),
16841fa1ea9SRuslan Ermilov 	COOKIE(SPLIT),
169*78a786f9SAlexander Motin 	COOKIE(TAG),
17041fa1ea9SRuslan Ermilov 	COOKIE(TCPMSS),
171f8307e12SArchie Cobbs 	COOKIE(TEE),
172f8307e12SArchie Cobbs 	COOKIE(TTY),
173f8307e12SArchie Cobbs 	COOKIE(VJC),
17441fa1ea9SRuslan Ermilov 	COOKIE(VLAN),
175f8307e12SArchie Cobbs #ifdef WHISTLE
176f8307e12SArchie Cobbs 	COOKIE(DF),
177f8307e12SArchie Cobbs 	COOKIE(IPAC),
178f8307e12SArchie Cobbs 	COOKIE(TN),
179f8307e12SArchie Cobbs 	COOKIE(WFRA),
180f8307e12SArchie Cobbs #endif
181f8307e12SArchie Cobbs 	{ 0, NULL }
182f8307e12SArchie Cobbs };
1834cf49a43SJulian Elischer 
1844cf49a43SJulian Elischer /*
1854cf49a43SJulian Elischer  * Set debug level, ie, verbosity, if "level" is non-negative.
1864cf49a43SJulian Elischer  * Returns old debug level.
1874cf49a43SJulian Elischer  */
1884cf49a43SJulian Elischer int
NgSetDebug(int level)1894cf49a43SJulian Elischer NgSetDebug(int level)
1904cf49a43SJulian Elischer {
1914cf49a43SJulian Elischer 	int old = _gNgDebugLevel;
1924cf49a43SJulian Elischer 
193*78a786f9SAlexander Motin 	if (level >= 0)
1944cf49a43SJulian Elischer 		_gNgDebugLevel = level;
1954cf49a43SJulian Elischer 	return (old);
1964cf49a43SJulian Elischer }
1974cf49a43SJulian Elischer 
1984cf49a43SJulian Elischer /*
1994cf49a43SJulian Elischer  * Set debug logging functions.
2004cf49a43SJulian Elischer  */
2014cf49a43SJulian Elischer void
NgSetErrLog(void (* log)(const char * fmt,...),void (* logx)(const char * fmt,...))2024cf49a43SJulian Elischer NgSetErrLog(void (*log) (const char *fmt,...),
2034cf49a43SJulian Elischer 		void (*logx) (const char *fmt,...))
2044cf49a43SJulian Elischer {
2054cf49a43SJulian Elischer 	_NgLog = log;
2064cf49a43SJulian Elischer 	_NgLogx = logx;
2074cf49a43SJulian Elischer }
2084cf49a43SJulian Elischer 
2094cf49a43SJulian Elischer /*
2104cf49a43SJulian Elischer  * Display a netgraph sockaddr
2114cf49a43SJulian Elischer  */
2124cf49a43SJulian Elischer void
_NgDebugSockaddr(const struct sockaddr_ng * sg)213f8307e12SArchie Cobbs _NgDebugSockaddr(const struct sockaddr_ng *sg)
2144cf49a43SJulian Elischer {
2154cf49a43SJulian Elischer 	NGLOGX("SOCKADDR: { fam=%d len=%d addr=\"%s\" }",
2164cf49a43SJulian Elischer 	       sg->sg_family, sg->sg_len, sg->sg_data);
2174cf49a43SJulian Elischer }
2184cf49a43SJulian Elischer 
2191816e452SArchie Cobbs #define ARGS_BUFSIZE		2048
2201816e452SArchie Cobbs #define RECURSIVE_DEBUG_ADJUST	4
221f8307e12SArchie Cobbs 
2224cf49a43SJulian Elischer /*
2234cf49a43SJulian Elischer  * Display a negraph message
2244cf49a43SJulian Elischer  */
2254cf49a43SJulian Elischer void
_NgDebugMsg(const struct ng_mesg * msg,const char * path)226f8307e12SArchie Cobbs _NgDebugMsg(const struct ng_mesg *msg, const char *path)
2274cf49a43SJulian Elischer {
228f8307e12SArchie Cobbs 	u_char buf[2 * sizeof(struct ng_mesg) + ARGS_BUFSIZE];
229f8307e12SArchie Cobbs 	struct ng_mesg *const req = (struct ng_mesg *)buf;
230f8307e12SArchie Cobbs 	struct ng_mesg *const bin = (struct ng_mesg *)req->data;
2311816e452SArchie Cobbs 	int arglen, csock = -1;
232f8307e12SArchie Cobbs 
233f8307e12SArchie Cobbs 	/* Display header stuff */
2344cf49a43SJulian Elischer 	NGLOGX("NG_MESG :");
2354cf49a43SJulian Elischer 	NGLOGX("  vers   %d", msg->header.version);
236*78a786f9SAlexander Motin 	NGLOGX("  arglen %u", msg->header.arglen);
237*78a786f9SAlexander Motin 	NGLOGX("  flags  %x", msg->header.flags);
238*78a786f9SAlexander Motin 	NGLOGX("  token  %u", msg->header.token);
239*78a786f9SAlexander Motin 	NGLOGX("  cookie %s (%u)",
240f8307e12SArchie Cobbs 	    NgCookie(msg->header.typecookie), msg->header.typecookie);
241f8307e12SArchie Cobbs 
242f8307e12SArchie Cobbs 	/* At lower debugging levels, skip ASCII translation */
243f8307e12SArchie Cobbs 	if (_gNgDebugLevel <= 2)
244f8307e12SArchie Cobbs 		goto fail2;
245f8307e12SArchie Cobbs 
246f8307e12SArchie Cobbs 	/* If path is not absolute, don't bother trying to use relative
247f8307e12SArchie Cobbs 	   address on a different socket for the ASCII translation */
248f8307e12SArchie Cobbs 	if (strchr(path, ':') == NULL)
249f8307e12SArchie Cobbs 		goto fail2;
250f8307e12SArchie Cobbs 
251f8307e12SArchie Cobbs 	/* Get a temporary socket */
252f8307e12SArchie Cobbs 	if (NgMkSockNode(NULL, &csock, NULL) < 0)
253f8307e12SArchie Cobbs 		goto fail;
254f8307e12SArchie Cobbs 
255f8307e12SArchie Cobbs 	/* Copy binary message into request message payload */
256f8307e12SArchie Cobbs 	arglen = msg->header.arglen;
257f8307e12SArchie Cobbs 	if (arglen > ARGS_BUFSIZE)
258f8307e12SArchie Cobbs 		arglen = ARGS_BUFSIZE;
259f8307e12SArchie Cobbs 	memcpy(bin, msg, sizeof(*msg) + arglen);
260f8307e12SArchie Cobbs 	bin->header.arglen = arglen;
261f8307e12SArchie Cobbs 
2621816e452SArchie Cobbs 	/* Lower debugging to avoid infinite recursion */
2631816e452SArchie Cobbs 	_gNgDebugLevel -= RECURSIVE_DEBUG_ADJUST;
2641816e452SArchie Cobbs 
265f8307e12SArchie Cobbs 	/* Ask the node to translate the binary message to ASCII for us */
266f8307e12SArchie Cobbs 	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
2671816e452SArchie Cobbs 	    NGM_BINARY2ASCII, bin, sizeof(*bin) + bin->header.arglen) < 0) {
2681816e452SArchie Cobbs 		_gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
269f8307e12SArchie Cobbs 		goto fail;
2701816e452SArchie Cobbs 	}
2711816e452SArchie Cobbs 	if (NgRecvMsg(csock, req, sizeof(buf), NULL) < 0) {
2721816e452SArchie Cobbs 		_gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
273f8307e12SArchie Cobbs 		goto fail;
2741816e452SArchie Cobbs 	}
2751816e452SArchie Cobbs 
2761816e452SArchie Cobbs 	/* Restore debugging level */
2771816e452SArchie Cobbs 	_gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
278f8307e12SArchie Cobbs 
279f8307e12SArchie Cobbs 	/* Display command string and arguments */
280f8307e12SArchie Cobbs 	NGLOGX("  cmd    %s (%d)", bin->header.cmdstr, bin->header.cmd);
281f8307e12SArchie Cobbs 	NGLOGX("  args   %s", bin->data);
282f8307e12SArchie Cobbs 	goto done;
283f8307e12SArchie Cobbs 
284f8307e12SArchie Cobbs fail:
285f8307e12SArchie Cobbs 	/* Just display binary version */
286f8307e12SArchie Cobbs 	NGLOGX("  [error decoding message: %s]", strerror(errno));
287f8307e12SArchie Cobbs fail2:
288f8307e12SArchie Cobbs 	NGLOGX("  cmd    %d", msg->header.cmd);
289f8307e12SArchie Cobbs 	NGLOGX("  args (%d bytes)", msg->header.arglen);
290b9236794SMaxime Henrion 	_NgDebugBytes((u_char *)msg->data, msg->header.arglen);
291f8307e12SArchie Cobbs 
292f8307e12SArchie Cobbs done:
293f8307e12SArchie Cobbs 	if (csock != -1)
294f8307e12SArchie Cobbs 		(void)close(csock);
2954cf49a43SJulian Elischer }
2964cf49a43SJulian Elischer 
2974cf49a43SJulian Elischer /*
2984cf49a43SJulian Elischer  * Return the name of the node type corresponding to the cookie
2994cf49a43SJulian Elischer  */
3004cf49a43SJulian Elischer static const char *
NgCookie(int cookie)3014cf49a43SJulian Elischer NgCookie(int cookie)
3024cf49a43SJulian Elischer {
303f8307e12SArchie Cobbs 	int k;
3044cf49a43SJulian Elischer 
305f8307e12SArchie Cobbs 	for (k = 0; cookies[k].cookie != 0; k++) {
306f8307e12SArchie Cobbs 		if (cookies[k].cookie == cookie)
307f8307e12SArchie Cobbs 			return cookies[k].type;
3084cf49a43SJulian Elischer 	}
309f8307e12SArchie Cobbs 	return "??";
3104cf49a43SJulian Elischer }
3114cf49a43SJulian Elischer 
3124cf49a43SJulian Elischer /*
3134cf49a43SJulian Elischer  * Dump bytes in hex
3144cf49a43SJulian Elischer  */
3154cf49a43SJulian Elischer void
_NgDebugBytes(const u_char * ptr,int len)3164cf49a43SJulian Elischer _NgDebugBytes(const u_char *ptr, int len)
3174cf49a43SJulian Elischer {
3184cf49a43SJulian Elischer 	char    buf[100];
3194cf49a43SJulian Elischer 	int     k, count;
3204cf49a43SJulian Elischer 
3214cf49a43SJulian Elischer #define BYPERLINE	16
3224cf49a43SJulian Elischer 
3234cf49a43SJulian Elischer 	for (count = 0; count < len; ptr += BYPERLINE, count += BYPERLINE) {
3244cf49a43SJulian Elischer 
3254cf49a43SJulian Elischer 		/* Do hex */
3264cf49a43SJulian Elischer 		snprintf(buf, sizeof(buf), "%04x:  ", count);
3274cf49a43SJulian Elischer 		for (k = 0; k < BYPERLINE; k++, count++)
3284cf49a43SJulian Elischer 			if (count < len)
3294cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
3304cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf), "%02x ", ptr[k]);
3314cf49a43SJulian Elischer 			else
3324cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
3334cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf), "   ");
3344cf49a43SJulian Elischer 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "  ");
3354cf49a43SJulian Elischer 		count -= BYPERLINE;
3364cf49a43SJulian Elischer 
3374cf49a43SJulian Elischer 		/* Do ASCII */
3384cf49a43SJulian Elischer 		for (k = 0; k < BYPERLINE; k++, count++)
3394cf49a43SJulian Elischer 			if (count < len)
3404cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
3414cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf),
3424cf49a43SJulian Elischer 				    "%c", isprint(ptr[k]) ? ptr[k] : '.');
3434cf49a43SJulian Elischer 			else
3444cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
3454cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf), "  ");
3464cf49a43SJulian Elischer 		count -= BYPERLINE;
3474cf49a43SJulian Elischer 
3484cf49a43SJulian Elischer 		/* Print it */
3494cf49a43SJulian Elischer 		NGLOGX("%s", buf);
3504cf49a43SJulian Elischer 	}
3514cf49a43SJulian Elischer }
3524cf49a43SJulian Elischer 
353