1 //==========================================================================
3 // ./lib/current/src/snmp_debug.c
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.
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.
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
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.
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.
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.
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####
41 // -------------------------------------------
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.
49 // The release used was version 4.1.2 of May 2000. "ucd-snmp-4.1.2"
50 // -------------------------------------------
52 //####UCDSNMPCOPYRIGHTEND####
53 //==========================================================================
54 //#####DESCRIPTIONBEGIN####
59 // Purpose: Port of UCD-SNMP distribution to eCos.
63 //####DESCRIPTIONEND####
65 //==========================================================================
66 /********************************************************************
67 Copyright 1989, 1991, 1992 by Carnegie Mellon University
70 Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
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
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 *********************************************************************/
103 #include <sys/types.h>
104 #if HAVE_NETINET_IN_H
105 #include <netinet/in.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"
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;
133 /* indent debugging: provide a space padded section to return an indent for */
134 static int debugindent=0;
136 static char debugindentchars[] = " ";
140 return debugindentchars;
144 debug_indent_add(int amount) {
145 if (debugindent+amount >= 0 && debugindent+amount < 80) {
146 debugindentchars[debugindent] = ' ';
147 debugindent += amount;
148 debugindentchars[debugindent] = '\0';
154 DEBUGP(const char *first, ...)
162 va_start(args, first);
166 first = va_arg(args, const char *);
169 if (dodebug && (debug_print_everything || debug_num_tokens == 0)) {
170 fprintf(stderr, "%s: ", DEBUG_ALWAYS_TOKEN);
171 vfprintf(stderr, first, args);
177 DEBUGPOID(oid *theoid,
180 char c_oid[SPRINT_MAX_LEN];
181 sprint_objid(c_oid,theoid,len);
185 void debug_config_register_tokens(const char *configtoken, char *tokens) {
186 debug_register_tokens(tokens);
189 void debug_config_turn_on_debugging(const char *configtoken, char *line) {
190 snmp_set_do_debugging(atoi(line));
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,
199 register_premib_handler("snmp","debugTokens",
200 debug_config_register_tokens, NULL,
204 void debug_register_tokens(char *tokens) {
207 if (tokens == 0 || *tokens == 0)
210 newp = strdup(tokens); /* strtok messes it up */
211 cp = strtok(newp, DEBUG_TOKEN_DELIMITER);
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);
219 cp = strtok(NULL, DEBUG_TOKEN_DELIMITER);
226 debug_is_token_registered(char *TOKEN):
228 returns SNMPERR_SUCCESS
231 if TOKEN has been registered and debugging support is turned on.
234 debug_is_token_registered(const char *token) {
237 /* debugging flag is on or off */
239 return SNMPERR_GENERR;
241 if (debug_num_tokens == 0 || debug_print_everything) {
242 /* no tokens specified, print everything */
243 return SNMPERR_SUCCESS;
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;
251 return SNMPERR_GENERR;
256 debugmsg(const char *token, const char *format, ...)
265 va_start(debugargs,format);
271 token = va_arg(debugargs, const char *);
272 format = va_arg(debugargs, const char *); /* ??? */
275 if (debug_is_token_registered(token) == SNMPERR_SUCCESS) {
276 snmp_vlog(LOG_DEBUG, format, debugargs);
282 debugmsg_oid(const char *token, oid *theoid, size_t len) {
283 char c_oid[SPRINT_MAX_LEN];
285 sprint_objid(c_oid, theoid, len);
286 debugmsg(token, c_oid);
290 debugmsg_hex(const char *token, u_char *thedata, size_t len) {
291 char buf[SPRINT_MAX_LEN];
293 sprint_hexstring(buf, thedata, len);
294 debugmsg(token, buf);
298 debugmsg_hextli(const char *token, u_char *thedata, size_t len) {
299 char buf[SPRINT_MAX_LEN];
302 /*XX tracing lines removed from this function DEBUGTRACE; */
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);
317 debugmsgtoken(const char *token, const char *format, ...)
319 debugmsgtoken(va_alist)
326 va_start(debugargs,format);
331 token = va_arg(debugargs, const char *);
334 debugmsg(token, "%s: ", token);
339 /* for speed, these shouldn't be in default_storage space */
341 snmp_set_do_debugging(int val)
347 snmp_get_do_debugging (void)