Annotation of mstools/samples/snmp/testdll/testdll.c, revision 1.1

1.1     ! root        1: /*++ BUILD Version: 0001    // Increment this if a change has global effects
        !             2: 
        !             3: Copyright (c) 1991  Microsoft Corporation
        !             4: 
        !             5: Module Name:
        !             6: 
        !             7:     testdll.c
        !             8: 
        !             9: Abstract:
        !            10: 
        !            11:     Sample SNMP Extension Agent for Windows NT.
        !            12: 
        !            13:     These files (testdll.c, testmib.c, and testmib.h) provide an example of 
        !            14:     how to structure an Extension Agent DLL which works in conjunction with 
        !            15:     the SNMP Extendible Agent for Windows NT.
        !            16: 
        !            17:     Extensive comments have been included to describe its structure and
        !            18:     operation.  See also "Microsoft Windows/NT SNMP Programmer's Reference".
        !            19: 
        !            20: Created:
        !            21: 
        !            22:     28-Jun-1991
        !            23: 
        !            24: Revision History:
        !            25: 
        !            26: --*/
        !            27: 
        !            28: 
        !            29: static char *vcsid = "@(#) $Logfile:   N:/xtest/vcs/testdll.c_v  $ $Revision:   1.6  $";
        !            30: 
        !            31: 
        !            32: // General notes:
        !            33: //
        !            34: //   Microsoft's Extendible Agent for Windows NT is implemented by dynamically
        !            35: // linking to Extension Agent DLLs that implement portions of the MIB.  These
        !            36: // Extension Agents are configured in the Windows NT Registration Database.
        !            37: // When the Extendible Agent Service is started, it queries the registry to
        !            38: // determine which Extension Agent DLLs have been installed and need to be
        !            39: // loaded and initialized.  The Extendible Agent invokes various DLL entry
        !            40: // points (examples follow in this file) to request MIB queries and obtain
        !            41: // Extension Agent generated traps.
        !            42: 
        !            43: 
        !            44: // Necessary includes.
        !            45: 
        !            46: #include <windows.h>
        !            47: #include <malloc.h>
        !            48: 
        !            49: #include <snmp.h>
        !            50: 
        !            51: 
        !            52: // Contains definitions for the table structure describing the MIB.  This
        !            53: // is used in conjunction with testmib.c where the MIB requests are resolved.
        !            54: 
        !            55: #include "testmib.h"
        !            56: 
        !            57: 
        !            58: // Extension Agent DLLs need access to elapsed time agent has been active.
        !            59: // This is implemented by initializing the Extension Agent with a time zero
        !            60: // reference, and allowing the agent to compute elapsed time by subtracting
        !            61: // the time zero reference from the current system time.  This example
        !            62: // Extension Agent implements this reference with dwTimeZero.
        !            63: 
        !            64: DWORD dwTimeZero = 0;
        !            65: 
        !            66: 
        !            67: // Extension Agent DLLs that generate traps must create a Win32 Event object
        !            68: // to communicate occurence of traps to the Extendible Agent.  The event
        !            69: // handle is given to the Extendible Agent when the Extension Agent is 
        !            70: // initialized, it should be NULL if traps will not be generated.  This
        !            71: // example Extension Agent simulates the occurance of traps with hSimulateTrap.
        !            72: 
        !            73: HANDLE hSimulateTrap = NULL;
        !            74: 
        !            75: 
        !            76: // This is a standard Win32 DLL entry point.  See the Win32 SDK for more
        !            77: // information on its arguments and their meanings.  This example DLL does 
        !            78: // not perform any special actions using this mechanism.
        !            79: 
        !            80: BOOL WINAPI DllMain(
        !            81:     HANDLE hDll,
        !            82:     DWORD  dwReason,
        !            83:     LPVOID lpReserved)
        !            84:     {
        !            85:     switch(dwReason)
        !            86:         {
        !            87:         case DLL_PROCESS_ATTACH:
        !            88:         case DLL_PROCESS_DETACH:
        !            89:         case DLL_THREAD_ATTACH:
        !            90:         case DLL_THREAD_DETACH:
        !            91:         default:
        !            92:             break;
        !            93: 
        !            94:         } // end switch()
        !            95: 
        !            96:     return TRUE;
        !            97: 
        !            98:     } // end DllEntryPoint()
        !            99: 
        !           100: 
        !           101: // Extension Agent DLLs provide the following entry point to coordinate the
        !           102: // initializations of the Extension Agent and the Extendible Agent.  The
        !           103: // Extendible Agent provides the Extension Agent with a time zero reference;
        !           104: // and the Extension Agent provides the Extendible Agent with an Event handle 
        !           105: // for communicating occurence of traps, and an object identifier representing
        !           106: // the root of the MIB subtree that the Extension Agent supports.
        !           107: 
        !           108: BOOL SnmpExtensionInit(
        !           109:     IN  DWORD               dwTimeZeroReference,
        !           110:     OUT HANDLE              *hPollForTrapEvent,
        !           111:     OUT AsnObjectIdentifier *supportedView)
        !           112:     {
        !           113: 
        !           114:     // Record the time reference provided by the Extendible Agent.
        !           115: 
        !           116:     dwTimeZero = dwTimeZeroReference;
        !           117: 
        !           118: 
        !           119:     // Create an Event that will be used to communicate the occurence of traps
        !           120:     // to the Extendible Agent.  The Extension Agent will assert this Event
        !           121:     // when a trap has occured.  This is explained further later in this file.
        !           122: 
        !           123:     if ((*hPollForTrapEvent = CreateEvent(NULL, FALSE, FALSE, NULL)) == NULL)
        !           124:         {
        !           125:         // Indicate error?, be sure that NULL is returned to Extendible Agent.
        !           126:         }
        !           127: 
        !           128: 
        !           129:     // Indicate the MIB view supported by this Extension Agent, an object
        !           130:     // identifier representing the sub root of the MIB that is supported.
        !           131: 
        !           132:     *supportedView = MIB_OidPrefix; // NOTE!  structure copy
        !           133: 
        !           134: 
        !           135:     // Record the trap Event.  This example Extension Agent simulates traps by 
        !           136:     // generating a trap after every given number of processed requests.
        !           137: 
        !           138:     hSimulateTrap = *hPollForTrapEvent;
        !           139: 
        !           140: 
        !           141:     // Indicate that Extension Agent initialization was sucessfull.
        !           142: 
        !           143:     return TRUE;
        !           144: 
        !           145:     } // end SnmpExtensionInit()
        !           146: 
        !           147: 
        !           148: // Extension Agent DLLs provide the following entry point to communcate traps
        !           149: // to the Extendible Agent.  The Extendible Agent will query this entry point
        !           150: // when the trap Event (supplied at initialization time) is asserted, which
        !           151: // indicates that zero or more traps may have occured.  The Extendible Agent 
        !           152: // will repetedly call this entry point until FALSE is returned, indicating 
        !           153: // that all outstanding traps have been processed.
        !           154: 
        !           155: BOOL SnmpExtensionTrap(
        !           156:     OUT AsnObjectIdentifier *enterprise,
        !           157:     OUT AsnInteger          *genericTrap,
        !           158:     OUT AsnInteger          *specificTrap,
        !           159:     OUT AsnTimeticks        *timeStamp,
        !           160:     OUT RFC1157VarBindList  *variableBindings)
        !           161:     {
        !           162:     // The body of this routine is an extremely simple example/simulation of
        !           163:     // the trap functionality.  A real implementation will be more complex.
        !           164: 
        !           165: 
        !           166:     // The following define data inserted into the trap below.  The Lan Manager
        !           167:     // bytesAvailAlert from the Lan Manager Alerts-2 MIB is generated with an
        !           168:     // empty variable bindings list.
        !           169: 
        !           170:     static UINT OidList[]  = { 1, 3, 6, 1, 4, 1, 77, 2 };
        !           171:     static UINT OidListLen = 8;
        !           172: 
        !           173: 
        !           174:     // The following variable is used for the simulation, it allows a single
        !           175:     // trap to be generated and then causes FALSE to be returned indicating
        !           176:     // no more traps.
        !           177: 
        !           178:     static whichTime = 0;
        !           179: 
        !           180: 
        !           181:     // The following if/else support the simulation.
        !           182: 
        !           183:     if (whichTime == 0)
        !           184:         {
        !           185:         whichTime = 1;    // Supports the simulation.
        !           186: 
        !           187: 
        !           188:         // Communicate the trap data to the Extendible Agent.
        !           189: 
        !           190:         enterprise->idLength = OidListLen;
        !           191:         enterprise->ids = (UINT *)malloc(sizeof(UINT) * OidListLen);
        !           192:         memcpy(enterprise->ids, OidList, sizeof(UINT) * OidListLen);
        !           193: 
        !           194:         *genericTrap      = SNMP_GENERICTRAP_ENTERSPECIFIC;
        !           195: 
        !           196:         *specificTrap     = 1;                    // the bytesAvailAlert trap
        !           197: 
        !           198:         *timeStamp        = GetCurrentTime() - dwTimeZero;
        !           199: 
        !           200:         variableBindings->list = NULL;
        !           201:         variableBindings->len  = 0;
        !           202: 
        !           203: 
        !           204:         // Indicate that valid trap data exists in the parameters.
        !           205: 
        !           206:         return TRUE;
        !           207:         }
        !           208:     else
        !           209:         {
        !           210:         whichTime = 0;    // Supports the simulation.
        !           211: 
        !           212: 
        !           213:         // Indicate that no more traps are available and parameters do not
        !           214:         // refer to any valid data.
        !           215: 
        !           216:         return FALSE;
        !           217:         }
        !           218: 
        !           219:     } // end SnmpExtensionTrap()
        !           220: 
        !           221: 
        !           222: // Extension Agent DLLs provide the following entry point to resolve queries
        !           223: // for MIB variables in their supported MIB view (supplied at initialization
        !           224: // time).  The requestType is Get/GetNext/Set.
        !           225: 
        !           226: BOOL SnmpExtensionQuery(
        !           227:     IN BYTE                   requestType,
        !           228:     IN OUT RFC1157VarBindList *variableBindings,
        !           229:     OUT AsnInteger            *errorStatus,
        !           230:     OUT AsnInteger            *errorIndex)
        !           231:     {
        !           232:     static unsigned long requestCount = 0;  // Supports the trap simulation.
        !           233:     UINT    I;
        !           234: 
        !           235: 
        !           236:     // Iterate through the variable bindings list to resolve individual
        !           237:     // variable bindings.
        !           238: 
        !           239:     for ( I=0;I < variableBindings->len;I++ )
        !           240:         {
        !           241:         *errorStatus = ResolveVarBind( &variableBindings->list[I],
        !           242:                                        requestType );
        !           243: 
        !           244: 
        !           245:         // Test and handle case where Get Next past end of MIB view supported
        !           246:         // by this Extension Agent occurs.  Special processing is required to 
        !           247:         // communicate this situation to the Extendible Agent so it can take 
        !           248:         // appropriate action, possibly querying other Extension Agents.
        !           249: 
        !           250:         if ( *errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME &&
        !           251:              requestType == MIB_ACTION_GETNEXT )
        !           252:            {
        !           253:            *errorStatus = SNMP_ERRORSTATUS_NOERROR;
        !           254: 
        !           255: 
        !           256:            // Modify variable binding of such variables so the OID points
        !           257:            // just outside the MIB view supported by this Extension Agent.
        !           258:            // The Extendible Agent tests for this, and takes appropriate
        !           259:            // action.
        !           260: 
        !           261:            SNMP_oidfree( &variableBindings->list[I].name );
        !           262:            SNMP_oidcpy( &variableBindings->list[I].name, &MIB_OidPrefix );
        !           263:            variableBindings->list[I].name.ids[MIB_PREFIX_LEN-1] ++;
        !           264:            }
        !           265: 
        !           266: 
        !           267:         // If an error was indicated, communicate error status and error
        !           268:         // index to the Extendible Agent.  The Extendible Agent will ensure
        !           269:         // that the origional variable bindings are returned in the response
        !           270:         // packet.
        !           271: 
        !           272:         if ( *errorStatus != SNMP_ERRORSTATUS_NOERROR )
        !           273:            {
        !           274:           *errorIndex = I+1;
        !           275:           goto Exit;
        !           276:           }
        !           277:         }
        !           278: 
        !           279: Exit:
        !           280: 
        !           281: 
        !           282:     // Supports the trap simulation.
        !           283: 
        !           284:     if (++requestCount % 3 == 0 && hSimulateTrap != NULL)
        !           285:         SetEvent(hSimulateTrap);
        !           286: 
        !           287: 
        !           288:     // Indicate that Extension Agent processing was sucessfull.
        !           289: 
        !           290:     return SNMPAPI_NOERROR;
        !           291: 
        !           292:     } // end SnmpExtensionQuery()
        !           293: 
        !           294: 
        !           295: //-------------------------------- END --------------------------------------
        !           296: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.