1 /*
2  * Note: this file originally auto-generated by mib2c using
3  *       version : 1.17 $ of : mfd-data-access.m2c,v $
4  *
5  * $Id$
6  */
7 /*
8  * standard Net-SNMP includes
9  */
10 #include <net-snmp/net-snmp-config.h>
11 #include <net-snmp/net-snmp-includes.h>
12 #include <net-snmp/agent/net-snmp-agent-includes.h>
13 
14 /*
15  * include our parent header
16  */
17 #include "tcpConnectionTable.h"
18 
19 
20 #include "tcpConnectionTable_data_access.h"
21 
22 /** @ingroup interface
23  * @addtogroup data_access data_access: Routines to access data
24  *
25  * These routines are used to locate the data used to satisfy
26  * requests.
27  *
28  * @{
29  */
30 /**********************************************************************
31  **********************************************************************
32  ***
33  *** Table tcpConnectionTable
34  ***
35  **********************************************************************
36  **********************************************************************/
37 /*
38  * TCP-MIB::tcpConnectionTable is subid 19 of tcp.
39  * Its status is Current.
40  * OID: .1.3.6.1.2.1.6.19, length: 8
41  */
42 
43 /**
44  * initialization for tcpConnectionTable data access
45  *
46  * This function is called during startup to allow you to
47  * allocate any resources you need for the data table.
48  *
49  * @param tcpConnectionTable_reg
50  *        Pointer to tcpConnectionTable_registration
51  *
52  * @retval MFD_SUCCESS : success.
53  * @retval MFD_ERROR   : unrecoverable error.
54  */
55 int
tcpConnectionTable_init_data(tcpConnectionTable_registration * tcpConnectionTable_reg)56 tcpConnectionTable_init_data(tcpConnectionTable_registration *
57                              tcpConnectionTable_reg)
58 {
59     DEBUGMSGTL(("verbose:tcpConnectionTable:tcpConnectionTable_init_data",
60                 "called\n"));
61 
62     /*
63      * TODO:303:o: Initialize tcpConnectionTable data.
64      */
65 
66     return MFD_SUCCESS;
67 }                               /* tcpConnectionTable_init_data */
68 
69 /**
70  * container overview
71  *
72  */
73 
74 /**
75  * container initialization
76  *
77  * @param container_ptr_ptr A pointer to a container pointer. If you
78  *        create a custom container, use this parameter to return it
79  *        to the MFD helper. If set to NULL, the MFD helper will
80  *        allocate a container for you.
81  * @param  cache A pointer to a cache structure. You can set the timeout
82  *         and other cache flags using this pointer.
83  *
84  *  This function is called at startup to allow you to customize certain
85  *  aspects of the access method. For the most part, it is for advanced
86  *  users. The default code should suffice for most cases. If no custom
87  *  container is allocated, the MFD code will create one for your.
88  *
89  *  This is also the place to set up cache behavior. The default, to
90  *  simply set the cache timeout, will work well with the default
91  *  container. If you are using a custom container, you may want to
92  *  look at the cache helper documentation to see if there are any
93  *  flags you want to set.
94  *
95  * @remark
96  *  This would also be a good place to do any initialization needed
97  *  for you data source. For example, opening a connection to another
98  *  process that will supply the data, opening a database, etc.
99  */
100 void
tcpConnectionTable_container_init(netsnmp_container ** container_ptr_ptr,netsnmp_cache * cache)101 tcpConnectionTable_container_init(netsnmp_container **container_ptr_ptr,
102                                   netsnmp_cache * cache)
103 {
104     DEBUGMSGTL(("verbose:tcpConnectionTable:tcpConnectionTable_container_init", "called\n"));
105 
106     if (NULL == container_ptr_ptr) {
107         snmp_log(LOG_ERR,
108                  "bad container param to tcpConnectionTable_container_init\n");
109         return;
110     }
111 
112     /*
113      * For advanced users, you can use a custom container. If you
114      * do not create one, one will be created for you.
115      */
116     *container_ptr_ptr = NULL;
117 
118     if (NULL == cache) {
119         snmp_log(LOG_ERR,
120                  "bad cache param to tcpConnectionTable_container_init\n");
121         return;
122     }
123 
124     /*
125      * TODO:345:A: Set up tcpConnectionTable cache properties.
126      *
127      * Also for advanced users, you can set parameters for the
128      * cache. Do not change the magic pointer, as it is used
129      * by the MFD helper. To completely disable caching, set
130      * cache->enabled to 0.
131      */
132     cache->timeout = TCPCONNECTIONTABLE_CACHE_TIMEOUT;  /* seconds */
133     cache->flags |= NETSNMP_CACHE_DONT_INVALIDATE_ON_SET;
134 }                               /* tcpConnectionTable_container_init */
135 
136 /**
137  * container shutdown
138  *
139  * @param container_ptr A pointer to the container.
140  *
141  *  This function is called at shutdown to allow you to customize certain
142  *  aspects of the access method. For the most part, it is for advanced
143  *  users. The default code should suffice for most cases.
144  *
145  *  This function is called before tcpConnectionTable_container_free().
146  *
147  * @remark
148  *  This would also be a good place to do any cleanup needed
149  *  for you data source. For example, closing a connection to another
150  *  process that supplied the data, closing a database, etc.
151  */
152 void
tcpConnectionTable_container_shutdown(netsnmp_container * container_ptr)153 tcpConnectionTable_container_shutdown(netsnmp_container *container_ptr)
154 {
155     DEBUGMSGTL(("verbose:tcpConnectionTable:tcpConnectionTable_container_shutdown", "called\n"));
156 
157     if (NULL == container_ptr) {
158         snmp_log(LOG_ERR,
159                  "bad params to tcpConnectionTable_container_shutdown\n");
160         return;
161     }
162 
163 }                               /* tcpConnectionTable_container_shutdown */
164 
165 /**
166  * add new entry
167  */
168 static void
_add_connection(netsnmp_tcpconn_entry * entry,netsnmp_container * container)169 _add_connection(netsnmp_tcpconn_entry *entry, netsnmp_container *container)
170 {
171     tcpConnectionTable_rowreq_ctx *rowreq_ctx;
172 
173     DEBUGMSGTL(("tcpConnectionTable:access", "creating new entry\n"));
174 
175     /*
176      * allocate an row context and set the index(es), then add it to
177      * the container
178      */
179     rowreq_ctx = tcpConnectionTable_allocate_rowreq_ctx(entry, NULL);
180     if ((NULL != rowreq_ctx) &&
181         (MFD_SUCCESS == tcpConnectionTable_indexes_set(rowreq_ctx,
182                                                        entry->loc_addr_len,
183                                                        entry->loc_addr,
184                                                        entry->loc_addr_len,
185                                                        entry->loc_port,
186                                                        entry->rmt_addr_len,
187                                                        entry->rmt_addr,
188                                                        entry->rmt_addr_len,
189                                                        entry->rmt_port))) {
190         if (CONTAINER_INSERT(container, rowreq_ctx)) {
191             NETSNMP_LOGONCE((LOG_DEBUG,
192                     "Error inserting entry to tcpConnectionTable,"\
193                     " entry already exists.\n"));
194             tcpConnectionTable_release_rowreq_ctx(rowreq_ctx);
195         }
196     } else {
197         if (rowreq_ctx) {
198             snmp_log(LOG_ERR, "error setting index while loading "
199                      "tcpConnectionTable cache.\n");
200             tcpConnectionTable_release_rowreq_ctx(rowreq_ctx);
201         } else {
202             snmp_log(LOG_ERR, "memory allocation failed while loading "
203                      "tcpConnectionTable cache.\n");
204             netsnmp_access_tcpconn_entry_free(entry);
205         }
206     }
207 }
208 
209 /**
210  * load initial data
211  *
212  * TODO:350:M: Implement tcpConnectionTable data load
213  * This function will also be called by the cache helper to load
214  * the container again (after the container free function has been
215  * called to free the previous contents).
216  *
217  * @param container container to which items should be inserted
218  *
219  * @retval MFD_SUCCESS              : success.
220  * @retval MFD_RESOURCE_UNAVAILABLE : Can't access data source
221  * @retval MFD_ERROR                : other error.
222  *
223  *  This function is called to load the index(es) (and data, optionally)
224  *  for the every row in the data set.
225  *
226  * @remark
227  *  While loading the data, the only important thing is the indexes.
228  *  If access to your data is cheap/fast (e.g. you have a pointer to a
229  *  structure in memory), it would make sense to update the data here.
230  *  If, however, the accessing the data invovles more work (e.g. parsing
231  *  some other existing data, or peforming calculations to derive the data),
232  *  then you can limit yourself to setting the indexes and saving any
233  *  information you will need later. Then use the saved information in
234  *  tcpConnectionTable_row_prep() for populating data.
235  *
236  * @note
237  *  If you need consistency between rows (like you want statistics
238  *  for each row to be from the same time frame), you should set all
239  *  data here.
240  *
241  */
242 int
tcpConnectionTable_container_load(netsnmp_container * container)243 tcpConnectionTable_container_load(netsnmp_container *container)
244 {
245     netsnmp_container *raw_data =
246         netsnmp_access_tcpconn_container_load(NULL,
247                                               NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN);
248 
249     DEBUGMSGTL(("verbose:tcpConnectionTable:tcpConnectionTable_container_load", "called\n"));
250 
251     if (NULL == raw_data)
252         return MFD_RESOURCE_UNAVAILABLE;        /* msg already logged */
253 
254     /*
255      * got all the connections. pull out the active ones.
256      */
257     CONTAINER_FOR_EACH(raw_data, (netsnmp_container_obj_func *)
258                        _add_connection, container);
259 
260     /*
261      * free the container. we've either claimed each entry, or released it,
262      * so the dal function doesn't need to clear the container.
263      */
264     netsnmp_access_tcpconn_container_free(raw_data,
265                                           NETSNMP_ACCESS_TCPCONN_FREE_DONT_CLEAR);
266 
267     DEBUGMSGT(("verbose:tcpConnectionTable:tcpConnectionTable_cache_load",
268                "%d records\n", (int)CONTAINER_SIZE(container)));
269 
270     return MFD_SUCCESS;
271 }                               /* tcpConnectionTable_container_load */
272 
273 /**
274  * container clean up
275  *
276  * @param container container with all current items
277  *
278  *  This optional callback is called prior to all
279  *  item's being removed from the container. If you
280  *  need to do any processing before that, do it here.
281  *
282  * @note
283  *  The MFD helper will take care of releasing all the row contexts.
284  *
285  */
286 void
tcpConnectionTable_container_free(netsnmp_container * container)287 tcpConnectionTable_container_free(netsnmp_container *container)
288 {
289     DEBUGMSGTL(("verbose:tcpConnectionTable:tcpConnectionTable_container_free", "called\n"));
290 
291     /*
292      * TODO:380:M: Free tcpConnectionTable container data.
293      */
294 }                               /* tcpConnectionTable_container_free */
295 
296 /**
297  * prepare row for processing.
298  *
299  *  When the agent has located the row for a request, this function is
300  *  called to prepare the row for processing. If you fully populated
301  *  the data context during the index setup phase, you may not need to
302  *  do anything.
303  *
304  * @param rowreq_ctx pointer to a context.
305  *
306  * @retval MFD_SUCCESS     : success.
307  * @retval MFD_ERROR       : other error.
308  */
309 int
tcpConnectionTable_row_prep(tcpConnectionTable_rowreq_ctx * rowreq_ctx)310 tcpConnectionTable_row_prep(tcpConnectionTable_rowreq_ctx * rowreq_ctx)
311 {
312     DEBUGMSGTL(("verbose:tcpConnectionTable:tcpConnectionTable_row_prep",
313                 "called\n"));
314 
315     netsnmp_assert(NULL != rowreq_ctx);
316 
317     /*
318      * TODO:390:o: Prepare row for request.
319      * If populating row data was delayed, this is the place to
320      * fill in the row for this request.
321      */
322 
323     return MFD_SUCCESS;
324 }                               /* tcpConnectionTable_row_prep */
325 
326 /** @} */
327