1 /*
2  * Note: this file originally auto-generated by mib2c using
3  *  : mib2c.container.conf,v 1.8 2006/07/26 15:58:26 dts12 Exp $
4  */
5 /* Portions of this file are subject to the following copyright(s).  See
6  * the Net-SNMP's COPYING file for more details and other copyrights
7  * that may apply:
8  */
9 /*
10  * Portions of this file are copyrighted by:
11  * Copyright (C) 2007 Apple, 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 
16 #include <net-snmp/net-snmp-config.h>
17 #include <net-snmp/net-snmp-includes.h>
18 #include <net-snmp/agent/net-snmp-agent-includes.h>
19 #include <net-snmp/agent/table_container.h>
20 #include <net-snmp/data_access/swinst.h>
21 #include <net-snmp/agent/cache_handler.h>
22 #include "hrSWInstalledTable.h"
23 
24 #define MYTABLE "hrSWInstalledTable"
25 
26 static netsnmp_table_registration_info *table_info;
27 
28 static void _cache_free(netsnmp_cache * cache, void *magic);
29 static int _cache_load(netsnmp_cache * cache, void *magic);
30 
31 /** Initializes the hrSWInstalledTable module */
32 void
init_hrSWInstalledTable(void)33 init_hrSWInstalledTable(void)
34 {
35     /*
36      * here we initialize all the tables we're planning on supporting
37      */
38     initialize_table_hrSWInstalledTable();
39 }
40 
41 void
shutdown_hrSWInstalledTable(void)42 shutdown_hrSWInstalledTable(void)
43 {
44     if (table_info) {
45 	netsnmp_table_registration_info_free(table_info);
46 	table_info = NULL;
47     }
48 }
49 
50 /** Initialize the hrSWInstalledTable table by defining its contents and how it's structured */
51 void
initialize_table_hrSWInstalledTable(void)52 initialize_table_hrSWInstalledTable(void)
53 {
54     static oid      hrSWInstalledTable_oid[] =
55         { 1, 3, 6, 1, 2, 1, 25, 6, 3 };
56     size_t          hrSWInstalledTable_oid_len =
57         OID_LENGTH(hrSWInstalledTable_oid);
58     netsnmp_handler_registration *reg;
59     netsnmp_mib_handler *handler = NULL;
60     netsnmp_container *container = NULL;
61     netsnmp_cache *cache = NULL;
62 
63     DEBUGMSGTL(("hrSWInstalled", "initialize\n"));
64 
65     reg =
66         netsnmp_create_handler_registration("hrSWInstalledTable",
67                                             hrSWInstalledTable_handler,
68                                             hrSWInstalledTable_oid,
69                                             hrSWInstalledTable_oid_len,
70                                             HANDLER_CAN_RONLY);
71     if (NULL == reg) {
72         snmp_log(LOG_ERR,"error creating handler registration for "
73                  MYTABLE "\n");
74         goto bail;
75     }
76 
77     container = netsnmp_container_find("hrSWInstalledTable:table_container");
78     if (NULL == container) {
79         snmp_log(LOG_ERR,"error creating container for "
80                  MYTABLE "\n");
81         goto bail;
82     }
83 
84     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
85     if (NULL == table_info) {
86         snmp_log(LOG_ERR,"error allocating table registration for "
87                  MYTABLE "\n");
88         goto bail;
89     }
90 
91     netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER,   /* index: hrSWInstalledIndex */
92                                      0);
93     table_info->min_column = COLUMN_HRSWINSTALLEDINDEX;
94     table_info->max_column = COLUMN_HRSWINSTALLEDDATE;
95 
96     /*************************************************
97      *
98      * inject container_table helper
99      */
100     handler = netsnmp_container_table_handler_get(table_info, container,
101                                                   TABLE_CONTAINER_KEY_NETSNMP_INDEX);
102     if (NULL == handler) {
103         snmp_log(LOG_ERR,"error allocating table registration for "
104                  MYTABLE "\n");
105         goto bail;
106     }
107     if (SNMPERR_SUCCESS != netsnmp_inject_handler(reg, handler)) {
108         snmp_log(LOG_ERR,"error injecting container_table handler for "
109                  MYTABLE "\n");
110         goto bail;
111     }
112     handler = NULL; /* reg has it, will reuse below */
113 
114     /*************************************************
115      *
116      * inject cache helper
117      */
118     cache = netsnmp_cache_create(30,    /* timeout in seconds */
119                                  _cache_load, _cache_free,
120                                  hrSWInstalledTable_oid,
121                                  hrSWInstalledTable_oid_len);
122 
123     if (NULL == cache) {
124         snmp_log(LOG_ERR, "error creating cache for "
125                  MYTABLE "\n");
126         goto bail;
127     }
128     cache->magic = container;
129 
130     handler = netsnmp_cache_handler_get(cache);
131     if (NULL == handler) {
132         snmp_log(LOG_ERR, "error creating cache handler for "
133                  MYTABLE "\n");
134         goto bail;
135     }
136     if (SNMPERR_SUCCESS != netsnmp_inject_handler(reg, handler)) {
137         snmp_log(LOG_ERR,"error injecting cache handler for "
138                  MYTABLE "\n");
139         goto bail;
140     }
141     handler = NULL; /* reg has it*/
142 
143     if (SNMPERR_SUCCESS != netsnmp_register_table(reg, table_info)) {
144         snmp_log(LOG_ERR,"error registering table handler for "
145                  MYTABLE "\n");
146         reg = NULL; /* it was freed inside netsnmp_register_table */
147         goto bail;
148     }
149 
150     return; /* ok */
151 
152 
153   bail: /* not ok */
154 
155     if (handler)
156         netsnmp_handler_free(handler);
157 
158     if (cache)
159         netsnmp_cache_free(cache);
160 
161     if (table_info)
162         netsnmp_table_registration_info_free(table_info);
163 
164     if (container)
165         CONTAINER_FREE(container);
166 
167     if (reg)
168         netsnmp_handler_registration_free(reg);
169 
170 }
171 
172 /** handles requests for the hrSWInstalledTable table */
173 int
hrSWInstalledTable_handler(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)174 hrSWInstalledTable_handler(netsnmp_mib_handler *handler,
175                            netsnmp_handler_registration *reginfo,
176                            netsnmp_agent_request_info *reqinfo,
177                            netsnmp_request_info *requests)
178 {
179 
180     netsnmp_request_info *request;
181     netsnmp_table_request_info *table_info;
182     netsnmp_swinst_entry *table_entry;
183 
184     switch (reqinfo->mode) {
185         /*
186          * Read-support (also covers GetNext requests)
187          */
188     case MODE_GET:
189         for (request = requests; request; request = request->next) {
190             if (request->processed)
191                continue;
192             table_entry = (netsnmp_swinst_entry *)
193                 netsnmp_container_table_extract_context(request);
194             table_info = netsnmp_extract_table_info(request);
195             if ((NULL == table_entry) || (NULL == table_info)) {
196                 snmp_log(LOG_ERR, "could not extract table entry or info for "
197                  MYTABLE "\n");
198                 snmp_set_var_typed_value(request->requestvb,
199                                          SNMP_ERR_GENERR, NULL, 0);
200                 continue;
201             }
202 
203             switch (table_info->colnum) {
204             case COLUMN_HRSWINSTALLEDINDEX:
205                 snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
206                                            table_entry->swIndex);
207                 break;
208             case COLUMN_HRSWINSTALLEDNAME: {
209                 snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
210                                          (u_char *) table_entry->swName,
211                                          table_entry->swName_len);
212                 } break;
213             case COLUMN_HRSWINSTALLEDID:
214                 snmp_set_var_typed_value(request->requestvb, ASN_OBJECT_ID,
215                                          (u_char *) &nullOid, nullOidLen);
216                 break;
217             case COLUMN_HRSWINSTALLEDTYPE:
218                 snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
219                                            table_entry->swType);
220                 break;
221             case COLUMN_HRSWINSTALLEDDATE:
222                 snmp_set_var_typed_value(request->requestvb, ASN_OCTET_STR,
223                                          (u_char *) table_entry->swDate,
224                                          table_entry->swDate_len);
225                 break;
226             default:
227                 /*
228                  * An unsupported/unreadable column (if applicable)
229                  */
230                 snmp_set_var_typed_value(request->requestvb,
231                                          SNMP_NOSUCHOBJECT, NULL, 0);
232             }
233         }
234         break;
235 
236     }
237     return SNMP_ERR_NOERROR;
238 }
239 
240 /***********************************************************************
241  *
242  * DATA ACCESS
243  *
244  * The data access mechanism here is rather simple: let newsnmp_swinst_*
245  * take care of it.
246  ***********************************************************************/
247 /**
248  * @internal
249  */
250 static int
_cache_load(netsnmp_cache * cache,void * vmagic)251 _cache_load(netsnmp_cache * cache, void *vmagic)
252 {
253     DEBUGMSGTL(("hrSWInstalledTable:cache", "load\n"));
254 
255     if ((NULL == cache) || (NULL == cache->magic)) {
256         snmp_log(LOG_ERR, "invalid cache for hrSWInstalledTable_cache_load\n");
257         return -1;
258     }
259 
260     /** should only be called for an invalid or expired cache */
261     netsnmp_assert((0 == cache->valid) || (1 == cache->expired));
262 
263     cache->magic =
264         netsnmp_swinst_container_load((netsnmp_container *) cache->magic, 0);
265 
266     return 0;
267 }                               /* _cache_load */
268 
269 /**
270  * @internal
271  */
272 static void
_cache_free(netsnmp_cache * cache,void * magic)273 _cache_free(netsnmp_cache * cache, void *magic)
274 {
275     if ((NULL == cache) || (NULL == cache->magic)) {
276         snmp_log(LOG_ERR, "invalid cache in hrSWInstalledTable_cache_free\n");
277         return;
278     }
279     DEBUGMSGTL(("hrSWInstalledTable:cache", "free\n"));
280 
281     netsnmp_swinst_container_free_items((netsnmp_container *) cache->magic);
282 }                               /* _cache_free */
283