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