mib.c

来自「eCos操作系统源码」· C语言 代码 · 共 2,472 行 · 第 1/5 页

C
2,472
字号
    if (*input == '"' || *input == '\'') {      /*       * This is a string that should be converted into an OID       *  Note:  assumes variable length index is required, and prepends       *         the string length.       */      if ((cp = strchr(input+1, *input)) == NULL) {        /* error.  Should be a matching quote somewhere. */        return (0);      }            /* is there room enough for the string in question plus its length */      len = cp-input-1;      if ((int)*out_len <= len){	return (SNMPERR_LONG_OID);      }      /* copy everything in */      if (*input++ == '"') {        /* add the length for " quoted objects */        *output++ = len++;      }      *out_len -= len;      while (input < cp) {        *output++ = *input++;      }      /* Now, we assume that nothing beyond this exists in the parse         tree, which should always be true (or else we have a really wacked         mib designer somewhere. */      input = cp + 1; /* past  the quote */      if (*input != '.')	return (len);      ret = parse_subtree(NULL, ++input, output, out_len);      if (ret <= 0)	return (ret);      return ret+len;    } else if (isdigit(*input)) {	/*	 * Read the number, then try to find it in the subtree.	 */	while (isdigit(*input)) {	    *to++ = *input;	    subid *= 10;	    subid += *input++ - '0';	}	if (*input != '.' && *input != 0) {	    while (*input != 0 && *input != '.') *to++ = *input++;	    *to = 0;	    snmp_set_detail(buf);	    return SNMPERR_BAD_SUBID;	}	*to = '\0';	for (tp = subtree; tp; tp = tp->next_peer) {	    if (tp->subid == subid)		goto found;	}    }    else {	/*	 * Read the name into a buffer.	 */	while ((*input != '\0') &&	       (*input != '.')) {	    *to++ = *input++;	}	*to = '\0';	/*	 * Find the name in the subtree;	 */	for (tp = subtree; tp; tp = tp->next_peer) {	    if (strcasecmp(tp->label, buf) == 0) {		subid = tp->subid;		goto found;	    }	}	/*	 * If we didn't find the entry, punt...	 */	if (tp == NULL) {	    snmp_set_detail(buf);	    return (SNMPERR_BAD_SUBID);	}    }found:    if(subid > (u_long)MAX_SUBID){	snmp_set_detail(buf);	return (SNMPERR_MAX_SUBID);    }    if ((int)*out_len <= 0){	return (SNMPERR_LONG_OID);    }    (*out_len)--;    *output++ = subid;    if (*input != '.')	return (1);    ret = parse_subtree(tp ? tp->child_list : NULL,                             ++input, output, out_len);    if (ret <= 0)	return (ret);    return ret+1;}static struct tree *_sprint_objid(char *buf,	     oid *objid,	     size_t objidlen)	/* number of subidentifiers */{    char    tempbuf[SPRINT_MAX_LEN], *cp;    struct tree    *subtree = tree_head;    char *midpoint = 0;    *tempbuf = '.';	/* this is a fully qualified name */    subtree = _get_symbol(objid, objidlen, subtree, tempbuf + 1, 0, &midpoint);    if (ds_get_boolean(DS_LIBRARY_ID,DS_LIB_PRINT_NUMERIC_OIDS)) {        cp = tempbuf;    } else if (ds_get_int(DS_LIBRARY_ID, DS_LIB_PRINT_SUFFIX_ONLY)){	for(cp = tempbuf; *cp; cp++)	    ;        if (midpoint)            cp = midpoint-2; /* beyond the '.' */        else {            while(cp >= tempbuf){                if (isalpha(*cp))                    break;                cp--;            }        }	while(cp >= tempbuf){	    if (*cp == '.')		break;	    cp--;	}	cp++;	if (ds_get_int(DS_LIBRARY_ID, DS_LIB_PRINT_SUFFIX_ONLY) == 2 && cp > tempbuf) {	    char modbuf[256];	    char *mod = module_name(subtree->modid, modbuf);	    size_t len = strlen(mod);	    if ((int)len+1 >= cp-tempbuf) {		memmove(tempbuf+len+2, cp, strlen(cp)+1);		cp = tempbuf+len+2;	    }	    cp -= len+2;	    memcpy(cp, mod, len);	    cp[len] = ':';	    cp[len+1] = ':';	}    }    else if (!ds_get_boolean(DS_LIBRARY_ID, DS_LIB_PRINT_FULL_OID)) {	PrefixListPtr pp = &mib_prefixes[0];	int ii;	size_t ilen, tlen;	const char *testcp;	cp = tempbuf; tlen = strlen(tempbuf);	ii = 0;	while (pp->str) {	    ilen = pp->len; testcp = pp->str;	    if ((tlen > ilen) && !memcmp(tempbuf, testcp, ilen)) {		cp += (ilen + 1);		break;	    }	    pp++;	}    }    else cp = tempbuf;    strcpy(buf, cp);    return subtree;}char * sprint_objid(char *buf, oid *objid, size_t objidlen){    _sprint_objid(buf,objid,objidlen);    return buf;}voidprint_objid(oid *objid,	    size_t objidlen)	/* number of subidentifiers */{  fprint_objid(stdout, objid, objidlen);}voidfprint_objid(FILE *f,	     oid *objid,	     size_t objidlen)	/* number of subidentifiers */{    char    buf[SPRINT_MAX_LEN];    _sprint_objid(buf, objid, objidlen);    fprintf(f, "%s\n", buf);}voidsprint_variable(char *buf,		oid *objid,		size_t objidlen,		struct variable_list *variable){    struct tree    *subtree;    subtree = _sprint_objid(buf, objid, objidlen);    buf += strlen(buf);    if (ds_get_boolean(DS_LIBRARY_ID, DS_LIB_QUICK_PRINT))	strcat(buf, " ");    else	strcat(buf, " = ");    buf += strlen(buf);    if (variable->type == SNMP_NOSUCHOBJECT)	strcpy(buf, "No Such Object available on this agent");    else if (variable->type == SNMP_NOSUCHINSTANCE)	strcpy(buf, "No Such Instance currently exists");    else if (variable->type == SNMP_ENDOFMIBVIEW)	strcpy(buf, "No more variables left in this MIB View");    else if (subtree) {	if (subtree->printer)	    (*subtree->printer)(buf, variable, subtree->enums, subtree->hint, subtree->units);	  else {	    sprint_by_type(buf, variable, subtree->enums, subtree->hint, subtree->units);	  }    }    else { /* handle rare case where tree is empty */        sprint_by_type(buf, variable, 0, 0, 0);    }}voidprint_variable(oid *objid,	       size_t objidlen,	       struct variable_list *variable){    fprint_variable(stdout, objid, objidlen, variable);}voidfprint_variable(FILE *f,		oid *objid,		size_t objidlen,		struct variable_list *variable){    char    buf[SPRINT_MAX_LEN];    sprint_variable(buf, objid, objidlen, variable);    fprintf(f, "%s\n", buf);}voidsprint_value(char *buf,	     oid *objid,	     size_t objidlen,	     struct variable_list *variable){    char    tempbuf[SPRINT_MAX_LEN];    struct tree    *subtree = tree_head;    if (variable->type == SNMP_NOSUCHOBJECT)	sprintf(buf, "No Such Object available on this agent");    else if (variable->type == SNMP_NOSUCHINSTANCE)	sprintf(buf, "No Such Instance currently exists");    else if (variable->type == SNMP_ENDOFMIBVIEW)	sprintf(buf, "No more variables left in this MIB View");    else {	subtree = get_symbol(objid, objidlen, subtree, tempbuf);	if (subtree->printer)	    (*subtree->printer)(buf, variable, subtree->enums, subtree->hint, subtree->units);	else {	    sprint_by_type(buf, variable, subtree->enums, subtree->hint, subtree->units);	}    }}voidprint_value(oid *objid,	    size_t objidlen,	    struct variable_list *variable){    fprint_value(stdout, objid, objidlen, variable);}voidfprint_value(FILE *f,	     oid *objid,	     size_t objidlen,	     struct variable_list *variable){    char    tempbuf[SPRINT_MAX_LEN];    sprint_value(tempbuf, objid, objidlen, variable);    fprintf(f, "%s\n", tempbuf);}/* * Append a quoted printable string to buffer "buf" * that represents a range of sub-identifiers "objid". * * Display '.' for all non-printable sub-identifiers. * If successful, "buf" points past the appended string. */char *dump_oid_to_string(oid *objid,                   size_t objidlen,                   char *buf,                   char quotechar){  if (buf)  { int ii, alen;    char *scp;    char *cp = buf + (strlen(buf));    scp = cp;    for (ii= 0, alen = 0; ii < (int)objidlen; ii++)    {        oid tst = objid[ii];        if ((tst > 254) || (!isprint(tst)))            tst = (oid)'.';                  if (alen == 0) *cp++ = quotechar;        *cp++ = (char)tst;        alen++;    }    if (alen) *cp++ = quotechar;    *cp = '\0';    buf = cp;  }  return buf;}struct tree *_get_symbol(oid *objid,	   size_t objidlen,	   struct tree *subtree,	   char *buf,	   struct index_list *in_dices,           char **end_of_known){    struct tree    *return_tree = NULL;    if (!objid || !buf)        return NULL;    for(; subtree; subtree = subtree->next_peer){	if (*objid == subtree->subid){	    if (subtree->indexes)                in_dices = subtree->indexes;	    if (!strncmp( subtree->label, ANON, ANON_LEN) ||                ds_get_boolean(DS_LIBRARY_ID,DS_LIB_PRINT_NUMERIC_OIDS))                sprintf(buf, "%lu", subtree->subid);	    else                strcpy(buf, subtree->label);	    goto found;	}    }    if (end_of_known)        *end_of_known = buf;    /* subtree not found */    while (in_dices && (objidlen > 0) &&           !ds_get_boolean(DS_LIBRARY_ID,DS_LIB_PRINT_NUMERIC_OIDS) &&           !ds_get_boolean(DS_LIBRARY_ID,DS_LIB_DONT_BREAKDOWN_OIDS)) {	size_t numids;	struct tree *tp;	tp = find_tree_node(in_dices->ilabel, -1);	if (0 == tp) {            /* ack.  Can't find an index in the mib tree.  bail */            goto finish_it;        }	switch(tp->type) {	case TYPE_OCTETSTR:	    if (in_dices->isimplied) {                numids = objidlen;                buf = dump_oid_to_string(objid, numids, buf, '\'');            } else {                numids = (size_t)*objid+1;                if (numids > objidlen)                    goto finish_it;		if (numids == 1) {		    *buf++ = '"'; *buf++ = '"';		}		else		    buf = dump_oid_to_string(objid+1, numids-1, buf, '"');            }            objid += (numids);            objidlen -= (numids);	    *buf++ = '.';	    *buf = '\0';	    break;	case TYPE_INTEGER:	    sprintf(buf, "%lu.", *objid++);	    while(*buf)		buf++;	    objidlen--;	    break;	case TYPE_OBJID:	    if (in_dices->isimplied) {                numids = objidlen;            } else {                numids = (size_t)*objid+1;            }	    if ( numids > objidlen)		goto finish_it;	    _get_symbol(objid, numids, NULL, buf, NULL, NULL);	    objid += (numids);	    objidlen -= (numids);            buf += strlen(buf);	    *buf++ = '.';	    *buf = '\0';	    break;	default:	    goto finish_it;	    break;	}        in_dices = in_dices->next;    }finish_it:    while(objidlen-- > 0){	/* output rest of name, uninterpreted */	sprintf(buf, "%lu.", *objid++);	while(*buf)	    buf++;    }    *(buf - 1) = '\0'; /* remove trailing dot */    return NULL;found:    if (objidlen > 1){	while(*buf)	    buf++;	*buf++ = '.';	*buf = '\0';	return_tree = _get_symbol(objid + 1, objidlen - 1, subtree->child_list,				 buf, in_dices, end_of_known);    }    if (return_tree != NULL)	return return_tree;    else	return subtree;}struct tree *get_symbol(oid *objid,	   size_t objidlen,	   struct tree *subtree,	   char *buf){   return _get_symbol(objid,objidlen,subtree,buf,0,0);}/* * Clone of get_symbol that doesn't take a buffer argument */struct tree *get_tree(oid *objid,	 size_t objidlen,	 struct tree *subtree){    struct tree    *return_tree = NULL;    for(; subtree; subtree = subtree->next_peer){        if (*objid == subtree->subid)            goto found;    }    return NULL;found:    if (objidlen > 1)        return_tree = get_tree(objid + 1, objidlen - 1, subtree->child_list);    if (return_tree != NULL)        return return_tree;    else        return subtree;}voidprint_description(oid *objid,		  size_t objidlen)   /* number of subidentifiers */{

⌨️ 快捷键说明

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