// SNMPTesting.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <snmp.h >
#include <Windows.h >
#include <iostream >
#include <string >
using namespace std;
#pragma comment(lib, "Snmpapi.lib" )
typedef BOOL (WINAPI *pSnmpExtensionQuery_t) (BYTE requestType, RFC1157VarBindList *Binding,
AsnInteger *errorStatus, AsnInteger *errorIndex);
typedef BOOL (WINAPI *pSnmpExtensionInit_t) (DWORD dwTimeZero, HANDLE *hPollEvent,
AsnObjectIdentifier *supportView);
const MAX_SECONDS = 120;
//HANDLE m_hEvent;
HANDLE m_hSemaph;
HINSTANCE m_hMIBlib;
pSnmpExtensionQuery_t m_pSnmpExtensionQuery;
pSnmpExtensionInit_t m_pSnmpExtensionInit;
DWORD m_iTCPSent, m_iTCPRecv, m_iUDPSend, m_iUDPRecv;
namespace {
const TCHAR MIB_DLL_NAME[] = "inetmib1.dll" ;
UINT oidReceives[] = {
1, 3, 6, 1, 2, 1, 4, 3, 0
};
AsnObjectIdentifier var_inReceives = {
sizeof (oidReceives) / sizeof (oidReceives[0]),
oidReceives
};
UINT oidOutRequests[] = {
1, 3, 6, 1, 2, 1, 4, 10, 0
};
AsnObjectIdentifier var_outReceives = {
sizeof (oidOutRequests) / sizeof (oidOutRequests[0]),
oidOutRequests
};
};
BOOL Init();
BOOL ReadSnmpCounter (const AsnObjectIdentifier& oid, DWORD &counter);
BOOL NumberOfSentPackets(DWORD &nPackets);
BOOL NumberOfReceivedPackets(DWORD &nPackets);
BOOL CleanupSnmp();
BOOL GetDefaultRouteInterfaceIndex(DWORD& interfaceIndex);
BOOL NumberOfSentUnicastPackets(DWORD &nPackets);
BOOL NumberOfReceivedUnicastPackets(DWORD &nPackets);
void Writeout()
{
cout << m_iTCPSent << "\t" << m_iTCPRecv << "\t" << m_iUDPSend << "\t" << m_iUDPRecv << endl;
}
DWORD WINAPI threadTCPSend(LPVOID lpParms)
{
DWORD currSent, currSent2;
for (int i=0; ireturn 0;
}
DWORD WINAPI threadTCPRecv(LPVOID lpParms)
{
DWORD currReceived, currReceived2;
for (int i=0; ireturn 0;
}
DWORD WINAPI threadUDPSend(LPVOID lpParms)
{
DWORD currUnicastSent, currUnicastSent2;
for (int i=0; ireturn 0;
}
DWORD WINAPI threadUDPRecv(LPVOID lpParms)
{
DWORD currUnicastReceived, currUnicastReceived2;
for (int i=0; ireturn 0;
}
int main(int argc, char * argv[])
{
Init();
m_hSemaph = CreateSemaphore(NULL, 0, 4, NULL);
HANDLE hThread0 = CreateThread(NULL, 0, threadTCPSend, 0, NULL, NULL);
CloseHandle(hThread0);
HANDLE hThread1 = CreateThread(NULL, 0, threadTCPRecv, 0, NULL, NULL);
CloseHandle(hThread1);
HANDLE hThread2 = CreateThread(NULL, 0, threadUDPSend, 0, NULL, NULL);
CloseHandle(hThread2);
HANDLE hThread3 = CreateThread(NULL, 0, threadUDPRecv, 0, NULL, NULL);
CloseHandle(hThread3);
cout << "TCPSent" << "\t" << "TCPRecv" << "\t" << "UDPSend" << "\t" << "UDPRecv" << endl;
do
{
Writeout();
} while (WaitForSingleObject(m_hSemaph,1000) == WAIT_TIMEOUT);
Sleep(100);
CloseHandle(m_hSemaph);
CleanupSnmp();
char c = 0;
printf("\nPress Enter to continue..." );
gets(&c);
return 0;
}
BOOL GetDefaultRouteInterfaceIndex(DWORD& interfaceIndex) {
UINT oid[] = {
1, 3, 6, 1, 2, 1, 4, 21, 1, 2, 0, 0, 0, 0
};
AsnObjectIdentifier var_InterfaceIndex = {
sizeof (oid) / sizeof (oid[0]),
oid
};
BOOL rc = ReadSnmpCounter (var_InterfaceIndex, interfaceIndex);
if (rc == FALSE) {
return FALSE;
}
return TRUE;
}
BOOL NumberOfSentPackets(DWORD &nPackets) {
return ReadSnmpCounter (var_outReceives, nPackets);
}
BOOL NumberOfReceivedPackets(DWORD &nPackets) {
return ReadSnmpCounter (var_inReceives, nPackets);
}
BOOL NumberOfReceivedUnicastPackets(DWORD &nPackets) {
// First, get the interface index for the default route:
DWORD interfaceIndex;
if (GetDefaultRouteInterfaceIndex(interfaceIndex) == FALSE) {
return FALSE;
}
// Now, get the number of unicast packets for this interface:
UINT oid[] = {
1, 3, 6, 1, 2, 1, 2, 2, 1, 11, 1
};
oid[10] = interfaceIndex;
AsnObjectIdentifier var_InterfaceInUCastPkts = {
sizeof (oid) / sizeof (oid[0]),
oid
};
return ReadSnmpCounter (var_InterfaceInUCastPkts, nPackets);
}
// Get the total number of sent unicast packets on the interface
// of the default route:
BOOL NumberOfSentUnicastPackets(DWORD &nPackets) {
// First, get the interface index for the default route:
DWORD interfaceIndex;
if (GetDefaultRouteInterfaceIndex(interfaceIndex) == FALSE) {
return FALSE;
}
// Now, get the number of unicast packets for this interface:
UINT oid[] = {
1, 3, 6, 1, 2, 1, 2, 2, 1, 17, 1
};
oid[10] = interfaceIndex;
AsnObjectIdentifier var_InterfaceOutUCastPkts = {
sizeof (oid) / sizeof (oid[0]),
oid
};
return ReadSnmpCounter (var_InterfaceOutUCastPkts, nPackets);
}
BOOL Init()
{
if (m_hMIBlib)
return TRUE;
if (!(m_hMIBlib = ::LoadLibrary(MIB_DLL_NAME))) {
DWORD err = GetLastError();
printf("Cannot load library '%s' (error %d)" , MIB_DLL_NAME, err);
return FALSE;
}
m_pSnmpExtensionQuery = (pSnmpExtensionQuery_t)GetProcAddress(m_hMIBlib, "SnmpExtensionQuery" );
m_pSnmpExtensionInit = (pSnmpExtensionInit_t)GetProcAddress(m_hMIBlib, "SnmpExtensionInit" );
//ASSERT(m_pSnmpExtensionQuery != NULL && m_pSnmpExtensionInit != NULL);
AsnObjectIdentifier suppView;
HANDLE eventer;
if ( !m_pSnmpExtensionInit(GetCurrentTime(), &eventer, &suppView) ) {
printf("Unable to initialize SNMP." );
return FALSE;
}
return TRUE;
}
BOOL ReadSnmpCounter (const AsnObjectIdentifier& oid, DWORD &counter)
{
if (!m_pSnmpExtensionQuery)
return FALSE;
RFC1157VarBind varBind;
varBind.value .asnType = ASN_NULL;
SnmpUtilOidCpy(&varBind.name, (AsnObjectIdentifier *)&oid);
RFC1157VarBindList varBindList;
varBindList.len = 1;
varBindList.list = &varBind;
AsnInteger errStatus, errIndex;
BOOL rc = m_pSnmpExtensionQuery(ASN_RFC1157_GETREQUEST, &varBindList,
&errStatus, &errIndex);
SnmpUtilOidFree(&varBind.name);
if (errStatus != SNMP_ERRORSTATUS_NOERROR)
rc = FALSE;
counter = (DWORD)varBind.value .asnValue.counter;
return rc;
}
BOOL CleanupSnmp()
{
return FreeLibrary(m_hMIBlib);
}