Do free overwritten names when registering oids. There was a comment claiming
[metze/wireshark/wip.git] / epan / oids.c
1 /* oids.c
2  * Object IDentifier Support
3  *
4  * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #include <glib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #include "emem.h"
35 #include "uat.h"
36 #include "prefs.h"
37 #include "proto.h"
38 #include "packet.h"
39 #include "report_err.h"
40 #include "filesystem.h"
41 #include "dissectors/packet-ber.h"
42
43 #ifdef HAVE_LIBSMI
44 #include <smi.h>
45
46 static gboolean oids_init_done = FALSE;
47 static gboolean load_smi_modules = FALSE;
48 static gboolean suppress_smi_errors = FALSE;
49 #endif
50
51 #define D(level,args) do if (debuglevel >= level) { printf args; printf("\n"); fflush(stdout); } while(0)
52
53 #include "oids.h"
54
55 static int debuglevel = 0;
56
57 /*
58  * From SNMPv2-SMI and X.690
59  *
60  * Counter32  ::= [APPLICATION 1] IMPLICIT INTEGER (0..4294967295)
61  * Gauge32    ::= [APPLICATION 2] IMPLICIT INTEGER (0..4294967295)
62  * Unsigned32 ::= [APPLICATION 2] IMPLICIT INTEGER (0..4294967295) (alias of Gauge32)
63  * TimeTicks  ::= [APPLICATION 3] IMPLICIT INTEGER (0..4294967295)
64  *
65  * If the BER encoding should not have the top bit set as to not become a negative number
66  * the BER encoding may take 5 octets to encode.
67  */
68
69 static const oid_value_type_t integer_type =    { FT_INT32,  BASE_DEC,  BER_CLASS_UNI, BER_UNI_TAG_INTEGER,     1,   4, OID_KEY_TYPE_INTEGER, 1};
70 static const oid_value_type_t bytes_type =      { FT_BYTES,  BASE_NONE, BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, 0,  -1, OID_KEY_TYPE_BYTES,   0};
71 static const oid_value_type_t oid_type =        { FT_OID,    BASE_NONE, BER_CLASS_UNI, BER_UNI_TAG_OID,         1,  -1, OID_KEY_TYPE_OID,     0};
72 static const oid_value_type_t ipv4_type =       { FT_IPv4,   BASE_NONE, BER_CLASS_APP, 0,                       4,   4, OID_KEY_TYPE_IPADDR,  4};
73 static const oid_value_type_t counter32_type =  { FT_UINT64, BASE_DEC,  BER_CLASS_APP, 1,                       1,   5, OID_KEY_TYPE_INTEGER, 1};
74 static const oid_value_type_t unsigned32_type = { FT_UINT64, BASE_DEC,  BER_CLASS_APP, 2,                       1,   5, OID_KEY_TYPE_INTEGER, 1};
75 static const oid_value_type_t timeticks_type =  { FT_UINT64, BASE_DEC,  BER_CLASS_APP, 3,                       1,   5, OID_KEY_TYPE_INTEGER, 1};
76 static const oid_value_type_t opaque_type =     { FT_BYTES,  BASE_NONE, BER_CLASS_APP, 4,                       1,   4, OID_KEY_TYPE_BYTES,   0};
77 static const oid_value_type_t nsap_type =       { FT_BYTES,  BASE_NONE, BER_CLASS_APP, 5,                       0,  -1, OID_KEY_TYPE_NSAP,    0};
78 static const oid_value_type_t counter64_type =  { FT_UINT64, BASE_DEC,  BER_CLASS_APP, 6,                       1,   8, OID_KEY_TYPE_INTEGER, 1};
79 static const oid_value_type_t ipv6_type =       { FT_IPv6,   BASE_NONE, BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, 16, 16, OID_KEY_TYPE_BYTES,   16};
80 static const oid_value_type_t float_type =      { FT_FLOAT,  BASE_DEC,  BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, 4,   4, OID_KEY_TYPE_WRONG,   0};
81 static const oid_value_type_t double_type =     { FT_DOUBLE, BASE_DEC,  BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, 8,   8, OID_KEY_TYPE_WRONG,   0};
82 static const oid_value_type_t ether_type =      { FT_ETHER,  BASE_NONE, BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, 6,   6, OID_KEY_TYPE_ETHER,   6};
83 static const oid_value_type_t string_type =     { FT_STRING, BASE_NONE, BER_CLASS_UNI, BER_UNI_TAG_OCTETSTRING, 0,  -1, OID_KEY_TYPE_STRING,  0};
84 static const oid_value_type_t unknown_type =    { FT_BYTES,  BASE_NONE, BER_CLASS_ANY, BER_TAG_ANY,             0,  -1, OID_KEY_TYPE_WRONG,   0};
85
86 static oid_info_t oid_root = { 0, NULL, OID_KIND_UNKNOWN, NULL, &unknown_type, -2, NULL, NULL, NULL};
87
88 static oid_info_t* add_oid(const char* name, oid_kind_t kind, const oid_value_type_t* type, oid_key_t* key, guint oid_len, guint32 *subids) {
89         guint i = 0;
90         oid_info_t* c = &oid_root;
91
92         if (!oid_root.children) {
93                 char* debug_env = getenv("WIRESHARK_DEBUG_MIBS");
94                 guint32 subid;
95
96                 debuglevel = debug_env ? strtoul(debug_env,NULL,10) : 0;
97
98                 oid_root.children = pe_tree_create(EMEM_TREE_TYPE_RED_BLACK,"oid_root");
99
100                 /*
101                  * make sure we got strings at least in the three root-children oids
102                  * that way oid_resolved() will always have a string to print
103                  */
104                 subid = 0; oid_add("itu-t",1,&subid);
105                 subid = 1; oid_add("iso",1,&subid);
106                 subid = 2; oid_add("joint-iso-itu-t",1,&subid);
107         }
108
109         oid_len--;
110
111         do {
112                 oid_info_t* n = emem_tree_lookup32(c->children,subids[i]);
113
114                 if(n) {
115                         if (i == oid_len) {
116                                 if (n->name) {
117                                         if (!g_str_equal(n->name,name)) {
118                                                 D(2,("Renaming Oid from: %s -> %s, this means the same oid is registered more than once",n->name,name));
119                                         }
120                                         /* There used to be a comment here that claimed we couldn't free
121                                          * n->name since it may be part of an hf_register_info struct
122                                          * that has been appended to the hfa GArray. I think that comment
123                                          * was wrong, because we only ever create oid_info_t's in this
124                                          * function, and we are always careful here to g_strdup the name.
125                                          * All that to justify freeing n->name in the next line, since
126                                          * doing so fixes some memory leaks. */
127                                         g_free(n->name);
128                                 }
129
130                                 n->name = g_strdup(name);
131
132                                 if (! n->value_type) {
133                                         n->value_type = type;
134                                 }
135
136                                 return n;
137                         }
138                 } else {
139                         n = g_malloc(sizeof(oid_info_t));
140                         n->subid = subids[i];
141                         n->kind = kind;
142                         n->children = pe_tree_create(EMEM_TREE_TYPE_RED_BLACK,"oid_children");
143                         n->value_hfid = -2;
144                         n->key = key;
145                         n->parent = c;
146                         n->bits = NULL;
147
148                         emem_tree_insert32(c->children,n->subid,n);
149
150                         if (i == oid_len) {
151                                 n->name = g_strdup(name);
152                                 n->value_type = type;
153                                 n->kind = kind;
154                                 return n;
155                         } else {
156                                 n->name = NULL;
157                                 n->value_type = NULL;
158                                 n->kind = OID_KIND_UNKNOWN;
159                         }
160                 }
161                 c = n;
162         } while(++i);
163
164         g_assert_not_reached();
165         return NULL;
166 }
167
168 void oid_add(const char* name, guint oid_len, guint32 *subids) {
169         g_assert(subids && *subids <= 2);
170         if (oid_len) {
171                 D(3,("\tOid (from subids): %s %s ",name?name:"NULL", oid_subid2string(subids,oid_len)));
172                 add_oid(name,OID_KIND_UNKNOWN,NULL,NULL,oid_len,subids);
173         } else {
174                 D(1,("Failed to add Oid: %s (from subids)",name?name:"NULL"));
175         }
176 }
177
178 void oid_add_from_string(const char* name, const gchar *oid_str) {
179         guint32* subids;
180         guint oid_len = oid_string2subid(oid_str, &subids);
181
182         if (oid_len) {
183                 D(3,("\tOid (from string): %s %s ",name?name:"NULL", oid_subid2string(subids,oid_len)));
184                 add_oid(name,OID_KIND_UNKNOWN,NULL,NULL,oid_len,subids);
185         } else {
186                 D(1,("Failed to add Oid: %s %s ",name?name:"NULL", oid_str?oid_str:NULL));
187         }
188 }
189
190 extern void oid_add_from_encoded(const char* name, const guint8 *oid, gint oid_len) {
191         guint32* subids;
192         guint subids_len = oid_encoded2subid(oid, oid_len, &subids);
193
194         if (subids_len) {
195                 D(3,("\tOid (from encoded): %s %s ",name, oid_subid2string(subids,subids_len)));
196                 add_oid(name,OID_KIND_UNKNOWN,NULL,NULL,subids_len,subids);
197         } else {
198                 D(1,("Failed to add Oid: %s [%d]%s ",name?name:"NULL", oid_len,bytestring_to_str(oid, oid_len, ':')));
199         }
200 }
201
202 #ifdef HAVE_LIBSMI
203 /* de-allocate storage mallocated by libsmi                            */
204 /*                                                                     */
205 /* XXX: libsmi provides access to smiFree as of libsmi v 0.4.8.        */
206 /*      On Windows: Wireshark 1.01 and later is built and distributed  */
207 /*      with libsmi 0.4.8 (or newer).                                  */
208 /*      On non-Windows systems, free() should be OK for libsmi         */
209 /*       versions older than 0.4.8.                                    */
210
211 static void smi_free(void *ptr) {
212
213 #if (SMI_VERSION_MAJOR >= 0) && (SMI_VERSION_MINOR >= 4) && (SMI_VERSION_PATCHLEVEL >= 8)
214        smiFree(ptr);
215 #else
216  #ifdef _WIN32
217  #error Invalid Windows libsmi version ?? !!
218  #endif
219 #define xx_free free  /* hack so checkAPIs.pl doesn't complain */
220        xx_free(ptr);
221 #endif
222 }
223
224
225 typedef struct smi_module_t {
226         char* name;
227 } smi_module_t;
228
229 static smi_module_t* smi_paths = NULL;
230 static guint num_smi_paths = 0;
231 static uat_t* smi_paths_uat = NULL;
232
233 static smi_module_t* smi_modules = NULL;
234 static guint num_smi_modules = 0;
235 static uat_t* smi_modules_uat = NULL;
236
237 static GString* smi_errors;
238
239 UAT_DIRECTORYNAME_CB_DEF(smi_mod,name,smi_module_t)
240
241 static void smi_error_handler(char *path, int line, int severity, char *msg, char *tag) {
242                 g_string_append_printf(smi_errors,"%s:%d %d %s %s\n",
243                                                   path ? path : "-",
244                                                   line, severity,
245                                                   tag ? tag : "-",
246                                                   msg ? msg : "");
247 }
248
249
250 static void* smi_mod_copy_cb(void* dest, const void* orig, size_t len _U_) {
251         const smi_module_t* m = orig;
252         smi_module_t* d = dest;
253
254         d->name = g_strdup(m->name);
255
256         return d;
257 }
258
259 static void smi_mod_free_cb(void* p) {
260         smi_module_t* m = p;
261         g_free(m->name);
262 }
263
264
265 static char* alnumerize(const char* name) {
266         char* s = g_strdup(name);
267         char* r = s;
268         char* w = r;
269         char c;
270
271         for (;(c = *r); r++) {
272                 if (isalnum(c) || c == '_' || c == '-' || c == '.') {
273                         *(w++) = c;
274                 } else if (c == ':' && r[1] == ':') {
275                         *(w++) = '.';
276                 }
277         }
278
279         *w = '\0';
280
281         return s;
282 }
283
284 static const oid_value_type_t* get_typedata(SmiType* smiType) {
285         /*
286          * There has to be a better way to know if a given
287          * OCTETSTRING type is actually human readable text,
288          * an address of some type or some moe specific FT_
289          * Until that is found, this is the mappping between
290          * SNMP Types and our FT_s
291          */
292         static const struct _type_mapping_t {
293                 const char* name;
294                 SmiBasetype base;
295                 const oid_value_type_t* type;
296         } types[] =  {
297                 {"IpAddress", SMI_BASETYPE_UNKNOWN, &ipv4_type},
298                 {"InetAddressIPv4",SMI_BASETYPE_UNKNOWN,&ipv4_type},
299                 {"InetAddressIPv6",SMI_BASETYPE_UNKNOWN,&ipv6_type},
300                 {"NetworkAddress",SMI_BASETYPE_UNKNOWN,&ipv4_type},
301                 {"MacAddress",SMI_BASETYPE_UNKNOWN,&ether_type},
302                 {"TimeTicks",SMI_BASETYPE_UNKNOWN,&timeticks_type},
303                 {"Ipv6Address",SMI_BASETYPE_UNKNOWN,&ipv6_type},
304                 {"TimeStamp",SMI_BASETYPE_UNKNOWN,&timeticks_type},
305                 {"DisplayString",SMI_BASETYPE_UNKNOWN,&string_type},
306                 {"SnmpAdminString",SMI_BASETYPE_UNKNOWN,&string_type},
307                 {"DateAndTime",SMI_BASETYPE_UNKNOWN,&string_type},
308                 {"Counter",SMI_BASETYPE_UNKNOWN,&counter32_type},
309                 {"Counter32",SMI_BASETYPE_UNKNOWN,&counter32_type},
310                 {"Unsigned32",SMI_BASETYPE_UNKNOWN,&unsigned32_type},
311                 {"Gauge",SMI_BASETYPE_UNKNOWN,&unsigned32_type},
312                 {"Gauge32",SMI_BASETYPE_UNKNOWN,&unsigned32_type},
313                 {"NsapAddress",SMI_BASETYPE_UNKNOWN,&nsap_type},
314                 {"i32",SMI_BASETYPE_INTEGER32,&integer_type},
315                 {"octets",SMI_BASETYPE_OCTETSTRING,&bytes_type},
316                 {"oid",SMI_BASETYPE_OBJECTIDENTIFIER,&oid_type},
317                 {"u32",SMI_BASETYPE_UNSIGNED32,&unsigned32_type},
318                 {"u64",SMI_BASETYPE_UNSIGNED64,&counter64_type},
319                 {"f32",SMI_BASETYPE_FLOAT32,&float_type},
320                 {"f64",SMI_BASETYPE_FLOAT64,&double_type},
321                 {"f128",SMI_BASETYPE_FLOAT128,&bytes_type},
322                 {"enum",SMI_BASETYPE_ENUM,&integer_type},
323                 {"bits",SMI_BASETYPE_BITS,&bytes_type},
324                 {"unk",SMI_BASETYPE_UNKNOWN,&unknown_type},
325                 {NULL,0,NULL}
326         };
327         const struct _type_mapping_t* t;
328         SmiType* sT = smiType;
329
330         if (!smiType) return NULL;
331
332         do {
333                 for (t = types; t->type ; t++ ) {
334                         char* name = smiRenderType(sT, SMI_RENDER_NAME);
335                         if (name && t->name && g_str_equal(name, t->name )) {
336                                 smi_free(name);
337                                 return t->type;
338                         }
339                         if (name) {
340                                 smi_free (name);
341                         }
342                 }
343         } while(( sT  = smiGetParentType(sT) ));
344
345         for (t = types; t->type ; t++ ) {
346                 if(smiType->basetype == t->base) {
347                         return t->type;
348                 }
349         }
350
351         return &unknown_type;
352 }
353
354 static guint get_non_implicit_size(SmiType* sT) {
355         SmiRange *sR;
356         guint size = 0xffffffff;
357
358         switch (sT->basetype) {
359                 case SMI_BASETYPE_OCTETSTRING:
360                 case SMI_BASETYPE_OBJECTIDENTIFIER:
361                         break;
362                 default:
363                         return 0;
364         }
365
366         for ( ; sT; sT = smiGetParentType(sT) ) {
367                 for (sR = smiGetFirstRange(sT); sR ; sR = smiGetNextRange(sR)) {
368                         if (size == 0xffffffff) {
369                                 if (sR->minValue.value.unsigned32 == sR->maxValue.value.unsigned32) {
370                                         size = sR->minValue.value.unsigned32;
371                                 } else {
372                                         return 0;
373                                 }
374                         } else {
375                                 if (sR->minValue.value.unsigned32 != size || sR->maxValue.value.unsigned32 != size) {
376                                         return 0;
377                                 }
378                         }
379                 }
380         }
381
382         return size == 0xffffffff ? 0 : size;
383 }
384
385
386 static inline oid_kind_t smikind(SmiNode* sN, oid_key_t** key_p) {
387         *key_p = NULL;
388
389         switch(sN->nodekind) {
390                 case SMI_NODEKIND_ROW: {
391                         SmiElement* sE;
392                         oid_key_t* kl = NULL;
393                         const oid_value_type_t* typedata = NULL;
394                         gboolean implied;
395
396                         switch (sN->indexkind) {
397                                 case SMI_INDEX_INDEX:
398                                         break;
399                                 case SMI_INDEX_AUGMENT:
400                                 case SMI_INDEX_REORDER:
401                                 case SMI_INDEX_SPARSE:
402                                 case SMI_INDEX_EXPAND:
403                                         sN = smiGetRelatedNode(sN);
404                                         break;
405                                 case SMI_INDEX_UNKNOWN:
406                                         return OID_KIND_UNKNOWN;
407                         };
408
409                         implied = sN->implied;
410
411                         for (sE = smiGetFirstElement(sN); sE; sE = smiGetNextElement(sE)) {
412                                 SmiNode* elNode =  smiGetElementNode(sE) ;
413                                 SmiType* elType = smiGetNodeType(elNode);
414                                 oid_key_t* k;
415                                 guint non_implicit_size = 0;
416                                 char *oid1, *oid2;
417
418                                 if (elType) {
419                                         non_implicit_size = get_non_implicit_size(elType);
420                                 }
421
422                                 typedata =  get_typedata(elType);
423
424                                 k = g_malloc(sizeof(oid_key_t));
425
426                                 oid1 = smiRenderOID(sN->oidlen, sN->oid, SMI_RENDER_QUALIFIED);
427                                 oid2 = smiRenderOID(elNode->oidlen, elNode->oid, SMI_RENDER_NAME);
428                                 k->name = g_strdup_printf("%s.%s", oid1, oid2);
429                                 smi_free (oid1);
430                                 smi_free (oid2);
431
432                                 k->hfid = -2;
433                                 k->ft_type = typedata ? typedata->ft_type : FT_BYTES;
434                                 k->display = typedata ? typedata->display : BASE_NONE;
435                                 k->next = NULL;
436
437
438                                 if (typedata) {
439                                         k->key_type = typedata->keytype;
440                                         k->num_subids = typedata->keysize;
441                                 } else {
442                                         if (elType) {
443                                                 switch (elType->basetype) {
444                                                         case SMI_BASETYPE_BITS:
445                                                         case SMI_BASETYPE_OCTETSTRING: {
446                                                                 k->key_type = OID_KEY_TYPE_BYTES;
447                                                                 k->num_subids = non_implicit_size;
448                                                                 break;
449                                                         }
450                                                         case SMI_BASETYPE_ENUM:
451                                                         case SMI_BASETYPE_OBJECTIDENTIFIER:
452                                                         case SMI_BASETYPE_INTEGER32:
453                                                         case SMI_BASETYPE_UNSIGNED32:
454                                                         case SMI_BASETYPE_INTEGER64:
455                                                         case SMI_BASETYPE_UNSIGNED64:
456                                                                 k->key_type = OID_KEY_TYPE_INTEGER;
457                                                                 k->num_subids = 1;
458                                                                 break;
459                                                         default:
460                                                                 k->key_type = OID_KEY_TYPE_WRONG;
461                                                                 k->num_subids = 0;
462                                                                 break;
463                                                 }
464                                         } else {
465                                                 k->key_type = OID_KEY_TYPE_WRONG;
466                                                 k->num_subids = 0;
467                                                 break;
468                                         }
469                                 }
470
471                                 if (!*key_p) *key_p = k;
472                                 if (kl) kl->next = k;
473
474                                 kl = k;
475                         }
476
477                         if (implied && kl) {
478                                 switch (kl->key_type) {
479                                         case OID_KEY_TYPE_BYTES:  kl->key_type = OID_KEY_TYPE_IMPLIED_BYTES; break;
480                                         case OID_KEY_TYPE_STRING: kl->key_type = OID_KEY_TYPE_IMPLIED_STRING; break;
481                                         case OID_KEY_TYPE_OID:    kl->key_type = OID_KEY_TYPE_IMPLIED_OID; break;
482                                         default: break;
483                                 }
484                         }
485
486                         return OID_KIND_ROW;
487                 }
488                 case SMI_NODEKIND_NODE: return OID_KIND_NODE;
489                 case SMI_NODEKIND_SCALAR: return OID_KIND_SCALAR;
490                 case SMI_NODEKIND_TABLE: return OID_KIND_TABLE;
491                 case SMI_NODEKIND_COLUMN: return OID_KIND_COLUMN;
492                 case SMI_NODEKIND_NOTIFICATION: return OID_KIND_NOTIFICATION;
493                 case SMI_NODEKIND_GROUP: return OID_KIND_GROUP;
494                 case SMI_NODEKIND_COMPLIANCE: return OID_KIND_COMPLIANCE;
495                 case SMI_NODEKIND_CAPABILITIES: return OID_KIND_CAPABILITIES;
496                 default: return OID_KIND_UNKNOWN;
497         }
498 }
499
500 #define IS_ENUMABLE(ft) ( (ft == FT_UINT8) || (ft == FT_UINT16) || (ft == FT_UINT24) || (ft == FT_UINT32) \
501                                                    || (ft == FT_INT8) || (ft == FT_INT16) || (ft == FT_INT24) || (ft == FT_INT32) \
502                                                    || (ft == FT_UINT64) || (ft == FT_INT64) )
503
504 static void unregister_mibs(void) {
505         /* TODO: Unregister "MIBs" proto and clean up field array and subtree array.
506          * Wireshark does not support that yet. :-( */
507
508         /* smiExit(); */
509 }
510
511 static void restart_needed_warning(void) {
512         if (oids_init_done)
513                 report_failure("Wireshark needs to be restarted for these changes to take effect");
514 }
515
516 static void register_mibs(void) {
517         SmiModule *smiModule;
518         SmiNode *smiNode;
519         guint i;
520         int proto_mibs = -1;
521         GArray* hfa = g_array_new(FALSE,TRUE,sizeof(hf_register_info));
522         GArray* etta = g_array_new(FALSE,TRUE,sizeof(gint*));
523         gchar* path_str;
524
525         if (!load_smi_modules) {
526                 D(1,("OID resolution not enabled"));
527                 return;
528         }
529
530         /* TODO: Remove this workaround when unregistration of "MIBs" proto is solved.
531          * Wireshark does not support that yet. :-( */
532         if (oids_init_done) {
533                 D(1,("Exiting register_mibs() to avoid double registration of MIBs proto."));
534                 return;
535         } else {
536                 oids_init_done = TRUE;
537         }
538
539         smiInit(NULL);
540
541         smi_errors = g_string_new("");
542         smiSetErrorHandler(smi_error_handler);
543
544         path_str = oid_get_default_mib_path();
545         D(1,("SMI Path: '%s'",path_str));
546
547         smiSetPath(path_str);
548
549         for(i=0;i<num_smi_modules;i++) {
550                 if (!smi_modules[i].name) continue;
551
552                 if (smiIsLoaded(smi_modules[i].name)) {
553                         continue;
554                 } else {
555                         char* mod_name =  smiLoadModule(smi_modules[i].name);
556                         if (mod_name)
557                                 D(2,("Loaded: '%s'[%d] as %s",smi_modules[i].name,i,mod_name ));
558                         else
559                                 D(1,("Failed to load: '%s'[%d]",smi_modules[i].name,i));
560                 }
561         }
562
563         if (smi_errors->len) {
564                 if (!suppress_smi_errors) {
565                         report_failure("The following errors were found while loading the MIBS:\n%s\n\n"
566                                            "The Current Path is: %s\n\nYou can avoid this error message "
567                                            "by removing the missing MIB modules at Edit -> Preferences"
568                                            " -> Name Resolution -> SMI (MIB and PIB) modules or by "
569                                            "installing them.\n" , smi_errors->str , path_str);
570                 }
571                 D(1,("Errors while loading:\n%s\n",smi_errors->str));
572         }
573
574         g_free(path_str);
575         g_string_free(smi_errors,TRUE);
576
577         for (smiModule = smiGetFirstModule();
578                  smiModule;
579                  smiModule = smiGetNextModule(smiModule)) {
580
581                 D(3,("\tModule: %s", smiModule->name));
582
583                 /* TODO: Check libsmi version at compile time and disable this
584                  * workaround for libsmi versions where this problem is fixed.
585                  * Currently there is no such version. :-(
586                  */
587                 if (smiModule->conformance == 1) {
588                         if (!suppress_smi_errors) {
589                                 report_failure("Stopped processing module %s due to "
590                                         "error(s) to prevent potential crash in libsmi.\n"
591                                         "Module's conformance level: %d.\n"
592                                         "See details at: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=560325\n",
593                                          smiModule->name, smiModule->conformance);
594                         }
595                         continue;
596                 }
597                 for (smiNode = smiGetFirstNode(smiModule, SMI_NODEKIND_ANY);
598                          smiNode;
599                          smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_ANY)) {
600
601                         SmiType* smiType =  smiGetNodeType(smiNode);
602                         const oid_value_type_t* typedata =  get_typedata(smiType);
603                         oid_key_t* key;
604                         oid_kind_t kind = smikind(smiNode,&key);
605                         char *oid = smiRenderOID(smiNode->oidlen, smiNode->oid, SMI_RENDER_QUALIFIED);
606                         oid_info_t* oid_data = add_oid(oid,
607                                                        kind,
608                                                        typedata,
609                                                        key,
610                                                        smiNode->oidlen,
611                                                        smiNode->oid);
612                         smi_free (oid);
613
614                         D(4,("\t\tNode: kind=%d oid=%s name=%s ",
615                                  oid_data->kind, oid_subid2string(smiNode->oid, smiNode->oidlen), oid_data->name ));
616
617                         if ( typedata && oid_data->value_hfid == -2 ) {
618                                 SmiNamedNumber* smiEnum;
619                                 hf_register_info hf = { &(oid_data->value_hfid), {
620                                         oid_data->name,
621                                         alnumerize(oid_data->name),
622                                         typedata->ft_type,
623                                         typedata->display,
624                                         NULL,
625                                         0,
626                                         smiRenderOID(smiNode->oidlen, smiNode->oid, SMI_RENDER_ALL),
627                                         HFILL }};
628
629                                 /* Don't allow duplicate blurb/name */
630                                 if (strcmp(hf.hfinfo.blurb, hf.hfinfo.name) == 0) {
631                                         smi_free((void *) hf.hfinfo.blurb);
632                                         hf.hfinfo.blurb = NULL;
633                                 }
634
635                                 oid_data->value_hfid = -1;
636
637                                 if ( IS_ENUMABLE(hf.hfinfo.type) && (smiEnum = smiGetFirstNamedNumber(smiType))) {
638                                         GArray* vals = g_array_new(TRUE,TRUE,sizeof(value_string));
639
640                                         for(;smiEnum; smiEnum = smiGetNextNamedNumber(smiEnum)) {
641                                                 if (smiEnum->name) {
642                                                         value_string val = {smiEnum->value.value.integer32,g_strdup(smiEnum->name)};
643                                                         g_array_append_val(vals,val);
644                                                 }
645                                         }
646
647                                         hf.hfinfo.strings = vals->data;
648                                         g_array_free(vals,FALSE);
649                                 }
650 #if 0 /* packet-snmp does not handle bits yet */
651                         } else if (smiType->basetype == SMI_BASETYPE_BITS && ( smiEnum = smiGetFirstNamedNumber(smiType) )) {
652                                 guint n = 0;
653                                 oid_bits_info_t* bits = g_malloc(sizeof(oid_bits_info_t));
654                                 gint* ettp = &(bits->ett);
655
656                                 bits->num = 0;
657                                 bits->ett = -1;
658
659                                 g_array_append_val(etta,ettp);
660
661                                 for(;smiEnum; smiEnum = smiGetNextNamedNumber(smiEnum), bits->num++);
662
663                                 bits->data = g_malloc(sizeof(struct _oid_bit_t)*bits->num);
664
665                                 for(smiEnum = smiGetFirstNamedNumber(smiType),n=0;
666                                         smiEnum;
667                                         smiEnum = smiGetNextNamedNumber(smiEnum),n++) {
668                                         guint mask = 1 << (smiEnum->value.value.integer32 % 8);
669                                         char* base = alnumerize(oid_data->name);
670                                         char* ext = alnumerize(smiEnum->name);
671                                         hf_register_info hf2 = { &(bits->data[n].hfid), { NULL, NULL, FT_UINT8, BASE_HEX, NULL, mask, NULL, HFILL }};
672
673                                         bits->data[n].hfid = -1;
674                                         bits->data[n].offset = smiEnum->value.value.integer32 / 8;
675
676                                         hf2.hfinfo.name = g_strdup_printf("%s:%s",oid_data->name,smiEnum->name);
677                                         hf2.hfinfo.abbrev = g_strdup_printf("%s.%s",base,ext);
678
679                                         g_free(base);
680                                         g_free(ext);
681                                         g_array_append_val(hfa,hf2);
682                                 }
683 #endif /* packet-snmp does not use this yet */
684                                 g_array_append_val(hfa,hf);
685                         }
686
687                         if ((key = oid_data->key)) {
688                                 for(; key; key = key->next) {
689                                         hf_register_info hf = { &(key->hfid), {
690                                                 key->name,
691                                                 alnumerize(key->name),
692                                                 key->ft_type,
693                                                 key->display,
694                                                 NULL,
695                                                 0,
696                                                 NULL,
697                                                 HFILL }};
698
699                                         D(5,("\t\t\tIndex: name=%s subids=%d key_type=%d",
700                                                  key->name, key->num_subids, key->key_type ));
701
702                                         if (key->hfid == -2) {
703                                                 g_array_append_val(hfa,hf);
704                                                 key->hfid = -1;
705                                         } else {
706                                                 g_free((void*)hf.hfinfo.abbrev);
707                                         }
708                                 }
709                         }
710                 }
711         }
712
713         proto_mibs = proto_register_protocol("MIBs", "MIBS", "mibs");
714
715         proto_register_field_array(proto_mibs, (hf_register_info*)(void*)hfa->data, hfa->len);
716
717         proto_register_subtree_array((gint**)(void*)etta->data, etta->len);
718
719
720         g_array_free(etta,TRUE);
721         g_array_free(hfa,FALSE);
722 }
723 #endif
724
725 void oid_pref_init(module_t *nameres)
726 {
727 #ifdef HAVE_LIBSMI
728         static uat_field_t smi_fields[] = {
729                 UAT_FLD_CSTRING(smi_mod,name,"Module name","The module's name"),
730                 UAT_END_FIELDS
731         };
732         static uat_field_t smi_paths_fields[] = {
733                 UAT_FLD_DIRECTORYNAME(smi_mod,name,"Directory path","The directory name"),
734                 UAT_END_FIELDS
735         };
736
737     prefs_register_bool_preference(nameres, "load_smi_modules",
738                                   "Enable OID resolution",
739                                   "You must restart Wireshark for this change to take effect",
740                                   &load_smi_modules);
741
742     prefs_register_bool_preference(nameres, "suppress_smi_errors",
743                                   "Suppress SMI errors",
744                                   "Some errors can be ignored. If unsure, set to false.",
745                                   &suppress_smi_errors);
746
747     smi_paths_uat = uat_new("SMI Paths",
748                             sizeof(smi_module_t),
749                             "smi_paths",
750                             FALSE,
751                             (void*)&smi_paths,
752                             &num_smi_paths,
753     /* affects dissection of packets (as the MIBs and PIBs affect the
754        interpretation of e.g. SNMP variable bindings), but not set of
755        named fields
756
757        XXX - if named fields are generated from the MIBs and PIBs
758        for particular variable bindings, this *does* affect the set
759        of named fields! */
760                             UAT_AFFECTS_DISSECTION,
761                             "ChSNMPSMIPaths",
762                             smi_mod_copy_cb,
763                             NULL,
764                             smi_mod_free_cb,
765                             restart_needed_warning,
766                             smi_paths_fields);
767
768     prefs_register_uat_preference(nameres,
769                                   "smi_paths",
770                                   "SMI (MIB and PIB) paths",
771                                   "Search paths for SMI (MIB and PIB) modules. You must\n"
772                                   "restart Wireshark for these changes to take effect.",
773                                   smi_paths_uat);
774
775     smi_modules_uat = uat_new("SMI Modules",
776                               sizeof(smi_module_t),
777                               "smi_modules",
778                               FALSE,
779                               (void*)&smi_modules,
780                               &num_smi_modules,
781     /* affects dissection of packets (as the MIBs and PIBs affect the
782        interpretation of e.g. SNMP variable bindings), but not set of
783        named fields
784
785        XXX - if named fields are generated from the MIBs and PIBs
786        for particular variable bindings, would this affect the set
787        of named fields? */
788                               UAT_AFFECTS_DISSECTION,
789                               "ChSNMPSMIModules",
790                               smi_mod_copy_cb,
791                               NULL,
792                               smi_mod_free_cb,
793                               restart_needed_warning,
794                               smi_fields);
795
796     prefs_register_uat_preference(nameres,
797                                   "smi_modules",
798                                   "SMI (MIB and PIB) modules",
799                                   "List of enabled SMI (MIB and PIB) modules. You must\n"
800                                   "restart Wireshark for these changes to take effect.",
801                                   smi_modules_uat);
802
803 #else
804     prefs_register_static_text_preference(nameres, "load_smi_modules_static",
805                             "Enable OID resolution: N/A", 
806                             "Support for OID resolution was not compiled into this version of Wireshark");
807
808     prefs_register_static_text_preference(nameres, "suppress_smi_errors_static",
809                             "Suppress SMI errors: N/A", 
810                             "Support for OID resolution was not compiled into this version of Wireshark");
811
812     prefs_register_static_text_preference(nameres, "smi_module_path",
813                             "SMI (MIB and PIB) modules and paths: N/A", 
814                             "Support for OID resolution was not compiled into this version of Wireshark");
815 #endif
816 }
817
818 void oids_init(void) {
819 #ifdef HAVE_LIBSMI
820         register_mibs();
821 #else
822         D(1,("libsmi disabled oid resolution not enabled"));
823 #endif
824 }
825
826 void oids_cleanup(void) {
827 #ifdef HAVE_LIBSMI
828         unregister_mibs();
829 #else
830         D(1,("libsmi disabled oid resolution not enabled"));
831 #endif
832 }
833
834 const char* oid_subid2string(guint32* subids, guint len) {
835         char* s = ep_alloc0(((len)*11)+1);
836         char* w = s;
837
838         if(!subids)
839                 return "*** Empty OID ***";
840
841         do {
842                 w += g_snprintf(w,12,"%u.",*subids++);
843         } while(--len);
844
845         if (w!=s) *(w-1) = '\0'; else *(s) = '\0';
846
847         return s;
848 }
849
850 static guint check_num_oid(const char* str) {
851         const char* r = str;
852         char c = '\0';
853         guint n = 0;
854
855         D(8,("check_num_oid: '%s'",str));
856         if (!r || *r == '.' || *r == '\0') return 0;
857
858         do {
859                 D(9,("\tcheck_num_oid: '%c' %d",*r,n));
860                 switch(*r) {
861                         case '.':
862                                 n++;
863                                 if (c == '.') return 0;
864                         case '1' : case '2' : case '3' : case '4' : case '5' :
865                         case '6' : case '7' : case '8' : case '9' : case '0' :
866                                 continue;
867                         case '\0':
868                                 n++;
869                                 break;
870                         default:
871                                 return 0;
872                 }
873         } while((c = *r++));
874
875         if (c == '.') return 0;
876
877         return n;
878 }
879
880 guint oid_string2subid(const char* str, guint32** subids_p) {
881         const char* r = str;
882         guint32* subids;
883         guint32* subids_overflow;
884         guint n = check_num_oid(str);
885         /*
886          * we cannot handle sub-ids greater than 32bytes
887          * keep a pilot subid of 64 bytes to check the limit
888          */
889         guint64 subid = 0;
890
891         D(6,("oid_string2subid: str='%s'",str));
892
893         if (!n) {
894                 *subids_p = NULL;
895                 return 0;
896         }
897
898         *subids_p = subids = ep_alloc0(sizeof(guint32)*n);
899         subids_overflow = subids + n;
900         do switch(*r) {
901                 case '.':
902                         subid = 0;
903                         subids++;
904                         continue;
905                 case '1' : case '2' : case '3' : case '4' : case '5' :
906                 case '6' : case '7' : case '8' : case '9' : case '0' :
907                         subid *= 10;
908                         subid += *r - '0';
909
910                         if( subids >= subids_overflow ||  subid > 0xffffffff) {
911                                 *subids_p=NULL;
912                                 return 0;
913                         }
914
915                         *(subids) *= 10;
916                         *(subids) += *r - '0';
917                         continue;
918                 case '\0':
919                         break;
920                 default:
921                         return 0;
922         } while(*r++);
923
924         return n;
925 }
926
927
928 guint oid_encoded2subid(const guint8 *oid_bytes, gint oid_len, guint32** subids_p) {
929         gint i;
930         guint n = 1;
931         gboolean is_first = TRUE;
932         guint32* subids;
933         guint32* subid_overflow;
934         /*
935          * we cannot handle sub-ids greater than 32bytes
936          * have the subid in 64 bytes to be able to check the limit
937          */
938         guint64 subid = 0;
939
940         for (i=0; i<oid_len; i++) { if (! (oid_bytes[i] & 0x80 )) n++; }
941
942         *subids_p = subids = ep_alloc(sizeof(guint32)*n);
943         subid_overflow = subids+n;
944
945         /* If n is 1 then we found no bytes in the OID with first bit cleared,
946          * so initialize our one byte to zero and return. This *seems* to be
947          * the right thing to do in this situation, and at the very least it
948          * avoids uninitialized memory errors that would otherwise occur. */
949         if (n == 1) {
950                 *subids = 0;
951                 return n;
952         }
953
954         for (i=0; i<oid_len; i++){
955                 guint8 byte = oid_bytes[i];
956
957                 subid <<= 7;
958                 subid |= byte & 0x7F;
959
960                 if (byte & 0x80) {
961                         continue;
962                 }
963
964                 if (is_first) {
965                         guint32 subid0 = 0;
966
967                         if (subid >= 40) { subid0++; subid-=40; }
968                         if (subid >= 40) { subid0++; subid-=40; }
969
970                         *subids++ = subid0;
971
972                         is_first = FALSE;
973                 }
974
975                 if( subids >= subid_overflow || subid > 0xffffffff) {
976                         *subids_p=NULL;
977                         return 0;
978                 }
979
980                 *subids++ = (guint32)subid;
981                 subid = 0;
982         }
983
984         return n;
985 }
986
987 oid_info_t* oid_get(guint len, guint32* subids, guint* matched, guint* left) {
988         oid_info_t* curr_oid = &oid_root;
989         guint i;
990
991         if(!(subids && *subids <= 2)) {
992                 *matched = 0;
993                 *left = len;
994                 return curr_oid;
995         }
996
997         for( i=0; i < len; i++) {
998                 oid_info_t* next_oid = emem_tree_lookup32(curr_oid->children,subids[i]);
999                 if (next_oid) {
1000                         curr_oid = next_oid;
1001                 } else {
1002                         goto done;
1003                 }
1004         }
1005 done:
1006         *matched = i;
1007         *left = len - i;
1008         return curr_oid;
1009 }
1010
1011
1012 oid_info_t* oid_get_from_encoded(const guint8 *bytes, gint byteslen, guint32** subids_p, guint* matched_p, guint* left_p) {
1013         guint subids_len = oid_encoded2subid(bytes, byteslen, subids_p);
1014         return oid_get(subids_len, *subids_p, matched_p, left_p);
1015 }
1016
1017 oid_info_t* oid_get_from_string(const gchar *oid_str, guint32** subids_p, guint* matched, guint* left) {
1018         guint subids_len = oid_string2subid(oid_str, subids_p);
1019         return oid_get(subids_len, *subids_p, matched, left);
1020 }
1021
1022 const gchar *oid_resolved_from_encoded(const guint8 *oid, gint oid_len) {
1023         guint32 *subid_oid;
1024         guint subid_oid_length = oid_encoded2subid(oid, oid_len, &subid_oid);
1025
1026         return oid_resolved(subid_oid_length, subid_oid);
1027 }
1028
1029
1030 guint oid_subid2encoded(guint subids_len, guint32* subids, guint8** bytes_p) {
1031         guint bytelen = 0;
1032         guint i;
1033         guint32 subid;
1034         guint8* b;
1035
1036         if ( !subids || subids_len <= 0) {
1037                 *bytes_p = NULL;
1038                 return 0;
1039         }
1040
1041         subid = (subids[0] * 40) + subids[1];
1042         i = 2;
1043
1044         do {
1045                 if (subid <= 0x0000007F) {
1046                         bytelen += 1;
1047                 } else if (subid <= 0x00003FFF ) {
1048                         bytelen += 2;
1049                 } else if (subid <= 0x001FFFFF ) {
1050                         bytelen += 3;
1051                 } else if (subid <= 0x0FFFFFFF ) {
1052                         bytelen += 4;
1053                 } else {
1054                         bytelen += 5;
1055                 }
1056
1057                         subid = subids[i];
1058         } while ( i++ < subids_len );
1059
1060         *bytes_p = b = ep_alloc(bytelen);
1061
1062         subid = (subids[0] * 40) + subids[1];
1063         i = 2;
1064
1065         do {
1066                 guint len;
1067
1068                 if ((subid <= 0x0000007F )) len = 1;
1069                 else if ((subid <= 0x00003FFF )) len = 2;
1070                 else if ((subid <= 0x001FFFFF )) len = 3;
1071                 else if ((subid <= 0x0FFFFFFF )) len = 4;
1072                 else len = 5;
1073
1074                 switch(len) {
1075                         default: *bytes_p=NULL; return 0;
1076                         case 5: *(b++) = ((subid & 0xF0000000) >> 28) | 0x80;
1077                         case 4: *(b++) = ((subid & 0x0FE00000) >> 21) | 0x80;
1078                         case 3: *(b++) = ((subid & 0x001FC000) >> 14) | 0x80;
1079                         case 2: *(b++) = ((subid & 0x00003F10) >> 7)  | 0x80;
1080                         case 1: *(b++) =   subid & 0x0000007F ; break;
1081                 }
1082
1083                 subid = subids[i];
1084         } while ( i++ < subids_len);
1085
1086         return bytelen;
1087 }
1088
1089 const gchar* oid_encoded2string(const guint8* encoded, guint len) {
1090         guint32* subids;
1091         guint subids_len = oid_encoded2subid(encoded, len, &subids);
1092
1093         if (subids_len) {
1094                 return oid_subid2string(subids,subids_len);
1095         } else {
1096                 return "";
1097         }
1098 }
1099
1100
1101
1102 guint oid_string2encoded(const char *oid_str, guint8 **bytes) {
1103         guint32* subids;
1104         guint32 subids_len;
1105         guint byteslen;
1106
1107                 if ( ( subids_len = oid_string2subid(oid_str, &subids) )
1108                          &&
1109                          ( byteslen = oid_subid2encoded(subids_len, subids, bytes) )  ) {
1110                         return byteslen;
1111                 }
1112         return 0;
1113 }
1114
1115 const gchar *oid_resolved_from_string(const gchar *oid_str) {
1116         guint32 *subid_oid;
1117         guint subid_oid_length = oid_string2subid(oid_str, &subid_oid);
1118
1119         return oid_resolved(subid_oid_length, subid_oid);
1120 }
1121
1122 const gchar *oid_resolved(guint32 num_subids, guint32* subids) {
1123         guint matched;
1124         guint left;
1125         oid_info_t* oid;
1126
1127         if(! (subids && *subids <= 2 ))
1128                 return "*** Malformed OID ***";
1129
1130         oid = oid_get(num_subids, subids, &matched, &left);
1131
1132         while (! oid->name ) {
1133                 if (!(oid = oid->parent)) {
1134                         return oid_subid2string(subids,num_subids);
1135                 }
1136                 left++;
1137                 matched--;
1138         }
1139
1140         if (left) {
1141                 return ep_strdup_printf("%s.%s",
1142                                         oid->name ? oid->name : oid_subid2string(subids,matched),
1143                                         oid_subid2string(&(subids[matched]),left));
1144         } else {
1145                 return oid->name ? oid->name : oid_subid2string(subids,matched);
1146         }
1147 }
1148
1149 extern void oid_both(guint oid_len, guint32 *subids, char** resolved_p, char** numeric_p) {
1150         *resolved_p = (void*)oid_resolved(oid_len,subids);
1151         *numeric_p = (void*)oid_subid2string(subids,oid_len);
1152 }
1153
1154 extern void oid_both_from_encoded(const guint8 *oid, gint oid_len, char** resolved_p, char** numeric_p) {
1155         guint32* subids;
1156         guint subids_len = oid_encoded2subid(oid, oid_len, &subids);
1157         *resolved_p = (void*)oid_resolved(subids_len,subids);
1158         *numeric_p = (void*)oid_subid2string(subids,subids_len);
1159 }
1160
1161 extern void oid_both_from_string(const gchar *oid_str, char** resolved_p, char** numeric_p) {
1162         guint32* subids;
1163         guint subids_len = oid_string2subid(oid_str, &subids);
1164         *resolved_p = (void*)oid_resolved(subids_len,subids);
1165         *numeric_p = (void*)oid_subid2string(subids,subids_len);
1166 }
1167
1168 /**
1169  * Fetch the default OID path.
1170  */
1171 extern gchar *
1172 oid_get_default_mib_path(void) {
1173 #ifdef HAVE_LIBSMI
1174         GString* path_str;
1175         gchar *path_ret;
1176         char *path;
1177         guint i;
1178
1179         path_str = g_string_new("");
1180         
1181         if (!load_smi_modules) {
1182                 D(1,("OID resolution not enabled"));
1183                 return path_str->str;
1184         }
1185 #ifdef _WIN32
1186 #define PATH_SEPARATOR ";"
1187         path = get_datafile_path("snmp\\mibs");
1188         g_string_append_printf(path_str, "%s;", path);
1189         g_free (path);
1190
1191         path = get_persconffile_path("snmp\\mibs", FALSE, FALSE);
1192         g_string_append_printf(path_str, "%s", path);
1193         g_free (path);
1194 #else
1195 #define PATH_SEPARATOR ":"
1196         path = smiGetPath();
1197         g_string_append(path_str, "/usr/share/snmp/mibs");
1198         if (strlen(path) > 0 ) {
1199                 g_string_append(path_str, PATH_SEPARATOR);
1200         }
1201         g_string_append_printf(path_str, "%s", path);
1202         free (path);
1203 #endif
1204
1205         for(i=0;i<num_smi_paths;i++) {
1206                 if (!( smi_paths[i].name && *smi_paths[i].name))
1207                         continue;
1208
1209                 g_string_append_printf(path_str,PATH_SEPARATOR "%s",smi_paths[i].name);
1210         }
1211
1212         path_ret = path_str->str;
1213         g_string_free(path_str, FALSE);
1214         return path_ret;
1215 #else /* HAVE_LIBSMI */
1216         return g_strdup("");
1217 #endif
1218 }
1219
1220 #ifdef DEBUG_OIDS
1221 char* oid_test_a2b(guint32 num_subids, guint32* subids) {
1222         guint8* sub2enc;
1223         guint8* str2enc;
1224         guint32* enc2sub;
1225         guint32* str2sub;
1226         const char* sub2str = oid_subid2string(subids, num_subids);
1227         guint sub2enc_len = oid_subid2encoded(num_subids, subids,&sub2enc);
1228         guint enc2sub_len = oid_encoded2subid(sub2enc, sub2enc_len, &enc2sub);
1229         const char* enc2str = oid_encoded2string(sub2enc, sub2enc_len);
1230         guint str2enc_len = oid_string2encoded(sub2str,&str2enc);
1231         guint str2sub_len = oid_string2subid(sub2str,&str2sub);
1232
1233         return ep_strdup_printf(
1234                                                         "oid_subid2string=%s \n"
1235                                                         "oid_subid2encoded=[%d]%s \n"
1236                                                         "oid_encoded2subid=%s \n "
1237                                                         "oid_encoded2string=%s \n"
1238                                                         "oid_string2encoded=[%d]%s \n"
1239                                                         "oid_string2subid=%s \n "
1240                                                         ,sub2str
1241                                                         ,sub2enc_len,bytestring_to_str(sub2enc, sub2enc_len, ':')
1242                                                         ,enc2sub ? oid_subid2string(enc2sub,enc2sub_len) : "-"
1243                                                         ,enc2str
1244                                                         ,str2enc_len,bytestring_to_str(str2enc, str2enc_len, ':')
1245                                                         ,str2sub ? oid_subid2string(str2sub,str2sub_len) : "-"
1246                                                         );
1247 }
1248
1249 void add_oid_debug_subtree(oid_info_t* oid_info, proto_tree *tree) {
1250         static const char* oid_kinds[] = { "Unknown", "Node", "Scalar", "Table", "Row", "Column", "Notification", "Group", "Compliance", "Capabilities"};
1251         static const char* key_types[] = {"OID_KEY_TYPE_WRONG","OID_KEY_TYPE_INTEGER",
1252                                                                                 "OID_KEY_TYPE_FIXED_STRING","OID_KEY_TYPE_FIXED_BYTES","OID_KEY_TYPE_STRING",
1253                                                                                 "OID_KEY_TYPE_BYTES","OID_KEY_TYPE_NSAP","OID_KEY_TYPE_OID","OID_KEY_TYPE_IPADDR"};
1254         proto_item* pi = proto_tree_add_text(tree,NULL,0,0,
1255         "OidInfo: Name='%s' sub-id=%u  kind=%s  hfid=%d",
1256         oid_info->name ? oid_info->name : "",
1257         oid_info->subid,
1258         oid_info->kind <= OID_KIND_CAPABILITIES ? oid_kinds[oid_info->kind] : "BROKEN",
1259         oid_info->value_hfid);
1260         proto_tree* pt = proto_item_add_subtree(pi,0);
1261         oid_key_t* key;
1262
1263         for(key = oid_info->key; key; key = key->next) {
1264                 proto_tree_add_text(pt,NULL,0,0,
1265                 "Key: name='%s' num_subids=%d type=%s",
1266                 key->name,
1267                 key->key_type <= OID_KEY_TYPE_IPADDR ? key_types[key->key_type] : "BROKEN"
1268                 );
1269         };
1270
1271         if (oid_info->parent) {
1272                 pi = proto_tree_add_text(pt,NULL,0,0,"Parent:");
1273                 pt = proto_item_add_subtree(pi,0);
1274                 add_oid_debug_subtree(oid_info->parent, pt);
1275         }
1276 }
1277 #endif
1278
1279 /*
1280  * Editor modelines
1281  *
1282  * Local Variables:
1283  * c-basic-offset: 8
1284  * tab-width: 8
1285  * indent-tabs-mode: t
1286  * End:
1287  *
1288  * ex: set shiftwidth=8 tabstop=8 noexpandtab:
1289  * :indentSize=8:tabSize=8:noTabs=false:
1290  */
1291