Debian Codegraph @

agent/helpers/table_row.c

raw
1 /*
2 * table_row.c
3 *
4 * Helper for registering single row slices of a shared table
5 *
6 * Portions of this file are subject to the following copyright(s). See
7 * the Net-SNMP's COPYING file for more details and other copyrights
8 * that may apply:
9 *
10 * Portions of this file are copyrighted by:
11 * Copyright (c) 2016 VMware, Inc. All rights reserved.
12 * Use is subject to license terms specified in the COPYING file
13 * distributed with the Net-SNMP package.
14 */
15 #define TABLE_ROW_DATA "table_row"
16
17 #include <net-snmp/net-snmp-config.h>
18 #include <net-snmp/net-snmp-features.h>
19
20 #ifdef HAVE_STRING_H
21 #include <string.h>
22 #else
23 #include <strings.h>
24 #endif
25
26 #include <net-snmp/net-snmp-includes.h>
27 #include <net-snmp/agent/net-snmp-agent-includes.h>
28
29 #include <net-snmp/agent/table.h>
30 #include <net-snmp/agent/table_container.h>
31 #include <net-snmp/library/container.h>
32 #include <net-snmp/library/snmp_assert.h>
33
34 netsnmp_feature_child_of(table_row_all, mib_helpers);
35
36 netsnmp_feature_child_of(table_row_extract, table_row_all);
37
38
39 /*
40 * snmp.h:#define SNMP_MSG_INTERNAL_SET_BEGIN -1
41 * snmp.h:#define SNMP_MSG_INTERNAL_SET_RESERVE1 0
42 * snmp.h:#define SNMP_MSG_INTERNAL_SET_RESERVE2 1
43 * snmp.h:#define SNMP_MSG_INTERNAL_SET_ACTION 2
44 * snmp.h:#define SNMP_MSG_INTERNAL_SET_COMMIT 3
45 * snmp.h:#define SNMP_MSG_INTERNAL_SET_FREE 4
46 * snmp.h:#define SNMP_MSG_INTERNAL_SET_UNDO 5
47 */
48
49 /** @defgroup table_row table_row
50 * Helps you implement a table shared across two or more subagents,
51 * or otherwise split into individual row slices.
52 * @ingroup table
53 *
54 * @{
55 */
56
57 static Netsnmp_Node_Handler _table_row_handler;
58 static Netsnmp_Node_Handler _table_row_default_handler;
59
60 /**********************************************************************
61 **********************************************************************
62 * *
63 * *
64 * PUBLIC Registration functions *
65 * *
66 * *
67 **********************************************************************
68 **********************************************************************/
69
70 /* ==================================
71 *
72 * Table Row API: Table maintenance
73 *
74 * This helper doesn't operate with the complete
75 * table, so these routines are not relevant.
76 *
77 * ================================== */
78
79
80 /* ==================================
81 *
82 * Table Row API: MIB maintenance
83 *
84 * ================================== */
85
86 /** returns a netsnmp_mib_handler object for the table_container helper */
87 netsnmp_mib_handler *
88 netsnmp_table_row_handler_get(void *row)
89 {
90 netsnmp_mib_handler *handler;
91
92 handler = netsnmp_create_handler("table_row",
93 _table_row_handler);
94 if(NULL == handler) {
95 snmp_log(LOG_ERR,
96 "malloc failure in netsnmp_table_row_register\n");
97 return NULL;
98 }
99
100 handler->myvoid = (void*)row;
101 handler->flags |= MIB_HANDLER_INSTANCE;
102 /* handler->flags |= MIB_HANDLER_AUTO_NEXT; ??? */
103
104 return handler;
105 }
106
107 int
108 netsnmp_table_row_register(netsnmp_handler_registration *reginfo,
109 netsnmp_table_registration_info *tabreg,
110 void *row, netsnmp_variable_list *index)
111 {
112 netsnmp_handler_registration *reg2;
113 netsnmp_mib_handler *handler;
114 oid row_oid[MAX_OID_LEN];
115 size_t row_oid_len, len;
116 char tmp[SNMP_MAXBUF_MEDIUM];
117
118 if ((NULL == reginfo) || (NULL == reginfo->handler) || (NULL == tabreg)) {
119 snmp_log(LOG_ERR, "bad param in netsnmp_table_row_register\n");
120 netsnmp_handler_registration_free(reginfo);
121 return SNMPERR_GENERR;
122 }
123
124 /*
125 * The first table_row invoked for a particular table should
126 * register the full table as well, with a default handler to
127 * process requests for non-existent (or incomplete) rows.
128 *
129 * Subsequent table_row registrations attempting to set up
130 * this default handler would fail - preferably silently!
131 */
132 snprintf(tmp, sizeof(tmp), "%s_table", reginfo->handlerName);
133 reg2 = netsnmp_create_handler_registration(
134 tmp, _table_row_default_handler,
135 reginfo->rootoid, reginfo->rootoid_len,
136 reginfo->modes);
137 netsnmp_register_table(reg2, tabreg); /* Ignore return value */
138
139 /*
140 * Adjust the OID being registered, to take account
141 * of the indexes and column range provided....
142 */
143 row_oid_len = reginfo->rootoid_len;
144 memcpy( row_oid, (u_char *) reginfo->rootoid, row_oid_len * sizeof(oid));
145 row_oid[row_oid_len++] = 1; /* tableEntry */
146 row_oid[row_oid_len++] = tabreg->min_column;
147 reginfo->range_ubound = tabreg->max_column;
148 reginfo->range_subid = row_oid_len-1;
149 build_oid_noalloc(&row_oid[row_oid_len],
150 MAX_OID_LEN-row_oid_len, &len, NULL, 0, index);
151 row_oid_len += len;
152 free(reginfo->rootoid);
153 reginfo->rootoid = snmp_duplicate_objid(row_oid, row_oid_len);
154 reginfo->rootoid_len = row_oid_len;
155
156
157 /*
158 * ... insert a minimal handler ...
159 */
160 handler = netsnmp_table_row_handler_get(row);
161 if (!handler ||
162 (netsnmp_inject_handler(reginfo, handler) != SNMPERR_SUCCESS)) {
163 snmp_log(LOG_ERR, "could not create table row handler\n");
164 netsnmp_handler_free(handler);
165 netsnmp_handler_registration_free(reginfo);
166 return SNMP_ERR_GENERR;
167 }
168
169 /*
170 * ... and register the row
171 */
172 return netsnmp_register_handler(reginfo);
173 }
174
175
176 /** return the row data structure supplied to the table_row helper */
177 #ifndef NETSNMP_FEATURE_REMOVE_TABLE_ROW_EXTRACT
178 void *
179 netsnmp_table_row_extract(netsnmp_request_info *request)
180 {
181 return netsnmp_request_get_list_data(request, TABLE_ROW_DATA);
182 }
183 #endif /* NETSNMP_FEATURE_REMOVE_TABLE_ROW_EXTRACT */
184 /** @cond */
185
186 /**********************************************************************
187 **********************************************************************
188 * *
189 * *
190 * netsnmp_table_row_helper_handler() *
191 * *
192 * *
193 **********************************************************************
194 **********************************************************************/
195
196 static int
197 _table_row_handler(netsnmp_mib_handler *handler,
198 netsnmp_handler_registration *reginfo,
199 netsnmp_agent_request_info *reqinfo,
200 netsnmp_request_info *requests)
201 {
202 int rc = SNMP_ERR_NOERROR;
203 netsnmp_request_info *req;
204 void *row;
205
206 /** sanity checks */
207 netsnmp_assert((NULL != handler) && (NULL != handler->myvoid));
208 netsnmp_assert((NULL != reginfo) && (NULL != reqinfo));
209
210 DEBUGMSGTL(("table_row", "Mode %s, Got request:\n",
211 se_find_label_in_slist("agent_mode",reqinfo->mode)));
212
213 /*
214 * First off, get our pointer from the handler.
215 * This contains the row that was actually registered.
216 * Make this available for each of the requests passed in.
217 */
218 row = handler->myvoid;
219 for (req = requests; req; req=req->next)
220 netsnmp_request_add_list_data(req,
221 netsnmp_create_data_list(TABLE_ROW_DATA, row, NULL));
222
223 /*
224 * Then call the next handler, to actually process the request
225 */
226 rc = netsnmp_call_next_handler(handler, reginfo, reqinfo, requests);
227 if (rc != SNMP_ERR_NOERROR) {
228 DEBUGMSGTL(("table_row", "next handler returned %d\n", rc));
229 }
230
231 return rc;
232 }
233
234 static int
235 _table_row_default_handler(netsnmp_mib_handler *handler,
236 netsnmp_handler_registration *reginfo,
237 netsnmp_agent_request_info *reqinfo,
238 netsnmp_request_info *requests)
239 {
240 netsnmp_request_info *req;
241 netsnmp_table_request_info *table_info;
242 netsnmp_table_registration_info *tabreg;
243
244 tabreg = netsnmp_find_table_registration_info(reginfo);
245 for ( req=requests; req; req=req->next ) {
246 table_info = netsnmp_extract_table_info( req );
247 if (( table_info->colnum >= tabreg->min_column ) ||
248 ( table_info->colnum <= tabreg->max_column )) {
249 netsnmp_set_request_error( reqinfo, req, SNMP_NOSUCHINSTANCE );
250 } else {
251 netsnmp_set_request_error( reqinfo, req, SNMP_NOSUCHOBJECT );
252 }
253 }
254 return SNMP_ERR_NOERROR;
255 }
256 /** @endcond */
257
258
259 /* ==================================
260 *
261 * Table Row API: Row operations
262 *
263 * This helper doesn't operate with the complete
264 * table, so these routines are not relevant.
265 *
266 * ================================== */
267
268
269 /* ==================================
270 *
271 * Table Row API: Index operations
272 *
273 * This helper doesn't operate with the complete
274 * table, so these routines are not relevant.
275 *
276 * ================================== */
277
278 /** @} */