/src/net-snmp/agent/helpers/serialize.c
Line | Count | Source (jump to first uncovered line) |
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 | | * usable 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 | 3.20k | { |
38 | 3.20k | return netsnmp_create_handler("serialize", |
39 | 3.20k | netsnmp_serialize_helper_handler); |
40 | 3.20k | } |
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 | 0 | { |
47 | 0 | netsnmp_mib_handler *handler = netsnmp_get_serialize_handler(); |
48 | 0 | if (!handler || |
49 | 0 | (netsnmp_inject_handler(reginfo, handler) != SNMPERR_SUCCESS)) { |
50 | 0 | snmp_log(LOG_ERR, "could not create serialize handler\n"); |
51 | 0 | netsnmp_handler_free(handler); |
52 | 0 | netsnmp_handler_registration_free(reginfo); |
53 | 0 | return MIB_REGISTRATION_FAILED; |
54 | 0 | } |
55 | | |
56 | 0 | return netsnmp_register_handler(reginfo); |
57 | 0 | } |
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 | 0 | { |
66 | |
|
67 | 0 | netsnmp_request_info *request, *requesttmp; |
68 | |
|
69 | 0 | DEBUGMSGTL(("helper:serialize", "Got request\n")); |
70 | | /* |
71 | | * loop through requests |
72 | | */ |
73 | 0 | for (request = requests; request; request = request->next) { |
74 | 0 | int ret; |
75 | | |
76 | | /* |
77 | | * store next pointer and delete it |
78 | | */ |
79 | 0 | requesttmp = request->next; |
80 | 0 | request->next = NULL; |
81 | | |
82 | | /* |
83 | | * call the next handler |
84 | | */ |
85 | 0 | ret = |
86 | 0 | netsnmp_call_next_handler(handler, reginfo, reqinfo, request); |
87 | | |
88 | | /* |
89 | | * restore original next pointer |
90 | | */ |
91 | 0 | request->next = requesttmp; |
92 | |
|
93 | 0 | if (ret != SNMP_ERR_NOERROR) |
94 | 0 | return ret; |
95 | 0 | } |
96 | | |
97 | 0 | return SNMP_ERR_NOERROR; |
98 | 0 | } |
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 | 3.20k | { |
108 | 3.20k | netsnmp_mib_handler *handler = netsnmp_get_serialize_handler(); |
109 | 3.20k | if (!handler) { |
110 | 0 | snmp_log(LOG_ERR, "could not create serialize handler\n"); |
111 | 0 | return; |
112 | 0 | } |
113 | 3.20k | netsnmp_register_handler_by_name("serialize", handler); |
114 | 3.20k | } |
115 | | /** @} */ |
116 | | |