fix outgoing QOS prios
[tomato.git] / release / src / router / snmp / snmplib / snmp_enum.c
blob6fcff2e8feb7ba525679cf0287cb2fedec3a448c
1 #include <net-snmp/net-snmp-config.h>
3 #ifdef HAVE_STDLIB_H
4 #include <stdlib.h>
5 #endif
6 #if HAVE_STRING_H
7 #include <string.h>
8 #else
9 #include <strings.h>
10 #endif
12 #if HAVE_DMALLOC_H
13 #include <dmalloc.h>
14 #endif
16 #include <net-snmp/library/snmp_enum.h>
18 struct snmp_enum_list_str {
19 char *name;
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;
29 int
30 init_snmp_enum(void)
32 int i;
34 if (!snmp_enum_lists)
35 snmp_enum_lists = (struct snmp_enum_list ***)
36 malloc(sizeof(struct snmp_enum_list **) * SE_MAX_IDS);
37 if (!snmp_enum_lists)
38 return SE_NOMEM;
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])
46 return SE_NOMEM;
48 current_min_num = SE_MAX_SUBIDS;
50 if (!sliststorage)
51 sliststorage = NULL;
52 return SE_OK;
55 int
56 se_store_list(struct snmp_enum_list *new_list,
57 unsigned int major, unsigned int minor)
59 int ret = SE_OK;
61 if (major > current_maj_num || minor > current_min_num) {
63 * XXX: realloc
65 return SE_NOMEM;
69 if (snmp_enum_lists[major][minor] != NULL)
70 ret = SE_ALREADY_THERE;
72 snmp_enum_lists[major][minor] = new_list;
74 return ret;
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)
81 return NULL;
83 return snmp_enum_lists[major][minor];
86 int
87 se_find_value_in_list(struct snmp_enum_list *list, char *label)
89 if (!list)
90 return SE_DNE; /* XXX: um, no good solution here */
91 while (list) {
92 if (strcmp(list->label, label) == 0)
93 return (list->value);
94 list = list->next;
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);
106 char *
107 se_find_label_in_list(struct snmp_enum_list *list, int value)
109 if (!list)
110 return NULL;
111 while (list) {
112 if (list->value == value)
113 return (list->label);
114 list = list->next;
116 return NULL;
119 char *
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;
130 if (!list)
131 return SE_DNE;
133 while (*list) {
134 if ((*list)->value == value)
135 return (SE_ALREADY_THERE);
136 lastnode = (*list);
137 (*list) = (*list)->next;
140 if (lastnode) {
141 lastnode->next = (struct snmp_enum_list *)
142 malloc(sizeof(struct snmp_enum_list));
143 lastnode = lastnode->next;
144 } else {
145 (*list) = (struct snmp_enum_list *)
146 malloc(sizeof(struct snmp_enum_list));
147 lastnode = (*list);
149 if (!lastnode)
150 return (SE_NOMEM);
151 lastnode->label = label;
152 lastnode->value = value;
153 lastnode->next = NULL;
154 return (SE_OK);
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);
163 if (!created)
164 se_store_list(list, major, minor);
165 return ret;
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;
175 if (!listname)
176 return NULL;
178 for (sptr = sliststorage;
179 sptr != NULL; lastp = sptr, sptr = sptr->next)
180 if (sptr->name && strcmp(sptr->name, listname) == 0)
181 return sptr->list;
183 if (lastp) {
184 lastp->next = (struct snmp_enum_list_str *)
185 malloc(sizeof(struct snmp_enum_list_str));
186 sptr = lastp->next;
187 } else {
188 sptr = (struct snmp_enum_list_str *)
189 malloc(sizeof(struct snmp_enum_list_str));
190 sliststorage = sptr;
192 sptr->list = NULL;
193 sptr->name = strdup(listname);
194 sptr->next = NULL;
195 return sptr->list;
199 char *
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);
219 if (!created) {
220 struct snmp_enum_list_str *sptr = (struct snmp_enum_list_str *)
221 malloc(sizeof(struct snmp_enum_list_str));
222 if (!sptr)
223 return SE_NOMEM;
224 sptr->next = sliststorage;
225 sptr->name = strdup(listname);
226 sptr->list = list;
227 sliststorage = sptr;
229 return ret;
232 #ifdef TESTING
233 main()
235 init_snmp_enum();
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));
249 #endif /* TESTING */