Debian Codegraph @

lib/smi.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * smi.c --
3 *
4 * Interface Implementation of libsmi.
5 *
6 * Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
7 *
8 * See the file "COPYING" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * @(#) $Id: smi.c 8071 2008-04-17 11:14:46Z schoenw $
12 */
13
14 #include <config.h>
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include <sys/types.h>
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24 #ifdef HAVE_PWD_H
25 #include <pwd.h>
26 #endif
27
28 #include "smi.h"
29 #include "data.h"
30 #include "error.h"
31 #include "util.h"
32 #include "snprintf.h"
33
34 #ifdef BACKEND_SMI
35 #include "scanner-smi.h"
36 #include "parser-smi.h"
37 #endif
38
39 #ifdef BACKEND_SMING
40 #include "scanner-sming.h"
41 #include "parser-sming.h"
42 #endif
43
44 #ifdef HAVE_DMALLOC_H
45 #include <dmalloc.h>
46 #endif
47
48
49
50 #ifndef MIN
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52 #define MAX(a, b) ((a) < (b) ? (b) : (a))
53 #endif
54
55
56
57 const char *smi_library_version = SMI_LIBRARY_VERSION;
58 const char *smi_version_string = SMI_VERSION_STRING;
59
60 Handle *smiHandle = NULL;
61
62
63
64 /*
65 * Internal functions.
66 */
67
68 static void getModulenameAndName(const char *arg1, const char *arg2,
69 char **module, char **name)
70 {
71 char *p;
72 int l;
73
74 if ((!arg1) && (!arg2)) {
75 *module = NULL;
76 *name = NULL;
77 } else if (!arg2) {
78 if (isupper((int)arg1[0])) {
79 if ((p = strstr(arg1, "::"))) {
80 /* SMIng style module/label separator */
81 *name = smiStrdup(&p[2]);
82 l = strcspn(arg1, "::");
83 *module = smiStrndup(arg1, l);
84 } else if ((p = strchr(arg1, '!'))) {
85 /* old scotty style module/label separator */
86 *name = smiStrdup(&p[1]);
87 l = strcspn(arg1, "!");
88 *module = smiStrndup(arg1, l);
89 } else if ((p = strchr(arg1, '.'))) {
90 /* SMIv1/v2 style module/label separator */
91 *name = smiStrdup(&p[1]);
92 l = strcspn(arg1, ".");
93 *module = smiStrndup(arg1, l);
94 } else {
95 *name = smiStrdup(arg1);
96 *module = smiStrdup("");
97 }
98 } else {
99 *name = smiStrdup(arg1);
100 *module = smiStrdup("");
101 }
102 } else if (!arg1) {
103 if (isupper((int)arg2[0])) {
104 if ((p = strstr(arg2, "::"))) {
105 /* SMIng style module/label separator */
106 *name = smiStrdup(&p[2]);
107 l = strcspn(arg2, "::");
108 *module = smiStrndup(arg2, l);
109 } else if ((p = strchr(arg2, '!'))) {
110 /* old scotty style module/label separator */
111 *name = smiStrdup(&p[1]);
112 l = strcspn(arg2, "!");
113 *module = smiStrndup(arg2, l);
114 } else if ((p = strchr(arg2, '.'))) {
115 /* SMIv1/v2 style module/label separator */
116 *name = smiStrdup(&p[1]);
117 l = strcspn(arg2, ".");
118 *module = smiStrndup(arg2, l);
119 } else {
120 *name = smiStrdup(arg2);
121 *module = smiStrdup("");
122 }
123 } else {
124 *name = smiStrdup(arg2);
125 *module = smiStrdup("");
126 }
127 } else {
128 *module = smiStrdup(arg1);
129 *name = smiStrdup(arg2);
130 }
131 }
132
133
134
135 static Node *getNode(unsigned int oidlen, SmiSubid oid[])
136 {
137 Node *nodePtr, *parentPtr;
138 unsigned int i;
139
140 for(nodePtr = smiHandle->rootNodePtr, i=0; i < oidlen; i++) {
141 parentPtr = nodePtr;
142 nodePtr = findNodeByParentAndSubid(parentPtr, oid[i]);
143 if (!nodePtr) {
144 return parentPtr;
145 }
146 }
147
148 return nodePtr;
149 }
150
151
152
153 static Object *getNextChildObject(Node *startNodePtr, Module *modulePtr,
154 SmiNodekind nodekind)
155 {
156 Node *nodePtr;
157 Object *objectPtr = NULL;
158
159 if (!startNodePtr || !modulePtr)
160 return NULL;
161
162 for (nodePtr = startNodePtr; nodePtr; nodePtr = nodePtr->nextPtr) {
163 for (objectPtr = nodePtr->firstObjectPtr; objectPtr;
164 objectPtr = objectPtr->nextSameNodePtr) {
165 if (((!modulePtr) || (objectPtr->modulePtr == modulePtr)) &&
166 ((nodekind == SMI_NODEKIND_ANY) ||
167 (nodekind & objectPtr->export.nodekind))) {
168 break;
169 }
170 }
171 if (objectPtr) break;
172 objectPtr = getNextChildObject(nodePtr->firstChildPtr,
173 modulePtr, nodekind);
174 if (objectPtr) break;
175 }
176
177 return objectPtr;
178 }
179
180
181
182 /*
183 * Interface Functions.
184 */
185
186 int smiInit(const char *tag)
187 {
188 char *p, *pp, *tag2;
189 #ifdef HAVE_PWD_H
190 struct passwd *pw;
191 #endif
192
193 smiHandle = findHandleByName(tag);
194 if (smiHandle) {
195 return 0;
196 }
197 smiHandle = addHandle(tag);
198
199 smiDepth = 0;
200
201 smiHandle->errorLevel = DEFAULT_ERRORLEVEL;
202 smiHandle->errorHandler = smiErrorHandler;
203 #if !defined(_MSC_VER)
204 smiHandle->cache = NULL;
205 smiHandle->cacheProg = NULL;
206 #endif
207
208 if (smiInitData()) {
209 return -1;
210 }
211
212 /*
213 * Setup the SMI MIB module search path:
214 * 1. set to builtin DEFAULT_SMIPATH
215 * 2. read global config file if present (append/prepend/replace)
216 * 3. read user config file if present (append/prepend/replace)
217 * 4. evaluate SMIPATH env-var if set (append/prepend/replace)
218 */
219
220 /* 1. set to builtin DEFAULT_SMIPATH */
221 smiHandle->path = smiStrdup(DEFAULT_SMIPATH);
222
223 tag2 = smiStrdup(tag);
224 if (tag2) tag2 = strtok(tag2, ":");
225 if (tag2) {
226 /* 2. read global config file if present (append/prepend/replace) */
227 smiReadConfig(DEFAULT_GLOBALCONFIG, tag2);
228 #ifdef HAVE_PWD_H
229 pw = getpwuid(getuid());
230 if (pw && pw->pw_dir) {
231 /* 3. read user config file if present (append/prepend/replace) */
232 smiAsprintf(&p, "%s%c%s",
233 pw->pw_dir, DIR_SEPARATOR, DEFAULT_USERCONFIG);
234 smiReadConfig(p, tag2);
235 smiFree(p);
236 }
237 #endif
238 }
239 smiFree(tag2);
240
241 /* 4. evaluate SMIPATH env-var if set (append/prepend/replace) */
242 p = getenv("SMIPATH");
243 if (p) {
244 if (p[0] == PATH_SEPARATOR) {
245 smiAsprintf(&pp, "%s%s", smiHandle->path, p);
246 smiFree(smiHandle->path);
247 smiHandle->path = pp;
248 } else if (p[strlen(p)-1] == PATH_SEPARATOR) {
249 smiAsprintf(&pp, "%s%s", p, smiHandle->path);
250 smiFree(smiHandle->path);
251 smiHandle->path = pp;
252 } else {
253 smiHandle->path = smiStrdup(p);
254 }
255 }
256
257 if (!smiHandle->path) {
258 return -1;
259 }
260
261 return 0;
262 }
263
264
265
266 void smiExit()
267 {
268 if (!smiHandle)
269 return;
270
271 smiFreeData();
272
273 smiFree(smiHandle->path);
274 #if !defined(_MSC_VER)
275 smiFree(smiHandle->cache);
276 smiFree(smiHandle->cacheProg);
277 #endif
278
279 removeHandle(smiHandle);
280
281 smiHandle = NULL;
282 return;
283 }
284
285
286
287 char *smiGetPath()
288 {
289 if (smiHandle->path) {
290 return smiStrdup(smiHandle->path);
291 } else {
292 return NULL;
293 }
294 }
295
296
297
298 int smiSetPath(const char *s)
299 {
300 char *s2;
301
302 if (!smiHandle) smiInit(NULL);
303
304 if (!s) {
305 smiFree(smiHandle->path);
306 smiHandle->path = NULL;
307 return 0;
308 }
309
310 s2 = smiStrdup(s);
311 if (s2) {
312 smiFree(smiHandle->path);
313 smiHandle->path = s2;
314 return 0;
315 } else {
316 return -1;
317 }
318
319 }
320
321
322
323 void smiSetSeverity(char *pattern, int severity)
324 {
325 smiSetErrorSeverity(pattern, severity);
326 }
327
328
329
330 int smiReadConfig(const char *filename, const char *tag)
331 {
332 FILE *file;
333 char buf[201];
334 char *cmd, *arg, *s;
335
336 file = fopen(filename, "r");
337 if (file) {
338 while (!feof(file)) {
339 if (!fgets(buf, 200, file)) continue;
340 if ((!strlen(buf)) || (buf[0] == '#')) continue;
341 cmd = strtok(buf, " \t\n\r");
342 if (!cmd) continue;
343 if (cmd[0] == '#') continue;
344 if (cmd[strlen(cmd)-1] == ':') {
345 if (!tag) continue;
346 cmd[strlen(cmd)-1] = 0;
347 if (strcmp(cmd, tag)) continue;
348 cmd = strtok(NULL, " \t\n\r");
349 }
350 arg = strtok(NULL, " \t\n\r");
351 if (!strcmp(cmd, "load")) {
352 smiLoadModule(arg);
353 } else if (!strcmp(cmd, "path")) {
354 if (arg) {
355 if (arg[0] == PATH_SEPARATOR) {
356 smiAsprintf(&s, "%s%s", smiHandle->path, arg);
357 smiFree(smiHandle->path);
358 smiHandle->path = s;
359 } else if (arg[strlen(arg)-1] == PATH_SEPARATOR) {
360 smiAsprintf(&s, "%s%s", arg, smiHandle->path);
361 smiFree(smiHandle->path);
362 smiHandle->path = s;
363 } else {
364 smiHandle->path = smiStrdup(arg);
365 }
366 }
367 } else if (!strcmp(cmd, "cache")) {
368 #if !defined(_MSC_VER)
369 smiFree(smiHandle->cache);
370 smiFree(smiHandle->cacheProg);
371 #endif
372 if (arg && strcmp(arg, "off")) {
373 #if !defined(_MSC_VER)
374 smiHandle->cache = smiStrdup(arg);
375 arg = strtok(NULL, "\n\r");
376 smiHandle->cacheProg = smiStrdup(arg);
377 #else
378 smiPrintError(NULL, ERR_CACHE_CONFIG_NOT_SUPPORTED,
379 filename);
380 #endif
381 }
382 } else if (!strcmp(cmd, "level")) {
383 smiSetErrorLevel(atoi(arg));
384 } else if (!strcmp(cmd, "hide")) {
385 smiSetSeverity(arg, 9);
386 } else {
387 smiPrintError(NULL, ERR_UNKNOWN_CONFIG_CMD, cmd, filename);
388 }
389 }
390 fclose(file);
391 return 0;
392 }
393 return -1;
394 }
395
396
397
398 int smiIsLoaded(const char *module)
399 {
400 if (!module)
401 return 0;
402
403 return isInView(module);
404 }
405
406
407
408 char *smiLoadModule(const char *module)
409 {
410 Module *modulePtr;
411
412 if (!smiHandle) smiInit(NULL);
413
414 if (smiIsPath(module)) {
415
416 modulePtr = loadModule(module, NULL);
417
418 if (modulePtr) {
419 if (!isInView(modulePtr->export.name)) {
420 addView(modulePtr->export.name);
421 }
422 return modulePtr->export.name;
423 } else {
424 return NULL;
425 }
426
427 } else {
428
429 if ((modulePtr = findModuleByName(module))) {
430 /* already loaded. */
431 if (!isInView(module)) {
432 addView(module);
433 }
434 return modulePtr->export.name;
435 } else {
436 if ((modulePtr = loadModule(module, NULL))) {
437 if (!isInView(module)) {
438 addView(module);
439 }
440 return modulePtr->export.name;
441 } else {
442 return NULL;
443 }
444 }
445 }
446 }
447
448
449
450 void smiSetErrorLevel(int level)
451 {
452 if (!smiHandle) smiInit(NULL);
453
454 smiHandle->errorLevel = level;
455 }
456
457
458
459 void smiSetFlags(int userflags)
460 {
461 if (!smiHandle) smiInit(NULL);
462
463 smiHandle->flags = (smiHandle->flags & ~SMI_FLAG_MASK) | userflags;
464 }
465
466
467
468 int smiGetFlags()
469 {
470 if (!smiHandle) smiInit(NULL);
471
472 return smiHandle->flags & SMI_FLAG_MASK;
473 }
474
475
476
477 SmiModule *smiGetModule(const char *module)
478 {
479 Module *modulePtr;
480
481 if (!module) {
482 return NULL;
483 }
484
485 modulePtr = findModuleByName(module);
486
487 if (!modulePtr) {
488 modulePtr = loadModule(module, NULL);
489 }
490
491 return &modulePtr->export;
492 }
493
494
495
496 SmiModule *smiGetFirstModule()
497 {
498 Module *modulePtr;
499
500 for (modulePtr = smiHandle->firstModulePtr;
501 modulePtr && modulePtr->export.name &&
502 (strlen(modulePtr->export.name) == 0);
503 modulePtr = modulePtr->nextPtr);
504
505 return &modulePtr->export;
506 }
507
508
509
510 SmiModule *smiGetNextModule(SmiModule *smiModulePtr)
511 {
512 Module *modulePtr;
513
514 if (!smiModulePtr) {
515 return NULL;
516 }
517
518
519 for (modulePtr = ((Module *)smiModulePtr)->nextPtr;
520 modulePtr && modulePtr->export.name &&
521 (strlen(modulePtr->export.name) == 0);
522 modulePtr = modulePtr->nextPtr);
523
524 return &modulePtr->export;
525 }
526
527
528
529 SmiImport *smiGetFirstImport(SmiModule *smiModulePtr)
530 {
531 if (!smiModulePtr) {
532 return NULL;
533 }
534
535 return &((Module *)smiModulePtr)->firstImportPtr->export;
536 }
537
538
539
540 SmiImport *smiGetNextImport(SmiImport *smiImportPtr)
541 {
542 if (!smiImportPtr) {
543 return NULL;
544 }
545
546 return &((Import *)smiImportPtr)->nextPtr->export;
547 }
548
549
550
551 int smiIsImported(SmiModule *smiModulePtr,
552 SmiModule *importedModulePtr,
553 char *importedName)
554 {
555 Import *importPtr;
556 Module *modulePtr;
557 char *importedModule;
558
559 if ((!smiModulePtr) || (!importedName)) {
560 return 0;
561 }
562
563 modulePtr = (Module *)smiModulePtr;
564
565 if (importedModulePtr) {
566 importedModule = importedModulePtr->name;
567 } else {
568 importedModule = NULL;
569 }
570
571 for (importPtr = modulePtr->firstImportPtr; importPtr;
572 importPtr = importPtr->nextPtr) {
573 if ((!strcmp(importedName, importPtr->export.name)) &&
574 ((!importedModule) ||
575 (!strcmp(importedModule, importPtr->export.module)))) {
576 return 1;
577 }
578 }
579
580 return 0;
581 }
582
583
584
585 SmiRevision *smiGetFirstRevision(SmiModule *smiModulePtr)
586 {
587 if (!smiModulePtr) {
588 return NULL;
589 }
590
591 return &((Module *)smiModulePtr)->firstRevisionPtr->export;
592 }
593
594
595
596 SmiRevision *smiGetNextRevision(SmiRevision *smiRevisionPtr)
597 {
598 if (!smiRevisionPtr) {
599 return NULL;
600 }
601
602 return &((Revision *)smiRevisionPtr)->nextPtr->export;
603 }
604
605
606
607 int smiGetRevisionLine(SmiRevision *smiRevisionPtr)
608 {
609 return ((Revision *)smiRevisionPtr)->line;
610 }
611
612
613
614 SmiType *smiGetType(SmiModule *smiModulePtr, char *type)
615 {
616 Type *typePtr = NULL;
617 Module *modulePtr = NULL;
618 char *module2, *type2;
619
620 if (!type) {
621 return NULL;
622 }
623
624 modulePtr = (Module *)smiModulePtr;
625
626 getModulenameAndName(smiModulePtr ? smiModulePtr->name : NULL, type,
627 &module2, &type2);
628
629 if (!modulePtr && module2 && strlen(module2)) {
630 if (!(modulePtr = findModuleByName(module2))) {
631 modulePtr = loadModule(module2, NULL);
632 }
633 }
634
635 if (modulePtr) {
636 typePtr = findTypeByModuleAndName(modulePtr, type2);
637 } else {
638 typePtr = findTypeByName(type2);
639 }
640
641 smiFree(module2);
642 smiFree(type2);
643
644 if (!typePtr ||
645 typePtr->export.basetype == SMI_BASETYPE_UNKNOWN) {
646 return NULL;
647 }
648
649 return &typePtr->export;
650 }
651
652
653
654 SmiType *smiGetFirstType(SmiModule *smiModulePtr)
655 {
656 Type *typePtr;
657
658 if (!smiModulePtr) {
659 return NULL;
660 }
661
662 for (typePtr = ((Module *)smiModulePtr)->firstTypePtr; typePtr;
663 typePtr = typePtr->nextPtr) {
664 /* loop until we found a `real' type */
665 if (typePtr->export.name &&
666 typePtr->export.basetype != SMI_BASETYPE_UNKNOWN) {
667 break;
668 }
669 }
670
671 return &typePtr->export;
672 }
673
674
675
676 SmiType *smiGetNextType(SmiType *smiTypePtr)
677 {
678 Type *typePtr;
679
680 if (!smiTypePtr) {
681 return NULL;
682 }
683
684 for (typePtr = ((Type *)smiTypePtr)->nextPtr; typePtr;
685 typePtr = typePtr->nextPtr) {
686 /* loop until we found a `real' type */
687 if (typePtr->export.name &&
688 typePtr->export.basetype != SMI_BASETYPE_UNKNOWN) {
689 break;
690 }
691 }
692
693 return &typePtr->export;
694 }
695
696
697 SmiType *smiGetParentType(SmiType *smiTypePtr)
698 {
699 Type *typePtr;
700
701 if (!smiTypePtr) {
702 return NULL;
703 }
704
705 typePtr = ((Type *)smiTypePtr)->parentPtr;
706
707 if (!typePtr ||
708 typePtr->export.basetype == SMI_BASETYPE_UNKNOWN) {
709 return NULL;
710 }
711
712 return &typePtr->export;
713 }
714
715
716
717 SmiModule *smiGetTypeModule(SmiType *smiTypePtr)
718 {
719 return &((Type *)smiTypePtr)->modulePtr->export;
720 }
721
722 int smiGetTypeLine(SmiType *smiTypePtr)
723 {
724 return ((Type *)smiTypePtr)->line;
725 }
726
727
728
729 SmiNamedNumber *smiGetFirstNamedNumber(SmiType *smiTypePtr)
730 {
731 Type *typePtr;
732
733 typePtr = (Type *)smiTypePtr;
734
735 if ((!typePtr) || (!typePtr->listPtr) ||
736 ((typePtr->export.basetype != SMI_BASETYPE_ENUM) &&
737 (typePtr->export.basetype != SMI_BASETYPE_BITS) &&
738 (typePtr->export.basetype != SMI_BASETYPE_POINTER))) {
739 return NULL;
740 }
741
742 return &((NamedNumber *)typePtr->listPtr->ptr)->export;
743 }
744
745
746
747 SmiNamedNumber *smiGetNextNamedNumber(SmiNamedNumber *smiNamedNumberPtr)
748 {
749 Type *typePtr;
750 List *listPtr;
751
752 if (!smiNamedNumberPtr) {
753 return NULL;
754 }
755
756 typePtr = ((NamedNumber *)smiNamedNumberPtr)->typePtr;
757
758
759 if ((!typePtr) || (!typePtr->listPtr) ||
760 ((typePtr->export.basetype != SMI_BASETYPE_ENUM) &&
761 (typePtr->export.basetype != SMI_BASETYPE_BITS))) {
762 return NULL;
763 }
764
765 for (listPtr = typePtr->listPtr; listPtr; listPtr = listPtr->nextPtr) {
766 if (((NamedNumber *)(listPtr->ptr))->export.name ==
767 smiNamedNumberPtr->name)
768 break;
769 }
770
771 if ((!listPtr) || (!listPtr->nextPtr)) {
772 return NULL;
773 }
774
775 return &((NamedNumber *)listPtr->nextPtr->ptr)->export;
776 }
777
778 SmiNamedNumber *smiGetAttributeFirstNamedNumber(SmiAttribute *smiAttributePtr)
779 {
780 Attribute *attributePtr;
781
782 attributePtr = (Attribute *)smiAttributePtr;
783
784 if ((!attributePtr) || (!attributePtr->listPtr) ||
785 ((attributePtr->export.basetype != SMI_BASETYPE_ENUM) &&
786 (attributePtr->export.basetype != SMI_BASETYPE_BITS) &&
787 (attributePtr->export.basetype != SMI_BASETYPE_POINTER))) {
788 return NULL;
789 }
790
791 return &((NamedNumber *)attributePtr->listPtr->ptr)->export;
792 }
793
794
795
796 SmiNamedNumber *smiGetAttributeNextNamedNumber(SmiNamedNumber *smiNamedNumberPtr)
797 {
798 Attribute *attributePtr;
799 List *listPtr;
800
801 if (!smiNamedNumberPtr) {
802 return NULL;
803 }
804
805 attributePtr = (Attribute*)(((NamedNumber *)smiNamedNumberPtr)->typePtr);
806
807
808 if ((!attributePtr) || (!attributePtr->listPtr) ||
809 ((attributePtr->export.basetype != SMI_BASETYPE_ENUM) &&
810 (attributePtr->export.basetype != SMI_BASETYPE_BITS))) {
811 return NULL;
812 }
813
814 for (listPtr = attributePtr->listPtr; listPtr; listPtr = listPtr->nextPtr) {
815 if (((NamedNumber *)(listPtr->ptr))->export.name ==
816 smiNamedNumberPtr->name)
817 break;
818 }
819
820 if ((!listPtr) || (!listPtr->nextPtr)) {
821 return NULL;
822 }
823
824 return &((NamedNumber *)listPtr->nextPtr->ptr)->export;
825 }
826
827 SmiRange *smiGetFirstRange(SmiType *smiTypePtr)
828 {
829 Type *typePtr;
830
831 typePtr = (Type *)smiTypePtr;
832
833 if ((!typePtr) || (!typePtr->listPtr) ||
834 (typePtr->export.basetype == SMI_BASETYPE_ENUM) ||
835 (typePtr->export.basetype == SMI_BASETYPE_BITS)) {
836 return NULL;
837 }
838
839 return &((Range *)typePtr->listPtr->ptr)->export;
840 }
841
842
843
844 SmiRange *smiGetNextRange(SmiRange *smiRangePtr)
845 {
846 Type *typePtr;
847 List *listPtr;
848
849 if (!smiRangePtr) {
850 return NULL;
851 }
852
853 typePtr = ((Range *)smiRangePtr)->typePtr;
854
855 if ((!typePtr) || (!typePtr->listPtr) ||
856 (typePtr->export.basetype == SMI_BASETYPE_ENUM) ||
857 (typePtr->export.basetype == SMI_BASETYPE_BITS)) {
858 return NULL;
859 }
860
861 for (listPtr = typePtr->listPtr; listPtr; listPtr = listPtr->nextPtr) {
862 if (!memcmp(&((Range *)listPtr->ptr)->export.minValue,
863 &smiRangePtr->minValue, sizeof(struct SmiValue)))
864 break;
865 }
866
867 if ((!listPtr) || (!listPtr->nextPtr)) {
868 return NULL;
869 }
870
871 return &((Range *)listPtr->nextPtr->ptr)->export;
872 }
873
874 SmiRange *smiGetAttributeFirstRange(SmiAttribute *smiAttributePtr)
875 {
876 Attribute *attributePtr;
877
878 attributePtr = (Attribute *)smiAttributePtr;
879
880 if ((!attributePtr) || (!attributePtr->listPtr) ||
881 (attributePtr->export.basetype == SMI_BASETYPE_ENUM) ||
882 (attributePtr->export.basetype == SMI_BASETYPE_BITS)) {
883 return NULL;
884 }
885
886 return &((Range *)attributePtr->listPtr->ptr)->export;
887 }
888
889
890
891 SmiRange *smiGetAttributeNextRange(SmiRange *smiRangePtr)
892 {
893 Attribute *attributePtr;
894 List *listPtr;
895
896 if (!smiRangePtr) {
897 return NULL;
898 }
899
900 attributePtr = (Attribute*)((Range *)smiRangePtr)->typePtr;
901
902 if ((!attributePtr) || (!attributePtr->listPtr) ||
903 (attributePtr->export.basetype == SMI_BASETYPE_ENUM) ||
904 (attributePtr->export.basetype == SMI_BASETYPE_BITS)) {
905 return NULL;
906 }
907
908 for (listPtr = attributePtr->listPtr; listPtr; listPtr = listPtr->nextPtr) {
909 if (!memcmp(&((Range *)listPtr->ptr)->export.minValue,
910 &smiRangePtr->minValue, sizeof(struct SmiValue)))
911 break;
912 }
913
914 if ((!listPtr) || (!listPtr->nextPtr)) {
915 return NULL;
916 }
917
918 return &((Range *)listPtr->nextPtr->ptr)->export;
919 }
920
921
922 SmiIdentity *smiGetFirstIdentity(SmiModule *smiModulePtr)
923 {
924 if (!smiModulePtr) {
925 return NULL;
926 }
927
928 return ((Module *)smiModulePtr)->firstIdentityPtr ?
929 &((Module *)smiModulePtr)->firstIdentityPtr->export : NULL;
930
931 }
932
933 SmiIdentity *smiGetNextIdentity(SmiIdentity *smiIdentityPtr)
934 {
935 if (!smiIdentityPtr) {
936 return NULL;
937 }
938
939 return ((Identity *)smiIdentityPtr)->nextPtr ?
940 &((Identity *)smiIdentityPtr)->nextPtr->export : NULL;
941 }
942
943 SmiModule *smiGetIdentityModule(SmiIdentity *smiIdentityPtr)
944 {
945 return &((Identity *)smiIdentityPtr)->modulePtr->export;
946 }
947
948 SmiIdentity *smiGetParentIdentity(SmiIdentity *smiIdentityPtr)
949 {
950 return (SmiIdentity*)(((Identity *)smiIdentityPtr)->parentPtr);
951 }
952
953 SmiIdentity *smiGetIdentity(SmiModule *smiModulePtr, char *identity)
954 {
955
956 if (!smiModulePtr) {
957 return NULL;
958 }
959 else
960 {
961 SmiIdentity *ide;
962
963 for(ide = smiGetFirstIdentity(smiModulePtr);
964 ide;
965 ide = smiGetNextIdentity(ide))
966 if(!strncmp(ide->name,identity,64))return ide;
967
968 return NULL;
969 }
970
971 }
972
973 int smiGetIdentityLine(SmiIdentity *smiIdentityPtr)
974 {
975 return ((Identity *)smiIdentityPtr)->line;
976 }
977
978
979 SmiClass *smiGetFirstClass(SmiModule *smiModulePtr)
980 {
981 if (!smiModulePtr) {
982 return NULL;
983 }
984
985 return ((Module *)smiModulePtr)->firstClassPtr ?
986 &((Module *)smiModulePtr)->firstClassPtr->export : NULL;
987
988 }
989
990 SmiClass *smiGetNextClass(SmiClass *smiClassPtr)
991 {
992 if (!smiClassPtr) {
993 return NULL;
994 }
995
996 return ((Class *)smiClassPtr)->nextPtr ?
997 &((Class *)smiClassPtr)->nextPtr->export : NULL;
998 }
999
1000 SmiModule *smiGetClassModule(SmiClass *smiClassPtr)
1001 {
1002 return &((Class *)smiClassPtr)->modulePtr->export;
1003 }
1004
1005 SmiClass *smiGetParentClass(SmiClass *smiClassPtr)
1006 {
1007 return (SmiClass*)(((Class *)smiClassPtr)->parentPtr);
1008 }
1009
1010 SmiClass *smiGetClass(SmiModule *smiModulePtr, char *class)
1011 {
1012
1013 if (!smiModulePtr) {
1014 return NULL;
1015 }
1016 else
1017 {
1018 SmiClass *cl;
1019
1020 for(cl = smiGetFirstClass(smiModulePtr);
1021 cl;
1022 cl = smiGetNextClass(cl))
1023 if(!strncmp(cl->name,class,64))return cl;
1024
1025 return NULL;
1026 }
1027
1028 }
1029
1030 int smiGetClassLine(SmiClass *smiClassPtr)
1031 {
1032 return ((Class *)smiClassPtr)->line;
1033 }
1034
1035 SmiAttribute *smiGetFirstAttribute(SmiClass *smiClassPtr)
1036 {
1037 Attribute *attributePtr;
1038
1039 if (!smiClassPtr) {
1040 return NULL;
1041 }
1042
1043 attributePtr = ((Class *)smiClassPtr)->firstAttributePtr;
1044
1045 return &attributePtr->export;
1046 }
1047
1048 SmiAttribute *smiGetNextAttribute( SmiAttribute *smiTypePtr)
1049 {
1050 Attribute *attributePtr;
1051
1052 if (!smiTypePtr) {
1053 return NULL;
1054 }
1055
1056 attributePtr = ((Attribute *)smiTypePtr)->nextPtr;
1057
1058 return &attributePtr->export;
1059 }
1060
1061 SmiAttribute *smiGetAttribute(SmiClass *smiClassPtr, char *attribute)
1062 {
1063 Attribute *attributePtr;
1064
1065 if (! smiClassPtr) {
1066 return NULL;
1067 }
1068
1069 attributePtr = ((Class *)smiClassPtr)->firstAttributePtr;
1070
1071 for (attributePtr = ((Class *)smiClassPtr)->firstAttributePtr;
1072 attributePtr; attributePtr = attributePtr->nextPtr)
1073 {
1074 if (!strncmp(attributePtr->export.name, attribute,64)) {
1075 return &attributePtr->export;
1076 }
1077 }
1078
1079 /*
1080 * attribute might belong to the parent so check parent if
1081 * attribute not found
1082 */
1083
1084 smiClassPtr = smiGetParentClass(smiClassPtr);
1085 attributePtr = (Attribute*)smiGetAttribute(smiClassPtr , attribute);
1086
1087 return &attributePtr->export;
1088 }
1089
1090 SmiType *smiGetAttributeParentType(SmiAttribute *smiAttributePtr)
1091 {
1092 Type *parentTypePtr;
1093
1094 if (! smiAttributePtr) {
1095 return NULL;
1096 }
1097
1098 parentTypePtr = ((Attribute*)smiAttributePtr)->parentTypePtr;
1099
1100 return (parentTypePtr) ? &parentTypePtr->export : NULL;
1101 }
1102
1103 SmiClass *smiGetAttributeParentClass( SmiAttribute *smiAttributePtr)
1104 {
1105 Class *parentClassPtr;
1106
1107 if (! smiAttributePtr) {
1108 return NULL;
1109 }
1110
1111 parentClassPtr = ((Attribute*)smiAttributePtr)->parentClassPtr;
1112
1113 return parentClassPtr ? &parentClassPtr->export : NULL;
1114 }
1115
1116 SmiAttribute *smiGetFirstUniqueAttribute(SmiClass *smiClassPtr)
1117 {
1118 Class *classPtr;
1119
1120 if (! smiClassPtr) {
1121 return NULL;
1122 }
1123
1124 classPtr = (Class*)smiClassPtr;
1125
1126 if (! classPtr->uniqueList) {
1127 return NULL;
1128 }
1129
1130 if (classPtr->uniqueList->ptr == classPtr) {
1131 return NULL; /* scalar class */
1132 }
1133
1134 return (SmiAttribute*)(classPtr->uniqueList->ptr);
1135 }
1136
1137 SmiAttribute *smiGetNextUniqueAttribute( SmiAttribute *smiTypePtr)
1138 {
1139 Class *classPtr;
1140 List *listPtr;
1141
1142 if (! smiTypePtr) {
1143 return NULL;
1144 }
1145
1146 classPtr = ((Attribute*)smiTypePtr)->classPtr;
1147
1148 if (classPtr && classPtr->uniqueList) {
1149 for (listPtr=classPtr->uniqueList;listPtr; listPtr=listPtr->nextPtr) {
1150 if (&((Attribute*)(listPtr->ptr))->export == smiTypePtr) {
1151 if (listPtr->nextPtr) {
1152 return &((Attribute*)(listPtr->nextPtr->ptr))->export;
1153 }
1154 }
1155 }
1156 }
1157 return NULL;
1158 }
1159
1160
1161
1162 int smiGetAttributeLine(SmiAttribute *smiAttributePtr)
1163 {
1164 return ((Attribute *)smiAttributePtr)->line;
1165 }
1166
1167
1168
1169 int smiIsClassScalar(SmiClass *smiClassPtr)
1170 {
1171 Class *classPtr;
1172
1173 if (! smiClassPtr) {
1174 return 0;
1175 }
1176
1177 classPtr = (Class*)smiClassPtr;
1178
1179 if (! classPtr->uniqueList) {
1180 return 0;
1181 }
1182
1183 return (classPtr->uniqueList->ptr == classPtr);
1184 }
1185
1186
1187
1188 SmiEvent *smiGetFirstEvent(SmiClass *smiClassPtr)
1189 {
1190 Event *eventPtr;
1191
1192 if (! smiClassPtr) {
1193 return NULL;
1194 }
1195
1196 eventPtr = ((Class *)smiClassPtr)->firstEventPtr;
1197 return &(eventPtr->export);
1198 }
1199
1200
1201
1202 SmiEvent *smiGetNextEvent(SmiEvent *smiEventPtr)
1203 {
1204 Event *eventPtr;
1205
1206 if (! smiEventPtr) {
1207 return NULL;
1208 }
1209
1210 eventPtr = ((Event *)smiEventPtr)->nextPtr;
1211 return &eventPtr->export;
1212 }
1213
1214
1215
1216 int smiGetEventLine(SmiEvent *smiEventPtr)
1217 {
1218 return ((Event *)smiEventPtr)->line;
1219 }
1220
1221
1222
1223 SmiMacro *smiGetMacro(SmiModule *smiModulePtr, char *macro)
1224 {
1225 Macro *macroPtr = NULL;
1226 Module *modulePtr = NULL;
1227 char *module2, *macro2;
1228
1229 if (!macro) {
1230 return NULL;
1231 }
1232
1233 modulePtr = (Module *)smiModulePtr;
1234
1235 getModulenameAndName(smiModulePtr ? smiModulePtr->name : NULL, macro,
1236 &module2, &macro2);
1237
1238 if (!modulePtr && module2 && strlen(module2)) {
1239 if (!(modulePtr = findModuleByName(module2))) {
1240 modulePtr = loadModule(module2, NULL);
1241 }
1242 }
1243
1244 if (modulePtr) {
1245 macroPtr = findMacroByModuleAndName(modulePtr, macro2);
1246 } else {
1247 macroPtr = findMacroByName(macro2);
1248 }
1249
1250 smiFree(module2);
1251 smiFree(macro2);
1252 return macroPtr ? &macroPtr->export : NULL;
1253 }
1254
1255
1256
1257 SmiMacro *smiGetFirstMacro(SmiModule *smiModulePtr)
1258 {
1259 if (!smiModulePtr) {
1260 return NULL;
1261 }
1262
1263 return ((Module *)smiModulePtr)->firstMacroPtr ?
1264 &((Module *)smiModulePtr)->firstMacroPtr->export : NULL;
1265 }
1266
1267
1268
1269 SmiMacro *smiGetNextMacro(SmiMacro *smiMacroPtr)
1270 {
1271 if (!smiMacroPtr) {
1272 return NULL;
1273 }
1274
1275 return ((Macro *)smiMacroPtr)->nextPtr ?
1276 &((Macro *)smiMacroPtr)->nextPtr->export : NULL;
1277 }
1278
1279
1280 SmiModule *smiGetMacroModule(SmiMacro *smiMacroPtr)
1281 {
1282 return &((Macro *)smiMacroPtr)->modulePtr->export;
1283 }
1284
1285
1286 int smiGetMacroLine(SmiMacro *smiMacroPtr)
1287 {
1288 return ((Macro *)smiMacroPtr)->line;
1289 }
1290
1291
1292 SmiNode *smiGetNode(SmiModule *smiModulePtr, const char *node)
1293 {
1294 Object *objectPtr = NULL;
1295 Module *modulePtr = NULL;
1296 Node *nodePtr;
1297 char *module2, *node2, *p;
1298 unsigned int oidlen;
1299 SmiSubid oid[128];
1300
1301 if (!node) {
1302 return NULL;
1303 }
1304
1305 modulePtr = (Module *)smiModulePtr;
1306
1307 getModulenameAndName(smiModulePtr ? smiModulePtr->name : NULL, node,
1308 &module2, &node2);
1309
1310 if (!modulePtr && module2 && strlen(module2)) {
1311 if (!(modulePtr = findModuleByName(module2))) {
1312 modulePtr = loadModule(module2, NULL);
1313 }
1314 }
1315
1316 if (isdigit((int)node2[0])) {
1317 for (oidlen = 0, p = strtok(node2, ". ");
1318 p && oidlen < sizeof(oid)/sizeof(oid[0]);
1319 oidlen++, p = strtok(NULL, ". ")) {
1320 oid[oidlen] = strtoul(p, NULL, 0);
1321 }
1322 if (p) {
1323 /* the numeric OID is too long */
1324 return NULL;
1325 }
1326 nodePtr = getNode(oidlen, oid);
1327 if (nodePtr) {
1328 if (modulePtr) {
1329 objectPtr = findObjectByModuleAndNode(modulePtr, nodePtr);
1330 } else {
1331 objectPtr = findObjectByNode(nodePtr);
1332 }
1333 }
1334 } else {
1335 p = strtok(node2, ". ");
1336 if (modulePtr) {
1337 objectPtr = findObjectByModuleAndName(modulePtr, p);
1338 } else {
1339 objectPtr = findObjectByName(p);
1340 }
1341 }
1342
1343 smiFree(module2);
1344 smiFree(node2);
1345 return objectPtr ? &objectPtr->export : NULL;
1346 }
1347
1348
1349
1350 SmiNode *smiGetNodeByOID(unsigned int oidlen, SmiSubid oid[])
1351 {
1352 Node *nodePtr;
1353 Object *objectPtr;
1354
1355 if (!oidlen) {
1356 return NULL;
1357 }
1358
1359 nodePtr = getNode(oidlen, oid);
1360
1361 if (!nodePtr) {
1362 return NULL;
1363 }
1364
1365 objectPtr = findObjectByNode(nodePtr);
1366
1367 return objectPtr ? &objectPtr->export : NULL;
1368 }
1369
1370
1371
1372 SmiNode *smiGetFirstNode(SmiModule *smiModulePtr, SmiNodekind nodekind)
1373 {
1374 Module *modulePtr;
1375 Node *nodePtr = NULL;
1376 Object *objectPtr;
1377
1378 if (!smiModulePtr) {
1379 return NULL;
1380 }
1381
1382 modulePtr = (Module *)smiModulePtr;
1383
1384 if (modulePtr && modulePtr->prefixNodePtr) {
1385 /* start at the common oid prefix of this module */
1386 nodePtr = modulePtr->prefixNodePtr;
1387 } else {
1388 nodePtr = smiHandle->rootNodePtr->firstChildPtr;
1389 }
1390
1391 do {
1392 objectPtr = getNextChildObject(nodePtr, modulePtr, nodekind);
1393
1394 if (objectPtr)
1395 return &objectPtr->export;
1396
1397 if (nodePtr->firstChildPtr) {
1398 nodePtr = nodePtr->firstChildPtr;
1399 } else if (nodePtr->nextPtr) {
1400 nodePtr = nodePtr->nextPtr;
1401 } else {
1402 for (nodePtr = nodePtr->parentPtr;
1403 nodePtr && (nodePtr->parentPtr) && (!nodePtr->nextPtr);
1404 nodePtr = nodePtr->parentPtr);
1405 if (nodePtr) nodePtr = nodePtr->nextPtr;
1406 }
1407 } while (nodePtr);
1408
1409 return NULL;
1410 }
1411
1412
1413
1414 SmiNode *smiGetNextNode(SmiNode *smiNodePtr, SmiNodekind nodekind)
1415 {
1416 Module *modulePtr;
1417 Object *objectPtr;
1418 Node *nodePtr;
1419 int i;
1420
1421 if (!smiNodePtr) {
1422 return NULL;
1423 }
1424
1425 objectPtr = (Object *)smiNodePtr;
1426 nodePtr = objectPtr->nodePtr;
1427 modulePtr = objectPtr->modulePtr;
1428
1429 if (!modulePtr) {
1430 return NULL;
1431 }
1432
1433 if (!nodePtr) {
1434 return NULL;
1435 }
1436
1437 do {
1438 if (nodePtr->firstChildPtr) {
1439 nodePtr = nodePtr->firstChildPtr;
1440 } else if (nodePtr->nextPtr) {
1441 nodePtr = nodePtr->nextPtr;
1442 } else {
1443 for (nodePtr = nodePtr->parentPtr;
1444 (nodePtr->parentPtr) && (!nodePtr->nextPtr);
1445 nodePtr = nodePtr->parentPtr);
1446 nodePtr = nodePtr->nextPtr;
1447 /* did we move outside the common oid prefix of this module? */
1448 for (i = 0; i < modulePtr->prefixNodePtr->oidlen; i++)
1449 if ((!nodePtr) || (!nodePtr->oid) ||
1450 (nodePtr->oid[i] != modulePtr->prefixNodePtr->oid[i]))
1451 return NULL;
1452 }
1453
1454 objectPtr = getNextChildObject(nodePtr, modulePtr, nodekind);
1455
1456 if (objectPtr)
1457 return &objectPtr->export;
1458
1459 } while (nodePtr);
1460
1461 return NULL;
1462 }
1463
1464
1465
1466 SmiNode *smiGetParentNode(SmiNode *smiNodePtr)
1467 {
1468 Module *modulePtr;
1469 Object *objectPtr;
1470 Import *importPtr;
1471 Node *nodePtr;
1472
1473 if (!smiNodePtr) {
1474 return NULL;
1475 }
1476
1477 objectPtr = (Object *)smiNodePtr;
1478 nodePtr = objectPtr->nodePtr;
1479 modulePtr = objectPtr->modulePtr;
1480
1481 if (!nodePtr) {
1482 return NULL;
1483 }
1484
1485 if (nodePtr == smiHandle->rootNodePtr) {
1486 return NULL;
1487 }
1488
1489 nodePtr = nodePtr->parentPtr;
1490 if (! nodePtr) {
1491 return NULL;
1492 }
1493
1494 /*
1495 * First, try to find a definition in the same module.
1496 */
1497 objectPtr = NULL;
1498 if (modulePtr) {
1499 objectPtr = findObjectByModuleAndNode(modulePtr, nodePtr);
1500 }
1501
1502 /*
1503 * If found, check if it's imported. In case, get the original definition.
1504 */
1505 if (objectPtr) {
1506 importPtr = findImportByName(objectPtr->export.name,
1507 objectPtr->modulePtr);
1508 if (importPtr) {
1509 objectPtr = findObjectByModulenameAndNode(importPtr->export.module,
1510 nodePtr);
1511 } else {
1512 objectPtr = NULL;
1513 }
1514 }
1515
1516 /*
1517 * If not yet found, try to find any definition.
1518 */
1519 if (!objectPtr) {
1520 objectPtr = findObjectByNode(nodePtr);
1521
1522 if ((!objectPtr) && (nodePtr->parentPtr)) {
1523 /* an implicitly created node, e.g. gaga.0 in an object
1524 * definition with oid == gaga.0.1.
1525 */
1526 objectPtr = addObject(SMI_UNKNOWN_LABEL,
1527 nodePtr->parentPtr, nodePtr->subid,
1528 0, NULL);
1529 objectPtr->nodePtr = nodePtr;
1530 objectPtr->modulePtr = modulePtr;
1531 }
1532 }
1533
1534 return objectPtr ? &objectPtr->export : NULL;
1535 }
1536
1537
1538
1539 SmiNode *smiGetRelatedNode(SmiNode *smiNodePtr)
1540 {
1541 if (!smiNodePtr) {
1542 return NULL;
1543 }
1544
1545 return &((Object *)smiNodePtr)->relatedPtr->export;
1546 }
1547
1548
1549
1550 SmiNode *smiGetFirstChildNode(SmiNode *smiNodePtr)
1551 {
1552 Module *modulePtr;
1553 Object *objectPtr;
1554 Node *nodePtr;
1555
1556 if (!smiNodePtr) {
1557 return NULL;
1558 }
1559
1560 objectPtr = (Object *)smiNodePtr;
1561 nodePtr = objectPtr->nodePtr;
1562 modulePtr = objectPtr->modulePtr;
1563
1564 if (!nodePtr) {
1565 return NULL;
1566 }
1567
1568 nodePtr = nodePtr->firstChildPtr;
1569
1570 if (!nodePtr) {
1571 return NULL;
1572 }
1573
1574 objectPtr = findObjectByModuleAndNode(modulePtr, nodePtr);
1575 if (!objectPtr) objectPtr = findObjectByNode(nodePtr);
1576
1577 return objectPtr ? &objectPtr->export : NULL;
1578 }
1579
1580
1581
1582 SmiNode *smiGetNextChildNode(SmiNode *smiNodePtr)
1583 {
1584 Module *modulePtr;
1585 Object *objectPtr;
1586 Node *nodePtr;
1587
1588 if (!smiNodePtr) {
1589 return NULL;
1590 }
1591
1592 objectPtr = (Object *)smiNodePtr;
1593 nodePtr = objectPtr->nodePtr;
1594 modulePtr = objectPtr->modulePtr;
1595
1596 if (!nodePtr) {
1597 return NULL;
1598 }
1599
1600 nodePtr = nodePtr->nextPtr;
1601
1602 if (!nodePtr) {
1603 return NULL;
1604 }
1605
1606 objectPtr = findObjectByModuleAndNode(modulePtr, nodePtr);
1607 if (!objectPtr) objectPtr = findObjectByNode(nodePtr);
1608
1609 return objectPtr ? &objectPtr->export : NULL;
1610 }
1611
1612
1613
1614 SmiNode *smiGetModuleIdentityNode(SmiModule *smiModulePtr)
1615 {
1616 if (!smiModulePtr) {
1617 return NULL;
1618 }
1619
1620 return &((Module *)smiModulePtr)->objectPtr->export;
1621 }
1622
1623
1624
1625 SmiModule *smiGetNodeModule(SmiNode *smiNodePtr)
1626 {
1627 return &((Object *)smiNodePtr)->modulePtr->export;
1628 }
1629
1630
1631
1632 SmiType *smiGetNodeType(SmiNode *smiNodePtr)
1633 {
1634 Type *typePtr;
1635
1636 typePtr = ((Object *)smiNodePtr)->typePtr;
1637
1638 if (!typePtr ||
1639 typePtr->export.basetype == SMI_BASETYPE_UNKNOWN) {
1640 return NULL;
1641 }
1642
1643 return &typePtr->export;
1644 }
1645
1646
1647
1648 int smiGetNodeLine(SmiNode *smiNodePtr)
1649 {
1650 return ((Object *)smiNodePtr)->line;
1651 }
1652
1653
1654
1655 SmiElement *smiGetFirstElement(SmiNode *smiNodePtr)
1656 {
1657 List *listPtr;
1658
1659 if (!smiNodePtr) {
1660 return NULL;
1661 }
1662
1663 listPtr = ((Object *)smiNodePtr)->listPtr;
1664
1665 return (SmiElement *)listPtr;
1666 }
1667
1668
1669
1670 SmiElement *smiGetNextElement(SmiElement *smiElementPtr)
1671 {
1672 List *listPtr;
1673
1674 if (!smiElementPtr) {
1675 return NULL;
1676 }
1677
1678 listPtr = ((List *)smiElementPtr)->nextPtr;
1679
1680 return (SmiElement *)listPtr;
1681 }
1682
1683
1684
1685 SmiNode *smiGetElementNode(SmiElement *smiElementPtr)
1686 {
1687 if ((Object *)((List *)smiElementPtr)->ptr)
1688 return &((Object *)((List *)smiElementPtr)->ptr)->export;
1689 else
1690 return NULL;
1691 }
1692
1693
1694
1695 SmiOption *smiGetFirstOption(SmiNode *smiComplianceNodePtr)
1696 {
1697 Object *objectPtr;
1698
1699 if (!smiComplianceNodePtr) {
1700 return NULL;
1701 }
1702
1703 objectPtr = (Object *)smiComplianceNodePtr;
1704
1705 if (!objectPtr->optionlistPtr) {
1706 return NULL;
1707 }
1708
1709 if (objectPtr->export.nodekind != SMI_NODEKIND_COMPLIANCE) {
1710 return NULL;
1711 }
1712
1713 return &((Option *)objectPtr->optionlistPtr->ptr)->export;
1714 }
1715
1716
1717
1718 SmiOption *smiGetNextOption(SmiOption *smiOptionPtr)
1719 {
1720 List *listPtr;
1721
1722 if (!smiOptionPtr) {
1723 return NULL;
1724 }
1725
1726 for (listPtr =
1727 ((Option *)smiOptionPtr)->compliancePtr->optionlistPtr;
1728 listPtr;
1729 listPtr = listPtr->nextPtr) {
1730 if ((Option *)(listPtr->ptr) == (Option *)smiOptionPtr) {
1731 if (listPtr->nextPtr) {
1732 return &((Option *)listPtr->nextPtr->ptr)->export;
1733 } else {
1734 return NULL;
1735 }
1736 }
1737 }
1738
1739 return NULL;
1740 }
1741
1742
1743
1744 SmiNode *smiGetOptionNode(SmiOption *smiOptionPtr)
1745 {
1746 return &((Option *)smiOptionPtr)->objectPtr->export;
1747 }
1748
1749
1750
1751 int smiGetOptionLine(SmiOption *smiOptionPtr)
1752 {
1753 return ((Option *)smiOptionPtr)->line;
1754 }
1755
1756
1757
1758 SmiRefinement *smiGetFirstRefinement(SmiNode *smiComplianceNodePtr)
1759 {
1760 Object *objectPtr;
1761
1762 if (!smiComplianceNodePtr) {
1763 return NULL;
1764 }
1765
1766 objectPtr = (Object *)smiComplianceNodePtr;
1767
1768 if (!objectPtr->refinementlistPtr) {
1769 return NULL;
1770 }
1771
1772 if (objectPtr->export.nodekind != SMI_NODEKIND_COMPLIANCE) {
1773 return NULL;
1774 }
1775
1776 return &((Refinement *)objectPtr->refinementlistPtr->ptr)->export;
1777 }
1778
1779
1780
1781 SmiRefinement *smiGetNextRefinement(SmiRefinement *smiRefinementPtr)
1782 {
1783 List *listPtr;
1784
1785 if (!smiRefinementPtr) {
1786 return NULL;
1787 }
1788
1789 for (listPtr =
1790 ((Refinement *)smiRefinementPtr)->compliancePtr->refinementlistPtr;
1791 listPtr;
1792 listPtr = listPtr->nextPtr) {
1793 if ((Refinement *)(listPtr->ptr) == (Refinement *)smiRefinementPtr) {
1794 if (listPtr->nextPtr) {
1795 return &((Refinement *)listPtr->nextPtr->ptr)->export;
1796 } else {
1797 return NULL;
1798 }
1799 }
1800 }
1801
1802 return NULL;
1803 }
1804
1805
1806
1807 SmiNode *smiGetRefinementNode(SmiRefinement *smiRefinementPtr)
1808 {
1809 return &((Refinement *)smiRefinementPtr)->objectPtr->export;
1810 }
1811
1812
1813
1814 SmiType *smiGetRefinementType(SmiRefinement *smiRefinementPtr)
1815 {
1816 Type *typePtr;
1817
1818 typePtr = ((Refinement *)smiRefinementPtr)->typePtr;
1819
1820 if (!typePtr ||
1821 typePtr->export.basetype == SMI_BASETYPE_UNKNOWN) {
1822 return NULL;
1823 }
1824
1825 return &typePtr->export;
1826 }
1827
1828
1829
1830 SmiType *smiGetRefinementWriteType(SmiRefinement *smiRefinementPtr)
1831 {
1832 Type *typePtr;
1833
1834 typePtr = ((Refinement *)smiRefinementPtr)->writetypePtr;
1835
1836 if (!typePtr ||
1837 typePtr->export.basetype == SMI_BASETYPE_UNKNOWN) {
1838 return NULL;
1839 }
1840
1841 return &typePtr->export;
1842 }
1843
1844
1845
1846 int smiGetRefinementLine(SmiRefinement *smiRefinementPtr)
1847 {
1848 return ((Refinement *)smiRefinementPtr)->line;
1849 }
1850
1851
1852
1853 SmiElement *smiGetFirstUniquenessElement(SmiNode *smiNodePtr)
1854 {
1855 List *listPtr;
1856
1857 if (!smiNodePtr) {
1858 return NULL;
1859 }
1860
1861 listPtr = ((Object *)smiNodePtr)->uniquenessPtr;
1862
1863 return (SmiElement *)listPtr;
1864 }
1865
1866
1867
1868 char *smiRenderOID(unsigned int oidlen, SmiSubid *oid, int flags)
1869 {
1870 SmiNode *nodePtr = NULL;
1871 SmiModule *modulePtr = NULL;
1872 unsigned int i = 0;
1873 char *ss, *s = NULL;
1874
1875 if (!oid) {
1876 if (flags & SMI_RENDER_UNKNOWN) {
1877 smiAsprintf(&s, SMI_UNKNOWN_LABEL);
1878 } else {
1879 s = NULL;
1880 }
1881 return s;
1882 }
1883
1884 if (flags & (SMI_RENDER_NAME | SMI_RENDER_QUALIFIED)) {
1885 int len;
1886 for (len = oidlen; len; len--) {
1887 nodePtr = smiGetNodeByOID(len, oid);
1888 if (! nodePtr || nodePtr->name) break;
1889 }
1890 if (nodePtr && nodePtr->name) {
1891 i = nodePtr->oidlen;
1892 if (flags & SMI_RENDER_QUALIFIED) {
1893 modulePtr = smiGetNodeModule(nodePtr);
1894 }
1895 if (modulePtr) {
1896 smiAsprintf(&s, "%s::%s",
1897 modulePtr->name, nodePtr->name);
1898 } else {
1899 smiAsprintf(&s, "%s", nodePtr->name);
1900 }
1901 }
1902 }
1903
1904 for (; i < oidlen; i++) {
1905 ss = s;
1906 smiAsprintf(&s, "%s%s%u", ss ? ss : "", i ? "." : "", oid[i]);
1907 smiFree(ss);
1908 }
1909
1910 if ((!s) && (flags & SMI_RENDER_UNKNOWN)) {
1911 smiAsprintf(&s, SMI_UNKNOWN_LABEL);
1912 }
1913
1914 return s;
1915 }
1916
1917
1918
1919 char *smiRenderValue(SmiValue *smiValuePtr, SmiType *smiTypePtr, int flags)
1920 {
1921 unsigned int i, pfx;
1922 int j, k, n, have_pfx;
1923 char *last_fmt, *fmt;
1924 SmiUnsigned64 vv;
1925 int xlen;
1926 SmiNamedNumber *nn;
1927 char *s, *ss;
1928 char f[8];
1929 SmiUnsigned32 v32;
1930 SmiUnsigned64 v64;
1931
1932 if (!smiValuePtr) {
1933 if (flags & SMI_RENDER_UNKNOWN) {
1934 smiAsprintf(&s, SMI_UNKNOWN_LABEL);
1935 } else {
1936 s = NULL;
1937 }
1938 return s;
1939 }
1940
1941 switch (smiValuePtr->basetype) {
1942 case SMI_BASETYPE_UNSIGNED32:
1943 if (!(flags & SMI_RENDER_FORMAT) ||
1944 !smiTypePtr || !smiTypePtr->format ||
1945 !strlen(smiTypePtr->format) || smiTypePtr->format[0] == 'd') {
1946 if (smiTypePtr->format && (strlen(smiTypePtr->format) >= 3) &&
1947 (smiTypePtr->format[1] == '-')) {
1948 i = atoi(&smiTypePtr->format[2]);
1949 if (i < 0) i = 0;
1950 if (i > 20) i = 20;
1951 smiAsprintf(&s, "%0*lu.",
1952 1 + i,
1953 smiValuePtr->value.unsigned32);
1954 if (s) {
1955 for (j = strlen(s) - 1; i > 0; i--, j--) {
1956 s[j] = s[j-1];
1957 }
1958 s[j] = '.';
1959 }
1960 } else {
1961 smiAsprintf(&s, "%lu", smiValuePtr->value.unsigned32);
1962 }
1963 } else if (smiTypePtr->format[0] == 'x') {
1964 smiAsprintf(&s, "%lx", smiValuePtr->value.unsigned32);
1965 } else if (smiTypePtr->format[0] == 'o') {
1966 smiAsprintf(&s, "%lo", smiValuePtr->value.unsigned32);
1967 } else if (smiTypePtr->format[0] == 'b') {
1968 for (i = 32 - 1;
1969 i > 0 && !(smiValuePtr->value.unsigned32 & (1 << i)); i--);
1970 s = smiMalloc(i + 1 + 1);
1971 if (s) {
1972 for (j = 0; i >= 0; i--, j++) {
1973 s[j] = smiValuePtr->value.unsigned32 & (1<<i) ? '1' : '0';
1974 }
1975 s[j] = 0;
1976 }
1977 }
1978 break;
1979 case SMI_BASETYPE_UNSIGNED64:
1980 if (!(flags & SMI_RENDER_FORMAT) ||
1981 !smiTypePtr || !smiTypePtr->format ||
1982 !strlen(smiTypePtr->format) || smiTypePtr->format[0] == 'd') {
1983 if (smiTypePtr->format && (strlen(smiTypePtr->format) >= 3) &&
1984 (smiTypePtr->format[1] == '-')) {
1985 i = atoi(&smiTypePtr->format[2]);
1986 if (i < 0) i = 0;
1987 if (i > 20) i = 20;
1988 sprintf(f, "%%0%s.", UINT64_FORMAT);
1989 f[2] = '*';
1990 smiAsprintf(&s, f,
1991 1 + i,
1992 smiValuePtr->value.unsigned64);
1993 if (s) {
1994 for (j = strlen(s) - 1; i > 0; i--, j--) {
1995 s[j] = s[j-1];
1996 }
1997 s[j] = '.';
1998 }
1999 } else {
2000 smiAsprintf(&s, UINT64_FORMAT, smiValuePtr->value.unsigned64);
2001 }
2002 } else if (smiTypePtr->format[0] == 'x') {
2003 strcpy(f, UINT64_FORMAT);
2004 f[strlen(f)-1] = 'x';
2005 smiAsprintf(&s, f, smiValuePtr->value.unsigned64);
2006 } else if (smiTypePtr->format[0] == 'o') {
2007 strcpy(f, UINT64_FORMAT);
2008 f[strlen(f)-1] = 'o';
2009 smiAsprintf(&s, f, smiValuePtr->value.unsigned64);
2010 } else if (smiTypePtr->format[0] == 'b') {
2011 for (i = 64 - 1;
2012 i > 0 && !(smiValuePtr->value.unsigned64 & (1 << i)); i--);
2013 s = smiMalloc(i + 1 + 1);
2014 if (s) {
2015 for (j = 0; i >= 0; i--, j++) {
2016 s[j] = smiValuePtr->value.unsigned64 & (1<<i) ? '1' : '0';
2017 }
2018 s[j] = 0;
2019 }
2020 }
2021 break;
2022 case SMI_BASETYPE_INTEGER32:
2023 if (!(flags & SMI_RENDER_FORMAT) ||
2024 !smiTypePtr || !smiTypePtr->format ||
2025 !strlen(smiTypePtr->format) || smiTypePtr->format[0] == 'd') {
2026 if (smiTypePtr->format && (strlen(smiTypePtr->format) >= 3) &&
2027 (smiTypePtr->format[1] == '-')) {
2028 i = atoi(&smiTypePtr->format[2]);
2029 if (i < 0) i = 0;
2030 if (i > 20) i = 20;
2031 smiAsprintf(&s, "%0*ld.",
2032 1 + i + (smiValuePtr->value.integer32 < 0 ? 1 : 0),
2033 smiValuePtr->value.integer32);
2034 if (s) {
2035 for (j = strlen(s) - 1; i > 0; i--, j--) {
2036 s[j] = s[j-1];
2037 }
2038 s[j] = '.';
2039 }
2040 } else {
2041 smiAsprintf(&s, "%ld", smiValuePtr->value.integer32);
2042 }
2043 } else if (smiTypePtr->format[0] == 'x') {
2044 if (smiValuePtr->value.integer32 >= 0) {
2045 smiAsprintf(&s, "%lx", smiValuePtr->value.integer32);
2046 } else {
2047 smiAsprintf(&s, "-%lx", - smiValuePtr->value.integer32);
2048 }
2049 } else if (smiTypePtr->format[0] == 'o') {
2050 if (smiValuePtr->value.integer32 >= 0) {
2051 smiAsprintf(&s, "%lo", smiValuePtr->value.integer32);
2052 } else {
2053 smiAsprintf(&s, "-%lo", - smiValuePtr->value.integer32);
2054 }
2055 } else if (smiTypePtr->format[0] == 'b') {
2056 if (smiValuePtr->value.integer32 >= 0) {
2057 v32 = smiValuePtr->value.integer32;
2058 j = 0;
2059 } else {
2060 v32 = - smiValuePtr->value.integer32;
2061 j = 1;
2062 }
2063 for (i = 32 - 1;
2064 i > 0 && !(v32 & (1 << i)); i--);
2065 s = smiMalloc(i + j + 1 + 1);
2066 if (s) {
2067 s[0] = '-';
2068 for (; i >= 0; i--, j++) {
2069 s[j] = v32 & (1<<i) ? '1' : '0';
2070 }
2071 s[j] = 0;
2072 }
2073 }
2074 break;
2075 case SMI_BASETYPE_INTEGER64:
2076 if (!(flags & SMI_RENDER_FORMAT) ||
2077 !smiTypePtr || !smiTypePtr->format ||
2078 !strlen(smiTypePtr->format) || smiTypePtr->format[0] == 'd') {
2079 if (smiTypePtr->format && (strlen(smiTypePtr->format) >= 3) &&
2080 (smiTypePtr->format[1] == '-')) {
2081 i = atoi(&smiTypePtr->format[2]);
2082 if (i < 0) i = 0;
2083 if (i > 20) i = 20;
2084 sprintf(f, "%%0%s.", INT64_FORMAT);
2085 f[2] = '*';
2086 smiAsprintf(&s, f,
2087 1 + i + (smiValuePtr->value.integer64 < 0 ? 1 : 0),
2088 smiValuePtr->value.integer64);
2089 if (s) {
2090 for (j = strlen(s) - 1; i > 0; i--, j--) {
2091 s[j] = s[j-1];
2092 }
2093 s[j] = '.';
2094 }
2095 } else {
2096 smiAsprintf(&s, INT64_FORMAT, smiValuePtr->value.integer64);
2097 }
2098 } else if (smiTypePtr->format[0] == 'x') {
2099 if (smiValuePtr->value.integer64 >= 0) {
2100 strcpy(f, UINT64_FORMAT);
2101 f[strlen(f)-1] = 'x';
2102 smiAsprintf(&s, f, smiValuePtr->value.integer64);
2103 } else {
2104 sprintf(f, "-%s", UINT64_FORMAT);
2105 f[strlen(f)-1] = 'x';
2106 smiAsprintf(&s, f, - smiValuePtr->value.integer64);
2107 }
2108 } else if (smiTypePtr->format[0] == 'o') {
2109 if (smiValuePtr->value.integer64 >= 0) {
2110 strcpy(f, UINT64_FORMAT);
2111 sprintf(f, "-%s", UINT64_FORMAT);
2112 f[strlen(f)-1] = 'o';
2113 smiAsprintf(&s, f, smiValuePtr->value.integer64);
2114 } else {
2115 smiAsprintf(&s, f, - smiValuePtr->value.integer64);
2116 }
2117 } else if (smiTypePtr->format[0] == 'b') {
2118 if (smiValuePtr->value.integer64 >= 0) {
2119 v64 = smiValuePtr->value.integer64;
2120 j = 0;
2121 } else {
2122 v64 = - smiValuePtr->value.integer64;
2123 j = 1;
2124 }
2125 for (i = 64 - 1;
2126 i > 0 && !(v64 & (1 << i)); i--);
2127 s = smiMalloc(i + j + 1 + 1);
2128 if (s) {
2129 s[0] = '-';
2130 for (; i >= 0; i--, j++) {
2131 s[j] = v64 & (1<<i) ? '1' : '0';
2132 }
2133 s[j] = 0;
2134 }
2135 }
2136 break;
2137 case SMI_BASETYPE_OBJECTIDENTIFIER:
2138 s = smiRenderOID(smiValuePtr->len, smiValuePtr->value.oid, flags);
2139 break;
2140 case SMI_BASETYPE_OCTETSTRING:
2141 if (!(flags & SMI_RENDER_FORMAT) ||
2142 (!smiTypePtr->format && !smiTypePtr->name) ||
2143 (!smiTypePtr->format &&
2144 (smiTypePtr->name && strcmp( smiTypePtr->name, "IpAddress")) ) ) {
2145 for (i = 0; i < smiValuePtr->len; i++) {
2146 if (!isprint((int)smiValuePtr->value.ptr[i])) break;
2147 }
2148 if ((i < smiValuePtr->len) ||
2149 !(flags & SMI_RENDER_PRINTABLE)) {
2150 smiAsprintf(&s, "");
2151 for (i=0; i < smiValuePtr->len; i++) {
2152 ss = s;
2153 smiAsprintf(&s, "%s%02x", ss, smiValuePtr->value.ptr[i]);
2154 smiFree(ss);
2155 }
2156 } else {
2157 smiAsprintf(&s, "%s", smiValuePtr->value.ptr);
2158 }
2159 } else {
2160 i = 0;
2161 smiAsprintf(&s, "");
2162 /* SNMPv2-SMI:IpAddress does not have a display hint.
2163 ==> let's use this one: "1d." if we have an IpAddress here */
2164 fmt = (smiTypePtr->name &&
2165 strcmp( smiTypePtr->name, "IpAddress" ) ) ?
2166 smiTypePtr->format : "1d.";
2167 while (*fmt && i < smiValuePtr->len) {
2168 last_fmt = fmt;
2169 have_pfx = pfx = 0; /* scan prefix: */
2170 while (*fmt && isdigit((int)*fmt)) {
2171 pfx = pfx * 10 + *fmt - '0', have_pfx = 1, fmt++;
2172 }
2173 if (! have_pfx) {
2174 pfx = 1;
2175 }
2176 switch (*fmt) {
2177 case 't':
2178 /* XXX UTF-8 not implemented, fall through to ASCII (a) */
2179 case 'a':
2180 n = (pfx < (smiValuePtr->len - i)) ?
2181 pfx : smiValuePtr->len - i;
2182 for (k = 0; k < n; k++) {
2183 if (! isascii((int) smiValuePtr->value.ptr[i+k])) {
2184 smiFree(s);
2185 if (flags & SMI_RENDER_UNKNOWN) {
2186 smiAsprintf(&s, SMI_UNKNOWN_LABEL);
2187 } else {
2188 s = NULL;
2189 }
2190 return s;
2191 }
2192 ss = s;
2193 smiAsprintf(&s, "%s%c", ss, smiValuePtr->value.ptr[i+k]);
2194 smiFree(ss);
2195 }
2196 i += n;
2197 break;
2198 case 'b':
2199 case 'd':
2200 case 'o':
2201 case 'x':
2202 /* XXX: limited to no more than
2203 sizeof(SmiUnsigned64) octets */
2204 vv = 0;
2205 xlen = pfx * 2;
2206 while (pfx > 0 && i < smiValuePtr->len) {
2207 vv = vv * 256 +
2208 ((unsigned char)smiValuePtr->value.ptr[i]);
2209 i++;
2210 pfx--;
2211 }
2212 switch (*fmt) {
2213 case 'd':
2214 ss = s;
2215 sprintf(f, "%%s%s", UINT64_FORMAT);
2216 smiAsprintf(&s, f, ss, vv);
2217 smiFree(ss);
2218 break;
2219 case 'o':
2220 ss = s;
2221 sprintf(f, "%%s%s", UINT64_FORMAT);
2222 f[strlen(f)-1] = 'o';
2223 smiAsprintf(&s, f, ss, vv);
2224 smiFree(ss);
2225 break;
2226 case 'x':
2227 ss = s;
2228 sprintf(f, "%%s%%0%s", UINT64_FORMAT);
2229 f[4] = '*';
2230 f[strlen(f)-1] = 'x';
2231 smiAsprintf(&s, f, ss, xlen, vv);
2232 smiFree(ss);
2233 break;
2234 case 'b':
2235 k = pfx * 8 - 1;
2236 if (k > sizeof(SmiUnsigned64) * 8 - 1)
2237 k = sizeof(SmiUnsigned64) * 8 - 1;
2238 for (j = 0; k >= 0; k--, j++) {
2239 ss = s;
2240 smiAsprintf(&s, "%s%c",
2241 ss, vv & (1 << k) ? '1' : '0');
2242 smiFree(ss);
2243 }
2244 break;
2245 }
2246 break;
2247 default:
2248 smiFree(s);
2249 if (flags & SMI_RENDER_UNKNOWN) {
2250 smiAsprintf(&s, SMI_UNKNOWN_LABEL);
2251 } else {
2252 s = NULL;
2253 }
2254 return s;
2255 }
2256 fmt++;
2257
2258 /*
2259 * Check for a separator and repeat with last format if
2260 * data is still available.
2261 */
2262 if (*fmt && ! isdigit((int) *fmt) && *fmt != '*') {
2263 if (i < smiValuePtr->len) {
2264 ss = s;
2265 smiAsprintf(&s, "%s%c", ss, fmt[0]);
2266 smiFree(ss);
2267 }
2268 fmt++;
2269 }
2270
2271 if (! *fmt && (i < smiValuePtr->len)) {
2272 fmt = last_fmt;
2273 }
2274 }
2275 }
2276 break;
2277 case SMI_BASETYPE_ENUM:
2278 if ((flags & SMI_RENDER_NAME) && (smiTypePtr)) {
2279 for (nn = smiGetFirstNamedNumber(smiTypePtr); nn;
2280 nn = smiGetNextNamedNumber(nn)) {
2281 if (nn->value.value.integer32 == smiValuePtr->value.integer32)
2282 break;
2283 }
2284 if (nn) {
2285 if (flags & SMI_RENDER_NUMERIC) {
2286 smiAsprintf(&s, "%s(%ld)",
2287 nn->name, nn->value.value.integer32);
2288 } else {
2289 smiAsprintf(&s, "%s", nn->name);
2290 }
2291 } else {
2292 smiAsprintf(&s, "%ld", smiValuePtr->value.integer32);
2293 }
2294 } else {
2295 smiAsprintf(&s, "%ld", smiValuePtr->value.integer32);
2296 }
2297 break;
2298 case SMI_BASETYPE_BITS:
2299 smiAsprintf(&s, "");
2300 for (i = 0, nn = NULL; i < smiValuePtr->len * 8; i++) {
2301 if (smiValuePtr->value.ptr[i/8] & (1 << (7-(i%8)))) {
2302 if ((flags & SMI_RENDER_NAME) && (smiTypePtr)) {
2303 for (nn = smiGetFirstNamedNumber(smiTypePtr); nn;
2304 nn = smiGetNextNamedNumber(nn)) {
2305 if (nn->value.value.unsigned32 == i)
2306 break;
2307 }
2308 }
2309 ss = s;
2310 if ((flags & SMI_RENDER_NAME) &&
2311 (flags & SMI_RENDER_NUMERIC) && nn) {
2312 smiAsprintf(&s, "%s%s%s(%d)",
2313 ss, strlen(ss) ? " " : "", nn->name, i);
2314 } else if (nn) {
2315 smiAsprintf(&s, "%s%s%s",
2316 ss, strlen(ss) ? " " : "", nn->name);
2317 } else {
2318 smiAsprintf(&s, "%s%s%d",
2319 ss, strlen(ss) ? " " : "", i);
2320 }
2321 smiFree(ss);
2322 }
2323 }
2324 break;
2325 case SMI_BASETYPE_FLOAT32:
2326 case SMI_BASETYPE_FLOAT64:
2327 case SMI_BASETYPE_FLOAT128:
2328 case SMI_BASETYPE_UNKNOWN:
2329 default:
2330 if (flags & SMI_RENDER_UNKNOWN) {
2331 smiAsprintf(&s, SMI_UNKNOWN_LABEL);
2332 } else {
2333 s = NULL;
2334 }
2335 break;
2336 }
2337
2338 return s;
2339 }
2340
2341 char *smiRenderNode(SmiNode *smiNodePtr, int flags)
2342 {
2343 char *s;
2344 SmiModule *modulePtr;
2345
2346 if ((!smiNodePtr) || (smiNodePtr->name == NULL)) {
2347 if (flags & SMI_RENDER_UNKNOWN) {
2348 smiAsprintf(&s, SMI_UNKNOWN_LABEL);
2349 } else {
2350 s = NULL;
2351 }
2352 } else {
2353 modulePtr = smiGetNodeModule(smiNodePtr);
2354 if ((!(flags & SMI_RENDER_QUALIFIED)) ||
2355 (!modulePtr) ||
2356 (!strlen(modulePtr->name))) {
2357 smiAsprintf(&s, "%s", smiNodePtr->name);
2358 } else {
2359 smiAsprintf(&s, "%s::%s", modulePtr->name, smiNodePtr->name);
2360 }
2361 }
2362 return s;
2363 }
2364
2365
2366
2367 char *smiRenderType(SmiType *smiTypePtr, int flags)
2368 {
2369 char *s;
2370 SmiModule *modulePtr;
2371
2372 if ((!smiTypePtr) || (smiTypePtr->name == NULL)) {
2373 if (flags & SMI_RENDER_UNKNOWN) {
2374 smiAsprintf(&s, SMI_UNKNOWN_LABEL);
2375 } else {
2376 s = NULL;
2377 }
2378 } else {
2379 modulePtr = smiGetTypeModule(smiTypePtr);
2380 if ((!(flags & SMI_RENDER_QUALIFIED)) ||
2381 (!modulePtr) ||
2382 (!strlen(modulePtr->name))) {
2383 smiAsprintf(&s, "%s", smiTypePtr->name);
2384 } else {
2385 smiAsprintf(&s, "%s::%s", modulePtr->name, smiTypePtr->name);
2386 }
2387 }
2388 return s;
2389 }
2390
2391
2392
2393 unsigned int smiGetMinSize(SmiType *smiType)
2394 {
2395 SmiRange *smiRange;
2396 SmiType *parentType;
2397 unsigned int min = 65535, size;
2398
2399 switch (smiType->basetype) {
2400 case SMI_BASETYPE_BITS:
2401 return 0;
2402 case SMI_BASETYPE_OCTETSTRING:
2403 case SMI_BASETYPE_OBJECTIDENTIFIER:
2404 size = 0;
2405 break;
2406 default:
2407 return 0;
2408 }
2409
2410 for (smiRange = smiGetFirstRange(smiType);
2411 smiRange ; smiRange = smiGetNextRange(smiRange)) {
2412 if (smiRange->minValue.value.unsigned32 < min) {
2413 min = smiRange->minValue.value.unsigned32;
2414 }
2415 }
2416 if (min < 65535 && min > size) {
2417 size = min;
2418 }
2419
2420 parentType = smiGetParentType(smiType);
2421 if (parentType) {
2422 unsigned int psize = smiGetMinSize(parentType);
2423 if (psize > size) {
2424 size = psize;
2425 }
2426 }
2427
2428 return size;
2429 }
2430
2431
2432
2433 unsigned int smiGetMaxSize(SmiType *smiType)
2434 {
2435 SmiRange *smiRange;
2436 SmiType *parentType;
2437 SmiNamedNumber *nn;
2438 unsigned int max = 0, size;
2439
2440 switch (smiType->basetype) {
2441 case SMI_BASETYPE_BITS:
2442 case SMI_BASETYPE_OCTETSTRING:
2443 size = 65535;
2444 break;
2445 case SMI_BASETYPE_OBJECTIDENTIFIER:
2446 size = 128;
2447 break;
2448 default:
2449 return 0xffffffff;
2450 }
2451
2452 if (smiType->basetype == SMI_BASETYPE_BITS) {
2453 for (nn = smiGetFirstNamedNumber(smiType);
2454 nn;
2455 nn = smiGetNextNamedNumber(nn)) {
2456 if (nn->value.value.unsigned32 > max) {
2457 max = nn->value.value.unsigned32;
2458 }
2459 }
2460 size = (max / 8) + 1;
2461 return size;
2462 }
2463
2464 for (smiRange = smiGetFirstRange(smiType);
2465 smiRange ; smiRange = smiGetNextRange(smiRange)) {
2466 if (smiRange->maxValue.value.unsigned32 > max) {
2467 max = smiRange->maxValue.value.unsigned32;
2468 }
2469 }
2470 if (max > 0 && max < size) {
2471 size = max;
2472 }
2473
2474 parentType = smiGetParentType(smiType);
2475 if (parentType) {
2476 unsigned int psize = smiGetMaxSize(parentType);
2477 if (psize < size) {
2478 size = psize;
2479 }
2480 }
2481
2482 return size;
2483 }
2484
2485
2486
2487 int smiUnpack(SmiNode *row, SmiSubid *oid, unsigned int oidlen,
2488 SmiValue **vals, int *valslen)
2489 {
2490 SmiNode *indexNode = NULL;
2491 SmiElement *smiElement;
2492 SmiNode *iNode;
2493 SmiType *iType;
2494 int i, j, last = 0;
2495
2496 if (!vals || !valslen || !row || !oid) {
2497 return 0;
2498 }
2499
2500 switch (row->indexkind) {
2501 case SMI_INDEX_INDEX:
2502 case SMI_INDEX_REORDER:
2503 indexNode = row;
2504 break;
2505 case SMI_INDEX_EXPAND: /* TODO: we have to do more work here! */
2506 indexNode = NULL;
2507 break;
2508 case SMI_INDEX_AUGMENT:
2509 case SMI_INDEX_SPARSE:
2510 indexNode = smiGetRelatedNode(row);
2511 break;
2512 case SMI_INDEX_UNKNOWN:
2513 indexNode = NULL;
2514 break;
2515 }
2516
2517 *valslen = 0;
2518 for (smiElement = smiGetFirstElement(indexNode);
2519 smiElement; smiElement = smiGetNextElement(smiElement)) {
2520 iNode = smiGetElementNode(smiElement);
2521 if (iNode) {
2522 iType = smiGetNodeType(iNode);
2523 if (! iType) break;
2524 (*valslen)++;
2525 }
2526 }
2527 if (smiElement) {
2528 return 0;
2529 }
2530
2531 *vals = smiMalloc(*valslen * sizeof(SmiValue));
2532
2533 for (smiElement = smiGetFirstElement(indexNode), i = 0, j = 0;
2534 smiElement; smiElement = smiGetNextElement(smiElement), i++) {
2535 iNode = smiGetElementNode(smiElement);
2536 last = (smiGetNextElement(smiElement) == NULL);
2537 iType = smiGetNodeType(iNode);
2538 fprintf(stderr, "** %s (%s)\n", iNode->name, iType->name);
2539 (*vals)[i].basetype = iType->basetype;
2540 switch (iType->basetype) {
2541 case SMI_BASETYPE_ENUM:
2542 case SMI_BASETYPE_INTEGER32:
2543 (*vals)[i].value.integer32 = oid[j]; j++;
2544 break;
2545 case SMI_BASETYPE_UNSIGNED32:
2546 (*vals)[i].value.unsigned32 = oid[j]; j++;
2547 break;
2548 case SMI_BASETYPE_OCTETSTRING:
2549 /* need to know whether implied/fixed length or not */
2550 break;
2551 case SMI_BASETYPE_OBJECTIDENTIFIER:
2552 /* need to know whether implied/fixed length or not */
2553 break;
2554 default:
2555 return 0;
2556 }
2557 }
2558
2559 return *valslen;
2560 }
2561
2562
2563
2564 int smiAsprintf(char **strp, const char *format, ...)
2565 {
2566 int rc;
2567 va_list ap;
2568
2569 va_start(ap, format);
2570 rc = vasprintf(strp, format, ap);
2571 va_end(ap);
2572 if (! strp) {
2573 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
2574 }
2575 return rc;
2576 }
2577
2578
2579
2580 int smiVasprintf(char **strp, const char *format, va_list ap)
2581 {
2582 int rc;
2583
2584 rc = vasprintf(strp, format, ap);
2585 if (! strp) {
2586 smiPrintError(NULL, ERR_OUT_OF_MEMORY);
2587 }
2588 return rc;
2589 }
2590
2591
2592 int smiGetMinMaxRange(SmiType *smiType, SmiValue *min, SmiValue *max)
2593 {
2594 SmiBasetype basetype = SMI_BASETYPE_UNKNOWN;
2595 SmiRange *range;
2596
2597 min->basetype = max->basetype = SMI_BASETYPE_UNKNOWN;
2598 min->len = max->len = 0;
2599
2600 range = smiGetFirstRange(smiType);
2601 if (!range) {
2602 return 0;
2603 }
2604
2605 basetype = range->minValue.basetype;
2606 min->basetype = max->basetype = basetype;
2607
2608 switch (basetype) {
2609 case SMI_BASETYPE_INTEGER32:
2610 min->value.integer32 = SMI_BASETYPE_INTEGER32_MAX;
2611 max->value.integer32 = SMI_BASETYPE_INTEGER32_MIN;
2612 break;
2613 case SMI_BASETYPE_INTEGER64:
2614 min->value.integer64 = SMI_BASETYPE_INTEGER64_MAX;
2615 max->value.integer64 = SMI_BASETYPE_INTEGER64_MIN;
2616 break;
2617 case SMI_BASETYPE_UNSIGNED32:
2618 min->value.unsigned32 = SMI_BASETYPE_UNSIGNED32_MAX;
2619 max->value.unsigned32 = SMI_BASETYPE_UNSIGNED32_MIN;
2620 break;
2621 case SMI_BASETYPE_UNSIGNED64:
2622 min->value.unsigned64 = SMI_BASETYPE_UNSIGNED64_MAX;
2623 max->value.unsigned64 = SMI_BASETYPE_UNSIGNED32_MIN;
2624 break;
2625 default:
2626 fprintf(stderr, "smidump: unexpected basetype %d\n", basetype);
2627 return -1;
2628 }
2629
2630 for (range = smiGetFirstRange(smiType);
2631 range;
2632 range = smiGetNextRange(range)) {
2633 switch (basetype) {
2634 case SMI_BASETYPE_INTEGER32:
2635 if (range->minValue.value.integer32 < min->value.integer32) {
2636 min->value.integer32 = range->minValue.value.integer32;
2637 }
2638 if (range->maxValue.value.integer32 > max->value.integer32) {
2639 max->value.integer32 = range->maxValue.value.integer32;
2640 }
2641 break;
2642 case SMI_BASETYPE_INTEGER64:
2643 if (range->minValue.value.integer64 < min->value.integer64) {
2644 min->value.integer64 = range->minValue.value.integer64;
2645 }
2646 if (range->maxValue.value.integer64 > max->value.integer64) {
2647 max->value.integer64 = range->maxValue.value.integer64;
2648 }
2649 break;
2650 case SMI_BASETYPE_UNSIGNED32:
2651 if (range->minValue.value.unsigned32 < min->value.unsigned32) {
2652 min->value.unsigned32 = range->minValue.value.unsigned32;
2653 }
2654 if (range->maxValue.value.unsigned32 > max->value.unsigned32) {
2655 max->value.unsigned32 = range->maxValue.value.unsigned32;
2656 }
2657 break;
2658 case SMI_BASETYPE_UNSIGNED64:
2659 if (range->minValue.value.unsigned64 < min->value.unsigned64) {
2660 min->value.unsigned64 = range->minValue.value.unsigned64;
2661 }
2662 if (range->maxValue.value.unsigned64 > max->value.unsigned64) {
2663 max->value.unsigned64 = range->maxValue.value.unsigned64;
2664 }
2665 break;
2666 default:
2667 fprintf(stderr, "smidump: unexpected basetype %d\n", basetype);
2668 return -1;
2669 }
2670 }
2671
2672 return 0;
2673 }