1 #include <net-snmp/net-snmp-config.h>
16 #include <net-snmp/library/snmp_enum.h>
18 struct snmp_enum_list_str
{
20 struct snmp_enum_list
*list
;
21 struct snmp_enum_list_str
*next
;
24 static struct snmp_enum_list
***snmp_enum_lists
;
25 unsigned int current_maj_num
;
26 unsigned int current_min_num
;
27 struct snmp_enum_list_str
*sliststorage
;
35 snmp_enum_lists
= (struct snmp_enum_list
***)
36 malloc(sizeof(struct snmp_enum_list
**) * SE_MAX_IDS
);
39 current_maj_num
= SE_MAX_IDS
;
41 for (i
= 0; i
< SE_MAX_IDS
; i
++) {
42 if (!snmp_enum_lists
[i
])
43 snmp_enum_lists
[i
] = (struct snmp_enum_list
**)
44 malloc(sizeof(struct snmp_enum_list
*) * SE_MAX_SUBIDS
);
45 if (!snmp_enum_lists
[i
])
48 current_min_num
= SE_MAX_SUBIDS
;
56 se_store_list(struct snmp_enum_list
*new_list
,
57 unsigned int major
, unsigned int minor
)
61 if (major
> current_maj_num
|| minor
> current_min_num
) {
69 if (snmp_enum_lists
[major
][minor
] != NULL
)
70 ret
= SE_ALREADY_THERE
;
72 snmp_enum_lists
[major
][minor
] = new_list
;
77 struct snmp_enum_list
*
78 se_find_list(unsigned int major
, unsigned int minor
)
80 if (major
> current_maj_num
|| minor
> current_min_num
)
83 return snmp_enum_lists
[major
][minor
];
87 se_find_value_in_list(struct snmp_enum_list
*list
, char *label
)
90 return SE_DNE
; /* XXX: um, no good solution here */
92 if (strcmp(list
->label
, label
) == 0)
97 return SE_DNE
; /* XXX: um, no good solution here */
101 se_find_value(unsigned int major
, unsigned int minor
, char *label
)
103 return se_find_value_in_list(se_find_list(major
, minor
), label
);
107 se_find_label_in_list(struct snmp_enum_list
*list
, int value
)
112 if (list
->value
== value
)
113 return (list
->label
);
120 se_find_label(unsigned int major
, unsigned int minor
, int value
)
122 return se_find_label_in_list(se_find_list(major
, minor
), value
);
126 se_add_pair_to_list(struct snmp_enum_list
**list
, char *label
, int value
)
128 struct snmp_enum_list
*lastnode
= NULL
;
134 if ((*list
)->value
== value
)
135 return (SE_ALREADY_THERE
);
137 (*list
) = (*list
)->next
;
141 lastnode
->next
= (struct snmp_enum_list
*)
142 malloc(sizeof(struct snmp_enum_list
));
143 lastnode
= lastnode
->next
;
145 (*list
) = (struct snmp_enum_list
*)
146 malloc(sizeof(struct snmp_enum_list
));
151 lastnode
->label
= label
;
152 lastnode
->value
= value
;
153 lastnode
->next
= NULL
;
158 se_add_pair(unsigned int major
, unsigned int minor
, char *label
, int value
)
160 struct snmp_enum_list
*list
= se_find_list(major
, minor
);
161 int created
= (list
) ? 1 : 0;
162 int ret
= se_add_pair_to_list(&list
, label
, value
);
164 se_store_list(list
, major
, minor
);
169 * remember a list of enums based on a lookup name.
171 struct snmp_enum_list
*
172 se_find_slist(const char *listname
)
174 struct snmp_enum_list_str
*sptr
, *lastp
= NULL
;
178 for (sptr
= sliststorage
;
179 sptr
!= NULL
; lastp
= sptr
, sptr
= sptr
->next
)
180 if (sptr
->name
&& strcmp(sptr
->name
, listname
) == 0)
184 lastp
->next
= (struct snmp_enum_list_str
*)
185 malloc(sizeof(struct snmp_enum_list_str
));
188 sptr
= (struct snmp_enum_list_str
*)
189 malloc(sizeof(struct snmp_enum_list_str
));
193 sptr
->name
= strdup(listname
);
200 se_find_label_in_slist(const char *listname
, int value
)
202 return (se_find_label_in_list(se_find_slist(listname
), value
));
207 se_find_value_in_slist(const char *listname
, char *label
)
209 return (se_find_value_in_list(se_find_slist(listname
), label
));
213 se_add_pair_to_slist(const char *listname
, char *label
, int value
)
215 struct snmp_enum_list
*list
= se_find_slist(listname
);
216 int created
= (list
) ? 1 : 0;
217 int ret
= se_add_pair_to_list(&list
, label
, value
);
220 struct snmp_enum_list_str
*sptr
= (struct snmp_enum_list_str
*)
221 malloc(sizeof(struct snmp_enum_list_str
));
224 sptr
->next
= sliststorage
;
225 sptr
->name
= strdup(listname
);
236 se_add_pair(1, 1, "hi", 1);
237 se_add_pair(1, 1, "there", 2);
238 printf("hi: %d\n", se_find_value(1, 1, "hi"));
239 printf("2: %s\n", se_find_label(1, 1, 2));
241 se_add_pair_to_slist("testing", "life, and everything", 42);
242 se_add_pair_to_slist("testing", "resturant at the end of the universe",
245 printf("life, and everything: %d\n",
246 se_find_value_in_slist("testing", "life, and everything"));
247 printf("2: %s\n", se_find_label_in_slist("testing", 2));