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

📄 v_vxdemo.c

📁 vxworks demo 有问题写信给我
💻 C
📖 第 1 页 / 共 4 页
字号:
/* v_vxdemo.c - VxWorks Demo MIB interface to SNMP Agent *//* Copyright 1984-1993 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01a,12oct93,jag  written*//*DESCRIPTIONThis module contains the necessary routines to interface the SNMP agent withthe WRS Demo MIB extension definition.  The MIB is defined in concise MIB format in the file vxdemo.my.  This file is the input to the MIB compiler which generates the following:  The data structures that need to be addedto the SNMP agent to support the new MIB extension; The necessary header files; The functions headers (or stubs optional) that need be written to support theMIB extension.The software that needs to be written to support the MIB extension is dividedin two layers.  The first layer is called "v_" and the second layer is called"k_".  The corresponding layers in the vxWorks demo MIB are v_vxdemo.c and k_vxdemo.c.  The functionality of each layer is described in the correspondingmodule.The SNMP agent is responsible for the communication protocol, proper encapsulation of the MIB variables, validation of OIDs and the invokation ofthe "v_" routines.  The "v_" layer validates the value and operation of theMIB variable and calls the respective "k_" routine.  The "k_" routine does theactual operation on the variable.The groups in the vxWorks Demo MIB are sysconfig, systasks, sysmemory and sysnfs.  Each have a set of "v_" and "k_" routines that support the group.  On a per group and tables within a group the following "v_" routines need to be provided.groupID_get (): Validate the request and retrive the requested variable from the k_ routine.groupID_test():Validate the request and the value to which a variable or table entry is to beset.  This routine dynamically allocates a structure used to save variablesvalues.  After all the related variables have been collected this routine setsthe state of the allocated structure to ADD_MODIFY or DELETE. The state of thisstructure informs the agent that the groupID_set() routine should be invoked.groupID_cleanup():This routine is invoked after the goupID_set routine to free any resourcesthat have allocated or locked in groupID_test.groupID_undo():This routine stub is only provided for compatability in this release.groupID_set():This routine is invoked to set the MIB variable(s).  The "k_" set routineis invoked by this routine.*//* includes */#include <vxWorks.h>#include <taskLib.h>#include <stdio.h>#include <string.h>#include "snmpd.h"#include "snmp.h"#include "diag.h"/********************************************************************************* sysconfig_get -  Read the value of a variable in the sysconfig group.** This routine returns the value of the requested MIB variable that corresponds* to its group.   All the values from the group are obtain from the "k_"* logic but only the requested value is returned by this function.** RETURNS: A pointer to a VarBind struct, or NULL if the request is invalid.** SEE ALSO:*/VarBind * sysconfig_get     (    OID         * incoming,     /* Ptr to the OID received in the SNMP PDU */    ObjectInfo  * object,       /* Ptr to MIB var in the SNMP MIB tree */    int           searchType,   /* EXACT = GetRequest,NEXT = Get[Next|Bulk] */    ContextInfo * contextInfo,  /* Reserved for future used */    int           serialNum     /* Used for caching */    )    {    static sysconfig_t  		 * sysVars;     /* Ptr to struct returned by the k_ routine */    static int     lastSerialNum;    void         * dp;          /* Generic ptr used for returned variable */    int            reqVar = -1; /* Variable Requested by the Agent */    int            instLength;  /* Length of the incoming OID */    static OID   * last_incoming;    if ((lastSerialNum != serialNum) || (sysVars == NULL) ||	(CmpOIDInst(incoming, last_incoming, object->oid.length) != 0))	{    /*      * instLength represents the length of the (incoming) requested OID     * beyond the Object Identifier specified in the MIB tree.  This value     * is used to validate the request on an EXACT or NEXT search.  Ex.     * The MIB variable fakeMibVar = OID 1.3.6.1.4.1.731.1.1.2 and is      * specified this way in the object variable.  The incoming would     * have fakeMibVar.0 = OID 1.3.6.1.4.1.731.1.1.2.0, the length would     * be 1 and the last value 0.  This information is used to valiate the     * EXACT search.  In the case of a NEXT search the length must be 0     * for leaf variables.  For table entries with an integer index the      * incoming OID would be 1.3.6.1.4.1.731.1.1.2.INDEX and the length     * would represent the index.     */	instLength = incoming->length - object->oid.length;	switch (searchType) 	    {	    case EXACT:		if (instLength == 1 && 		    incoming->oid_ptr [incoming->length - 1] == 0) 		{		reqVar = object->nominator;		}		break;	    case NEXT:		if (instLength <= 0) 		{		reqVar = object->nominator;		}		break;	    default:		DPRINTF ((0, "snmpd: Internal error. (invalid search type in \			  sysconfig_get -- %d)\n", searchType));	    }				/* switch */	/* Retrieve the data from the kernel-specific routine. */	if (reqVar == -1 || 	    ((sysVars = k_sysconfig_get (serialNum, contextInfo, 					 reqVar)) == NULL))	    {	    return ((VarBind *) NULL);	    }	/* Get new catche information */     	lastSerialNum = serialNum;	if (last_incoming != NULL)	    FreeOID(last_incoming);		     	last_incoming = CloneOID(incoming);	}    else	{	reqVar = object->nominator;	}    /*     * Set up the variable requested by the agent.     */    switch (reqVar) 	{	case I_sysState:	    dp = (void *) &sysVars->sysState;	    break;	case I_sysUserName:	    dp = (void *) MakeOctetString (sysVars->sysUserName->octet_ptr,					    sysVars->sysUserName->length);	    break;	case I_sysUserPassw:	    dp = (void *) MakeOctetString (sysVars->sysUserPassw->octet_ptr,					    sysVars->sysUserPassw->length);	    break;	default:	    return ((VarBind *) NULL);	}    /*     *  Construct the response for the SNMP agent.  The structure returned     *  will be strung onto the response PDU.  The global strucutre ZeroOid     *  is used for leaf variables.     */    return (MakeVarBind (object, &ZeroOid, dp));    }/********************************************************************************* system_free - Free the systems data structure.** This routine is invoked indirectly by the SNMP agent to free the system* configuration structure that was allocated during the call to test.  It* gets call after the call to set, or after the test fails.** RETURNS: N/A* * SEE ALSO:*/LOCAL void sysconfig_free     (    sysconfig_t * sysVars    )    {    if (sysVars != NULL) 	{	if (sysVars->sysUserName != NULL) 	    {	    FreeOctetString (sysVars->sysUserName);	    }	if (sysVars->sysUserPassw != NULL) 	    {	    FreeOctetString (sysVars->sysUserPassw);	    }	free ((char *) sysVars);	}    }/********************************************************************************* sysconfig_cleanup -  Free all the resources allocated in sysconfig_test.** This routine is invoked by the SNMP agent after the set operation has taken* placed or if the test routine failed.** RETURNS: NO_ERROR always successful.** SEE ALSO:*/LOCAL int sysconfig_cleanup    (    doList_t *trash    )    {    sysconfig_free (trash->data);    sysconfig_free (trash->undodata);    return NO_ERROR;    }/********************************************************************************* sysconfig_undo -  provided for future use** RETURNS: ERROR** SEE ALSO:*/LOCAL int sysconfig_undo    (    )    {    return UNDO_FAILED_ERROR;    }/********************************************************************************* sysconfig_test -  Validate new values for the MIB variables.** This routine ensures that the value to which a MIB variable is to be set is* in accordance to its MIB definition in the file vxdemo.my.  After all the* variables required to update the an entry or variable are collected, the state* of the doList structure is set to ADD_MODIFY or DELETE, based on the operaion* requested.** RETURNS: NO_ERROR for successful validation, otherwise an error is returned.* * SEE ALSO:*/int sysconfig_test     (    OID           * incoming,    /* Ptr to the OID received in the SNMP PDU */    ObjectInfo    * object,      /* Ptr to MIB var in the SNMP MIB tree */    ObjectSyntax  * value,       /* Ptr the value to set the MIB variable */    doList_t      * doHead,      /* Ptr to the list of SNMP outstanding reqs */    doList_t      * doCur,       /* Ptr to free doList element */    ContextInfo   * contextInfo  /* Reserved for future used. */    )    {    sysconfig_t   * sysVars;    int             instLength = incoming->length - object->oid.length;    /*      * instLength represents the length of the (incoming) requested OID     * beyond the Object Identifier specified in the MIB tree.  This value     * is used to validate the request on an EXACT or NEXT search.  Ex.     * The MIB variable fakeMibVar = OID 1.3.6.1.4.1.731.1.1.2 and is      * specified this way in the object variable.  The incoming would     * have fakeMibVar.0 = OID 1.3.6.1.4.1.731.1.1.2.0, the length would     * be 1 and the last value 0.  This information is used to valiate the     * EXACT search.  In the case of a NEXT search the length must be 0     * for leaf variables.  For table entries with an integer index the      * incoming OID would be 1.3.6.1.4.1.731.1.1.2.INDEX and the length     * would represent the index.  In the case of a set operation the variable     * needs to be specified exactly and therefore the length must be 1.     */    if (instLength != 1 || incoming->oid_ptr [object->oid.length] != 0) 	return (NO_ACCESS_ERROR);    /*     * Create the data portion of the do-list element and initialize it. This     * needs to use "calloc" so all the pointers in this structure are     * initialized to NULL.     */    if ((doCur->data = (void *) calloc (1, sizeof(sysconfig_t))) == NULL) 	{	return (GEN_ERROR);	}    doCur->setMethod = sysconfig_set;    doCur->cleanupMethod = sysconfig_cleanup;    doCur->undoMethod = sysconfig_undo;    doCur->state = UNKNOWN;    sysVars = (sysconfig_t *) (doCur->data);    /*     * Validate the value and store it in the do-list.     */    switch (object->nominator) 	{	case I_sysState:		    /* The system state can only be set to running = 1 or reboot = 2 */	    if (value->sl_value != D_sysState_system_running && 		value->sl_value != D_sysState_system_reboot)		return (WRONG_VALUE_ERROR);	    SET_VALID(I_sysState, sysVars->valid);  /* Set valid vector */	    sysVars->sysState = value->sl_value;    /* Cpy to alloc struct */	    break;	case I_sysUserName:	    if (value->os_value->length <= 0)		return (WRONG_LENGTH_ERROR);	    SET_VALID(I_sysUserName, sysVars->valid);	    sysVars->sysUserName = MakeOctetString (value->os_value->octet_ptr,						value->os_value->length);	    break;	case I_sysUserPassw:	    if (value->os_value->length <= 0) 		return (WRONG_LENGTH_ERROR);	    SET_VALID(I_sysUserPassw, sysVars->valid);	    sysVars->sysUserPassw = MakeOctetString (value->os_value->octet_ptr,						value->os_value->length);	    break;	default:	    DPRINTF((0, "snmpd: Internal error (invalid nominator \		    in sysconfig_test)\n"));	    return (GEN_ERROR);    }				/* switch */    doCur->state = ADD_MODIFY;    return (NO_ERROR);    }/********************************************************************************* sysconfig_set -  Invokes k_ logic to set the variable.** This set routine assumes that the variables to be set are correct.  This* assumption is safe because the test routine has already validated the* variable(s) and set the state of the operation.** RETURNS: Success or Error to the SNMP agent.* * SEE ALSO:*/int sysconfig_set     (    doList_t    * doHead,      /* Ptr to the list of SNMP outstanding reqs */    doList_t    * doCur,       /* Ptr to free doList element */    ContextInfo * contextInfo  /* Reserved for future use */    )    {    return (k_sysconfig_set ((sysconfig_t *) (doCur->data), contextInfo, 			      doCur->state));    }/********************************************************************************* taskEntry_get -  Read the value of a variable in the requested task entry.** This routine returns the value of the requested MIB variable that corresponds* to its group.   All the values from the group are obtain from the "k_"* logic but only the requested value is returned by this function.** RETURNS: A pointer to a VarBind struct, or NULL if the request is invalid.** SEE ALSO:*/VarBind * taskEntry_get     (    OID          * incoming,    /* Ptr to the OID received in the SNMP PDU */    ObjectInfo   * object,      /* Ptr to MIB var in the SNMP MIB tree */    int            searchType,  /* EXACT = GetRequest,NEXT = Get[Next|Bulk] */    ContextInfo  * contextInfo, /* Reserved for future used */    int            serialNum    /* Used for caching */    )    {    static taskEntry_t    		   * pTaskEntry;    void           * dp;         /* Generic ptr used for returned variable */    static OID       inst;	 /* Table entry index to return to agent */    static unsigned long    		     oidbuff;    /* OID buffer for inst variable */    long             taskId;     /* Task ID is used for table index */    int              instLength; /* Length of the incoming OID */

⌨️ 快捷键说明

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