1 #ifdef BRCM_SNMP_SUPPORT
2 #include <net-snmp/net-snmp-config.h>
13 #include <net-snmp/net-snmp-includes.h>
16 * xxx-rks: when you have some spare time:
18 * a) add get-next access
20 * b) basically, everything currently creates one node per sub-oid,
21 * which is less than optimal. add code to create nodes with the
22 * longest possible OID per node, and split nodes when necessary
25 * c) If you are feeling really ambitious, also merge split nodes if
26 * possible on a delete.
30 /***************************************************************************
33 ***************************************************************************/
36 * Create an netsnmp_oid_stash node
38 * @param mysize the size of the child pointer array
40 * @return NULL on error, otherwise the newly allocated node
42 netsnmp_oid_stash_node *
43 netsnmp_oid_stash_create_sized_node(size_t mysize)
45 netsnmp_oid_stash_node *ret;
46 ret = SNMP_MALLOC_TYPEDEF(netsnmp_oid_stash_node);
49 ret->children = calloc(mysize, sizeof(netsnmp_oid_stash_node *));
54 ret->children_size = mysize;
58 NETSNMP_INLINE netsnmp_oid_stash_node *
59 netsnmp_oid_stash_create_node(void)
61 return netsnmp_oid_stash_create_sized_node(OID_STASH_CHILDREN_SIZE);
64 /** adds data to the stash at a given oid.
65 * returns SNMPERR_SUCCESS on success.
66 * returns SNMPERR_GENERR if data is already there.
67 * returns SNMPERR_MALLOC on malloc failures or if arguments passed in
71 netsnmp_oid_stash_add_data(netsnmp_oid_stash_node **root,
72 oid * lookup, size_t lookup_len, void *mydata)
74 netsnmp_oid_stash_node *curnode, *tmpp, *loopp;
77 if (!root || !lookup || lookup_len == 0)
78 return SNMPERR_GENERR;
81 *root = netsnmp_oid_stash_create_node();
83 return SNMPERR_MALLOC;
86 for (curnode = *root, i = 0; i < lookup_len; i++) {
87 tmpp = curnode->children[lookup[i] % curnode->children_size];
90 * no child in array at all
92 tmpp = curnode->children[lookup[i] % curnode->children_size] =
93 netsnmp_oid_stash_create_node();
94 tmpp->value = lookup[i];
96 for (loopp = tmpp; loopp; loopp = loopp->next_sibling) {
97 if (loopp->value == lookup[i])
104 * none exists. Create it
106 loopp = netsnmp_oid_stash_create_node();
107 loopp->value = lookup[i];
108 loopp->next_sibling = tmpp;
109 tmpp->prev_sibling = loopp;
110 curnode->children[lookup[i] % curnode->children_size] =
115 * tmpp now points to the proper node
121 * tmpp now points to the exact match
123 if (curnode->thedata)
124 return SNMPERR_GENERR;
126 return SNMPERR_GENERR;
127 tmpp->thedata = mydata;
128 return SNMPERR_SUCCESS;
131 /** returns a node associated with a given OID.
133 netsnmp_oid_stash_node *
134 netsnmp_oid_stash_get_node(netsnmp_oid_stash_node *root,
135 oid * lookup, size_t lookup_len)
137 netsnmp_oid_stash_node *curnode, *tmpp, *loopp;
143 for (curnode = root, i = 0; i < lookup_len; i++) {
144 tmpp = curnode->children[lookup[i] % curnode->children_size];
148 for (loopp = tmpp; loopp; loopp = loopp->next_sibling) {
149 if (loopp->value == lookup[i])
163 /** returns a data pointer associated with a given OID.
166 netsnmp_oid_stash_get_data(netsnmp_oid_stash_node *root,
167 oid * lookup, size_t lookup_len)
169 netsnmp_oid_stash_node *ret;
170 ret = netsnmp_oid_stash_get_node(root, lookup, lookup_len);
177 netsnmp_oid_stash_store_all(int majorID, int minorID,
178 void *serverarg, void *clientarg) {
179 oid oidbase[MAX_OID_LEN];
180 netsnmp_oid_stash_save_info *sinfo;
183 return SNMP_ERR_NOERROR;
186 netsnmp_oid_stash_store(*(sinfo->root), sinfo->token, sinfo->dumpfn,
188 return SNMP_ERR_NOERROR;
192 netsnmp_oid_stash_store(netsnmp_oid_stash_node *root,
193 const char *tokenname, NetSNMPStashDump *dumpfn,
194 oid *curoid, size_t curoid_len) {
196 char buf[SNMP_MAXBUF];
197 netsnmp_oid_stash_node *tmpp;
199 char *appname = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
200 NETSNMP_DS_LIB_APPTYPE);
203 if (!tokenname || !root || !curoid || !dumpfn)
206 for (i = 0; i < root->children_size; i++) {
207 if (root->children[i]) {
208 for (tmpp = root->children[i]; tmpp; tmpp = tmpp->next_sibling) {
209 curoid[curoid_len] = tmpp->value;
211 snprintf(buf, sizeof(buf), "%s ", tokenname);
212 cp = read_config_save_objid(buf+strlen(buf), curoid,
216 if ((*dumpfn)(cp, sizeof(buf) - strlen(buf),
217 tmpp->thedata, tmpp))
218 read_config_store(appname, buf);
220 netsnmp_oid_stash_store(tmpp, tokenname, dumpfn,
221 curoid, curoid_len+1);
228 oid_stash_dump(netsnmp_oid_stash_node *root, char *prefix)
230 char myprefix[MAX_OID_LEN * 4];
231 netsnmp_oid_stash_node *tmpp;
232 int prefix_len = strlen(prefix) + 1; /* actually it's +2 */
235 memset(myprefix, ' ', MAX_OID_LEN * 4);
236 myprefix[prefix_len] = '\0';
238 for (i = 0; i < root->children_size; i++) {
239 if (root->children[i]) {
240 for (tmpp = root->children[i]; tmpp; tmpp = tmpp->next_sibling) {
241 printf("%s%ld@%d:\n", prefix, tmpp->value, i);
242 oid_stash_dump(tmpp, myprefix);
247 #endif /* BRCM_SNMP_SUPPORT */