summaryrefslogtreecommitdiffstats
path: root/lib/libnetgraph/debug.c
blob: bf57da695ceeee538dd7a48894c4534d711da49d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403

/*
 * debug.c
 *
 * Copyright (c) 1996-1999 Whistle Communications, Inc.
 * All rights reserved.
 * 
 * Subject to the following obligations and disclaimer of warranty, use and
 * redistribution of this software, in source or object code forms, with or
 * without modifications are expressly permitted by Whistle Communications;
 * provided, however, that:
 * 1. Any and all reproductions of the source or object code must include the
 *    copyright notice above and the following disclaimer of warranties; and
 * 2. No rights are granted, in any manner or form, to use Whistle
 *    Communications, Inc. trademarks, including the mark "WHISTLE
 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
 *    such appears in the above copyright notice or in the software.
 * 
 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * Author: Archie Cobbs <archie@whistle.com>
 *
 * $FreeBSD$
 * $Whistle: debug.c,v 1.24 1999/01/24 01:15:33 archie Exp $
 */

#include <sys/types.h>
#include <stdarg.h>
#include <netgraph/ng_message.h>
#include <netgraph/ng_socket.h>

#include "netgraph.h"
#include "internal.h"

#include <netgraph/ng_socket.h>
#include <netgraph/ng_message.h>
#include <netgraph/ng_iface.h>
#include <netgraph/ng_rfc1490.h>
#include <netgraph/ng_cisco.h>
#include <netgraph/ng_async.h>
#include <netgraph/ng_ppp.h>
#include <netgraph/ng_frame_relay.h>
#include <netgraph/ng_lmi.h>
#include <netgraph/ng_tty.h>
#include <netgraph/ng_tty.h>

/* Global debug level */
int     _gNgDebugLevel = 0;

/* Debug printing functions */
void    (*_NgLog) (const char *fmt,...) = warn;
void    (*_NgLogx) (const char *fmt,...) = warnx;

/* Internal functions */
static const	char *NgCookie(int cookie);
static const	char *NgCmd(int cookie, int cmd);
static void	NgArgs(int cookie, int cmd, int resp, void *args, int arglen);

/*
 * Set debug level, ie, verbosity, if "level" is non-negative.
 * Returns old debug level.
 */
int
NgSetDebug(int level)
{
	int old = _gNgDebugLevel;

	if (level < 0)
		level = old;
	_gNgDebugLevel = level;
	return (old);
}

/*
 * Set debug logging functions.
 */
void
NgSetErrLog(void (*log) (const char *fmt,...),
		void (*logx) (const char *fmt,...))
{
	_NgLog = log;
	_NgLogx = logx;
}

/*
 * Display a netgraph sockaddr
 */
void
_NgDebugSockaddr(struct sockaddr_ng *sg)
{
	NGLOGX("SOCKADDR: { fam=%d len=%d addr=\"%s\" }",
	       sg->sg_family, sg->sg_len, sg->sg_data);
}

/*
 * Display a negraph message
 */
void
_NgDebugMsg(struct ng_mesg * msg)
{
	NGLOGX("NG_MESG :");
	NGLOGX("  vers   %d", msg->header.version);
	NGLOGX("  arglen %d", msg->header.arglen);
	NGLOGX("  flags  %ld", msg->header.flags);
	NGLOGX("  token  %lu", (u_long) msg->header.token);
	NGLOGX("  cookie %s", NgCookie(msg->header.typecookie));
	NGLOGX("  cmd    %s", NgCmd(msg->header.typecookie, msg->header.cmd));
	NgArgs(msg->header.typecookie, msg->header.cmd,
	       (msg->header.flags & NGF_RESP), msg->data, msg->header.arglen);
}

/*
 * Return the name of the node type corresponding to the cookie
 */
static const char *
NgCookie(int cookie)
{
	static char buf[20];

	switch (cookie) {
	case NGM_GENERIC_COOKIE:
		return "generic";
	case NGM_TTY_COOKIE:
		return "tty";
	case NGM_ASYNC_COOKIE:
		return "async";
	case NGM_IFACE_COOKIE:
		return "iface";
	case NGM_FRAMERELAY_COOKIE:
		return "frame_relay";
	case NGM_LMI_COOKIE:
		return "lmi";
	case NGM_CISCO_COOKIE:
		return "cisco";
	case NGM_PPP_COOKIE:
		return "ppp";
	case NGM_RFC1490_NODE_COOKIE:
		return "rfc1490";
	case NGM_SOCKET_COOKIE:
		return "socket";
	}
	snprintf(buf, sizeof(buf), "?? (%d)", cookie);
	return buf;
}

/*
 * Return the name of the command
 */
static const char *
NgCmd(int cookie, int cmd)
{
	static char buf[20];

	switch (cookie) {
	case NGM_GENERIC_COOKIE:
		switch (cmd) {
		case NGM_SHUTDOWN:
			return "shutdown";
		case NGM_MKPEER:
			return "mkpeer";
		case NGM_CONNECT:
			return "connect";
		case NGM_NAME:
			return "name";
		case NGM_RMHOOK:
			return "rmhook";
		case NGM_NODEINFO:
			return "nodeinfo";
		case NGM_LISTHOOKS:
			return "listhooks";
		case NGM_LISTNAMES:
			return "listnames";
		case NGM_LISTNODES:
			return "listnodes";
		case NGM_TEXT_STATUS:
			return "text_status";
		}
		break;
	case NGM_TTY_COOKIE:
		switch (cmd) {
		case NGM_TTY_GET_HOTCHAR:
			return "getHotChar";
		case NGM_TTY_SET_HOTCHAR:
			return "setHotChar";
		}
		break;
	case NGM_ASYNC_COOKIE:
		switch (cmd) {
		case NGM_ASYNC_CMD_GET_STATS:
			return "getStats";
		case NGM_ASYNC_CMD_CLR_STATS:
			return "setStats";
		case NGM_ASYNC_CMD_SET_CONFIG:
			return "setConfig";
		case NGM_ASYNC_CMD_GET_CONFIG:
			return "getConfig";
		}
		break;
	case NGM_IFACE_COOKIE:
		switch (cmd) {
		case NGM_IFACE_GET_IFNAME:
			return "getIfName";
		case NGM_IFACE_GET_IFADDRS:
			return "getIfAddrs";
		}
		break;
	case NGM_LMI_COOKIE:
		switch (cmd) {
		case NGM_LMI_GET_STATUS:
			return "get-status";
		}
		break;
	}
	snprintf(buf, sizeof(buf), "?? (%d)", cmd);
	return buf;
}

/*
 * Decode message arguments
 */
static void
NgArgs(int cookie, int cmd, int resp, void *args, int arglen)
{

switch (cookie) {
case NGM_GENERIC_COOKIE:
	switch (cmd) {
	case NGM_SHUTDOWN:
		return;
	case NGM_MKPEER:
	    {
		struct ngm_mkpeer *const mkp = (struct ngm_mkpeer *) args;

		if (resp)
			return;
		NGLOGX("    type     \"%s\"", mkp->type);
		NGLOGX("    ourhook  \"%s\"", mkp->ourhook);
		NGLOGX("    peerhook \"%s\"", mkp->peerhook);
		return;
	    }
	case NGM_CONNECT:
	    {
		struct ngm_connect *const ngc = (struct ngm_connect *) args;

		if (resp)
			return;
		NGLOGX("    path     \"%s\"", ngc->path);
		NGLOGX("    ourhook  \"%s\"", ngc->ourhook);
		NGLOGX("    peerhook \"%s\"", ngc->peerhook);
		return;
	    }
	case NGM_NAME:
	    {
		struct ngm_name *const ngn = (struct ngm_name *) args;

		if (resp)
			return;
		NGLOGX("    name \"%s\"", ngn->name);
		return;
	    }
	case NGM_RMHOOK:
	    {
		struct ngm_rmhook *const ngr = (struct ngm_rmhook *) args;

		if (resp)
			return;
		NGLOGX("    hook \"%s\"", ngr->ourhook);
		return;
	    }
	case NGM_NODEINFO:
		return;
	case NGM_LISTHOOKS:
		return;
	case NGM_LISTNAMES:
	case NGM_LISTNODES:
		return;
	case NGM_TEXT_STATUS:
		if (!resp)
			return;
		NGLOGX("    status \"%s\"", (char *) args);
	    	return;
	}
	break;

case NGM_TTY_COOKIE:
	switch (cmd) {
	case NGM_TTY_GET_HOTCHAR:
		if (!resp)
			return;
		NGLOGX("    char 0x%02x", *((int *) args));
		return;
	case NGM_TTY_SET_HOTCHAR:
		NGLOGX("    char 0x%02x", *((int *) args));
		return;
	}
	break;

case NGM_ASYNC_COOKIE:
	switch (cmd) {
	case NGM_ASYNC_CMD_GET_STATS:
	    {
		struct ng_async_stat *const as = (struct ng_async_stat *) args;

		if (!resp)
			return;
		NGLOGX("    syncOctets = %lu", as->syncOctets);
		NGLOGX("    syncFrames = %lu", as->syncFrames);
		NGLOGX("    syncOverflows = %lu", as->syncOverflows);
		NGLOGX("    asyncOctets = %lu", as->asyncOctets);
		NGLOGX("    asyncFrames = %lu", as->asyncFrames);
		NGLOGX("    asyncRunts = %lu", as->asyncRunts);
		NGLOGX("    asyncOverflows = %lu", as->asyncOverflows);
		NGLOGX("    asyncBadCheckSums = %lu", as->asyncBadCheckSums);
		return;
	    }
	case NGM_ASYNC_CMD_GET_CONFIG:
	case NGM_ASYNC_CMD_SET_CONFIG:
	    {
		struct ng_async_cfg *const ac = (struct ng_async_cfg *) args;

		if (!resp ^ (cmd != NGM_ASYNC_CMD_GET_CONFIG))
			return;
		NGLOGX("    enabled   %s", ac->enabled ? "YES" : "NO");
		NGLOGX("    Async MRU %u", ac->amru);
		NGLOGX("    Sync MRU  %u", ac->smru);
		NGLOGX("    ACCM      0x%08x", ac->accm);
		return;
	    }
	case NGM_ASYNC_CMD_CLR_STATS:
		return;
	}
	break;

case NGM_IFACE_COOKIE:
	switch (cmd) {
	case NGM_IFACE_GET_IFNAME:
		return;
	case NGM_IFACE_GET_IFADDRS:
		return;
	}
	break;

	}
	_NgDebugBytes(args, arglen);
}

/*
 * Dump bytes in hex
 */
void
_NgDebugBytes(const u_char * ptr, int len)
{
	char    buf[100];
	int     k, count;

#define BYPERLINE	16

	for (count = 0; count < len; ptr += BYPERLINE, count += BYPERLINE) {

		/* Do hex */
		snprintf(buf, sizeof(buf), "%04x:  ", count);
		for (k = 0; k < BYPERLINE; k++, count++)
			if (count < len)
				snprintf(buf + strlen(buf),
				    sizeof(buf) - strlen(buf), "%02x ", ptr[k]);
			else
				snprintf(buf + strlen(buf),
				    sizeof(buf) - strlen(buf), "   ");
		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "  ");
		count -= BYPERLINE;

		/* Do ASCII */
		for (k = 0; k < BYPERLINE; k++, count++)
			if (count < len)
				snprintf(buf + strlen(buf),
				    sizeof(buf) - strlen(buf),
				    "%c", isprint(ptr[k]) ? ptr[k] : '.');
			else
				snprintf(buf + strlen(buf),
				    sizeof(buf) - strlen(buf), "  ");
		count -= BYPERLINE;

		/* Print it */
		NGLOGX("%s", buf);
	}
}

OpenPOWER on IntegriCloud