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

📄 example.c

📁 非常不错的网管开发包
💻 C
📖 第 1 页 / 共 2 页
字号:
     *     * This routine 'header_simple_table' does the same thing for "simple"     *  tables. (See the AGENT.txt file for the definition of a simple table).     *     * Both these utility routines also set up default values for the     *  return arguments (assuming the check succeeded).     * The name and length are set suitably for the current object,     *  var_len assumes that the result is an integer of some form,     *  and write_method assumes that the object cannot be set.     *     * If these assumptions are correct, this callback routine simply     * needs to return a pointer to the appropriate value (using 'long_ret').     * Otherwise, 'var_len' and/or 'write_method' should be set suitably.     */  if (header_generic(vp, name, length, exact, var_len, write_method) == MATCH_FAILED)    return NULL;    /*     * Many object will need to obtain data from the operating system in     *  order to return the appropriate value.  Typically, this is done     *  here - immediately following the 'header' call, and before the     *  switch statement. This is particularly appropriate if a single      *  interface call can return data for all the objects supported.     *     * This example module does not rely on external data, so no such     *  calls are needed in this case.       */    /*     * Now use the magic number from the variable pointer 'vp' to     *  select the particular object being queried.     * In each case, one of the static objects is set up with the     *  appropriate information, and returned mapped to a 'u_char *'     */  switch (vp->magic){    case EXAMPLESTRING:      sprintf(string, example_str);		/*		 * Note that the assumption that the answer will be an		 *  integer does not hold true in this case, so the length		 *  of the answer needs to be set explicitly.				 */      *var_len = strlen(string);      return (u_char *) string;    case EXAMPLEINTEGER:		/*		 * Here the length assumption is correct, but the		 *  object is writeable, so we need to set the		 *  write_method pointer as well as the current value.		 */      long_ret = example_int;      *write_method = write_exampleint;      return (u_char *) &long_ret;          case EXAMPLEOBJECTID:      oid_ret[0] = 1;      oid_ret[1] = 3;      oid_ret[2] = 6;      oid_ret[3] = 1;      oid_ret[4] = 4;      oid_ret[5] = oid_ret[6] = oid_ret[7] = 42;		/*		 * Again, the assumption regarding the answer length is wrong.		 */      *var_len = 8*sizeof(oid);      return (u_char *) oid_ret;          case EXAMPLETIMETICKS:		/*		 * Here both assumptions are correct,		 *  so we just need to set up the answer.		 */      long_ret = 363136200;	/* 42 days, 42 minutes and 42.0 seconds */      return (u_char *) &long_ret;          case EXAMPLEIPADDRESS:      /* ipaddresses get returned as a long.  ick */      /* we're returning 127.0.0.1 */      long_ret = ((127 << (8*3)) + (0 << (8*2)) + (0 << (8*1)) + 1);      return (u_char *) &long_ret;          case EXAMPLECOUNTER:      long_ret = 42;      return (u_char *) &long_ret;          case EXAMPLEGAUGE:      long_ret = 42;	/* Do we detect a theme running through these answers? */      return (u_char *) &long_ret;    case EXAMPLETRIGGERTRAP:		/*		 * This object is essentially "write-only".		 * It only exists to trigger the sending of a trap.		 * Reading it will always return 0.		 */      long_ret = 0;      *write_method = write_exampletrap;      return (u_char *) &long_ret;          default:		/*		 *  This will only be triggered if there's a problem with		 *   the coding of the module.  SNMP requests that reference		 *   a non-existant OID will be directed elsewhere.		 *  If this branch is reached, log an error, so that		 *   the problem can be investigated.		 */      DEBUGMSGTL(("snmpd", "unknown sub-id %d in examples/var_example\n", vp->magic));  }  /*   * If we fall through to here, fail by returning NULL.   * This is essentially a continuation of the 'default' case above.   */  return NULL;}	/*********************	 *	 *  Writeable object SET handling routines	 *	 *********************/intwrite_exampleint(int	action,		 u_char	*var_val,		 u_char	var_val_type,		 size_t	var_val_len,		 u_char	*statP,		 oid	*name,		 size_t	name_len){		/* Define an arbitrary maximum permissible value */#define MAX_EXAMPLE_INT	100    static long intval;    static long old_intval;    switch ( action ) {	case RESERVE1:			/*			 *  Check that the value being set is acceptable			 */	    if (var_val_type != ASN_INTEGER ) {		DEBUGMSGTL(("example", "%x not integer type", var_val_type));		return SNMP_ERR_WRONGTYPE;	    }	    if (var_val_len > sizeof(long)) {		DEBUGMSGTL(("example", "wrong length %x", var_val_len));		return SNMP_ERR_WRONGLENGTH;	    }	    intval = *((long *) var_val);	    if (intval > MAX_EXAMPLE_INT) {		DEBUGMSGTL(("example", "wrong value %x", intval));		return SNMP_ERR_WRONGVALUE;	    }	    break;	case RESERVE2:			/*			 *  This is conventially where any necesary			 *   resources are allocated (e.g. calls to malloc)			 *  Here, we are using static variables			 *   so don't need to worry about this.			 */	    break;	case FREE:			/*			 *  This is where any of the above resources			 *   are freed again (because one of the other			 *   values being SET failed for some reason).			 *  Again, since we are using static variables			 *   we don't need to worry about this either.			 */	    break;	case ACTION:			/*			 *  Set the variable as requested.			 *   Note that this may need to be reversed,			 *   so save any information needed to do this.			 */	    old_intval	= example_int;	    example_int	= intval;	    break;	case UNDO:			/*			 *  Something failed, so re-set the			 *   variable to its previous value			 *  (and free any allocated resources).			 */	    example_int	= old_intval;	    break;	case COMMIT:			/*			 *  Everything worked, so we can discard any			 *   saved information, and make the change			 *   permanent (e.g. write to the config file).			 *  We also free any allocated resources.			 *			 *  In this case, there's nothing to do.			 */	    break;    }    return SNMP_ERR_NOERROR;}intwrite_exampletrap(int	action,		 u_char	*var_val,		 u_char	var_val_type,		 size_t	var_val_len,		 u_char	*statP,		 oid	*name,		 size_t	name_len){    long intval;    switch ( action ) {	case RESERVE1:			/*			 *  The only acceptable value is the integer 1			 */	    if (var_val_type != ASN_INTEGER ) {		DEBUGMSGTL(("example", "%x not integer type", var_val_type));		return SNMP_ERR_WRONGTYPE;	    }	    if (var_val_len > sizeof(long)) {		DEBUGMSGTL(("example", "wrong length %x", var_val_len));		return SNMP_ERR_WRONGLENGTH;	    }	    intval = *((long *) var_val);	    if  ( intval != 1 ) {		DEBUGMSGTL(("example", "wrong value %x", intval));		return SNMP_ERR_WRONGVALUE;	    }	    break;	case RESERVE2:			/* No resources are required.... */	    break;	case FREE:			/* ... so no resources need be freed */	    break;	case ACTION:			/*			 *  Having triggered the sending of a trap,			 *   it would be impossible to revoke this,			 *   so we can't actually invoke the action here.			 */	    break;	case UNDO:			/*  We haven't done anything yet,				so there's nothing to undo */	    break;	case COMMIT:			/*			 *  Everything else worked, so it's now safe			 *   to trigger the trap.			 *  Note that this is *only* acceptable since			 *   the trap sending routines are "failsafe".			 *  (In fact, they can fail, but they return no			 *   indication of this, which is the next best thing!)			 */	    send_easy_trap( SNMP_TRAP_ENTERPRISESPECIFIC, 3 );	    break;    }    return SNMP_ERR_NOERROR;}

⌨️ 快捷键说明

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