added files
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / snmp_debug.c
1 #ifdef BRCM_SNMP_DEBUG
2
3 #include <net-snmp/net-snmp-config.h>
4
5 #include <stdio.h>
6 #if HAVE_STDLIB_H
7 #include <stdlib.h>
8 #endif
9 #if HAVE_STRING_H
10 #include <string.h>
11 #else
12 #include <strings.h>
13 #endif
14 #include <sys/types.h>
15 #if HAVE_NETINET_IN_H
16 #include <netinet/in.h>
17 #endif
18 #if HAVE_STDARG_H
19 #include <stdarg.h>
20 #else
21 #include <varargs.h>
22 #endif
23 #if HAVE_WINSOCK_H
24 #include <winsock.h>
25 #endif
26
27 #if HAVE_DMALLOC_H
28 #include <dmalloc.h>
29 #endif
30
31 #include <net-snmp/types.h>
32 #include <net-snmp/output_api.h>
33 #include <net-snmp/library/snmp_debug.h>        /* For this file's "internal" definitions */
34 #include <net-snmp/config_api.h>
35 #include <net-snmp/utilities.h>
36
37 #include <net-snmp/library/mib.h>
38 #include <net-snmp/library/snmp_api.h>
39
40 static int      dodebug = SNMP_ALWAYS_DEBUG;
41 static int      debug_num_tokens = 0;
42 static char    *debug_tokens[MAX_DEBUG_TOKENS];
43 static int      debug_print_everything = 0;
44
45 /*
46  * indent debugging:  provide a space padded section to return an indent for 
47  */
48 static int      debugindent = 0;
49 #define INDENTMAX 80
50 static char     debugindentchars[] =
51     "                                                                                ";
52
53 /*
54  * Prototype definitions 
55  */
56 void            debug_config_register_tokens(const char *configtoken,
57                                              char *tokens);
58 void            debug_config_turn_on_debugging(const char *configtoken,
59                                                char *line);
60
61 char           *
62 debug_indent(void)
63 {
64     return debugindentchars;
65 }
66
67 void
68 debug_indent_add(int amount)
69 {
70     if (debugindent + amount >= 0 && debugindent + amount < 80) {
71         debugindentchars[debugindent] = ' ';
72         debugindent += amount;
73         debugindentchars[debugindent] = '\0';
74     }
75 }
76
77 void
78 debug_config_register_tokens(const char *configtoken, char *tokens)
79 {
80     debug_register_tokens(tokens);
81 }
82
83 void
84 debug_config_turn_on_debugging(const char *configtoken, char *line)
85 {
86     snmp_set_do_debugging(atoi(line));
87 }
88
89 void
90 snmp_debug_init(void)
91 {
92     debugindentchars[0] = '\0'; /* zero out the debugging indent array. */
93 #ifdef BRCM_SNMP_SUPPORT
94     register_prenetsnmp_mib_handler("snmp", "doDebugging",
95                                     debug_config_turn_on_debugging, NULL,
96                                     "(1|0)");
97     register_prenetsnmp_mib_handler("snmp", "debugTokens",
98                                     debug_config_register_tokens, NULL,
99                                     "token[,token...]");
100 #endif /* BRCM_SNMP_SUPPORT */
101 }
102
103 void
104 debug_register_tokens(char *tokens)
105 {
106     char           *newp, *cp;
107
108     if (tokens == 0 || *tokens == 0)
109         return;
110
111     newp = strdup(tokens);      /* strtok messes it up */
112     cp = strtok(newp, DEBUG_TOKEN_DELIMITER);
113     while (cp) {
114         if (strlen(cp) < MAX_DEBUG_TOKEN_LEN) {
115             if (strcasecmp(cp, DEBUG_ALWAYS_TOKEN) == 0)
116                 debug_print_everything = 1;
117             else if (debug_num_tokens < MAX_DEBUG_TOKENS)
118                 debug_tokens[debug_num_tokens++] = strdup(cp);
119         }
120         cp = strtok(NULL, DEBUG_TOKEN_DELIMITER);
121     }
122     free(newp);
123 }
124
125
126 /*
127  * debug_is_token_registered(char *TOKEN):
128  * 
129  * returns SNMPERR_SUCCESS
130  * or SNMPERR_GENERR
131  * 
132  * if TOKEN has been registered and debugging support is turned on.
133  */
134 int
135 debug_is_token_registered(const char *token)
136 {
137     int             i;
138
139     /*
140      * debugging flag is on or off 
141      */
142     if (!dodebug)
143         return SNMPERR_GENERR;
144
145     if (debug_num_tokens == 0 || debug_print_everything) {
146         /*
147          * no tokens specified, print everything 
148          */
149         return SNMPERR_SUCCESS;
150     } else {
151         for (i = 0; i < debug_num_tokens; i++) {
152             if (strncmp(debug_tokens[i], token, strlen(debug_tokens[i])) ==
153                 0) {
154                 return SNMPERR_SUCCESS;
155             }
156         }
157     }
158     return SNMPERR_GENERR;
159 }
160
161 void
162 #if HAVE_STDARG_H
163 debugmsg(const char *token, const char *format, ...)
164 #else
165 debugmsg(va_alist)
166      va_dcl
167 #endif
168 {
169     va_list         debugargs;
170
171 #if HAVE_STDARG_H
172     va_start(debugargs, format);
173 #else
174     const char     *format;
175     const char     *token;
176
177     va_start(debugargs);
178     token = va_arg(debugargs, const char *);
179     format = va_arg(debugargs, const char *);   /* ??? */
180 #endif
181
182     if (debug_is_token_registered(token) == SNMPERR_SUCCESS) {
183         snmp_vlog(LOG_DEBUG, format, debugargs);
184     }
185     va_end(debugargs);
186 }
187
188 void
189 debugmsg_oid(const char *token, const oid * theoid, size_t len)
190 {
191     u_char         *buf = NULL;
192     size_t          buf_len = 0, out_len = 0;
193
194     if (sprint_realloc_objid(&buf, &buf_len, &out_len, 1, theoid, len)) {
195         if (buf != NULL) {
196             debugmsg(token, "%s", buf);
197         }
198     } else {
199         if (buf != NULL) {
200             debugmsg(token, "%s [TRUNCATED]", buf);
201         }
202     }
203
204     if (buf != NULL) {
205         free(buf);
206     }
207 }
208
209 void
210 debugmsg_var(const char *token, netsnmp_variable_list * var)
211 {
212     u_char         *buf = NULL;
213     size_t          buf_len = 0, out_len = 0;
214
215     if (var == NULL || token == NULL) {
216         return;
217     }
218
219     if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1,
220                                 var->name, var->name_length, var)) {
221         if (buf != NULL) {
222             debugmsg(token, "%s", buf);
223         }
224     } else {
225         if (buf != NULL) {
226             debugmsg(token, "%s [TRUNCATED]", buf);
227         }
228     }
229
230     if (buf != NULL) {
231         free(buf);
232     }
233 }
234
235 void
236 debugmsg_oidrange(const char *token, const oid * theoid, size_t len,
237                   size_t var_subid, oid range_ubound)
238 {
239     u_char         *buf = NULL;
240     size_t          buf_len = 0, out_len = 0, i = 0;
241     int             rc = 0;
242
243     if (var_subid == 0) {
244         rc = sprint_realloc_objid(&buf, &buf_len, &out_len, 1, theoid,
245                                   len);
246     } else {
247         char            tmpbuf[128];
248         rc = sprint_realloc_objid(&buf, &buf_len, &out_len, 1, theoid,
249                                   var_subid);
250         if (rc) {
251             sprintf(tmpbuf, ".%lu--%lu", theoid[var_subid - 1],
252                     range_ubound);
253             rc = snmp_strcat(&buf, &buf_len, &out_len, 1, tmpbuf);
254             if (rc) {
255                 for (i = var_subid; i < len; i++) {
256                     sprintf(tmpbuf, ".%lu", theoid[i]);
257                     if (!snmp_strcat(&buf, &buf_len, &out_len, 1, tmpbuf)) {
258                         break;
259                     }
260                 }
261             }
262         }
263     }
264
265
266     if (buf != NULL) {
267         debugmsg(token, "%s%s", buf, rc ? "" : " [TRUNCATED]");
268         free(buf);
269     }
270 }
271
272 void
273 debugmsg_hex(const char *token, u_char * thedata, size_t len)
274 {
275     u_char         *buf = NULL;
276     size_t          buf_len = 0, out_len = 0;
277
278     if (sprint_realloc_hexstring
279         (&buf, &buf_len, &out_len, 1, thedata, len)) {
280         if (buf != NULL) {
281             debugmsg(token, "%s", buf);
282         }
283     } else {
284         if (buf != NULL) {
285             debugmsg(token, "%s [TRUNCATED]", buf);
286         }
287     }
288
289     if (buf != NULL) {
290         free(buf);
291     }
292 }
293
294 void
295 debugmsg_hextli(const char *token, u_char * thedata, size_t len)
296 {
297     char            buf[SPRINT_MAX_LEN], token2[SPRINT_MAX_LEN];
298     u_char         *b3 = NULL;
299     size_t          b3_len = 0, o3_len = 0;
300     int             incr;
301     sprintf(token2, "dumpx_%s", token);
302
303     /*
304      * XX tracing lines removed from this function DEBUGTRACE; 
305      */
306     DEBUGIF(token2) {
307         for (incr = 16; len > 0; len -= incr, thedata += incr) {
308             if ((int) len < incr) {
309                 incr = len;
310             }
311             /*
312              * XXnext two lines were DEBUGPRINTINDENT(token);
313              */
314             sprintf(buf, "dumpx%s", token);
315             debugmsg(buf, "%s: %s", token2, debug_indent());
316             if (sprint_realloc_hexstring
317                 (&b3, &b3_len, &o3_len, 1, thedata, incr)) {
318                 if (b3 != NULL) {
319                     debugmsg(token2, "%s", b3);
320                 }
321             } else {
322                 if (b3 != NULL) {
323                     debugmsg(token2, "%s [TRUNCATED]", b3);
324                 }
325             }
326             o3_len = 0;
327         }
328     }
329     if (b3 != NULL) {
330         free(b3);
331     }
332 }
333
334 void
335 #if HAVE_STDARG_H
336 debugmsgtoken(const char *token, const char *format, ...)
337 #else
338 debugmsgtoken(va_alist)
339      va_dcl
340 #endif
341 {
342     va_list         debugargs;
343
344 #if HAVE_STDARG_H
345     va_start(debugargs, format);
346 #else
347     const char     *token;
348
349     va_start(debugargs);
350     token = va_arg(debugargs, const char *);
351 #endif
352
353     debugmsg(token, "%s: ", token);
354
355     va_end(debugargs);
356 }
357
358 /*
359  * for speed, these shouldn't be in default_storage space 
360  */
361 void
362 snmp_set_do_debugging(int val)
363 {
364     dodebug = val;
365 }
366
367 int
368 snmp_get_do_debugging(void)
369 {
370     return dodebug;
371 }
372
373 #else /* BRCM_SNMP_DEBUG not defined */
374
375 #include <net-snmp/net-snmp-config.h>
376
377 #include <stdio.h>
378 #if HAVE_STDLIB_H
379 #include <stdlib.h>
380 #endif
381 #if HAVE_STRING_H
382 #include <string.h>
383 #else
384 #include <strings.h>
385 #endif
386 #include <sys/types.h>
387 #if HAVE_NETINET_IN_H
388 #include <netinet/in.h>
389 #endif
390 #if HAVE_STDARG_H
391 #include <stdarg.h>
392 #else
393 #include <varargs.h>
394 #endif
395 #if HAVE_WINSOCK_H
396 #include <winsock.h>
397 #endif
398
399 #if HAVE_DMALLOC_H
400 #include <dmalloc.h>
401 #endif
402
403 #include <net-snmp/types.h>
404 #include <net-snmp/output_api.h>
405 #include <net-snmp/library/snmp_debug.h>        /* For this file's "internal" definitions */
406 #include <net-snmp/config_api.h>
407 #include <net-snmp/utilities.h>
408
409 #include <net-snmp/library/mib.h>
410 #include <net-snmp/library/snmp_api.h>
411
412 /*
413  * Prototype definitions 
414  */
415 void            debug_config_register_tokens(const char *configtoken,
416                                              char *tokens);
417 void            debug_config_turn_on_debugging(const char *configtoken,
418                                                char *line);
419 static int      dodebug = SNMP_ALWAYS_DEBUG;
420
421 char           *
422 debug_indent(void)
423 {
424   return NULL;
425 }
426
427 void
428 debug_indent_add(int amount)
429 {
430   return;
431 }
432
433 void
434 debug_config_register_tokens(const char *configtoken, char *tokens)
435 {
436   return;
437 }
438
439 void
440 debug_config_turn_on_debugging(const char *configtoken, char *line)
441 {
442   return;
443 }
444
445 void
446 snmp_debug_init(void)
447 {
448   return;
449 }
450
451 void
452 debug_register_tokens(char *tokens)
453 {
454   return;
455 }
456
457
458 /*
459  * debug_is_token_registered(char *TOKEN):
460  * 
461  * returns SNMPERR_SUCCESS
462  * or SNMPERR_GENERR
463  * 
464  * if TOKEN has been registered and debugging support is turned on.
465  */
466 int
467 debug_is_token_registered(const char *token)
468 {
469   return SNMPERR_GENERR;
470 }
471
472 void
473 #if HAVE_STDARG_H
474 debugmsg(const char *token, const char *format, ...)
475 #else
476 debugmsg(va_alist)
477      va_dcl
478 #endif
479 {
480   return;
481 }
482
483 void
484 debugmsg_oid(const char *token, const oid * theoid, size_t len)
485 {
486   return;
487 }
488
489 void
490 debugmsg_var(const char *token, netsnmp_variable_list * var)
491 {
492   return;
493 }
494
495 void
496 debugmsg_oidrange(const char *token, const oid * theoid, size_t len,
497                   size_t var_subid, oid range_ubound)
498 {
499   return;
500 }
501
502 void
503 debugmsg_hex(const char *token, u_char * thedata, size_t len)
504 {
505   return;
506 }
507
508 void
509 debugmsg_hextli(const char *token, u_char * thedata, size_t len)
510 {
511   return;
512 }
513
514 void
515 #if HAVE_STDARG_H
516 debugmsgtoken(const char *token, const char *format, ...)
517 #else
518 debugmsgtoken(va_alist)
519      va_dcl
520 #endif
521 {
522   return;
523 }
524
525 /*
526  * for speed, these shouldn't be in default_storage space 
527  */
528 void
529 snmp_set_do_debugging(int val)
530 {
531   return;
532 }
533
534 int
535 snmp_get_do_debugging(void)
536 {
537   return 0;
538 }
539
540 #endif /* BRCM_SNMP_DEBUG */