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

📄 helper.cpp

📁 简单网络管理的应用,适用于初学snmp的人。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*++

Copyright (c) Microsoft Corporation

Module Name: helper.cpp

Abstract:

    helper functions used by wsnmputil.cpp.

--*/ 


#include  <stdio.h>
#include  <string.h>
#include  <stdlib.h>
#include  <winsock2.h>
#include  <winsnmp.h>
#include  <snmp.h>
#include  <mgmtapi.h>

#include  "wsnmputil.h"



//
// abstract: print the usage for wsnmputil
// input  :  none
// output :  none
//
void    Usage( )
{  
    printf("\nusage:  wsnmputil [-v1|-v2] [get|getnext|walk|getbulk|subtree] agent community [non_repeaters max_repetitions] oid [oid ...]\n");
    printf("Examples:\n");
    printf("  wsnmputil trap\n");
    printf("  wsnmputil -v1 get localhost public 1.3.6.1.2.1.1.1.0\n");
    printf("  wsnmputil -v1 getnext localhost public 1.3.6.1.2.1.1.1.0\n");
    printf("  wsnmputil -v1 subtree localhost public 1.3.6.1.2.1.1\n");
    printf("  wsnmputil -v1 set toaster public 1.3.6.1.4.1.12.2.5.0   2\n");
    printf("  wsnmputil -v2 walk localhost public 1\n");
    printf("  wsnmputil -v2 getbulk toaster public 1  2   1.3.6.1.2.1.1.2.0  1.3.6.1.2.1.4.22.1.2  1.3.6.1.2.1.4.22.1.4\n");
}  // end of Usage



//
// abstract: Print the debug message.
// input:    variable
// output:   none
//
void    PrintDbgMessage( LPSTR szFormat, ... )
{  
    va_list argList;
    char    szBuf[ 1024 ];

    va_start ( argList, szFormat );
    vsprintf     ( szBuf, szFormat, argList );
    va_end     ( argList );
    
    printf     ( szBuf );
}  //end of PrintDbgMessage



//
// abstract: convert agentAddress ( "a.b.c.d" format or "text.xyz.com" format )
// input  : text string from the command line
// output : TRUE if name is resolved to IP address, FALSE otherwise. 
//
BOOL ConvertTexttoIp( char *agentAddress )
{  

    DWORD           ipAddr;
    struct hostent *phost = NULL;


    if ( agentAddress == NULL )
        return ( FALSE );

    gVars.agentSockAddr.sin_family = AF_INET;

    if ( (ipAddr = inet_addr( agentAddress )) == INADDR_NONE )
    {  
        if ( (phost = gethostbyname( agentAddress )) == NULL )
        {
            return ( FALSE );
        }
        else
            memcpy( (char *)&gVars.agentSockAddr.sin_addr, (char *)phost->h_addr, sizeof(unsigned long) );
    } 
    else
        memcpy( (char *)&gVars.agentSockAddr.sin_addr, (char *)&ipAddr, sizeof(unsigned long) );

    return (TRUE);

} // end of ConvertTexttoIp



//
// abstract: Parse the command line parameters and fill up the pSession structure.
// input parameters:
// argc   number of command line parameters
// argv   list of all the command line parameters.
//
// output parameters:
// TRUE   Command line parsing is successful.
// FALSE  Command line parsing failed, print the usage.
//
BOOL    ParseCommandLine( int argc, char **argv )
{  

    char        localOid[] = "1.3.6.1.2.1";
    
    // should we wait for traps?
    if ( (argc < 5) && (argc != 2) )
    {  
        Usage( );
        return ( FALSE );
    } 
    else if ( (_stricmp( argv[1], "trap")) && (argc < 5) )
    {  
        Usage( );
        return ( FALSE );
    } 

    // init the class
    gVars.oidCount = 0;

    // update the pointers.
    ++argv; 
    --argc;

    if ( !_stricmp( *argv, "trap" ) )
    {  
        gVars.operation = TRAP;        // wait for traps.
        return ( TRUE );
    } 
    else if ( !_stricmp(*argv, "-v2") )
    {  
        gVars.version = TRUE;   // use v2c mode.
    } 
    else if ( !_stricmp(*argv, "-v1") )
    {  
        gVars.version = FALSE;   // use v1 mode.
    } 

    // update the pointers.
    ++argv;
    --argc;

    // check the operation specified.
    if ( !_stricmp( *argv, "get" ) )
    {  
        gVars.operation = GET;        // do a get.
    } 
    else if ( !_stricmp( *argv, "getnext" ) )
    {  
        gVars.operation = GET_NEXT;    // do get next.
    } 
    else if ( !_stricmp( *argv, "walk" ) )
    {          
        gVars.operation = WALK;        // do a walk.
    } 
    else if ( !_stricmp( *argv, "set" ) )
    {  
        gVars.operation = SET;        // do a set.
    } 
    else if ( !_stricmp( *argv, "getbulk" ) )
    {  
        gVars.operation = GET_BULK;    // do get bulk operation.
    } 
    else if ( !_stricmp( *argv, "subtree" ) )
    {          
        gVars.operation = SUB_TREE;    // go through the sub tree.
    } 
    else 
    {  
        Usage( );
        return ( FALSE );
    }     

    // Get agent address...
    argv++;
    argc--;

    gVars.pAgentStrAddr = (LPSTR)SnmpUtilMemAlloc( (int) (strlen(*argv) + 1) );
    if ( gVars.pAgentStrAddr == NULL )
    {  
        PrintDbgMessage( "wsnmputil: Memory allocation failed ..\n" );
        return ( FALSE );
    } 

    strcpy( gVars.pAgentStrAddr, *argv );
    
    // convert the string to IP address.
    if ( ConvertTexttoIp(*argv) == FALSE )
    {  
        PrintDbgMessage( "Invalid SNMP agent ..\n" );
        return ( FALSE );
    } 

    // Get agent community...
    argv++;
    argc--;

    gVars.pAgentCommunity = (LPSTR)SnmpUtilMemAlloc( (int) (strlen( *argv ) + 1 ));
    if ( gVars.pAgentCommunity == NULL )
    {  
        PrintDbgMessage( "wsnmputil: Memory allocation failed ..\n" );
        return ( FALSE );
    } 

    strcpy( gVars.pAgentCommunity, *argv );

    //   If it is a GetBulkRequest
    //   Get non_repeaters field and max_repetitions field for
    //   SNMP_PDU_GETBULK PDU.

    if (gVars.operation == GET_BULK)
    {  
        argv++;
        argc--;
        if (argc)
            gVars.non_repeaters = atol(*argv);
        else
            return FALSE;
        argv++;
        argc--;
        if (argc)
            gVars.max_repetitions = atol(*argv);
        else
            return FALSE;
    }

    // ensure that the command line arguments are less than CMD_LINE_ARGS
    while( ( --argc ) && ( gVars.oidCount < CMD_LINE_ARGS ) )
    {  
        argv++;

        if ( ( ( gVars.operation == WALK  ) || ( gVars.operation == SUB_TREE ) ) 
             && ( strlen( *argv ) <= 3 ) )
        {  
            
            if ( ( *argv[0] ) == '.' ) 
            {  
                // trying to preserve old snmputil behavior.
                // if the user wants to browse a tree starting from .1
                // or if the user wants to browse the tree from .2

                gVars.pszOid[ gVars.oidCount ] = (LPSTR)SnmpUtilMemAlloc( (int)(strlen( localOid )) + (int) (strlen( *argv ) + 1 ));
                if ( gVars.pszOid[ gVars.oidCount ] == NULL )
                {  
                    PrintDbgMessage( "wsnmputil: Memory allocation failed ..\n" );
                    return ( FALSE );
                } 
                strcpy( gVars.pszOid[ gVars.oidCount ], localOid );
                strcat( gVars.pszOid[ gVars.oidCount ], *argv );
            } 
            else if ( isdigit( *argv[0] ) != 0 ) 
            {  
                // if the user wants to browse the tree from 1 or 2

                gVars.pszOid[ gVars.oidCount ] = (LPSTR)SnmpUtilMemAlloc( (int)(strlen( localOid )) + (int) (strlen( *argv ) + 2 ));
                if ( gVars.pszOid[ gVars.oidCount ] == NULL )
                {  
                    PrintDbgMessage( "wsnmputil: Memory allocation failed ..\n" );
                    return ( FALSE );
                } 
                strcpy( gVars.pszOid[ gVars.oidCount ], localOid );
                strcat( gVars.pszOid[ gVars.oidCount ], "." );
                strcat( gVars.pszOid[ gVars.oidCount ], *argv );
            } 
            else 
            {  
                gVars.pszOid[ gVars.oidCount ] = (LPSTR)SnmpUtilMemAlloc( (int)(strlen( *argv ) + 1 ));
                if ( gVars.pszOid[ gVars.oidCount ] == NULL )
                {  
                    PrintDbgMessage( "wsnmputil: Memory allocation failed ..\n" );
                    return ( FALSE );
                } 
                strcpy( gVars.pszOid[ gVars.oidCount ], *argv );
            } 

            ++gVars.oidCount;
            // do a walk or SUB_TREE on one oid.
            break;
        } 
        else if ( gVars.operation == SET )
        {  
            if ( gVars.pszOid[ gVars.oidCount ] == NULL )
            {  
                gVars.pszOid[ gVars.oidCount ] = ( LPSTR )SnmpUtilMemAlloc( (int) (strlen( ( *argv ) ) + 1 ));
                if ( gVars.pszOid[ gVars.oidCount ] == NULL )
                {  
                    PrintDbgMessage( "wsnmputil: Memory allocation failed ..\n" );
                    return ( FALSE );
                } 
                strcpy( gVars.pszOid[ gVars.oidCount ], *argv );
            } 
            else
            {  
                gVars.pSetValue = ( LPSTR )SnmpUtilMemAlloc( (int)(strlen( *argv ) + 1 ));
                if ( gVars.pszOid[ gVars.oidCount ] == NULL )
                {  
                    PrintDbgMessage( "wsnmputil: Memory allocation failed ..\n" );
                    return ( FALSE );
                } 
                strcpy( gVars.pSetValue, *argv );
                break; // we only do a SET for 1 pair of (name, value) varbinding
            } 
            
        } 
        else 
        {  
            gVars.pszOid[ gVars.oidCount ] = (LPSTR)SnmpUtilMemAlloc( (int)(strlen( *argv ) + 1 ));
            if ( gVars.pszOid[ gVars.oidCount ] == NULL )
            {  
                PrintDbgMessage( "wsnmputil: Memory allocation failed ..\n" );
                return ( FALSE );
            } 
            strcpy( gVars.pszOid[ gVars.oidCount ], *argv );
            ++gVars.oidCount;
        } 

    } 

    if (gVars.operation == GET_BULK)
    {  
        //check if user has entered valid arguments for GET_BULK operation
        if (gVars.non_repeaters < 0 || gVars.max_repetitions < 0 )
            return (FALSE);
        if (gVars.oidCount < gVars.non_repeaters)
            return (FALSE);
    } 

    return ( TRUE );
}  // end of ParseCommandLine



//
// abstract: convert the passes string to smiVALUE
// input  :  a pointer to smiValue
// output :  none
// 
void ConvertStringToSmiValue( smiVALUE *pValue )
{  

    int     i;
    DWORD   ipAddr;
    BYTE    *pBytePtr;

    switch( pValue->syntax )
    {  

        case SNMP_SYNTAX_INT:
            pValue->value.sNumber = atol( gVars.pSetValue );
            break;

        case SNMP_SYNTAX_UINT32:
        case SNMP_SYNTAX_CNTR32:
        case SNMP_SYNTAX_GAUGE32:
        case SNMP_SYNTAX_TIMETICKS:
            pValue->value.uNumber = atol( gVars.pSetValue );
            break;
 
        case SNMP_SYNTAX_CNTR64:
            pValue->value.hNumber.hipart = ( smiUINT32 )( _atoi64( gVars.pSetValue ) >> 32);
            pValue->value.hNumber.lopart = (smiUINT32)( _atoi64( gVars.pSetValue ) & 0xFFFFFFFF);
            break;

        case SNMP_SYNTAX_OCTETS:
        case SNMP_SYNTAX_BITS:
        case SNMP_SYNTAX_OPAQUE:
            pValue->value.string.len = (int) strlen( gVars.pSetValue );
            pValue->value.string.ptr = ( smiBYTE * )SnmpUtilMemAlloc( pValue->value.string.len * sizeof( smiBYTE ) );

            if ( pValue->value.string.ptr )
            {  

                memcpy( pValue->value.string.ptr, 
                        gVars.pSetValue,
                        pValue->value.string.len * sizeof( smiBYTE ) 
                      ); 
            } 
            break;
        case SNMP_SYNTAX_NSAPADDR:
            break;
        case SNMP_SYNTAX_IPADDR:

            pValue->value.string.len = 4;        // len of IPAddress field.
            pValue->value.string.ptr = ( smiBYTE * )SnmpUtilMemAlloc( pValue->value.string.len * sizeof( smiBYTE ) );

            ipAddr = inet_addr( gVars.pSetValue );
            
            pBytePtr = ( BYTE * )&ipAddr;
            
            for( i = 0; i < 4; i++ )
            {  
                pValue->value.string.ptr[ i ] = *pBytePtr++;

⌨️ 快捷键说明

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