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

📄 client_intf.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 5 页
字号:
    /* pre-allocate the return tuples */    val_tuple = PyTuple_New(0);    if (!val_tuple) {      /* propagate error */      if (verbose)        printf("error: walk: couldn't allocate a new value tuple");      snmp_free_pdu(pdu);      goto done;    }                /*    ** Set up for numeric or full OID's, if necessary.  Save the old    ** output format so that it can be restored when we finish -- this    ** is a library-wide global, and has to be set/restored for each    ** session.    */    old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,                                    NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);    if (py_netsnmp_attr_long(session, "UseLongNames")) {      getlabel_flag |= USE_LONG_NAMES;      netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,                         NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,                         NETSNMP_OID_OUTPUT_FULL);    }    /* Setting UseNumeric forces UseLongNames on so check for UseNumeric       after UseLongNames (above) to make sure the final outcome of        NETSNMP_DS_LIB_OID_OUTPUT_FORMAT is NETSNMP_OID_OUTPUT_NUMERIC */    if (py_netsnmp_attr_long(session, "UseNumeric")) {      getlabel_flag |= USE_LONG_NAMES;      getlabel_flag |= USE_NUMERIC_OIDS;      netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,                         NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,                         NETSNMP_OID_OUTPUT_NUMERIC);    }    /* delete the existing varbinds that we'll replace */    PySequence_DelSlice(varbinds, 0, PySequence_Length(varbinds));    if (PyErr_Occurred()) {      /* propagate error */      if (verbose)        printf("error: walk: deleting old varbinds failed\n");      snmp_free_pdu(pdu);      goto done;    }    while(notdone) {      status = __send_sync_pdu(ss, pdu, &response, retry_nosuch,                                err_str, &err_num, &err_ind);            if (!response || !response->variables ||          (response->variables->name_length < oid_arr_len) ||          (memcmp(oid_arr, response->variables->name,                  oid_arr_len * sizeof(oid))) ||          status != STAT_SUCCESS ||          response->errstat != SNMP_ERR_NOERROR) {          notdone = 0;      } else {        for(vars = (response ? response->variables : NULL), varlist_ind = 0;    	vars && (varlist_ind < varlist_len);    	vars = vars->next_variable, varlist_ind++) {          if ((vars->type == SNMP_ENDOFMIBVIEW) ||              (vars->type == SNMP_NOSUCHOBJECT) ||              (vars->type == SNMP_NOSUCHINSTANCE)) {              notdone = 0;              break;          }          varbind = py_netsnmp_construct_varbind();          if (PyObject_HasAttrString(varbind, "tag")) {    	  str_buf[0] = '.';    	  str_buf[1] = '\0';    	  out_len = 0;    	  tp = netsnmp_sprint_realloc_objid_tree(&str_bufp, &str_buf_len,                                                   &out_len, 0, &buf_over,                                                   vars->name,vars->name_length);    	  str_buf[sizeof(str_buf)-1] = '\0';    	  if (__is_leaf(tp)) {	    type = (tp->type ? tp->type : tp->parent->type);    	    getlabel_flag &= ~NON_LEAF_NAME;    	  } else {    	    getlabel_flag |= NON_LEAF_NAME;    	    type = __translate_asn_type(vars->type);    	  }    	  __get_label_iid(str_buf, &tag, &iid, getlabel_flag);    	  if (_debug_level) printf("netsnmp_walk: filling response: %s:%s\n", tag, iid);	  py_netsnmp_attr_set_string(varbind, "tag", tag, STRLEN(tag));	  py_netsnmp_attr_set_string(varbind, "iid", iid, STRLEN(iid));    	  __get_type_str(type, type_str);	  py_netsnmp_attr_set_string(varbind, "type", type_str, 				     STRLEN(type_str));    	  len = __snprint_value(str_buf,sizeof(str_buf),				vars,tp,type,sprintval_flag);    	  str_buf[len] = '\0';	  py_netsnmp_attr_set_string(varbind, "val", str_buf, len);            	  /* push the varbind onto the return varbinds */	  PyList_Append(varbinds, varbind);    	  /* save in return tuple as well */	  /* save in return tuple as well - steals ref */	  _PyTuple_Resize(&val_tuple, result_count+1);	  PyTuple_SetItem(val_tuple, result_count++, 			  (len ? Py_BuildValue("s#", str_buf, len) :			   Py_BuildValue("")));                	  Py_DECREF(varbind);          } else {	    /* Return None for this variable. */            _PyTuple_Resize(&val_tuple, result_count+1);	    PyTuple_SetItem(val_tuple, result_count++, Py_BuildValue(""));	    printf("netsnmp_walk: bad varbind (%d)\n", varlist_ind);          }	        }        /* reuse the response as the next pdu to send */        pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);        snmp_add_null_var(pdu, response->variables->name,                          response->variables->name_length);      }      if (response)	snmp_free_pdu(response);    }    /* Reset the library's behavior for numeric/symbolic OID's. */    netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,		       NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,		       old_format);    if (PyErr_Occurred()) {      /* propagate error */      if (verbose)	printf("error: walk response processing: unknown python error");      Py_DECREF(val_tuple);    }   } done:  SAFE_FREE(oid_arr);  return (val_tuple ? val_tuple : Py_BuildValue(""));}static PyObject *netsnmp_getbulk(PyObject *self, PyObject *args){  int nonrepeaters;  int maxrepetitions;  PyObject *session;  PyObject *varlist;  PyObject *varbinds;  PyObject *varbind;  PyObject *val_tuple = NULL;  int varbind_ind;  netsnmp_session *ss;  netsnmp_pdu *pdu, *response;  netsnmp_variable_list *vars;  struct tree *tp;  int len;  oid *oid_arr;  int oid_arr_len = MAX_OID_LEN;  int type;  char type_str[MAX_TYPE_NAME_LEN];  int status;  u_char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;  size_t str_buf_len = sizeof(str_buf);  size_t out_len = 0;  int buf_over = 0;  char *tag;  char *iid;  int getlabel_flag = NO_FLAGS;  int sprintval_flag = USE_BASIC;  int verbose = py_netsnmp_verbose();  int old_format;  int best_guess;  int retry_nosuch;  int err_ind;  int err_num;  char err_str[STR_BUF_SIZE];	     oid_arr = calloc(MAX_OID_LEN, sizeof(oid));  if (oid_arr && args) {    if (!PyArg_ParseTuple(args, "OiiO", &session, &nonrepeaters, 			  &maxrepetitions, &varlist)) {      goto done;    }    if (varlist && (varbinds = PyObject_GetAttrString(varlist, "varbinds"))) {            ss = (SnmpSession *)py_netsnmp_attr_long(session, "sess_ptr");      strcpy(err_str, py_netsnmp_attr_string(session, "ErrorStr"));      err_num = py_netsnmp_attr_long(session, "ErrorNum");      err_ind = py_netsnmp_attr_long(session, "ErrorInd");      if (py_netsnmp_attr_long(session, "UseLongNames"))	getlabel_flag |= USE_LONG_NAMES;      if (py_netsnmp_attr_long(session, "UseNumeric"))	getlabel_flag |= USE_NUMERIC_OIDS;      if (py_netsnmp_attr_long(session, "UseEnums"))	sprintval_flag = USE_ENUMS;      if (py_netsnmp_attr_long(session, "UseSprintValue"))	sprintval_flag = USE_SPRINT_VALUE;	      best_guess = py_netsnmp_attr_long(session, "BestGuess");      retry_nosuch = py_netsnmp_attr_long(session, "RetryNoSuch");            pdu = snmp_pdu_create(SNMP_MSG_GETBULK);      pdu->errstat = nonrepeaters;      pdu->errindex = maxrepetitions;      PyObject *varbinds_iter = PyObject_GetIter(varbinds);      while (varbinds_iter && (varbind = PyIter_Next(varbinds_iter))) {	tag = py_netsnmp_attr_string(varbind, "tag");	iid = py_netsnmp_attr_string(varbind, "iid");	tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, 		       NULL, best_guess);	if (oid_arr_len) {	  snmp_add_null_var(pdu, oid_arr, oid_arr_len);	} else {	  if (verbose)	    printf("error: get: unknown object ID (%s)",		   (tag ? tag : "<null>"));	  snmp_free_pdu(pdu);	  goto done;	}	/* release reference when done */	Py_DECREF(varbind);      }      Py_DECREF(varbinds_iter);      if (PyErr_Occurred()) {	/* propagate error */	if (verbose)	  printf("error: get: unknown python error");	snmp_free_pdu(pdu);	goto done;      }      status = __send_sync_pdu(ss, pdu, &response, retry_nosuch, 			       err_str, &err_num, &err_ind);      /*      ** Set up for numeric or full OID's, if necessary.  Save the old      ** output format so that it can be restored when we finish -- this      ** is a library-wide global, and has to be set/restored for each      ** session.      */      old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,				      NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);      if (py_netsnmp_attr_long(session, "UseLongNames")) {	getlabel_flag |= USE_LONG_NAMES;	netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,			   NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,			   NETSNMP_OID_OUTPUT_FULL);      }      /* Setting UseNumeric forces UseLongNames on so check for UseNumeric	 after UseLongNames (above) to make sure the final outcome of 	 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT is NETSNMP_OID_OUTPUT_NUMERIC */      if (py_netsnmp_attr_long(session, "UseNumeric")) {	getlabel_flag |= USE_LONG_NAMES;	getlabel_flag |= USE_NUMERIC_OIDS;	netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,			   NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,			   NETSNMP_OID_OUTPUT_NUMERIC);      }      /* create tuple in which to return results */      val_tuple = PyTuple_New(0);      if(response && response->variables) {	/* clear varlist to receive response varbinds*/	PySequence_DelSlice(varbinds, 0, PySequence_Length(varbinds));        if (PyErr_Occurred()) {            /* propagate error */            if (verbose)                printf("error: bulk: deleting old varbinds failed\n");            snmp_free_pdu(pdu);            goto done;        }	for(vars = response->variables, varbind_ind=0; 	    vars; 	    vars = vars->next_variable, varbind_ind++) {	  varbind = py_netsnmp_construct_varbind();	  if (PyObject_HasAttrString(varbind, "tag")) {	    *str_buf = '.';	    *(str_buf+1) = '\0';	    out_len = 0;	    buf_over = 0;	    str_bufp = str_buf;	    tp = netsnmp_sprint_realloc_objid_tree(&str_bufp, &str_buf_len,						   &out_len, 0, &buf_over,						   vars->name,vars->name_length);	    str_buf[sizeof(str_buf)-1] = '\0';	    if (__is_leaf(tp)) {	      type = (tp->type ? tp->type : tp->parent->type);	      getlabel_flag &= ~NON_LEAF_NAME;	    } else {	      getlabel_flag |= NON_LEAF_NAME;	      type = __translate_asn_type(vars->type);	    }	    __get_label_iid(str_buf, &tag, &iid, getlabel_flag);	    py_netsnmp_attr_set_string(varbind, "tag", tag, STRLEN(tag));	    py_netsnmp_attr_set_string(varbind, "iid", iid, STRLEN(iid));	    __get_type_str(type, type_str);	    py_netsnmp_attr_set_string(varbind, "type", type_str, 				       STRLEN(type_str));	    len = __snprint_value(str_buf,sizeof(str_buf),				  vars,tp,type,sprintval_flag);	    str_buf[len] = '\0';	    py_netsnmp_attr_set_string(varbind, "val", str_buf, len);	    /* push varbind onto varbinds */	    PyList_Append(varbinds, varbind);	    /* save in return tuple as well - steals ref */	    _PyTuple_Resize(&val_tuple, varbind_ind+1);	    PyTuple_SetItem(val_tuple, varbind_ind, 			       Py_BuildValue("s#", str_buf, len));	    Py_DECREF(varbind);	  } else {	    PyObject *none = Py_BuildValue(""); /* new ref */	    /* not sure why making vabind failed - should not happen*/	    PyList_Append(varbinds, none); /* increments ref */	    /* Return None for this variable. */	    PyTuple_SetItem(val_tuple, varbind_ind, none); /* steals ref */	  }		}      }      /* Reset the library's behavior for numeric/symbolic OID's. */      netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,			 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,			 old_format);      if (response) snmp_free_pdu(response);      Py_DECREF(varbinds);    }    if (PyErr_Occurred()) {      /* propagate error */      if (verbose)	printf("error: getbulk response processing: unknown python error");      Py_DECREF(val_tuple);      val_tuple = NULL;    }  } done:  SAFE_FREE(oid_arr);  return (val_tuple ? val_tuple : Py_BuildValue(""));}static PyObject *netsnmp_set(PyObject *self, PyObject *args){  PyObject *session;  PyObject *varlist;  PyObject *varbind;  PyObject *ret = NULL;  netsnmp_session *ss;  netsnmp_pdu *pdu, *response;  struct tree *tp;  char *tag;  char *iid;  char *val;  char *type_str;  int len;  oid *oid_arr;  int oid_arr_len = MAX_OID_LEN;  int type;  u_char tmp_val_str[STR_BUF_SIZE];  int use_enums;  struct enum_list *ep;  int verbose = py_netsnmp_verbose();  int best_guess;  int status;  int err_ind;  int err_num;  char err_str[STR_BUF_SIZE];	     oid_arr = calloc(MAX_OID_LEN, sizeof(oid));  if (oid_arr && args) {    if (!PyArg_ParseTuple(args, "OO", &session, &varlist)) {      goto done;    }    ss = (SnmpSession *)py_netsnmp_attr_long(session, "sess_ptr");    /* PyObject_SetAttrString(); */    strcpy(err_str, py_netsnmp_attr_string(session, "ErrorStr"));    err_num = py_netsnmp_attr_long(session, "ErrorNum");    err_ind = py_netsnmp_attr_long(session, "ErrorInd");    use_enums = py_netsnmp_attr_long(session, "UseEnums");	    best_guess = py_netsnmp_attr_long(session, "BestGuess");          pdu = snmp_pdu_create(SNMP_MSG_SET);    if (varlist) {      PyObject *varlist_iter = PyObject_GetIter(varlist);      while (varlist_iter && (varbind = PyIter_Next(varlist_iter))) {	tag = py_netsnmp_attr_string(varbind, "tag");	iid = py_netsnmp_attr_string(varbind, "iid");	tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, 		       &type, best_guess);	if (oid_arr_len==0) {	  if (verbose)	    printf("error: set: unknown object ID (%s)",		 (tag?tag:"<null>"));	  snmp_free_pdu(pdu);	  goto done;	}	if (type == TYPE_UNKNOWN) {	  type_str = py_netsnmp_attr_string(varbind, "type");	  type = __translate_appl_type(type_str);	  if (type == TYPE_UNKNOWN) {	    if (verbose)	      printf("error: set: no type found for object");	    snmp_free_pdu(pdu);	    

⌨️ 快捷键说明

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