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

📄 example.c

📁 ucd-snmp源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
      *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 = ntohl(INADDR_LOOPBACK);      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;      case EXAMPLETRIGGERTRAP2:		/*		 * This object is essentially "write-only".		 * It only exists to trigger the sending of a v2 trap.		 * Reading it will always return 0.		 */      long_ret = 0;      *write_method = write_exampletrap2;      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;    DEBUGMSGTL(("example","write_exampletrap entered: action=%d\n",action));    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!)			 */            DEBUGMSGTL(("example","write_exampletrap sending the trap\n",                        action));	    send_easy_trap( SNMP_TRAP_ENTERPRISESPECIFIC, 99 );            DEBUGMSGTL(("example","write_exampletrap trap sent\n",action));	    break;    }    return SNMP_ERR_NOERROR;}/* this documents how to send a SNMPv2 (and higher) trap via the   send_v2trap() API.   Coding SNMP-v2 Trap:   The SNMPv2-Trap PDU contains at least a pair of object names and   values: - sysUpTime.0 whose value is the time in hundredths of a   second since the netwok management portion of system was last   reinitialized.  - snmpTrapOID.0 which is part of the trap group SNMPv2   MIB whose value is the object-id of the specific trap you have defined   in your own MIB.  Other variables can be added to caracterize the   trap.   The function send_v2trap adds automaticallys the two objects but the   value of snmpTrapOID.0 is 0.0 by default. If you want to add your trap   name, you have to reconstruct this object and to add your own   variable.*/   intwrite_exampletrap2(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;    /* these variales will be used when we send the trap */    oid objid_snmptrap[] = { 1,3,6,1,6,3,1,1,4,1,0}; /* snmpTrapOID.0 */    oid demo_trap[] = { 1,3,6,1,4,1,2021,13,990}; /*demo-trap */    oid example_string_oid[] = { 1,3,6,1,4,1,2021,254,1,0 };    static struct variable_list var_trap;    static struct variable_list var_obj;        DEBUGMSGTL(("example","write_exampletrap2 entered: action=%d\n",action));    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!)			 *//* trap definition objects */            var_trap.next_variable = &var_obj; /* next variable */            var_trap.name = objid_snmptrap; /* snmpTrapOID.0 */            var_trap.name_length = sizeof(objid_snmptrap)/sizeof(oid); /* number of sub-ids */            var_trap.type = ASN_OBJECT_ID;            var_trap.val.objid = demo_trap; /* demo-trap objid */            var_trap.val_len = sizeof(demo_trap); /* length in bytes (not number of subids!) *//* additional objects */            var_obj.next_variable = NULL; /* No more variables after this one */            var_obj.name = example_string_oid;            var_obj.name_length = sizeof(example_string_oid)/sizeof(oid); /* number of sub-ids */            var_obj.type = ASN_OCTET_STR; /* type of variable */            var_obj.val.string = example_str; /* value */            var_obj.val_len = strlen(example_str);            DEBUGMSGTL(("example","write_exampletrap2 sending the v2 trap\n",                        action));            send_v2trap(&var_trap);            DEBUGMSGTL(("example","write_exampletrap2 v2 trap sent\n",action));	    break;    }    return SNMP_ERR_NOERROR;}

⌨️ 快捷键说明

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