Debian Codegraph @

agent/helpers/serialize.c

raw
1 /*
2 * Portions of this file are subject to the following copyright(s). See
3 * the Net-SNMP's COPYING file for more details and other copyrights
4 * that may apply:
5 *
6 * Portions of this file are copyrighted by:
7 * Copyright (c) 2016 VMware, Inc. All rights reserved.
8 * Use is subject to license terms specified in the COPYING file
9 * distributed with the Net-SNMP package.
10 */
11
12 #include <net-snmp/net-snmp-config.h>
13
14 #include <net-snmp/net-snmp-includes.h>
15 #include <net-snmp/agent/net-snmp-agent-includes.h>
16
17 #include <net-snmp/agent/serialize.h>
18
19 /** @defgroup serialize serialize
20 * Calls sub handlers one request at a time.
21 * @ingroup utilities
22 * This functionally passes in one request at a time
23 * into lower handlers rather than a whole bunch of requests at once.
24 * This is useful for handlers that don't want to iterate through the
25 * request lists themselves. Generally, this is probably less
26 * efficient so use with caution. The serialize handler might be
27 * useable to dynamically fix handlers with broken looping code,
28 * however.
29 * @{
30 */
31
32 /** returns a serialize handler that can be injected into a given
33 * handler chain.
34 */
35 netsnmp_mib_handler *
36 netsnmp_get_serialize_handler(void)
37 {
38 return netsnmp_create_handler("serialize",
39 netsnmp_serialize_helper_handler);
40 }
41
42 /** functionally the same as calling netsnmp_register_handler() but also
43 * injects a serialize handler at the same time for you. */
44 int
45 netsnmp_register_serialize(netsnmp_handler_registration *reginfo)
46 {
47 netsnmp_mib_handler *handler = netsnmp_get_serialize_handler();
48 if (!handler ||
49 (netsnmp_inject_handler(reginfo, handler) != SNMPERR_SUCCESS)) {
50 snmp_log(LOG_ERR, "could not create serialize handler\n");
51 netsnmp_handler_free(handler);
52 netsnmp_handler_registration_free(reginfo);
53 return MIB_REGISTRATION_FAILED;
54 }
55
56 return netsnmp_register_handler(reginfo);
57 }
58
59 /** Implements the serial handler */
60 int
61 netsnmp_serialize_helper_handler(netsnmp_mib_handler *handler,
62 netsnmp_handler_registration *reginfo,
63 netsnmp_agent_request_info *reqinfo,
64 netsnmp_request_info *requests)
65 {
66
67 netsnmp_request_info *request, *requesttmp;
68
69 DEBUGMSGTL(("helper:serialize", "Got request\n"));
70 /*
71 * loop through requests
72 */
73 for (request = requests; request; request = request->next) {
74 int ret;
75
76 /*
77 * store next pointer and delete it
78 */
79 requesttmp = request->next;
80 request->next = NULL;
81
82 /*
83 * call the next handler
84 */
85 ret =
86 netsnmp_call_next_handler(handler, reginfo, reqinfo, request);
87
88 /*
89 * restore original next pointer
90 */
91 request->next = requesttmp;
92
93 if (ret != SNMP_ERR_NOERROR)
94 return ret;
95 }
96
97 return SNMP_ERR_NOERROR;
98 }
99
100 /**
101 * initializes the serialize helper which then registers a serialize
102 * handler as a run-time injectable handler for configuration file
103 * use.
104 */
105 void
106 netsnmp_init_serialize(void)
107 {
108 netsnmp_mib_handler *handler = netsnmp_get_serialize_handler();
109 if (!handler) {
110 snmp_log(LOG_ERR, "could not create serialize handler\n");
111 return;
112 }
113 netsnmp_register_handler_by_name("serialize", handler);
114 }
115 /** @} */
116