]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/snmp/lib/v2_0/src/snmp_debug.c
Initial revision
[karo-tx-redboot.git] / packages / net / snmp / lib / v2_0 / src / snmp_debug.c
1 //==========================================================================
2 //
3 //      ./lib/current/src/snmp_debug.c
4 //
5 //
6 //==========================================================================
7 //####ECOSGPLCOPYRIGHTBEGIN####
8 // -------------------------------------------
9 // This file is part of eCos, the Embedded Configurable Operating System.
10 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
11 //
12 // eCos is free software; you can redistribute it and/or modify it under
13 // the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 2 or (at your option) any later version.
15 //
16 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 // for more details.
20 //
21 // You should have received a copy of the GNU General Public License along
22 // with eCos; if not, write to the Free Software Foundation, Inc.,
23 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 //
25 // As a special exception, if other files instantiate templates or use macros
26 // or inline functions from this file, or you compile this file and link it
27 // with other works to produce a work based on this file, this file does not
28 // by itself cause the resulting work to be covered by the GNU General Public
29 // License. However the source code for this file must still be made available
30 // in accordance with section (3) of the GNU General Public License.
31 //
32 // This exception does not invalidate any other reasons why a work based on
33 // this file might be covered by the GNU General Public License.
34 //
35 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
36 // at http://sources.redhat.com/ecos/ecos-license/
37 // -------------------------------------------
38 //####ECOSGPLCOPYRIGHTEND####
39 //####UCDSNMPCOPYRIGHTBEGIN####
40 //
41 // -------------------------------------------
42 //
43 // Portions of this software may have been derived from the UCD-SNMP
44 // project,  <http://ucd-snmp.ucdavis.edu/>  from the University of
45 // California at Davis, which was originally based on the Carnegie Mellon
46 // University SNMP implementation.  Portions of this software are therefore
47 // covered by the appropriate copyright disclaimers included herein.
48 //
49 // The release used was version 4.1.2 of May 2000.  "ucd-snmp-4.1.2"
50 // -------------------------------------------
51 //
52 //####UCDSNMPCOPYRIGHTEND####
53 //==========================================================================
54 //#####DESCRIPTIONBEGIN####
55 //
56 // Author(s):    hmt
57 // Contributors: hmt
58 // Date:         2000-05-30
59 // Purpose:      Port of UCD-SNMP distribution to eCos.
60 // Description:  
61 //              
62 //
63 //####DESCRIPTIONEND####
64 //
65 //==========================================================================
66 /********************************************************************
67        Copyright 1989, 1991, 1992 by Carnegie Mellon University
68
69                           Derivative Work -
70 Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
71
72                          All Rights Reserved
73
74 Permission to use, copy, modify and distribute this software and its
75 documentation for any purpose and without fee is hereby granted,
76 provided that the above copyright notice appears in all copies and
77 that both that copyright notice and this permission notice appear in
78 supporting documentation, and that the name of CMU and The Regents of
79 the University of California not be used in advertising or publicity
80 pertaining to distribution of the software without specific written
81 permission.
82
83 CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
84 WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
85 WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL CMU OR
86 THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
87 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
88 FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
89 CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
90 CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
91 *********************************************************************/
92 #include <config.h>
93
94 #include <stdio.h>
95 #if HAVE_STDLIB_H
96 #include <stdlib.h>
97 #endif
98 #if HAVE_STRING_H
99 #include <string.h>
100 #else
101 #include <strings.h>
102 #endif
103 #include <sys/types.h>
104 #if HAVE_NETINET_IN_H
105 #include <netinet/in.h>
106 #endif
107 #if HAVE_STDARG_H
108 #include <stdarg.h>
109 #else
110 #include <varargs.h>
111 #endif
112 #if HAVE_WINSOCK_H
113 #include <winsock.h>
114 #endif
115
116 #if HAVE_DMALLOC_H
117 #include <dmalloc.h>
118 #endif
119
120 #include "asn1.h"
121 #include "mib.h"
122 #include "snmp_api.h"
123 #include "read_config.h"
124 #include "snmp_debug.h"
125 #include "snmp_impl.h"
126 #include "snmp_logging.h"
127
128 static int   dodebug = SNMP_ALWAYS_DEBUG;
129 static int   debug_num_tokens=0;
130 static char *debug_tokens[MAX_DEBUG_TOKENS];
131 static int   debug_print_everything=0;
132
133 /* indent debugging:  provide a space padded section to return an indent for */
134 static int debugindent=0;
135 #define INDENTMAX 80
136 static char debugindentchars[] = "                                                                                ";
137
138 char *
139 debug_indent(void) {
140   return debugindentchars;
141 }
142
143 void
144 debug_indent_add(int amount) {
145   if (debugindent+amount >= 0 && debugindent+amount < 80) {
146     debugindentchars[debugindent] = ' ';
147     debugindent += amount;
148     debugindentchars[debugindent] = '\0';
149   }
150 }
151
152 void
153 #if HAVE_STDARG_H
154 DEBUGP(const char *first, ...)
155 #else
156 DEBUGP(va_alist)
157   va_dcl
158 #endif
159 {
160   va_list args;
161 #if HAVE_STDARG_H
162   va_start(args, first);
163 #else
164   const char *first;
165   va_start(args);
166   first = va_arg(args, const char *);
167 #endif
168
169   if (dodebug && (debug_print_everything || debug_num_tokens == 0)) {
170     fprintf(stderr, "%s: ", DEBUG_ALWAYS_TOKEN);
171     vfprintf(stderr, first, args);
172   }
173   va_end(args);
174 }
175
176 void
177 DEBUGPOID(oid *theoid,
178           size_t len)
179 {
180   char c_oid[SPRINT_MAX_LEN];
181   sprint_objid(c_oid,theoid,len);
182   DEBUGP(c_oid);
183 }
184
185 void debug_config_register_tokens(const char *configtoken, char *tokens) {
186   debug_register_tokens(tokens);
187 }
188
189 void debug_config_turn_on_debugging(const char *configtoken, char *line) {
190   snmp_set_do_debugging(atoi(line));
191 }
192
193 void
194 snmp_debug_init(void) {
195   debugindentchars[0] = '\0'; /* zero out the debugging indent array. */
196   register_premib_handler("snmp","doDebugging",
197                           debug_config_turn_on_debugging, NULL,
198                           "(1|0)");
199   register_premib_handler("snmp","debugTokens",
200                           debug_config_register_tokens, NULL,
201                           "token[,token...]");
202 }
203
204 void debug_register_tokens(char *tokens) {
205   char *newp, *cp;
206   
207   if (tokens == 0 || *tokens == 0)
208     return;
209
210   newp = strdup(tokens); /* strtok messes it up */
211   cp = strtok(newp, DEBUG_TOKEN_DELIMITER);
212   while(cp) {
213     if (strlen(cp) < MAX_DEBUG_TOKEN_LEN) {
214       if (strcasecmp(cp, DEBUG_ALWAYS_TOKEN) == 0)
215         debug_print_everything = 1;
216       else if (debug_num_tokens < MAX_DEBUG_TOKENS)
217         debug_tokens[debug_num_tokens++] = strdup(cp);
218     }
219     cp = strtok(NULL, DEBUG_TOKEN_DELIMITER);
220   }
221   free(newp);
222 }
223
224
225 /*
226   debug_is_token_registered(char *TOKEN):
227
228   returns SNMPERR_SUCCESS
229        or SNMPERR_GENERR
230
231   if TOKEN has been registered and debugging support is turned on.
232 */
233 int
234 debug_is_token_registered(const char *token) {
235   int i;
236
237   /* debugging flag is on or off */
238   if (!dodebug)
239     return SNMPERR_GENERR;
240   
241   if (debug_num_tokens == 0 || debug_print_everything) {
242     /* no tokens specified, print everything */
243     return SNMPERR_SUCCESS;
244   } else {
245     for(i=0; i < debug_num_tokens; i++) {
246       if (strncmp(debug_tokens[i], token, strlen(debug_tokens[i])) == 0) {
247         return SNMPERR_SUCCESS;
248       }
249     }
250   }
251   return SNMPERR_GENERR;
252 }
253
254 void
255 #if HAVE_STDARG_H
256 debugmsg(const char *token, const char *format, ...)
257 #else
258 debugmsg(va_alist)
259   va_dcl
260 #endif
261 {
262   va_list debugargs;
263   
264 #if HAVE_STDARG_H
265   va_start(debugargs,format);
266 #else
267   const char *format;
268   const char *token;
269
270   va_start(debugargs);
271   token = va_arg(debugargs, const char *);
272   format = va_arg(debugargs, const char *); /* ??? */
273 #endif
274
275   if (debug_is_token_registered(token) == SNMPERR_SUCCESS) {
276     snmp_vlog(LOG_DEBUG, format, debugargs);
277   }
278   va_end(debugargs);
279 }
280
281 void
282 debugmsg_oid(const char *token, oid *theoid, size_t len) {
283   char c_oid[SPRINT_MAX_LEN];
284   
285   sprint_objid(c_oid, theoid, len);
286   debugmsg(token, c_oid);
287 }
288
289 void
290 debugmsg_hex(const char *token, u_char *thedata, size_t len) {
291   char buf[SPRINT_MAX_LEN];
292   
293   sprint_hexstring(buf, thedata, len);
294   debugmsg(token, buf);
295 }
296
297 void
298 debugmsg_hextli(const char *token, u_char *thedata, size_t len) {
299   char buf[SPRINT_MAX_LEN];
300   int incr; 
301
302   /*XX tracing lines removed from this function DEBUGTRACE; */
303   DEBUGIF(token) {
304     for(incr = 16; len > 0; len -= incr, thedata += incr) {
305       if ((int)len < incr) incr = len;
306       /*XXnext two lines were DEBUGPRINTINDENT(token);*/
307       debugmsgtoken(token, "%s", debug_indent());
308       debugmsg(token, "%s", debug_indent());
309       sprint_hexstring(buf, thedata, incr);
310       debugmsg(token, buf);
311     }
312   }
313 }
314
315 void
316 #if HAVE_STDARG_H
317 debugmsgtoken(const char *token, const char *format, ...)
318 #else
319 debugmsgtoken(va_alist)
320   va_dcl
321 #endif
322 {
323   va_list debugargs;
324
325 #if HAVE_STDARG_H
326   va_start(debugargs,format);
327 #else
328   const char *token;
329
330   va_start(debugargs);
331   token = va_arg(debugargs, const char *);
332 #endif
333
334   debugmsg(token, "%s: ", token);
335
336   va_end(debugargs);
337 }
338   
339 /* for speed, these shouldn't be in default_storage space */
340 void
341 snmp_set_do_debugging(int val)
342 {
343   dodebug = val;
344 }
345
346 int
347 snmp_get_do_debugging (void)
348 {
349   return dodebug;
350 }