Realize NetStat via SNMP

zhaozj2021-02-16  88

#include "windows.h" #include "stdio.h" #include "snmp.h" #include "winsock.h"

#define hostnamelen 256 # Define portnamelen 256 # Define Addresslen Hostnamelen Portnamelen

// Define TCP Connection Links TypeDef struct _tcpinfo {structure _tcpinfo * prev; struct _tcpinfo * next; uint state; uint localip; uint localport; uint remoteip; uint remoteport;} tcpinfo, * ptcpinfo;

// definition of SNMP Get pointer pSnmpExtensionInitBOOL SnmpExtensionInit extended library function (__stdcall * pSnmpExtensionInit) (IN DWORD dwTimeZeroReference, OUT HANDLE * hPollForTrapEvent, OUT AsnObjectIdentifier * supportedView); // definition of SNMP Get SnmpExtensionQuery extended library function pointers pSnmpExtensionQueryBOOL (__stdcall * PSNMPEXTensionQuery) (in Byte RequestType, In Out RFC1157Varbindlist * variablebindings, out asninteger * errorstatus, out asninteger * errorindex);

/ / Define the state that may occur when the TCP connection is defined: static char tcpState [] [32] = {"???", "closed", "listening", "syn_sent", "Sen_Received", "ESTABLISHED", "Fin_Wait" , "Fin_Wait 2", "Closen_Wait", "Closing", "Last_ack", "Time_Wait"}; TCPINFO TCPINFOTABLE; / / Define Get Get Port Number Text Value CHAR * GETPORTNAME (Uint Port, Char * Proto, Char * Name, INT Namelen) {strunt server * psrvent;

IF (psrvent = getservbyport (htons (ushort) port), proto) {

STRCPY (Name, Psrvent-> S_Name);

} else {

Sprintf (Name, "% d", port);

} Return name;}

/ / Define the function of the conversion IP address display format to obtain hostname char * getiphostname (Bool Local, int namelen) {struct hostent * phostent; uint nipaddr; nipaddr = htonl (iPaddr); if (! iPaddr) {

IF (! local) {

Sprintf (Name, "% D.% d.% d.% d", (NipAddr >> 24) & 0xFF, (NipAddr >> 16) & 0xFF, (NipAddr >> 8) & 0xFF, (NIPADDR) & 0xFF } else {

Gethostname (Name, Namelen);

} else IF (ipaddr == 0x0100007f) {

IF (local) {

GethostName (Name, Namelen);} else {

STRCPY (Name, "LocalHost");

} else if (phostent = gethostbyaddr ((char *) & ipaddr, sizeof (nipaddr), pf_inet) {

Strcpy (name, phostent-> h_name);

} else {

Sprintf (Name, "% D.% d.% d.% d", (NipAddr >> 24) & 0xFF, (NipAddr >> 16) & 0xFF, (NipAddr >> 8) & 0xFF, (NIPADDR) & 0xFF } Return name;

// Defines the loadInetMibentryPoints function of the required SNMP interface function to export the required SNMP interface function Boolean LoadinetMibentryPoints () {Hinstance Hinetlib;

IF (! ("inetlib = loadingLibrary (" inetmib1.dll "))))

Return false;}

IF (! (psnmpextensioninit = (void *) getProcaddress (Hinetlib, "snmpextensioninit))))) {

Return false;}

If (! (psnmpextensionquery = (void *) getProcaddress (Hinetlib, "SNMPEXTensionQuery)))) {

Return false;} Return True;

int main (int argc, char * argv []) {HANDLE hTrapEvent; AsnObjectIdentifier hIdentifier; RFC1157VarBindList bindList; RFC1157VarBind bindEntry; UINT tcpidentifiers [] = {1,3,6,1,2,1,6,13,1,1 }; Uint udpidentifiers [] = {1, 3, 6, 1, 2, 1, 7, 5, 1, 1}; Asninteger ErrorStatus, Errorindex; TCPINFO * CurrenTentry, * Newentry; Uint CurrentIndex; Word WvelSionRequested; Wsadata Wsadata; char localname [HOSTNAMELEN], remotename [HOSTNAMELEN]; char remoteport [PORTNAMELEN], localport [PORTNAMELEN]; char localaddr [ADDRESSLEN], remoteaddr [ADDRESSLEN]; wVersionRequested = MAKEWORD (1, 1); if (WSAStartup (wVersionRequested, & wsaData) ) {PrintF ("Could Not Initialize Winsock./N"); Return 1;} if (! LoadinginetMibentryPoints ()) {PrintF ("Could Not Load Extension Dll./N); Return 1;} IF (! Psnmpextensioninit) GetcurrentTime (), & htrapevent, & hidentifier) ​​{Printf ("Could Not Initialize Extension DLL./N); Return 1;} BindenTry.Name.Idlength = 0xA; Bindentry.na me.ids = tcpidentifiers; bindList.list = & bindEntry; bindList.len = 1; TcpInfoTable.prev = & TcpInfoTable; TcpInfoTable.next = & TcpInfoTable; currentIndex = 1; currentEntry = & TcpInfoTable;! while (1) {if (pSnmpExtensionQuery (ASN_RFC1157_GETNEXTREQUEST, & bindList, & errorStatus, & errorIndex)) {return 1;} if (bindEntry.name.idLength <0xA) break; if (currentIndex = bindEntry.name.ids [9]) {currentEntry = TcpInfoTable.next;! currentIndex = bindEntry.name .ids [9];} switch (bindentry.name.ids [9]) {casse 1: newentry = (tcpinfo *) malloc (tcpinfo); newentry-> prev =

currentEntry; newEntry-> next = & TcpInfoTable; currentEntry-> next = newEntry; currentEntry = newEntry; currentEntry-> state = bindEntry.value.asnValue.number; break; case 2: currentEntry-> localip = * (UINT *) bindEntry. value.asnValue.address.stream; currentEntry = currentEntry-> next; break; case 3: currentEntry-> localport = bindEntry.value.asnValue.number; currentEntry = currentEntry-> next; break; case 4: currentEntry-> remoteip = * (UINT *) bindEntry.value.asnValue.address.stream; currentEntry = currentEntry-> next; break; case 5: currentEntry-> remoteport = bindEntry.value.asnValue.number; currentEntry = currentEntry-> next; break;} } Printf ("% 7S% -30S% -30S% S / N", "Proto", "Local", "Remote", "State"); currentent = tcpinfotable.next; while (currententry! = & Tcpinfotable) {sprintf (Localaddr, "% s:% s", getiphostname (True, Currententry-> localip, localname, hostnamelen, getportname (Currententry-> localport, "TCP" , Localport, PORTNAMELEN)); sprintf (remoteaddr, "% s:% s", GetIpHostName (FALSE, currentEntry-> remoteip, remotename, HOSTNAMELEN), currentEntry-> remoteip GetPortName (currentEntry-> remoteport, "tcp", remoteport? , Portnamelen: "0"); Printf ("% 7S% -30S% -30S% S / N", "TCP", Localaddr, Remoteaddr, TcpState [currententry-> state]); currententry = currententry-> next; }

转载请注明原文地址:https://www.9cbs.com/read-12888.html

New Post(0)