📄 snmp_api.c
字号:
Msgid = retVal; snmp_res_unlock(MT_LIBRARY_ID, MT_LIB_MESSAGEID); return retVal;}longsnmp_get_next_sessid(void){ long retVal; snmp_res_lock(MT_LIBRARY_ID, MT_LIB_SESSIONID); retVal = 1 + Sessid; /*MTCRITICAL_RESOURCE */ if (!retVal) retVal = 2; Sessid = retVal; snmp_res_unlock(MT_LIBRARY_ID, MT_LIB_SESSIONID); return retVal;}longsnmp_get_next_transid(void){ long retVal; snmp_res_lock(MT_LIBRARY_ID, MT_LIB_TRANSID); retVal = 1 + Transid; /*MTCRITICAL_RESOURCE */ if (!retVal) retVal = 2; Transid = retVal; snmp_res_unlock(MT_LIBRARY_ID, MT_LIB_TRANSID); return retVal;}voidsnmp_perror(const char *prog_string){ const char *str; int xerr; xerr = snmp_errno; /*MTCRITICAL_RESOURCE */ str = snmp_api_errstring(xerr); snmp_log(LOG_ERR, "%s: %s\n", prog_string, str);}voidsnmp_set_detail(const char *detail_string){ if (detail_string != NULL) { strncpy((char *) snmp_detail, detail_string, sizeof(snmp_detail)); snmp_detail[sizeof(snmp_detail) - 1] = '\0'; snmp_detail_f = 1; }}/* * returns pointer to static data *//* * results not guaranteed in multi-threaded use */const char *snmp_api_errstring(int snmp_errnumber){ const char *msg = ""; static char msg_buf[256]; if (snmp_errnumber >= SNMPERR_MAX && snmp_errnumber <= SNMPERR_GENERR) { msg = api_errors[-snmp_errnumber]; } else if (snmp_errnumber != SNMPERR_SUCCESS) { msg = "Unknown Error"; } if (snmp_detail_f) { snprintf(msg_buf, 256, "%s (%s)", msg, snmp_detail); snmp_detail_f = 0; } else { strncpy(msg_buf, msg, 256); } msg_buf[255] = '\0'; return (msg_buf);}/* * snmp_error - return error data * Inputs : address of errno, address of snmp_errno, address of string * Caller must free the string returned after use. */voidsnmp_error(netsnmp_session * psess, int *p_errno, int *p_snmp_errno, char **p_str){ char buf[SPRINT_MAX_LEN]; int snmp_errnumber; if (p_errno) *p_errno = psess->s_errno; if (p_snmp_errno) *p_snmp_errno = psess->s_snmp_errno; if (p_str == NULL) return; strcpy(buf, ""); snmp_errnumber = psess->s_snmp_errno; if (snmp_errnumber >= SNMPERR_MAX && snmp_errnumber <= SNMPERR_GENERR) { strncpy(buf, api_errors[-snmp_errnumber], 256); } else { if (snmp_errnumber) snprintf(buf, 256, "Unknown Error %d", snmp_errnumber); } buf[255] = '\0'; /* * append a useful system errno interpretation. */ if (psess->s_errno) { const char* error = strerror(psess->s_errno); if(error == NULL) error = "Unknown Error"; snprintf (&buf[strlen(buf)], 256-strlen(buf), " (%s)", error); } buf[255] = '\0'; *p_str = strdup(buf);}/* * snmp_sess_error - same as snmp_error for single session API use. */voidsnmp_sess_error(void *sessp, int *p_errno, int *p_snmp_errno, char **p_str){ struct session_list *slp = (struct session_list *) sessp; if ((slp) && (slp->session)) snmp_error(slp->session, p_errno, p_snmp_errno, p_str);}/* * snmp_sess_perror(): print a error stored in a session pointer */voidnetsnmp_sess_log_error(int priority, const char *prog_string, netsnmp_session * ss){ char *err; snmp_error(ss, NULL, NULL, &err); snmp_log(priority, "%s: %s\n", prog_string, err); free(err);}/* * snmp_sess_perror(): print a error stored in a session pointer */voidsnmp_sess_perror(const char *prog_string, netsnmp_session * ss){ netsnmp_sess_log_error(LOG_ERR, prog_string, ss);}/* * Primordial SNMP library initialization. * Initializes mutex locks. * Invokes minimum required initialization for displaying MIB objects. * Gets initial request ID for all transactions, * and finds which port SNMP over UDP uses. * SNMP over AppleTalk is not currently supported. * * Warning: no debug messages here. */static void_init_snmp(void){#ifdef HAVE_GETSERVBYNAME struct servent *servp;#endif static char have_done_init = 0; struct timeval tv; long tmpReqid, tmpMsgid; u_short s_port = SNMP_PORT; if (have_done_init) return; have_done_init = 1; Reqid = 1; snmp_res_init(); /* initialize the mt locking structures */ init_mib_internals(); netsnmp_tdomain_init(); gettimeofday(&tv, (struct timezone *) 0); /* * Now = tv; */ /* * get pseudo-random values for request ID and message ID */ /* * don't allow zero value to repeat init */#ifdef SVR4 srand48(tv.tv_sec ^ tv.tv_usec); tmpReqid = lrand48(); tmpMsgid = lrand48();#else srandom(tv.tv_sec ^ tv.tv_usec); tmpReqid = random(); tmpMsgid = random();#endif if (tmpReqid == 0) tmpReqid = 1; if (tmpMsgid == 0) tmpMsgid = 1; Reqid = tmpReqid; Msgid = tmpMsgid;#ifdef HAVE_GETSERVBYNAME servp = getservbyname("snmp", "udp"); if (servp) { /* * store it in host byte order */ s_port = ntohs(servp->s_port); }#endif netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DEFAULT_PORT, s_port);#ifdef USE_REVERSE_ASNENCODING netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_REVERSE_ENCODE, DEFAULT_ASNENCODING_DIRECTION);#endif}/* * Initializes the session structure. * May perform one time minimal library initialization. * No MIB file processing is done via this call. */voidsnmp_sess_init(netsnmp_session * session){ _init_snmp(); /* * initialize session to default values */ memset(session, 0, sizeof(netsnmp_session)); session->remote_port = SNMP_DEFAULT_REMPORT; session->timeout = SNMP_DEFAULT_TIMEOUT; session->retries = SNMP_DEFAULT_RETRIES; session->version = SNMP_DEFAULT_VERSION; session->securityModel = SNMP_DEFAULT_SECMODEL; session->rcvMsgMaxSize = SNMP_MAX_MSG_SIZE;}static voidregister_default_handlers(void){ netsnmp_ds_register_config(ASN_BOOLEAN, "snmp", "dumpPacket", NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DUMP_PACKET); netsnmp_ds_register_config(ASN_BOOLEAN, "snmp", "reverseEncodeBER", NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_REVERSE_ENCODE); netsnmp_ds_register_config(ASN_INTEGER, "snmp", "defaultPort", NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DEFAULT_PORT); netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "defCommunity", NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_COMMUNITY); netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "noTokenWarnings", NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NO_TOKEN_WARNINGS); netsnmp_ds_register_config(ASN_BOOLEAN, "snmp", "noRangeCheck", NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_CHECK_RANGE); netsnmp_ds_register_config(ASN_OCTET_STR, "snmp", "persistentDir", NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PERSISTENT_DIR); netsnmp_ds_register_config(ASN_BOOLEAN, "snmp", "noDisplayHint", NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NO_DISPLAY_HINT);}voidinit_snmp_enums(void){ se_add_pair_to_slist("asntypes", strdup("integer"), ASN_INTEGER); se_add_pair_to_slist("asntypes", strdup("counter"), ASN_COUNTER); se_add_pair_to_slist("asntypes", strdup("gauge"), ASN_GAUGE); se_add_pair_to_slist("asntypes", strdup("timeticks"), ASN_TIMETICKS); se_add_pair_to_slist("asntypes", strdup("uinteger"), ASN_UINTEGER); se_add_pair_to_slist("asntypes", strdup("counter64"), ASN_COUNTER64); se_add_pair_to_slist("asntypes", strdup("octet_str"), ASN_OCTET_STR); se_add_pair_to_slist("asntypes", strdup("ipaddress"), ASN_IPADDRESS); se_add_pair_to_slist("asntypes", strdup("opaque"), ASN_OPAQUE); se_add_pair_to_slist("asntypes", strdup("nsap"), ASN_NSAP); se_add_pair_to_slist("asntypes", strdup("object_id"), ASN_OBJECT_ID); se_add_pair_to_slist("asntypes", strdup("null"), ASN_NULL); se_add_pair_to_slist("asntypes", strdup("bit_str"), ASN_BIT_STR);#ifdef OPAQUE_SPECIAL_TYPES se_add_pair_to_slist("asntypes", strdup("opaque_counter64"), ASN_OPAQUE_COUNTER64); se_add_pair_to_slist("asntypes", strdup("opaque_u64"), ASN_OPAQUE_U64); se_add_pair_to_slist("asntypes", strdup("opaque_float"), ASN_OPAQUE_FLOAT); se_add_pair_to_slist("asntypes", strdup("opaque_double"), ASN_OPAQUE_DOUBLE); se_add_pair_to_slist("asntypes", strdup("opaque_i64"), ASN_OPAQUE_I64);#endif}/*******************************************************************-o-****** * init_snmp * * Parameters: * *type Label for the config file "type" used by calling entity. * * Call appropriately the functions to do config file loading and * mib module parsing in the correct order. */voidinit_snmp(const char *type){ static int done_init = 0; /* To prevent double init's. */ if (done_init) { return; } done_init = 1; /* * make the type available everywhere else */ if (type && !netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE)) { netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE, type); } _init_snmp(); /* * set our current locale properly to initialize isprint() type functions */#ifdef HAVE_SETLOCALE setlocale(LC_CTYPE, "");#endif snmp_debug_init(); /* should be done first, to turn on debugging ASAP */ netsnmp_container_init_list(); init_callbacks(); init_snmp_logging(); snmp_init_statistics(); register_mib_handlers(); register_default_handlers(); init_snmpv3(type); init_snmp_alarm(); init_snmp_enums(); read_premib_configs(); init_mib(); read_configs();} /* end init_snmp() */voidsnmp_store(const char *type){ DEBUGMSGTL(("snmp_store", "storing stuff...\n")); snmp_save_persistent(type); snmp_call_callbacks(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA, NULL); snmp_clean_persistent(type);}/* * snmp_shutdown(const char *type): * * Parameters: * *type Label for the config file "type" used by calling entity. * * Does the appropriate shutdown calls for the library, saving * persistent data, clean up, etc... */voidsnmp_shutdown(const char *type){ snmp_store(type); snmp_call_callbacks(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_SHUTDOWN, NULL); snmp_alarm_unregister_all(); snmp_close_sessions(); shutdown_mib(); unregister_all_config_handlers(); netsnmp_ds_shutdown();}/* * Sets up the session with the snmp_session information provided by the user. * Then opens and binds the necessary low-level transport. A handle to the * created session is returned (this is NOT the same as the pointer passed to * snmp_open()). On any error, NULL is returned and snmp_errno is set to the * appropriate error code. */netsnmp_session *snmp_open(netsnmp_session *session){ struct session_list *slp; slp = (struct session_list *) snmp_sess_open(session); if (!slp) { return NULL; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -