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

📄 snmp_client.c

📁 eCos/RedBoot for勤研ARM AnywhereII(4510) 含全部源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
static
struct snmp_pdu *
_copy_pdu_vars(struct snmp_pdu *pdu,  /* source PDU */
        struct snmp_pdu *newpdu,      /* target PDU */
        int drop_err,                 /* !=0 drop errored variable */
        int skip_count,               /* !=0 number of variables to skip */
        int copy_count)               /* !=0 number of variables to copy */
{
    struct variable_list *var, *newvar, *oldvar;
    int ii, copied;

    if (!newpdu) return 0;            /* where is PDU to copy to ? */

    var = pdu->variables;
    while (var && (skip_count-- > 0)) /* skip over pdu variables */
        var = var->next_variable;

    oldvar = 0; ii = 0; copied = 0;
    if (pdu->flags & UCD_MSG_FLAG_FORCE_PDU_COPY)
	copied = 1;	/* We're interested in 'empty' responses too */
    while (var && (copy_count-- > 0))
    {
        /* errindex starts from 1. If drop_err, skip the errored variable */
        if (drop_err && (++ii == pdu->errindex)) {
            var = var->next_variable; continue;
        }

        /* clone the next variable. Cleanup if alloc fails */
        newvar = (struct variable_list *)malloc(sizeof(struct variable_list));
        if (snmp_clone_var(var, newvar)){
            if (newvar) free((char *)newvar);
            snmp_free_pdu(newpdu); return 0;
        }
        copied++;

        /* add cloned variable to new PDU */
        if (0 == newpdu->variables) newpdu->variables = newvar;
        if (oldvar) oldvar->next_variable = newvar;
        oldvar = newvar;

        var = var->next_variable;
    }
    /* Error if bad errindex or if target PDU has no variables copied */
    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;
    }
    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.
 */
static
struct snmp_pdu *
_clone_pdu(struct snmp_pdu *pdu, int drop_err)
{
    struct snmp_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 PDU including all of its variables.
 *
 * Returns a pointer to the cloned PDU if successful.
 * Returns 0 if failure
 */
struct snmp_pdu *
snmp_clone_pdu(struct snmp_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.
 */
struct snmp_pdu *
snmp_split_pdu(struct snmp_pdu *pdu, int skip_count, int copy_count)
{
    struct snmp_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.
 */
struct snmp_pdu *
snmp_fix_pdu(struct snmp_pdu *pdu, int command)
{
    struct snmp_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 long
snmp_varbind_len(struct snmp_pdu * pdu)
{
    register struct 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.
 */

int
snmp_set_var_objid (struct variable_list *vp,
                    const oid *objid, size_t name_length)
{
    size_t len = sizeof(oid) * name_length;

    /* 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;
    }
    memmove(vp->name, objid, len);
    vp->name_length = name_length;
    return 0;
}

/*
 * Add some value to SNMP variable.
 * If the value is large, additional memory is allocated.
 * Returns 0 if successful.
 */

int
snmp_set_var_value(struct variable_list *newvar,
                    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;
    }

    return 0;
}


int
snmp_synch_response_cb(struct snmp_session *ss,
		    struct snmp_pdu *pdu,
		    struct snmp_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;
}

int
snmp_synch_response(struct snmp_session *ss,
		    struct snmp_pdu *pdu,
		    struct snmp_pdu **response)
{
    return snmp_synch_response_cb(ss,pdu,response,snmp_synch_input);
}

int
snmp_sess_synch_response(void *sessp,
			 struct snmp_pdu *pdu,
			 struct snmp_pdu **response)
{
    struct snmp_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",
    "wrongLength",
    "wrongEncoding",
    "wrongValue",
    "noCreation",
    "inconsistentValue",
    "resourceUnavailable",
    "commitFailed",
    "undoFailed",
    "authorizationError",
    "notWritable",
    "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 + -