| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
|
| 3 |
#include <net-snmp/net-snmp-features.h> |
| 4 |
#include <net-snmp/net-snmp-includes.h> |
| 5 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 6 |
|
| 7 |
netsnmp_feature_provide(stash_cache); |
| 8 |
netsnmp_feature_child_of(stash_cache, mib_helpers); |
| 9 |
#ifdef NETSNMP_FEATURE_REQUIRE_STASH_CACHE |
| 10 |
netsnmp_feature_require(oid_stash); |
| 11 |
netsnmp_feature_require(oid_stash_iterate); |
| 12 |
netsnmp_feature_require(oid_stash_get_data); |
| 13 |
#endif |
| 14 |
|
| 15 |
#ifndef NETSNMP_FEATURE_REMOVE_STASH_CACHE |
| 16 |
#include <net-snmp/agent/stash_to_next.h> |
| 17 |
|
| 18 |
#include <net-snmp/agent/stash_cache.h> |
| 19 |
|
| 20 |
extern NetsnmpCacheLoad _netsnmp_stash_cache_load; |
| 21 |
extern NetsnmpCacheFree _netsnmp_stash_cache_free; |
| 22 |
|
| 23 |
/** @defgroup stash_cache stash_cache |
| 24 |
* Automatically caches data for certain handlers. |
| 25 |
* This handler caches data in an optimized way which may alleviate |
| 26 |
* the need for the lower level handlers to perform as much |
| 27 |
* optimization. Specifically, somewhere in the lower level handlers |
| 28 |
* must be a handler that supports the MODE_GET_STASH operation. |
| 29 |
* Note that the table_iterator helper supports this. |
| 30 |
* @ingroup handler |
| 31 |
* @{ |
| 32 |
*/ |
| 33 |
|
| 34 |
netsnmp_stash_cache_info * |
| 35 |
netsnmp_get_new_stash_cache(void) |
| 36 |
{ |
| 37 |
netsnmp_stash_cache_info *cinfo; |
| 38 |
|
| 39 |
cinfo = SNMP_MALLOC_TYPEDEF(netsnmp_stash_cache_info); |
| 40 |
if (cinfo != NULL) |
| 41 |
cinfo->cache_length = 30; |
| 42 |
return cinfo; |
| 43 |
} |
| 44 |
|
| 45 |
/** returns a stash_cache handler that can be injected into a given |
| 46 |
* handler chain (with the specified timeout and root OID values), |
| 47 |
* but *only* if that handler chain explicitly supports stash cache processing. |
| 48 |
*/ |
| 49 |
netsnmp_mib_handler * |
| 50 |
netsnmp_get_timed_bare_stash_cache_handler(int timeout, oid *rootoid, size_t rootoid_len) |
| 51 |
{ |
| 52 |
netsnmp_mib_handler *handler; |
| 53 |
netsnmp_cache *cinfo; |
| 54 |
|
| 55 |
cinfo = netsnmp_cache_create( timeout, _netsnmp_stash_cache_load, |
| 56 |
_netsnmp_stash_cache_free, rootoid, rootoid_len ); |
| 57 |
|
| 58 |
if (!cinfo) |
| 59 |
return NULL; |
| 60 |
|
| 61 |
handler = netsnmp_cache_handler_get( cinfo ); |
| 62 |
if (!handler) { |
| 63 |
free(cinfo); |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
handler->next = netsnmp_create_handler("stash_cache", netsnmp_stash_cache_helper); |
| 68 |
if (!handler->next) { |
| 69 |
netsnmp_handler_free(handler); |
| 70 |
free(cinfo); |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
|
| 74 |
handler->myvoid = cinfo; |
| 75 |
netsnmp_cache_handler_owns_cache(handler); |
| 76 |
|
| 77 |
return handler; |
| 78 |
} |
| 79 |
|
| 80 |
/** returns a single stash_cache handler that can be injected into a given |
| 81 |
* handler chain (with a fixed timeout), but *only* if that handler chain |
| 82 |
* explicitly supports stash cache processing. |
| 83 |
*/ |
| 84 |
netsnmp_mib_handler * |
| 85 |
netsnmp_get_bare_stash_cache_handler(void) |
| 86 |
{ |
| 87 |
return netsnmp_get_timed_bare_stash_cache_handler( 30, NULL, 0 ); |
| 88 |
} |
| 89 |
|
| 90 |
/** returns a stash_cache handler sub-chain that can be injected into a given |
| 91 |
* (arbitrary) handler chain, using a fixed cache timeout. |
| 92 |
*/ |
| 93 |
netsnmp_mib_handler * |
| 94 |
netsnmp_get_stash_cache_handler(void) |
| 95 |
{ |
| 96 |
netsnmp_mib_handler *handler = netsnmp_get_bare_stash_cache_handler(); |
| 97 |
if (handler && handler->next) { |
| 98 |
handler->next->next = netsnmp_get_stash_to_next_handler(); |
| 99 |
} |
| 100 |
return handler; |
| 101 |
} |
| 102 |
|
| 103 |
/** returns a stash_cache handler sub-chain that can be injected into a given |
| 104 |
* (arbitrary) handler chain, using a configurable cache timeout. |
| 105 |
*/ |
| 106 |
netsnmp_mib_handler * |
| 107 |
netsnmp_get_timed_stash_cache_handler(int timeout, oid *rootoid, size_t rootoid_len) |
| 108 |
{ |
| 109 |
netsnmp_mib_handler *handler = |
| 110 |
netsnmp_get_timed_bare_stash_cache_handler(timeout, rootoid, rootoid_len); |
| 111 |
if (handler && handler->next) { |
| 112 |
handler->next->next = netsnmp_get_stash_to_next_handler(); |
| 113 |
} |
| 114 |
return handler; |
| 115 |
} |
| 116 |
|
| 117 |
/** extracts a pointer to the stash_cache info from the reqinfo structure. */ |
| 118 |
netsnmp_oid_stash_node ** |
| 119 |
netsnmp_extract_stash_cache(netsnmp_agent_request_info *reqinfo) |
| 120 |
{ |
| 121 |
return (netsnmp_oid_stash_node**)netsnmp_agent_get_list_data(reqinfo, STASH_CACHE_NAME); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/** @internal Implements the stash_cache handler */ |
| 126 |
int |
| 127 |
netsnmp_stash_cache_helper(netsnmp_mib_handler *handler, |
| 128 |
netsnmp_handler_registration *reginfo, |
| 129 |
netsnmp_agent_request_info *reqinfo, |
| 130 |
netsnmp_request_info *requests) |
| 131 |
{ |
| 132 |
netsnmp_cache *cache; |
| 133 |
netsnmp_stash_cache_info *cinfo; |
| 134 |
netsnmp_oid_stash_node *cnode; |
| 135 |
netsnmp_variable_list *cdata; |
| 136 |
netsnmp_request_info *request; |
| 137 |
|
| 138 |
DEBUGMSGTL(("helper:stash_cache", "Got request\n")); |
| 139 |
|
| 140 |
cache = netsnmp_cache_reqinfo_extract( reqinfo, reginfo->handlerName ); |
| 141 |
if (!cache) { |
| 142 |
DEBUGMSGTL(("helper:stash_cache", "No cache structure\n")); |
| 143 |
return SNMP_ERR_GENERR; |
| 144 |
} |
| 145 |
cinfo = (netsnmp_stash_cache_info *) cache->magic; |
| 146 |
|
| 147 |
switch (reqinfo->mode) { |
| 148 |
|
| 149 |
case MODE_GET: |
| 150 |
DEBUGMSGTL(("helper:stash_cache", "Processing GET request\n")); |
| 151 |
for(request = requests; request; request = request->next) { |
| 152 |
cdata = (netsnmp_variable_list*) |
| 153 |
netsnmp_oid_stash_get_data(cinfo->cache, |
| 154 |
requests->requestvb->name, |
| 155 |
requests->requestvb->name_length); |
| 156 |
if (cdata && cdata->val.string && cdata->val_len) { |
| 157 |
DEBUGMSGTL(("helper:stash_cache", "Found cached GET varbind\n")); |
| 158 |
DEBUGMSGOID(("helper:stash_cache", cdata->name, cdata->name_length)); |
| 159 |
DEBUGMSG(("helper:stash_cache", "\n")); |
| 160 |
snmp_set_var_typed_value(request->requestvb, cdata->type, |
| 161 |
cdata->val.string, cdata->val_len); |
| 162 |
} |
| 163 |
} |
| 164 |
break; |
| 165 |
|
| 166 |
case MODE_GETNEXT: |
| 167 |
DEBUGMSGTL(("helper:stash_cache", "Processing GETNEXT request\n")); |
| 168 |
for(request = requests; request; request = request->next) { |
| 169 |
cnode = |
| 170 |
netsnmp_oid_stash_getnext_node(cinfo->cache, |
| 171 |
requests->requestvb->name, |
| 172 |
requests->requestvb->name_length); |
| 173 |
if (cnode && cnode->thedata) { |
| 174 |
cdata = (netsnmp_variable_list*)cnode->thedata; |
| 175 |
if (cdata->val.string && cdata->name && cdata->name_length) { |
| 176 |
DEBUGMSGTL(("helper:stash_cache", "Found cached GETNEXT varbind\n")); |
| 177 |
DEBUGMSGOID(("helper:stash_cache", cdata->name, cdata->name_length)); |
| 178 |
DEBUGMSG(("helper:stash_cache", "\n")); |
| 179 |
snmp_set_var_typed_value(request->requestvb, cdata->type, |
| 180 |
cdata->val.string, cdata->val_len); |
| 181 |
snmp_set_var_objid(request->requestvb, cdata->name, |
| 182 |
cdata->name_length); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
break; |
| 187 |
|
| 188 |
default: |
| 189 |
cinfo->cache_valid = 0; |
| 190 |
return netsnmp_call_next_handler(handler, reginfo, reqinfo, |
| 191 |
requests); |
| 192 |
} |
| 193 |
|
| 194 |
return SNMP_ERR_NOERROR; |
| 195 |
} |
| 196 |
|
| 197 |
/** updates a given cache depending on whether it needs to or not. |
| 198 |
*/ |
| 199 |
int |
| 200 |
_netsnmp_stash_cache_load( netsnmp_cache *cache, void *magic ) |
| 201 |
{ |
| 202 |
netsnmp_mib_handler *handler = cache->cache_hint->handler; |
| 203 |
netsnmp_handler_registration *reginfo = cache->cache_hint->reginfo; |
| 204 |
netsnmp_agent_request_info *reqinfo = cache->cache_hint->reqinfo; |
| 205 |
netsnmp_request_info *requests = cache->cache_hint->requests; |
| 206 |
netsnmp_stash_cache_info *cinfo = (netsnmp_stash_cache_info*) magic; |
| 207 |
int old_mode; |
| 208 |
int ret; |
| 209 |
|
| 210 |
if (!cinfo) { |
| 211 |
cinfo = netsnmp_get_new_stash_cache(); |
| 212 |
cache->magic = cinfo; |
| 213 |
} |
| 214 |
|
| 215 |
/* change modes to the GET_STASH mode */ |
| 216 |
old_mode = reqinfo->mode; |
| 217 |
reqinfo->mode = MODE_GET_STASH; |
| 218 |
netsnmp_agent_add_list_data(reqinfo, |
| 219 |
netsnmp_create_data_list(STASH_CACHE_NAME, |
| 220 |
&cinfo->cache, NULL)); |
| 221 |
|
| 222 |
/* have the next handler fill stuff in and switch modes back */ |
| 223 |
ret = netsnmp_call_next_handler(handler->next, reginfo, reqinfo, requests); |
| 224 |
reqinfo->mode = old_mode; |
| 225 |
|
| 226 |
return ret; |
| 227 |
} |
| 228 |
|
| 229 |
static void netsnmp_free_var(void *var) |
| 230 |
{ |
| 231 |
snmp_free_var(var); |
| 232 |
} |
| 233 |
|
| 234 |
void |
| 235 |
_netsnmp_stash_cache_free( netsnmp_cache *cache, void *magic ) |
| 236 |
{ |
| 237 |
netsnmp_stash_cache_info *cinfo = magic; |
| 238 |
|
| 239 |
netsnmp_oid_stash_free(&cinfo->cache, netsnmp_free_var); |
| 240 |
} |
| 241 |
|
| 242 |
/** initializes the stash_cache helper which then registers a stash_cache |
| 243 |
* handler as a run-time injectable handler for configuration file |
| 244 |
* use. |
| 245 |
*/ |
| 246 |
void |
| 247 |
netsnmp_init_stash_cache_helper(void) |
| 248 |
{ |
| 249 |
netsnmp_register_handler_by_name("stash_cache", |
| 250 |
netsnmp_get_stash_cache_handler()); |
| 251 |
} |
| 252 |
/** @} */ |
| 253 |
|
| 254 |
#else /* NETSNMP_FEATURE_REMOVE_STASH_CACHE */ |
| 255 |
netsnmp_feature_unused(stash_cache); |
| 256 |
#endif /* NETSNMP_FEATURE_REMOVE_STASH_CACHE */ |