fix outgoing QOS prios
[tomato.git] / release / src / router / snmp / snmplib / snmp_debug.c
blobb14b38d8069ed9470593493203eadd99f35009a0
1 #include <net-snmp/net-snmp-config.h>
3 #include <stdio.h>
4 #if HAVE_STDLIB_H
5 #include <stdlib.h>
6 #endif
7 #if HAVE_STRING_H
8 #include <string.h>
9 #else
10 #include <strings.h>
11 #endif
12 #include <sys/types.h>
13 #if HAVE_NETINET_IN_H
14 #include <netinet/in.h>
15 #endif
16 #if HAVE_STDARG_H
17 #include <stdarg.h>
18 #else
19 #include <varargs.h>
20 #endif
21 #if HAVE_WINSOCK_H
22 #include <winsock.h>
23 #endif
25 #if HAVE_DMALLOC_H
26 #include <dmalloc.h>
27 #endif
29 #include <net-snmp/types.h>
30 #include <net-snmp/output_api.h>
31 #include <net-snmp/library/snmp_debug.h> /* For this file's "internal" definitions */
32 #include <net-snmp/config_api.h>
33 #include <net-snmp/utilities.h>
35 #include <net-snmp/library/mib.h>
36 #include <net-snmp/library/snmp_api.h>
38 static int dodebug = SNMP_ALWAYS_DEBUG;
39 static int debug_num_tokens = 0;
40 static char *debug_tokens[MAX_DEBUG_TOKENS];
41 static int debug_print_everything = 0;
44 * indent debugging: provide a space padded section to return an indent for
46 static int debugindent = 0;
47 #define INDENTMAX 80
48 static char debugindentchars[] =
49 " ";
52 * Prototype definitions
54 void debug_config_register_tokens(const char *configtoken,
55 char *tokens);
56 void debug_config_turn_on_debugging(const char *configtoken,
57 char *line);
59 char *
60 debug_indent(void)
62 return debugindentchars;
65 void
66 debug_indent_add(int amount)
68 if (debugindent + amount >= 0 && debugindent + amount < 80) {
69 debugindentchars[debugindent] = ' ';
70 debugindent += amount;
71 debugindentchars[debugindent] = '\0';
75 void
76 debug_config_register_tokens(const char *configtoken, char *tokens)
78 debug_register_tokens(tokens);
81 void
82 debug_config_turn_on_debugging(const char *configtoken, char *line)
84 snmp_set_do_debugging(atoi(line));
87 void
88 snmp_debug_init(void)
90 debugindentchars[0] = '\0'; /* zero out the debugging indent array. */
91 register_prenetsnmp_mib_handler("snmp", "doDebugging",
92 debug_config_turn_on_debugging, NULL,
93 "(1|0)");
94 register_prenetsnmp_mib_handler("snmp", "debugTokens",
95 debug_config_register_tokens, NULL,
96 "token[,token...]");
99 void
100 debug_register_tokens(char *tokens)
102 char *newp, *cp;
104 if (tokens == 0 || *tokens == 0)
105 return;
107 newp = strdup(tokens); /* strtok messes it up */
108 cp = strtok(newp, DEBUG_TOKEN_DELIMITER);
109 while (cp) {
110 if (strlen(cp) < MAX_DEBUG_TOKEN_LEN) {
111 if (strcasecmp(cp, DEBUG_ALWAYS_TOKEN) == 0)
112 debug_print_everything = 1;
113 else if (debug_num_tokens < MAX_DEBUG_TOKENS)
114 debug_tokens[debug_num_tokens++] = strdup(cp);
116 cp = strtok(NULL, DEBUG_TOKEN_DELIMITER);
118 free(newp);
123 * debug_is_token_registered(char *TOKEN):
125 * returns SNMPERR_SUCCESS
126 * or SNMPERR_GENERR
128 * if TOKEN has been registered and debugging support is turned on.
131 debug_is_token_registered(const char *token)
133 int i;
136 * debugging flag is on or off
138 if (!dodebug)
139 return SNMPERR_GENERR;
141 if (debug_num_tokens == 0 || debug_print_everything) {
143 * no tokens specified, print everything
145 return SNMPERR_SUCCESS;
146 } else {
147 for (i = 0; i < debug_num_tokens; i++) {
148 if (strncmp(debug_tokens[i], token, strlen(debug_tokens[i])) ==
149 0) {
150 return SNMPERR_SUCCESS;
154 return SNMPERR_GENERR;
157 void
158 #if HAVE_STDARG_H
159 debugmsg(const char *token, const char *format, ...)
160 #else
161 debugmsg(va_alist)
162 va_dcl
163 #endif
165 va_list debugargs;
167 #if HAVE_STDARG_H
168 va_start(debugargs, format);
169 #else
170 const char *format;
171 const char *token;
173 va_start(debugargs);
174 token = va_arg(debugargs, const char *);
175 format = va_arg(debugargs, const char *); /* ??? */
176 #endif
178 if (debug_is_token_registered(token) == SNMPERR_SUCCESS) {
179 snmp_vlog(LOG_DEBUG, format, debugargs);
181 va_end(debugargs);
184 void
185 debugmsg_oid(const char *token, const oid * theoid, size_t len)
187 u_char *buf = NULL;
188 size_t buf_len = 0, out_len = 0;
190 if (sprint_realloc_objid(&buf, &buf_len, &out_len, 1, theoid, len)) {
191 if (buf != NULL) {
192 debugmsg(token, "%s", buf);
194 } else {
195 if (buf != NULL) {
196 debugmsg(token, "%s [TRUNCATED]", buf);
200 if (buf != NULL) {
201 free(buf);
205 void
206 debugmsg_var(const char *token, netsnmp_variable_list * var)
208 u_char *buf = NULL;
209 size_t buf_len = 0, out_len = 0;
211 if (var == NULL || token == NULL) {
212 return;
215 if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1,
216 var->name, var->name_length, var)) {
217 if (buf != NULL) {
218 debugmsg(token, "%s", buf);
220 } else {
221 if (buf != NULL) {
222 debugmsg(token, "%s [TRUNCATED]", buf);
226 if (buf != NULL) {
227 free(buf);
231 void
232 debugmsg_oidrange(const char *token, const oid * theoid, size_t len,
233 size_t var_subid, oid range_ubound)
235 u_char *buf = NULL;
236 size_t buf_len = 0, out_len = 0, i = 0;
237 int rc = 0;
239 if (var_subid == 0) {
240 rc = sprint_realloc_objid(&buf, &buf_len, &out_len, 1, theoid,
241 len);
242 } else {
243 char tmpbuf[128];
244 rc = sprint_realloc_objid(&buf, &buf_len, &out_len, 1, theoid,
245 var_subid);
246 if (rc) {
247 sprintf(tmpbuf, ".%lu--%lu", theoid[var_subid - 1],
248 range_ubound);
249 rc = snmp_strcat(&buf, &buf_len, &out_len, 1, tmpbuf);
250 if (rc) {
251 for (i = var_subid; i < len; i++) {
252 sprintf(tmpbuf, ".%lu", theoid[i]);
253 if (!snmp_strcat(&buf, &buf_len, &out_len, 1, tmpbuf)) {
254 break;
262 if (buf != NULL) {
263 debugmsg(token, "%s%s", buf, rc ? "" : " [TRUNCATED]");
264 free(buf);
268 void
269 debugmsg_hex(const char *token, u_char * thedata, size_t len)
271 u_char *buf = NULL;
272 size_t buf_len = 0, out_len = 0;
274 if (sprint_realloc_hexstring
275 (&buf, &buf_len, &out_len, 1, thedata, len)) {
276 if (buf != NULL) {
277 debugmsg(token, "%s", buf);
279 } else {
280 if (buf != NULL) {
281 debugmsg(token, "%s [TRUNCATED]", buf);
285 if (buf != NULL) {
286 free(buf);
290 void
291 debugmsg_hextli(const char *token, u_char * thedata, size_t len)
293 char buf[SPRINT_MAX_LEN], token2[SPRINT_MAX_LEN];
294 u_char *b3 = NULL;
295 size_t b3_len = 0, o3_len = 0;
296 int incr;
297 sprintf(token2, "dumpx_%s", token);
300 * XX tracing lines removed from this function DEBUGTRACE;
302 DEBUGIF(token2) {
303 for (incr = 16; len > 0; len -= incr, thedata += incr) {
304 if ((int) len < incr) {
305 incr = len;
308 * XXnext two lines were DEBUGPRINTINDENT(token);
310 sprintf(buf, "dumpx%s", token);
311 debugmsg(buf, "%s: %s", token2, debug_indent());
312 if (sprint_realloc_hexstring
313 (&b3, &b3_len, &o3_len, 1, thedata, incr)) {
314 if (b3 != NULL) {
315 debugmsg(token2, "%s", b3);
317 } else {
318 if (b3 != NULL) {
319 debugmsg(token2, "%s [TRUNCATED]", b3);
322 o3_len = 0;
325 if (b3 != NULL) {
326 free(b3);
330 void
331 #if HAVE_STDARG_H
332 debugmsgtoken(const char *token, const char *format, ...)
333 #else
334 debugmsgtoken(va_alist)
335 va_dcl
336 #endif
338 va_list debugargs;
340 #if HAVE_STDARG_H
341 va_start(debugargs, format);
342 #else
343 const char *token;
345 va_start(debugargs);
346 token = va_arg(debugargs, const char *);
347 #endif
349 debugmsg(token, "%s: ", token);
351 va_end(debugargs);
355 * for speed, these shouldn't be in default_storage space
357 void
358 snmp_set_do_debugging(int val)
360 dodebug = val;
364 snmp_get_do_debugging(void)
366 return dodebug;