📄 proxy.c
字号:
/* Portions of this file are subject to the following copyright(s). See * the Net-SNMP's COPYING file for more details and other copyrights * that may apply: *//* * Portions of this file are copyrighted by: * Copyright @ 2003 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms specified in the COPYING file * distributed with the Net-SNMP package. */#include <net-snmp/net-snmp-config.h>#include <sys/types.h>#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_STRING_H#include <string.h>#endif#ifdef HAVE_NETINET_IN_H#include <netinet/in.h>#endif#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include "proxy.h"static struct simple_proxy *proxies = NULL;oid testoid[] = { 1, 3, 6, 1, 4, 1, 2021, 8888, 1 };/* * this must be standardized somewhere, right? */#define MAX_ARGS 128char *context_string;static voidproxyOptProc(int argc, char *const *argv, int opt){ switch (opt) { case 'C': while (*optarg) { switch (*optarg++) { case 'n': optind++; if (optind < argc) { context_string = argv[optind - 1]; } else { config_perror("No context name passed to -Cn"); } break; case 'c': netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_IGNORE_NO_COMMUNITY, 1); break; default: config_perror("unknown argument passed to -C"); break; } } break; default: break; /* * shouldn't get here */ }}voidproxy_parse_config(const char *token, char *line){ /* * proxy args [base-oid] [remap-to-remote-oid] */ netsnmp_session session, *ss; struct simple_proxy *newp, **listpp; char args[MAX_ARGS][SPRINT_MAX_LEN], *argv[MAX_ARGS]; int argn, arg; char *cp; netsnmp_handler_registration *reg; context_string = NULL; DEBUGMSGTL(("proxy_config", "entering\n")); /* * create the argv[] like array */ strcpy(argv[0] = args[0], "snmpd-proxy"); /* bogus entry for getopt() */ for (argn = 1, cp = line; cp && argn < MAX_ARGS;) { argv[argn] = args[argn]; cp = copy_nword(cp, argv[argn], SPRINT_MAX_LEN); argn++; } for (arg = 0; arg < argn; arg++) { DEBUGMSGTL(("proxy_args", "final args: %d = %s\n", arg, argv[arg])); } DEBUGMSGTL(("proxy_config", "parsing args: %d\n", argn)); /* Call special parse_args that allows for no specified community string */ arg = snmp_parse_args(argn, argv, &session, "C:", proxyOptProc); /* reset this in case we modified it */ netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_IGNORE_NO_COMMUNITY, 0); if (arg < 0) { config_perror("failed to parse proxy args"); return; } DEBUGMSGTL(("proxy_config", "done parsing args\n")); if (arg >= argn) { config_perror("missing base oid"); return; } SOCK_STARTUP; /* * usm_set_reportErrorOnUnknownID(0); * * hack, stupid v3 ASIs. */ /* * XXX: on a side note, we don't really need to be a reference * platform any more so the proper thing to do would be to fix * snmplib/snmpusm.c to pass in the pdu type to usm_process_incoming * so this isn't needed. */ ss = snmp_open(&session); /* * usm_set_reportErrorOnUnknownID(1); */ if (ss == NULL) { /* * diagnose snmp_open errors with the input netsnmp_session pointer */ snmp_sess_perror("snmpget", &session); SOCK_CLEANUP; return; } newp = (struct simple_proxy *) calloc(1, sizeof(struct simple_proxy)); newp->sess = ss; DEBUGMSGTL(("proxy_init", "name = %s\n", args[arg])); newp->name_len = MAX_OID_LEN; if (!snmp_parse_oid(args[arg++], newp->name, &newp->name_len)) { snmp_perror("proxy"); config_perror("illegal proxy oid specified\n"); return; } if (arg < argn) { DEBUGMSGTL(("proxy_init", "base = %s\n", args[arg])); newp->base_len = MAX_OID_LEN; if (!snmp_parse_oid(args[arg++], newp->base, &newp->base_len)) { snmp_perror("proxy"); config_perror("illegal variable name specified (base oid)\n"); return; } } if ( context_string ) newp->context = strdup(context_string); DEBUGMSGTL(("proxy_init", "registering at: ")); DEBUGMSGOID(("proxy_init", newp->name, newp->name_len)); DEBUGMSG(("proxy_init", "\n")); /* * add to our chain */ /* * must be sorted! */ listpp = &proxies; while (*listpp && snmp_oid_compare(newp->name, newp->name_len, (*listpp)->name, (*listpp)->name_len) > 0) { listpp = &((*listpp)->next); } /* * listpp should be next in line from us. */ if (*listpp) { /* * make our next in the link point to the current link */ newp->next = *listpp; } /* * replace current link with us */ *listpp = newp; reg = netsnmp_create_handler_registration("proxy", proxy_handler, newp->name, newp->name_len, HANDLER_CAN_RWRITE); reg->handler->myvoid = newp; if (context_string) reg->contextName = strdup(context_string); netsnmp_register_handler(reg);}voidproxy_free_config(void){ struct simple_proxy *rm; DEBUGMSGTL(("proxy_free_config", "Free config\n")); while (proxies) { rm = proxies; proxies = rm->next; DEBUGMSGTL(( "proxy_free_config", "freeing ")); DEBUGMSGOID(("proxy_free_config", rm->name, rm->name_len)); DEBUGMSG(( "proxy_free_config", " (%s)\n", rm->context)); unregister_mib_context(rm->name, rm->name_len, DEFAULT_MIB_PRIORITY, 0, 0, rm->context); SNMP_FREE(rm->variables); SNMP_FREE(rm->context); snmp_close(rm->sess); SNMP_FREE(rm); }}/* * Configure special parameters on the session. * Currently takes the parameter configured and changes it if something * was configured. It becomes "-c" if the community string from the pdu * is placed on the session. */intproxy_fill_in_session(netsnmp_mib_handler *handler, netsnmp_agent_request_info *reqinfo, void **configured){ netsnmp_session *session; struct simple_proxy *sp; sp = (struct simple_proxy *) handler->myvoid; if (!sp) { return 0; } session = sp->sess; if (!session) { return 0; }#if !defined(DISABLE_SNMPV1) || !defined(DISABLE_SNMPV2C) if (session->version == SNMP_VERSION_1 || session->version == SNMP_VERSION_2c) { /* * Check if session has community string defined for it. * If not, need to extract community string from the pdu. * Copy to session and set 'configured' to indicate this. */ if (session->community_len == 0) { DEBUGMSGTL(("proxy", "session has no community string\n")); if (reqinfo->asp == NULL || reqinfo->asp->pdu == NULL || reqinfo->asp->pdu->community_len == 0) { return 0; } *configured = malloc(strlen("-c") + 1); strcpy(*configured, "-c"); DEBUGMSGTL(("proxy", "pdu has community string\n")); session->community_len = reqinfo->asp->pdu->community_len; session->community = malloc(session->community_len + 1); strncpy((char *)session->community, (const char *)reqinfo->asp->pdu->community, session->community_len); } }#endif return 1;}/* * Free any specially configured parameters used on the session. */voidproxy_free_filled_in_session_args(netsnmp_session *session, void **configured){ /* Only do comparisions, etc., if something was configured */ if (*configured == NULL) { return; } /* If used community string from pdu, release it from session now */ if (strcmp((const char *)(*configured), "-c") == 0) { free(session->community); session->community = NULL; session->community_len = 0; } free((u_char *)(*configured));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -