⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmp_client.c

📁 snmp up 2
💻 C
📖 第 1 页 / 共 2 页
字号:
    if ((drop_err && (ii < pdu->errindex))#if TEMPORARILY_DISABLED        /*         * SNMPv3 engineID probes are allowed to be empty.         * See the comment in snmp_api.c for further details          */        || copied == 0#endif        ) {        snmp_free_pdu(newpdu);        return 0;    }#endif    return newpdu;}/* * Creates (allocates and copies) a clone of the input PDU. * If drop_err is set, don't copy any variable associated with errindex. * This function is called by snmp_clone_pdu and snmp_fix_pdu. * * Returns a pointer to the cloned PDU if successful. * Returns 0 if failure. */staticnetsnmp_pdu    *_clone_pdu(netsnmp_pdu *pdu, int drop_err){    netsnmp_pdu    *newpdu;    newpdu = _clone_pdu_header(pdu);    newpdu = _copy_pdu_vars(pdu, newpdu, drop_err, 0, 10000);   /* skip none, copy all */    return newpdu;}/* * This function will clone a full varbind list * * Returns a pointer to the cloned PDU if successful. * Returns 0 if failure */netsnmp_variable_list *snmp_clone_varbind(netsnmp_variable_list * varlist){    return _copy_varlist(varlist, 0, 10000);    /* skip none, copy all */}/* * This function will clone a PDU including all of its variables. * * Returns a pointer to the cloned PDU if successful. * Returns 0 if failure */netsnmp_pdu    *snmp_clone_pdu(netsnmp_pdu *pdu){    return _clone_pdu(pdu, 0);  /* copies all variables */}/* * This function will clone a PDU including some of its variables. * * If skip_count is not zero, it defines the number of variables to skip. * If copy_count is not zero, it defines the number of variables to copy. * * Returns a pointer to the cloned PDU if successful. * Returns 0 if failure. */netsnmp_pdu    *snmp_split_pdu(netsnmp_pdu *pdu, int skip_count, int copy_count){    netsnmp_pdu    *newpdu;    newpdu = _clone_pdu_header(pdu);    newpdu = _copy_pdu_vars(pdu, newpdu, 0,     /* don't drop any variables */                            skip_count, copy_count);    return newpdu;}/* * If there was an error in the input pdu, creates a clone of the pdu * that includes all the variables except the one marked by the errindex. * The command is set to the input command and the reqid, errstat, and * errindex are set to default values. * If the error status didn't indicate an error, the error index didn't * indicate a variable, the pdu wasn't a get response message, or there * would be no remaining variables, this function will return 0. * If everything was successful, a pointer to the fixed cloned pdu will * be returned. */netsnmp_pdu    *snmp_fix_pdu(netsnmp_pdu *pdu, int command){    netsnmp_pdu    *newpdu;    if ((pdu->command != SNMP_MSG_RESPONSE)        || (pdu->errstat == SNMP_ERR_NOERROR)        || (0 == pdu->variables)        || (pdu->errindex <= 0)) {        return 0;               /* pre-condition tests fail */    }    newpdu = _clone_pdu(pdu, 1);        /* copies all except errored variable */    if (!newpdu)        return 0;    if (!newpdu->variables) {        snmp_free_pdu(newpdu);        return 0;               /* no variables. "should not happen" */    }    newpdu->command = command;    newpdu->reqid = snmp_get_next_reqid();    newpdu->msgid = snmp_get_next_msgid();    newpdu->errstat = SNMP_DEFAULT_ERRSTAT;    newpdu->errindex = SNMP_DEFAULT_ERRINDEX;    return newpdu;}/* * Returns the number of variables bound to a PDU structure */unsigned longsnmp_varbind_len(netsnmp_pdu *pdu){    register netsnmp_variable_list *vars;    unsigned long   retVal = 0;    if (pdu)        for (vars = pdu->variables; vars; vars = vars->next_variable) {            retVal++;        }    return retVal;}/* * Add object identifier name to SNMP variable. * If the name is large, additional memory is allocated. * Returns 0 if successful. */intsnmp_set_var_objid(netsnmp_variable_list * vp,                   const oid * objid, size_t name_length){    size_t          len = sizeof(oid) * name_length;    if (vp->name != vp->name_loc && vp->name != NULL &&        vp->name_length > (sizeof(vp->name_loc) / sizeof(oid))) {        /*         * Probably previously-allocated "big storage".  Better free it         * else memory leaks possible.           */        free(vp->name);    }    /*     * use built-in storage for smaller values      */    if (len <= sizeof(vp->name_loc)) {        vp->name = vp->name_loc;    } else {        vp->name = (oid *) malloc(len);        if (!vp->name)            return 1;    }    if (objid)        memmove(vp->name, objid, len);    vp->name_length = name_length;    return 0;}intsnmp_set_var_typed_value(netsnmp_variable_list * newvar, u_char type,                         const u_char * val_str, size_t val_len){    newvar->type = type;    return snmp_set_var_value(newvar, val_str, val_len);}intcount_varbinds(netsnmp_variable_list * var_ptr){    int             count = 0;    for (; var_ptr != NULL; var_ptr = var_ptr->next_variable)        count++;    return count;}intcount_varbinds_of_type(netsnmp_variable_list * var_ptr, u_char type){    int             count = 0;    for (; var_ptr != NULL; var_ptr = var_ptr->next_variable)        if (var_ptr->type == type)            count++;    return count;}netsnmp_variable_list *find_varbind_of_type(netsnmp_variable_list * var_ptr, u_char type){    for (; var_ptr != NULL && var_ptr->type != type;         var_ptr = var_ptr->next_variable);    return var_ptr;}/* * Add some value to SNMP variable. * If the value is large, additional memory is allocated. * Returns 0 if successful. */intsnmp_set_var_value(netsnmp_variable_list * newvar,                   const u_char * val_str, size_t val_len){    if (newvar->val.string && newvar->val.string != newvar->buf) {        free(newvar->val.string);    }    newvar->val.string = 0;    newvar->val_len = 0;    /*     * need a pointer and a length to copy a string value.      */    if (val_str && val_len) {        if (val_len <= sizeof(newvar->buf))            newvar->val.string = newvar->buf;        else {            newvar->val.string = (u_char *) malloc(val_len);            if (!newvar->val.string)                return 1;        }        memmove(newvar->val.string, val_str, val_len);        newvar->val_len = val_len;    } else if (val_str) {        /*         * NULL STRING != NULL ptr          */        newvar->val.string = newvar->buf;        newvar->val.string[0] = '\0';        newvar->val_len = 0;    }    return 0;}voidsnmp_replace_var_types(netsnmp_variable_list * vbl, u_char old_type,                       u_char new_type){    while (vbl) {        if (vbl->type == old_type) {            snmp_set_var_typed_value(vbl, new_type, NULL, 0);        }        vbl = vbl->next_variable;    }}voidsnmp_reset_var_types(netsnmp_variable_list * vbl, u_char new_type){    while (vbl) {        snmp_set_var_typed_value(vbl, new_type, NULL, 0);        vbl = vbl->next_variable;    }}intsnmp_synch_response_cb(netsnmp_session * ss,                       netsnmp_pdu *pdu,                       netsnmp_pdu **response, snmp_callback pcb){    struct synch_state lstate, *state;    snmp_callback   cbsav;    void           *cbmagsav;    int             numfds, count;    fd_set          fdset;    struct timeval  timeout, *tvp;    int             block;    memset((void *) &lstate, 0, sizeof(lstate));    state = &lstate;    cbsav = ss->callback;    cbmagsav = ss->callback_magic;    ss->callback = pcb;    ss->callback_magic = (void *) state;    if ((state->reqid = snmp_send(ss, pdu)) == 0) {        snmp_free_pdu(pdu);        state->status = STAT_ERROR;    } else        state->waiting = 1;    while (state->waiting) {        numfds = 0;        FD_ZERO(&fdset);        block = SNMPBLOCK;        tvp = &timeout;        timerclear(tvp);        snmp_select_info(&numfds, &fdset, tvp, &block);        if (block == 1)            tvp = NULL;         /* block without timeout */        count = select(numfds, &fdset, 0, 0, tvp);        if (count > 0) {            snmp_read(&fdset);        } else            switch (count) {            case 0:                snmp_timeout();                break;            case -1:                if (errno == EINTR) {                    continue;                } else {                    snmp_errno = SNMPERR_GENERR;                    /*                     * CAUTION! if another thread closed the socket(s)                     * waited on here, the session structure was freed.                     * It would be nice, but we can't rely on the pointer.                     * ss->s_snmp_errno = SNMPERR_GENERR;                     * ss->s_errno = errno;                     */                    snmp_set_detail(strerror(errno));                }                /*                 * FALLTHRU                  */            default:                state->status = STAT_ERROR;                state->waiting = 0;            }    }    *response = state->pdu;    ss->callback = cbsav;    ss->callback_magic = cbmagsav;    return state->status;}intsnmp_synch_response(netsnmp_session * ss,                    netsnmp_pdu *pdu, netsnmp_pdu **response){    return snmp_synch_response_cb(ss, pdu, response, snmp_synch_input);}intsnmp_sess_synch_response(void *sessp,                         netsnmp_pdu *pdu, netsnmp_pdu **response){    netsnmp_session *ss;    struct synch_state lstate, *state;    snmp_callback   cbsav;    void           *cbmagsav;    int             numfds, count;    fd_set          fdset;    struct timeval  timeout, *tvp;    int             block;    ss = snmp_sess_session(sessp);    memset((void *) &lstate, 0, sizeof(lstate));    state = &lstate;    cbsav = ss->callback;    cbmagsav = ss->callback_magic;    ss->callback = snmp_synch_input;    ss->callback_magic = (void *) state;    if ((state->reqid = snmp_sess_send(sessp, pdu)) == 0) {        snmp_free_pdu(pdu);        state->status = STAT_ERROR;    } else        state->waiting = 1;    while (state->waiting) {        numfds = 0;        FD_ZERO(&fdset);        block = SNMPBLOCK;        tvp = &timeout;        timerclear(tvp);        snmp_sess_select_info(sessp, &numfds, &fdset, tvp, &block);        if (block == 1)            tvp = NULL;         /* block without timeout */        count = select(numfds, &fdset, 0, 0, tvp);        if (count > 0) {            snmp_sess_read(sessp, &fdset);        } else            switch (count) {            case 0:                snmp_sess_timeout(sessp);                break;            case -1:                if (errno == EINTR) {                    continue;                } else {                    snmp_errno = SNMPERR_GENERR;                    /*                     * CAUTION! if another thread closed the socket(s)                     * waited on here, the session structure was freed.                     * It would be nice, but we can't rely on the pointer.                     * ss->s_snmp_errno = SNMPERR_GENERR;                     * ss->s_errno = errno;                     */                    snmp_set_detail(strerror(errno));                }                /*                 * FALLTHRU                  */            default:                state->status = STAT_ERROR;                state->waiting = 0;            }    }    *response = state->pdu;    ss->callback = cbsav;    ss->callback_magic = cbmagsav;    return state->status;}const char     *error_string[19] = {    "(noError) No Error",    "(tooBig) Response message would have been too large.",    "(noSuchName) There is no such variable name in this MIB.",    "(badValue) The value given has the wrong type or length.",    "(readOnly) The two parties used do not have access to use the specified SNMP PDU.",    "(genError) A general failure occured",    "noAccess",    "wrongType (The set datatype does not match the data type the agent expects)",    "wrongLength (The set value has an illegal length from what the agent expects)",    "wrongEncoding",    "wrongValue (The set value is illegal or unsupported in some way)",    "noCreation (that table does not support row creation)",    "inconsistentValue (The set value is illegal or unsupported in some way)",    "resourceUnavailable (This is likely a out-of-memory failure within the agent)",    "commitFailed",    "undoFailed",    "authorizationError (access denied to that object)",    "notWritable (that object does not support modification)",    "inconsistentName"};const char     *snmp_errstring(int errstat){    if (errstat <= MAX_SNMP_ERR && errstat >= SNMP_ERR_NOERROR) {        return error_string[errstat];    } else {        return "Unknown Error";    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -