1 #ifdef BRCM_SNMP_WRITE_SUPPORT
3 /** allows overriding of a given oid with a new type and value */
5 #include <net-snmp/net-snmp-config.h>
6 #include <net-snmp/net-snmp-includes.h>
7 #include <net-snmp/agent/net-snmp-agent-includes.h>
9 #include "util_funcs.h"
11 typedef struct override_data_s {
17 /** @todo: (optionally) save values persistently when configured for
20 override_handler(netsnmp_mib_handler *handler,
21 netsnmp_handler_registration *reginfo,
22 netsnmp_agent_request_info *reqinfo,
23 netsnmp_request_info *requests)
26 override_data *data = handler->myvoid;
28 netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_GENERR);
29 return SNMP_ERR_NOERROR;
32 switch (reqinfo->mode) {
34 DEBUGMSGTL(("override", "overriding oid "));
35 DEBUGMSGOID(("override", requests->requestvb->name,
36 requests->requestvb->name_length));
37 DEBUGMSG(("override", "\n"));
38 snmp_set_var_typed_value(requests->requestvb, data->type,
39 (u_char *) data->value, data->value_len);
43 snmp_log(LOG_ERR, "unsupported mode in override handler");
46 return SNMP_ERR_NOERROR;
49 #define MALLOC_OR_DIE(x) \
50 thedata->value = malloc(x); \
51 thedata->value_len = x; \
52 if (!thedata->value) { \
54 config_perror("memory allocation failure"); \
59 netsnmp_parse_override(const char *token, char *line)
62 char buf[SNMP_MAXBUF];
64 oid oidbuf[MAX_OID_LEN];
65 size_t oidbuf_len = sizeof(oidbuf);
67 override_data *thedata;
68 netsnmp_handler_registration *the_reg;
70 cp = copy_nword(line, buf, sizeof(buf) - 1);
71 if (strcmp(buf, "-rw") == 0) {
73 cp = copy_nword(cp, buf, sizeof(buf) - 1);
77 config_perror("no oid specified");
81 if (!snmp_parse_oid(buf, oidbuf, &oidbuf_len)) {
82 config_perror("illegal oid");
85 cp = copy_nword(cp, buf, sizeof(buf) - 1);
87 if (!cp && strcmp(buf, "null") != 0) {
88 config_perror("no variable value specified");
92 type = se_find_value_in_slist("asntypes", buf);
94 config_perror("unknown type specified");
99 copy_nword(cp, buf, sizeof(buf) - 1);
103 thedata = SNMP_MALLOC_TYPEDEF(override_data);
104 thedata->type = type;
106 config_perror("memory allocation failure");
112 MALLOC_OR_DIE(sizeof(long));
113 *((long *) thedata->value) = strtol(buf, NULL, 0);
118 MALLOC_OR_DIE(sizeof(u_long));
119 *((u_long *) thedata->value) = strtoul(buf, NULL, 0);
124 if (buf[0] == '0' && buf[1] == 'x') {
129 hex_to_binary2(buf + 2, strlen(buf) - 2,
130 (char **) &thedata->value);
132 thedata->value = strdup(buf);
133 thedata->value_len = strlen(buf);
138 read_config_read_objid(buf, (oid **) & thedata->value,
139 &thedata->value_len);
143 thedata->value_len = 0;
147 config_perror("illegal/unsupported type specified");
151 if (!thedata->value && thedata->type != ASN_NULL) {
152 config_perror("memory allocation failure");
157 the_reg = SNMP_MALLOC_TYPEDEF(netsnmp_handler_registration);
159 config_perror("memory allocation failure");
164 the_reg->priority = 255;
165 the_reg->modes = (readwrite) ? HANDLER_CAN_RWRITE : HANDLER_CAN_RONLY;
167 netsnmp_create_handler("override", override_handler);
168 memdup((u_char **) & the_reg->rootoid, (const u_char *) oidbuf,
169 oidbuf_len * sizeof(oid));
170 the_reg->rootoid_len = oidbuf_len;
171 if (!the_reg->rootoid) {
172 config_perror("memory allocation failure");
176 the_reg->handler->myvoid = thedata;
178 if (netsnmp_register_instance(the_reg)) {
179 config_perror("oid registration failed within the agent");
180 SNMP_FREE(thedata->value);
191 snmpd_register_config_handler("override", netsnmp_parse_override, NULL, /* XXX: free func */
192 "[-rw] mibnode type value");
194 #endif /* BRCM_SNMP_WRITE_SUPPORT */