void init_mysnmpagent() { netsnmp_handler_registration *reg; reg=netsnmp_create_handler_registration("mytestoid",MY_MIB_handler,myOid,OID_LENGTH(myOid),HANDLER_CAN_RWRITE); netsnmp_register_handler(reg); printf("Register my oid %s\n",myOid); }
/** Registers an old API set into the mib tree. Functionally this * mimics the old register_mib_context() function (and in fact the new * register_mib_context() function merely calls this new old_api one). */ int netsnmp_register_old_api(const char *moduleName, struct variable *var, size_t varsize, size_t numvars, oid * mibloc, size_t mibloclen, int priority, int range_subid, oid range_ubound, netsnmp_session * ss, const char *context, int timeout, int flags) { unsigned int i; /* * register all subtree nodes */ for (i = 0; i < numvars; i++) { struct variable *vp; netsnmp_handler_registration *reginfo = SNMP_MALLOC_TYPEDEF(netsnmp_handler_registration); memdup((void *) &vp, (void *) (struct variable *) ((char *) var + varsize * i), varsize); reginfo->handler = get_old_api_handler(); reginfo->handlerName = strdup(moduleName); reginfo->rootoid_len = (mibloclen + vp->namelen); reginfo->rootoid = (oid *) malloc(reginfo->rootoid_len * sizeof(oid)); memcpy(reginfo->rootoid, mibloc, mibloclen * sizeof(oid)); memcpy(reginfo->rootoid + mibloclen, vp->name, vp->namelen * sizeof(oid)); reginfo->handler->myvoid = (void *) vp; reginfo->priority = priority; reginfo->range_subid = range_subid; reginfo->range_ubound = range_ubound; reginfo->timeout = timeout; reginfo->contextName = (context) ? strdup(context) : NULL; reginfo->modes = HANDLER_CAN_RWRITE; /* * register ourselves in the mib tree */ if (netsnmp_register_handler(reginfo) != MIB_REGISTERED_OK) { /** netsnmp_handler_registration_free(reginfo); already freed */ SNMP_FREE(vp); } } return SNMPERR_SUCCESS; }
int netsnmp_cache_handler_register(netsnmp_handler_registration * reginfo, netsnmp_cache* cache) { netsnmp_mib_handler *handler = NULL; handler = netsnmp_cache_handler_get(cache); netsnmp_inject_handler(reginfo, handler); return netsnmp_register_handler(reginfo); }
int netsnmp_register_cache_handler(netsnmp_handler_registration * reginfo, int timeout, NetsnmpCacheLoad * load_hook, NetsnmpCacheFree * free_hook) { netsnmp_mib_handler *handler = NULL; handler = netsnmp_get_cache_handler(timeout, load_hook, free_hook, reginfo->rootoid, reginfo->rootoid_len); netsnmp_inject_handler(reginfo, handler); return netsnmp_register_handler(reginfo); }
int netsnmp_register_null_context(oid * loc, size_t loc_len, const char *contextName) { netsnmp_handler_registration *reginfo; reginfo = SNMP_MALLOC_TYPEDEF(netsnmp_handler_registration); reginfo->handlerName = strdup(""); reginfo->rootoid = loc; reginfo->rootoid_len = loc_len; reginfo->handler = netsnmp_create_handler("null", netsnmp_null_handler); if (contextName) reginfo->contextName = strdup(contextName); reginfo->modes = HANDLER_CAN_DEFAULT; return netsnmp_register_handler(reginfo); }
static int _cache_handler_register(netsnmp_handler_registration * reginfo, netsnmp_mib_handler *handler) { /** success path */ if (reginfo && handler && (netsnmp_inject_handler(reginfo, handler) == SNMPERR_SUCCESS)) return netsnmp_register_handler(reginfo); /** error path */ snmp_log(LOG_ERR, "could not register cache handler\n"); if (handler) netsnmp_handler_free(handler); netsnmp_handler_registration_free(reginfo); return MIB_REGISTRATION_FAILED; }
/** functionally the same as calling netsnmp_register_handler() but also * injects a serialize handler at the same time for you. */ int netsnmp_register_serialize(netsnmp_handler_registration *reginfo) { netsnmp_inject_handler(reginfo, netsnmp_get_serialize_handler()); return netsnmp_register_handler(reginfo); }
void init_testhandler(void) { /* * we're registering at .1.2.3.4 */ netsnmp_handler_registration *my_test; netsnmp_table_registration_info *table_info; u_long ind1; netsnmp_table_data *table; netsnmp_table_data_set *table_set; netsnmp_table_row *row; DEBUGMSGTL(("testhandler", "initializing\n")); /* * basic handler test */ netsnmp_register_handler(netsnmp_create_handler_registration ("myTest", my_test_handler, my_test_oid, 4, HANDLER_CAN_RONLY)); /* * instance handler test */ netsnmp_register_instance(netsnmp_create_handler_registration ("myInstance", my_test_instance_handler, my_instance_oid, 5, HANDLER_CAN_RWRITE)); netsnmp_register_ulong_instance("myulong", my_data_ulong_instance, 4, &my_ulong, NULL); /* * table helper test */ my_test = netsnmp_create_handler_registration("myTable", my_test_table_handler, my_table_oid, 4, HANDLER_CAN_RONLY); if (!my_test) return; table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info); if (table_info == NULL) return; netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, ASN_INTEGER, 0); table_info->min_column = 3; table_info->max_column = 3; netsnmp_register_table(my_test, table_info); /* * data table helper test */ /* * we'll construct a simple table here with two indexes: an * integer and a string (why not). It'll contain only one * column so the data pointer is merely the data in that * column. */ table = netsnmp_create_table_data("data_table_test"); netsnmp_table_data_add_index(table, ASN_INTEGER); netsnmp_table_data_add_index(table, ASN_OCTET_STR); /* * 1 partridge in a pear tree */ row = netsnmp_create_table_data_row(); ind1 = 1; netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1)); netsnmp_table_row_add_index(row, ASN_OCTET_STR, "partridge", strlen("partridge")); row->data = (void *) "pear tree"; netsnmp_table_data_add_row(table, row); /* * 2 turtle doves */ row = netsnmp_create_table_data_row(); ind1 = 2; netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1)); netsnmp_table_row_add_index(row, ASN_OCTET_STR, "turtle", strlen("turtle")); row->data = (void *) "doves"; netsnmp_table_data_add_row(table, row); /* * we're going to register it as a normal table too, so we get the * automatically parsed column and index information */ table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info); if (table_info == NULL) return; netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, ASN_OCTET_STR, 0); table_info->min_column = 3; table_info->max_column = 3; netsnmp_register_read_only_table_data (netsnmp_create_handler_registration ("12days", my_data_table_handler, my_data_table_oid, 4, HANDLER_CAN_RONLY), table, table_info); }
void proxy_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; } /* * 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); }
int netsnmp_table_row_register(netsnmp_handler_registration *reginfo, netsnmp_table_registration_info *tabreg, void *row, netsnmp_variable_list *index) { netsnmp_handler_registration *reg2; netsnmp_mib_handler *handler; oid row_oid[MAX_OID_LEN]; size_t row_oid_len, len; char tmp[SNMP_MAXBUF_MEDIUM]; if ((NULL == reginfo) || (NULL == reginfo->handler) || (NULL == tabreg)) { snmp_log(LOG_ERR, "bad param in netsnmp_table_row_register\n"); return SNMPERR_GENERR; } /* * The first table_row invoked for a particular table should * register the full table as well, with a default handler to * process requests for non-existent (or incomplete) rows. * * Subsequent table_row registrations attempting to set up * this default handler would fail - preferably silently! */ snprintf(tmp, sizeof(tmp), "%s_table", reginfo->handlerName); reg2 = netsnmp_create_handler_registration( tmp, _table_row_default_handler, reginfo->rootoid, reginfo->rootoid_len, reginfo->modes); netsnmp_register_table(reg2, tabreg); /* Ignore return value */ /* * Adjust the OID being registered, to take account * of the indexes and column range provided.... */ row_oid_len = reginfo->rootoid_len; memcpy( row_oid, (u_char *) reginfo->rootoid, row_oid_len * sizeof(oid)); row_oid[row_oid_len++] = 1; /* tableEntry */ row_oid[row_oid_len++] = tabreg->min_column; reginfo->range_ubound = tabreg->max_column; reginfo->range_subid = row_oid_len-1; build_oid_noalloc(&row_oid[row_oid_len], MAX_OID_LEN-row_oid_len, &len, NULL, 0, index); row_oid_len += len; free(reginfo->rootoid); memdup((u_char **) & reginfo->rootoid, (const u_char *) row_oid, row_oid_len * sizeof(oid)); reginfo->rootoid_len = row_oid_len; /* * ... insert a minimal handler ... */ handler = netsnmp_table_row_handler_get(row); netsnmp_inject_handler(reginfo, handler ); /* * ... and register the row */ return netsnmp_register_handler(reginfo); }