Debian Codegraph @

agent/helpers/multiplexer.c

raw
1 #include <net-snmp/net-snmp-config.h>
2
3 #include <sys/types.h>
4
5 #include <net-snmp/net-snmp-includes.h>
6 #include <net-snmp/agent/net-snmp-agent-includes.h>
7
8 #include <net-snmp/agent/multiplexer.h>
9
10 /** @defgroup multiplexer multiplexer
11 * Splits mode requests into calls to different handlers.
12 * @ingroup utilities
13 * The multiplexer helper lets you split the calling chain depending
14 * on the calling mode (get vs getnext vs set). Useful if you want
15 * different routines to handle different aspects of SNMP requests,
16 * which is very common for GET vs SET type actions.
17 *
18 * Functionally:
19 *
20 * -# GET requests call the get_method
21 * -# GETNEXT requests call the getnext_method, or if not present, the
22 * get_method.
23 * -# GETBULK requests call the getbulk_method, or if not present, the
24 * getnext_method, or if even that isn't present the get_method.
25 * -# SET requests call the set_method, or if not present return a
26 * SNMP_ERR_NOTWRITABLE error.
27 * @{
28 */
29
30 /** returns a multiplixer handler given a netsnmp_mib_handler_methods structure of subhandlers.
31 */
32 netsnmp_mib_handler *
33 netsnmp_get_multiplexer_handler(netsnmp_mib_handler_methods *req)
34 {
35 netsnmp_mib_handler *ret = NULL;
36
37 if (!req) {
38 snmp_log(LOG_INFO,
39 "netsnmp_get_multiplexer_handler(NULL) called\n");
40 return NULL;
41 }
42
43 ret =
44 netsnmp_create_handler("multiplexer",
45 netsnmp_multiplexer_helper_handler);
46 if (ret) {
47 ret->myvoid = (void *) req;
48 }
49 return ret;
50 }
51
52 /** implements the multiplexer helper */
53 int
54 netsnmp_multiplexer_helper_handler(netsnmp_mib_handler *handler,
55 netsnmp_handler_registration *reginfo,
56 netsnmp_agent_request_info *reqinfo,
57 netsnmp_request_info *requests)
58 {
59
60 netsnmp_mib_handler_methods *methods;
61
62 if (!handler->myvoid) {
63 snmp_log(LOG_INFO, "improperly registered multiplexer found\n");
64 return SNMP_ERR_GENERR;
65 }
66
67 methods = (netsnmp_mib_handler_methods *) handler->myvoid;
68
69 switch (reqinfo->mode) {
70 case MODE_GETBULK:
71 handler = methods->getbulk_handler;
72 if (handler)
73 break;
74 /* Deliberate fallthrough to use GetNext handler */
75 /* FALL THROUGH */
76 case MODE_GETNEXT:
77 handler = methods->getnext_handler;
78 if (handler)
79 break;
80 /* Deliberate fallthrough to use Get handler */
81 /* FALL THROUGH */
82 case MODE_GET:
83 handler = methods->get_handler;
84 if (!handler) {
85 netsnmp_request_set_error_all(requests, SNMP_NOSUCHOBJECT);
86 }
87 break;
88
89 #ifndef NETSNMP_NO_WRITE_SUPPORT
90 case MODE_SET_RESERVE1:
91 case MODE_SET_RESERVE2:
92 case MODE_SET_ACTION:
93 case MODE_SET_COMMIT:
94 case MODE_SET_FREE:
95 case MODE_SET_UNDO:
96 handler = methods->set_handler;
97 if (!handler) {
98 netsnmp_request_set_error_all(requests, SNMP_ERR_NOTWRITABLE);
99 return SNMP_ERR_NOERROR;
100 }
101 break;
102
103 /*
104 * XXX: process SETs specially, and possibly others
105 */
106 #endif /* NETSNMP_NO_WRITE_SUPPORT */
107 default:
108 snmp_log(LOG_ERR, "unsupported mode for multiplexer: %d\n",
109 reqinfo->mode);
110 return SNMP_ERR_GENERR;
111 }
112 if (!handler) {
113 snmp_log(LOG_ERR,
114 "No handler enabled for mode %d in multiplexer\n",
115 reqinfo->mode);
116 return SNMP_ERR_GENERR;
117 }
118 return netsnmp_call_handler(handler, reginfo, reqinfo, requests);
119 }
120 /** @} */
121