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

📄 client_intf.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 5 页
字号:
         if (isalpha((int)*buf)) {	    return FAILURE;         }      }   }   sscanf(cp, "%lu", objid++);   /* *objid++ = atoi(cp); */   (*len)++;   return SUCCESS;}static int__get_type_str (type, str)int type;char * str;{   switch (type) {	case TYPE_OBJID:       		strcpy(str, "OBJECTID");	        break;	case TYPE_OCTETSTR:       		strcpy(str, "OCTETSTR");	        break;	case TYPE_INTEGER:       		strcpy(str, "INTEGER");	        break;	case TYPE_INTEGER32:       		strcpy(str, "INTEGER32");	        break;	case TYPE_UNSIGNED32:       		strcpy(str, "UNSIGNED32");	        break;	case TYPE_NETADDR:       		strcpy(str, "NETADDR");	        break;	case TYPE_IPADDR:       		strcpy(str, "IPADDR");	        break;	case TYPE_COUNTER:       		strcpy(str, "COUNTER");	        break;	case TYPE_GAUGE:       		strcpy(str, "GAUGE");	        break;	case TYPE_TIMETICKS:       		strcpy(str, "TICKS");	        break;	case TYPE_OPAQUE:       		strcpy(str, "OPAQUE");	        break;	case TYPE_COUNTER64:       		strcpy(str, "COUNTER64");	        break;	case TYPE_NULL:                strcpy(str, "NULL");                break;	case SNMP_ENDOFMIBVIEW:                strcpy(str, "ENDOFMIBVIEW");                break;	case SNMP_NOSUCHOBJECT:                strcpy(str, "NOSUCHOBJECT");                break;	case SNMP_NOSUCHINSTANCE:                strcpy(str, "NOSUCHINSTANCE");                break;	case TYPE_UINTEGER:                strcpy(str, "UINTEGER"); /* historic - should not show up */                                          /* but it does?                  */                break;	case TYPE_NOTIFTYPE:		strcpy(str, "NOTIF");		break;	case TYPE_BITSTRING:		strcpy(str, "BITS");		break;	case TYPE_TRAPTYPE:		strcpy(str, "TRAP");		break;	case TYPE_OTHER: /* not sure if this is a valid leaf type?? */	case TYPE_NSAPADDRESS:        default: /* unsupported types for now */           strcpy(str, "");	   if (_debug_level) printf("__get_type_str:FAILURE(%d)\n", type);           return(FAILURE);   }   return SUCCESS;}/* does a destructive disection of <label1>...<labeln>.<iid> returning   <labeln> and <iid> in seperate strings (note: will destructively   alter input string, 'name') */static int__get_label_iid (name, last_label, iid, flag)char * name;char ** last_label;char ** iid;int flag;{   char *lcp;   char *icp;   int len = STRLEN(name);   int found_label = 0;   *last_label = *iid = NULL;   if (len == 0) return(FAILURE);   /* Handle case where numeric oid's have been requested.  The input 'name'   ** in this case should be a numeric OID -- return failure if not.   */   if ((flag & USE_NUMERIC_OIDS)) {      if (!__is_numeric_oid(name))       return(FAILURE);      /* Walk backward through the string, looking for first two '.' chars */      lcp = &(name[len]);      icp = NULL;      while (lcp > name) {       if (*lcp == '.') {          /* If this is the first occurence of '.', note it in icp.          ** Otherwise, this must be the second occurrence, so break          ** out of the loop.          */          if (icp == NULL)             icp = lcp;          else             break;       }       lcp --;      }      /* Make sure we found at least a label and index. */      if (!icp)         return(FAILURE);      /* Push forward past leading '.' chars and separate the strings. */      lcp ++;      *icp ++ = '\0';      *last_label = (flag & USE_LONG_NAMES) ? name : lcp;      *iid        = icp;      return(SUCCESS);   }   lcp = icp = &(name[len]);   while (lcp > name) {      if (*lcp == '.') {	if (found_label) {	   lcp++;           break;        } else {           icp = lcp;        }      }      if (!found_label && isalpha((int)*lcp)) found_label = 1;      lcp--;   }   if (!found_label || (!isdigit((int)*(icp+1)) && (flag & FAIL_ON_NULL_IID)))      return(FAILURE);   if (flag & NON_LEAF_NAME) { /* dont know where to start instance id */     /* put the whole thing in label */     icp = &(name[len]);     flag |= USE_LONG_NAMES;     /* special hack in case no mib loaded - object identifiers will      * start with .iso.<num>.<num>...., in which case it is preferable      * to make the label entirely numeric (i.e., convert "iso" => "1")      */      if (*lcp == '.' && lcp == name) {         if (!strncmp(".ccitt.",lcp,7)) {            name += 2;            *name = '.';            *(name+1) = '0';         } else if (!strncmp(".iso.",lcp,5)) {            name += 2;            *name = '.';            *(name+1) = '1';         } else if (!strncmp(".joint-iso-ccitt.",lcp,17)) {            name += 2;            *name = '.';            *(name+1) = '2';         }      }   } else if (*icp) {      *(icp++) = '\0';   }   *last_label = (flag & USE_LONG_NAMES ? name : lcp);   *iid = icp;   return(SUCCESS);}static int__oid_cmp(oida_arr, oida_arr_len, oidb_arr, oidb_arr_len)oid *oida_arr;int oida_arr_len;oid *oidb_arr;int oidb_arr_len;{   for (;oida_arr_len && oidb_arr_len;	oida_arr++, oida_arr_len--, oidb_arr++, oidb_arr_len--) {	if (*oida_arr == *oidb_arr) continue;	return(*oida_arr > *oidb_arr ? 1 : -1);   }   if (oida_arr_len == oidb_arr_len) return(0);   return(oida_arr_len > oidb_arr_len ? 1 : -1);}/* Convert a tag (string) to an OID array              *//* Tag can be either a symbolic name, or an OID string */static struct tree *__tag2oid(tag, iid, oid_arr, oid_arr_len, type, best_guess)char * tag;char * iid;oid  * oid_arr;int  * oid_arr_len;int  * type;int    best_guess;{   struct tree *tp = NULL;   struct tree *rtp = NULL;   oid newname[MAX_OID_LEN], *op;   size_t newname_len = 0;   char str_buf[STR_BUF_SIZE];   str_buf[0] = '\0';   if (type) *type = TYPE_UNKNOWN;   if (oid_arr_len) *oid_arr_len = 0;   if (!tag) goto done;   /*********************************************************/   /* best_guess = 0 - same as no switches (read_objid)     */   /*                  if multiple parts, or uses find_node */   /*                  if a single leaf                     */   /* best_guess = 1 - same as -Ib (get_wild_node)          */   /* best_guess = 2 - same as -IR (get_node)               */   /*********************************************************/   /* numeric scalar                (1,2) */   /* single symbolic               (1,2) */   /* single regex                  (1)   */   /* partial full symbolic         (2)   */   /* full symbolic                 (2)   */   /* module::single symbolic       (2)   */   /* module::partial full symbolic (2)   */   if (best_guess == 1 || best_guess == 2) {      if (!__scan_num_objid(tag, newname, &newname_len)) { /* make sure it's not a numeric tag */       newname_len = MAX_OID_LEN;       if (best_guess == 2) {		/* Random search -IR */         if (get_node(tag, newname, &newname_len)) {	   rtp = tp = get_tree(newname, newname_len, get_tree_head());         }       }       else if (best_guess == 1) {	/* Regex search -Ib */	 clear_tree_flags(get_tree_head());          if (get_wild_node(tag, newname, &newname_len)) {	   rtp = tp = get_tree(newname, newname_len, get_tree_head());         }       }     }     else {       rtp = tp = get_tree(newname, newname_len, get_tree_head());     }     if (type) *type = (tp ? tp->type : TYPE_UNKNOWN);     if ((oid_arr == NULL) || (oid_arr_len == NULL)) return rtp;     memcpy(oid_arr,(char*)newname,newname_len*sizeof(oid));     *oid_arr_len = newname_len;   }      /* if best_guess is off and multi part tag or module::tag */   /* numeric scalar                                         */   /* module::single symbolic                                */   /* module::partial full symbolic                          */   /* FULL symbolic OID                                      */   else if (strchr(tag,'.') || strchr(tag,':')) {      if (!__scan_num_objid(tag, newname, &newname_len)) { /* make sure it's not a numeric tag */	newname_len = MAX_OID_LEN;	if (read_objid(tag, newname, &newname_len)) {	/* long name */	  rtp = tp = get_tree(newname, newname_len, get_tree_head());	}      }      else {	rtp = tp = get_tree(newname, newname_len, get_tree_head());      }      if (type) *type = (tp ? tp->type : TYPE_UNKNOWN);      if ((oid_arr == NULL) || (oid_arr_len == NULL)) return rtp;      memcpy(oid_arr,(char*)newname,newname_len*sizeof(oid));      *oid_arr_len = newname_len;   }      /* else best_guess is off and it is a single leaf */   /* single symbolic                                */   else {       rtp = tp = find_node(tag, get_tree_head());      if (tp) {         if (type) *type = tp->type;         if ((oid_arr == NULL) || (oid_arr_len == NULL)) return rtp;         /* code taken from get_node in snmp_client.c */         for(op = newname + MAX_OID_LEN - 1; op >= newname; op--){           *op = tp->subid;	   tp = tp->parent;	   if (tp == NULL)	      break;         }         *oid_arr_len = newname + MAX_OID_LEN - op;         memcpy(oid_arr, op, *oid_arr_len * sizeof(oid));      } else {         return(rtp);   /* HACK: otherwise, concat_oid_str confuses things */      }   } done:   if (iid && *iid) __concat_oid_str(oid_arr, oid_arr_len, iid);   return(rtp);}/* searches down the mib tree for the given oid   returns the last found tp and its index in lastind */static struct tree *__oid2tp (oidp, len, subtree, lastind)oid* oidp;int len;struct tree * subtree;int* lastind;{    struct tree    *return_tree = NULL;    for (; subtree; subtree = subtree->next_peer) {	if (*oidp == subtree->subid){	    goto found;	}    }    *lastind=0;    return NULL;found:    if (len > 1){       return_tree =          __oid2tp(oidp + 1, len - 1, subtree->child_list, lastind);       (*lastind)++;    } else {       *lastind=1;    }    if (return_tree)	return return_tree;    else	return subtree;}/* function: __concat_oid_str * * This function converts a dotted-decimal string, soid_str, to an array * of oid types and concatenates them on doid_arr begining at the index * specified by doid_arr_len. * * returns : SUCCESS, FAILURE */static int__concat_oid_str(doid_arr, doid_arr_len, soid_str)oid *doid_arr;int *doid_arr_len;char * soid_str;{   char soid_buf[STR_BUF_SIZE];   char *cp;   char *st;   if (!soid_str || !*soid_str) return SUCCESS;/* successfully added nothing */   if (*soid_str == '.') soid_str++;   strcpy(soid_buf, soid_str);   cp = strtok_r(soid_buf,".",&st);   while (cp) {     sscanf(cp, "%lu", &(doid_arr[(*doid_arr_len)++]));     /* doid_arr[(*doid_arr_len)++] =  atoi(cp); */     cp = strtok_r(NULL,".",&st);   }   return(SUCCESS);}/* * add a varbind to PDU */static int__add_var_val_str(pdu, name, name_length, val, len, type)    netsnmp_pdu *pdu;    oid *name;    int name_length;    char * val;    int len;    int type;{    netsnmp_variable_list *vars;    oid oidbuf[MAX_OID_LEN];    int ret = SUCCESS;    if (pdu->variables == NULL){	pdu->variables = vars =           (netsnmp_variable_list *)calloc(1,sizeof(netsnmp_variable_list));    } else {	for(vars = pdu->variables;            vars->next_variable;            vars = vars->next_variable)	    /*EXIT*/;	vars->next_variable =           (netsnmp_variable_list *)calloc(1,sizeof(netsnmp_variable_list));	vars = vars->next_variable;    }    vars->next_variable = NULL;    vars->name = (oid *)malloc(name_length * sizeof(oid));    memcpy((char *)vars->name, (char *)name, name_length * sizeof(oid));    vars->name_length = name_length;    switch (type) {      case TYPE_INTEGER:      case TYPE_INTEGER32:        vars->type = ASN_INTEGER;        vars->val.integer = (long *)malloc(sizeof(long));        if (val)            *(vars->val.integer) = strtol(val,NULL,0);        else {            ret = FAILURE;            *(vars->val.integer) = 0;        }        vars->val_len = sizeof(long);        break;      case TYPE_GAUGE:      case TYPE_UNSIGNED32:        vars->type = ASN_GAUGE;        goto UINT;      case TYPE_COUNTER:        vars->type = ASN_COUNTER;        goto UINT;      case TYPE_TIMETICKS:        vars->type = ASN_TIMETICKS;        goto UINT;      case TYPE_UINTEGER:        vars->type = ASN_UINTEGER;UINT:        vars->val.integer = (long *)malloc(sizeof(long));        if (val)            sscanf(val,"%lu",vars->val.integer);        else {            ret = FAILURE;            *(vars->val.integer) = 0;        }        vars->val_len = sizeof(long);        break;      case TYPE_OCTETSTR:	vars->type = ASN_OCTET_STR;	goto OCT;      case TYPE_BITSTRING:	vars->type = ASN_OCTET_STR;	goto OCT;      case TYPE_OPAQUE:        vars->type = ASN_OCTET_STR;OCT:        vars->val.string = (u_char *)malloc(len);

⌨️ 快捷键说明

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