Snmp.cpp
资源名称:NetFinder.rar [点击查看]
上传用户:qiye66692
上传日期:2022-04-25
资源大小:72k
文件大小:7k
源码类别:
SNMP编程
开发平台:
Visual C++
- #include "StdAfx.h"
- #include ".snmp.h"
- #include ".Mib.h"
- #include ".func.h"
- CSnmp::CSnmp( LPCSTR ip, LPCSTR community )
- {
- this->ip = ip;
- this->community.len = strlen(community);
- this->community.ptr = (smiLPBYTE)community;
- m_event.ResetEvent();
- }
- CSnmp::~CSnmp(void)
- {
- }
- bool CSnmp::Prepare()
- {
- smiUINT32 nMajorVersion; // major version number of the WinSNMP API
- smiUINT32 nMinorVersion; // minor version number of the WinSNMP API
- smiUINT32 nLevel; // level of SNMP the implementation supports
- smiUINT32 nTranslateMode; // default entity/context translation mode
- smiUINT32 nRetransmitMode; // default retransmission mode
- //加载snmp
- if( SNMPAPI_FAILURE == SnmpStartup( &nMajorVersion, &nMinorVersion, &nLevel, &nTranslateMode, &nRetransmitMode) )
- //五个参数作为接收参数返回SNMP的主版本号,副版本号,支持最高的操作标准,
- //默认的实体/上下文传输模式,默认的重发机制
- {
- AfxMessageBox("Startup SNMP Failed");
- return FALSE;
- }
- //建立会话
- hSession = SnmpCreateSession( NULL, NULL, CSnmp::Callback, (LPVOID)this );
- if( hSession == SNMPAPI_FAILURE )
- {
- AfxMessageBox("Create Session Failed");
- return FALSE;
- }
- //建立上下文句柄
- hContext = SnmpStrToContext( hSession, &community );
- if( hContext == SNMPAPI_FAILURE )
- {
- AfxMessageBox("Get Context Failed");
- return FALSE;
- }
- return TRUE;
- }
- bool CSnmp::GetTable( CStringArray* string, CRouter* router )
- {
- HSNMP_VBL hVbl;
- smiOID oid_send;
- smiOID oid_recv;
- smiVALUE value;
- int count;
- char buf[100];
- while(TRUE)
- {
- if( Send(string) == FALSE ) //string即请求列表
- {
- router->m_strName = "unknown";
- return FALSE;
- }
- if( Receive(hVbl) == FALSE )
- {
- router->m_strName = "unknown";
- return FALSE;
- }
- //计算返回列表数
- count = SnmpCountVbl( hVbl );
- for( int i = 0; i < count; i ++ )
- {
- //取回返回结果
- if( SNMPAPI_FAILURE == SnmpGetVb( hVbl, i+1, &oid_recv, &value ) )
- {
- AfxMessageBox("Get vb failed");
- return FALSE;
- }
- if( value.syntax == SNMP_SYNTAX_IPADDR ) //32位的internet地址
- {
- CString ip;
- ip.Format("%d.%d.%d.%d", value.value.string.ptr[0], value.value.string.ptr[1], value.value.string.ptr[2], value.value.string.ptr[3] );
- if( string->GetAt(i).Find(ipAdEntAddr) != -1 ) //ip
- {
- router->m_IpArray.Add( ip );
- }
- if( string->GetAt(i).Find(ipAdEntNetMask) != -1 ) //掩码
- {
- router->m_IpMaskArray.Add( ip );
- }
- //路由表中
- if( string->GetAt(i).Find(ipRouteDest) != -1 ) //目的
- {
- if( string->GetAt(i).Left(21) != ipRouteMask )
- router->m_DestArray.Add( ip );
- }
- if( string->GetAt(i).Find(ipRouteNextHop) != -1 ) //下一跳
- {
- router->m_NextHopArray.Add( ip );
- }
- if( string->GetAt(i).Find(ipRouteMask) != -1 ) //掩码
- {
- router->m_NetMaskArray.Add( ip );
- }
- }
- if( value.syntax == SNMP_SYNTAX_OCTETS ) //是串
- {
- if( string->GetAt(i).Find(sysName) != -1 )
- {
- router->m_strName = value.value.string.ptr;
- router->m_strName = router->m_strName.Left(value.value.string.len);
- }
- if( string->GetAt(i).Find(sysDescr) != -1 )
- {
- router->m_strDescr = value.value.string.ptr;
- router->m_strDescr = router->m_strDescr.Left(value.value.string.len);
- }
- }
- if( value.syntax == SNMP_SYNTAX_TIMETICKS )
- {
- int nHours, nMinutes, nSeconds;
- long nUpTime;
- nUpTime = value.value.uNumber / 100;
- nHours = (int)(nUpTime / 3600);
- nMinutes = (int)((nUpTime % 3600) / 60);
- nSeconds = (int)(nUpTime % 60);
- router->m_strUpTime.Format("%dhours, %dminutes, %dseconds", nHours, nMinutes, nSeconds );
- }
- SnmpStrToOid( string->GetAt(i), &oid_send );
- SnmpOidToStr( &oid_recv, 100, buf );
- string->SetAt( i, buf );
- }
- long result;
- SnmpOidCompare( &oid_send, &oid_recv, 10, &result ); //判断格式是否一致,决定是否继续循环
- if( result != 0 )
- return TRUE;
- }
- SnmpFreeVbl( hVbl );
- return TRUE;
- }
- bool CSnmp::Send( CStringArray* string )
- {
- HSNMP_VBL hVbl;
- HSNMP_PDU hPdu;
- HSNMP_ENTITY hSrcEntity, hDestEntity;
- smiOID oid;
- //创建变量捆绑列表
- hVbl = SnmpCreateVbl( hSession, NULL, NULL );
- if( hVbl == SNMPAPI_FAILURE )
- {
- return FALSE;
- }
- //for循环把请求列表reqList中的项都绑定好
- for( int i = 0; i < string->GetSize(); i ++ )
- {
- //converts the dotted numeric string format of an SNMP object identifierto its internal binary representation.
- SnmpStrToOid( string->GetAt(i), &oid );
- //appends new variable binding entries to an existing variable bindings list.
- SnmpSetVb(hVbl,0,&oid,NULL);
- }
- //按照特定pdu格式发送,该程序返回一个pdu句柄
- hPdu = SnmpCreatePdu( hSession, SNMP_PDU_GETNEXT, 0, NULL, NULL, hVbl);
- if( hPdu == SNMPAPI_FAILURE )
- {
- return FALSE;
- }
- // returns a handle to information about an SNMP management entity that is specific
- // to the Microsoft WinSNMP implementation
- hSrcEntity = SnmpStrToEntity( hSession, (LPCSTR)GetLocalIP() ); //源
- hDestEntity = SnmpStrToEntity( hSession, ip ); //目的
- if( hSrcEntity==SNMPAPI_FAILURE || hDestEntity==SNMPAPI_FAILURE )
- {
- return FALSE;
- }
- //发送
- if( SNMPAPI_FAILURE == SnmpSendMsg( hSession, hSrcEntity, hDestEntity, hContext, hPdu ) )
- {
- return FALSE;
- }
- //释放句柄
- SnmpFreeEntity( hSrcEntity );
- SnmpFreeEntity( hDestEntity );
- SnmpFreePdu( hPdu );
- SnmpFreeVbl( hVbl );
- return TRUE;
- }
- bool CSnmp::Receive(HSNMP_VBL& hVbl)
- {
- WaitForSingleObject( m_event, INFINITE);
- m_event.ResetEvent();
- HSNMP_ENTITY hSrcEntity;
- HSNMP_ENTITY hDestEntity;
- HSNMP_CONTEXT hContext;
- HSNMP_PDU hPdu;
- if( SNMPAPI_FAILURE == SnmpRecvMsg( hSession, &hSrcEntity, &hDestEntity, &hContext, &hPdu ) )
- {
- return FALSE;
- }
- smiINT PDU_type;
- smiINT error_status;
- smiINT error_index;
- if( SNMPAPI_FAILURE == SnmpGetPduData( hPdu, &PDU_type, NULL,//提取数据报
- &error_status, &error_index, &hVbl) )
- {
- return FALSE;
- }
- return TRUE;
- }
- bool CSnmp::Close()
- {
- SnmpFreeContext( hContext );
- SnmpClose( hSession );
- return TRUE;
- }
- SNMPAPI_STATUS CALLBACK CSnmp::Callback(
- HSNMP_SESSION hSession, // handle to the WinSNMP session
- HWND hWnd, // handle to the notification window
- UINT wMsg, // window notification message number
- WPARAM wParam, // type of notification
- LPARAM lParam, // request identifier of PDU
- LPVOID lpClientData // optional application-defined data
- )
- {
- ((CSnmp *)lpClientData)->m_event.SetEvent();
- return 1;
- }