?
Solved

Sending SNMP trap message

Posted on 2003-03-05
11
Medium Priority
?
860 Views
Last Modified: 2007-12-19
I need to send some SNMP trap messages from my Win2K server to SNMP manager application.
I can do it by creating a SNMP dll that will communicate with the SNMP win service but it’s an overhead since I only need to send traps.
Is there a way to send traps without dll and without using some external tools?
Is there some API function I can call to do it?
0
Comment
Question by:alto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8071600
Have you seen http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B192796 ("HOWTO: Send SNMP Trap with Variable Bindings from Testdll.dll")?
0
 

Author Comment

by:alto
ID: 8072341
That exactly is what I don't want to do, I don’t won’t to write an SNMP agent dll, I want to call a trap from a simple application without using the dll.
Is it possible? (I’m sure it is but how?)
0
 
LVL 86

Expert Comment

by:jkr
ID: 8072409
It is possible. See e.g. http://www.sysinternals.com/files/netstatp.zip - the source code demonstrates how to send traps from an application
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:alto
ID: 8072564
Tn'x, nice application.
But from version 2 its don't uses SNMP anymore.
Do you have an old version maybe?
0
 

Author Comment

by:alto
ID: 8072596
Tn'x, nice application.
But from version 2 its don't uses SNMP anymore.
Do you have an old version maybe?
0
 

Author Comment

by:alto
ID: 8072614
Tn'x, nice application.
But from version 2 its don't uses SNMP anymore.
Do you have an old version maybe?
0
 
LVL 86

Expert Comment

by:jkr
ID: 8073359
Ooops - yes, I still have the old one here:

//------------------------------------------------------------
//
// Netstatp
//
// Copyright (C) 1998 Mark Russinovich
// Systems Internals
// http://www.sysinternals.com
//
// This program implements a subset of the Netstat program's
// functionality. Specifically, it enumerates and displays
// information about all UDP and TCP endpoints.
//
//------------------------------------------------------------
#include "windows.h"
#include "stdio.h"
#include "snmp.h"
#include "winsock.h"

#define HOSTNAMELEN 256
#define PORTNAMELEN 256
#define ADDRESSLEN HOSTNAMELEN+PORTNAMELEN

typedef struct _tcpinfo {
      struct _tcpinfo            *prev;
      struct _tcpinfo            *next;
      UINT                        state;
      UINT                        localip;
      UINT                        localport;
      UINT                        remoteip;
      UINT                        remoteport;
} TCPINFO, *PTCPINFO;


BOOL (__stdcall *SnmpExtensionInit)(
    IN  DWORD               dwTimeZeroReference,
    OUT HANDLE              *hPollForTrapEvent,
    OUT AsnObjectIdentifier *supportedView);

BOOL (__stdcall *SnmpExtensionQuery)(
    IN BYTE                   requestType,
    IN OUT RFC1157VarBindList *variableBindings,
    OUT AsnInteger            *errorStatus,
    OUT AsnInteger            *errorIndex);

//
// Possible TCP endpoint states
//
static char TcpState[][32] = {
      "???",
      "CLOSED",
      "LISTENING",
      "SYN_SENT",
      "SEN_RECEIVED",
      "ESTABLISHED",
      "FIN_WAIT",
      "FIN_WAIT2",
      "CLOSE_WAIT",
      "CLOSING",
      "LAST_ACK",
      "TIME_WAIT"
};
      
//
// Lists of endpoints
//
TCPINFO            TcpInfoTable;
TCPINFO            UdpInfoTable;


//------------------------------------------------------------
//
// GetPortName
//
// Translate port numbers into their text equivalent if
// there is one
//
//------------------------------------------------------------
char *GetPortName( UINT port, char *proto, char *name, int namelen )
{
      struct servent *psrvent;

      if( psrvent = getservbyport( htons( (USHORT) port ), proto )) {

            strcpy( name, psrvent->s_name );

      } else {

            sprintf(name, "%d", port);

      }            
      return name;
}


//------------------------------------------------------------
//
// GetIpHostName
//
// Translate IP addresses into their name-resolved form
// if possible.
//
//------------------------------------------------------------
char *GetIpHostName( BOOL local, UINT ipaddr, char *name, 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;
}


//------------------------------------------------------------
//
// LoadInetMibEntryPoints
//
// Load the TCP/IP SNMP extension DLL and locate the entry
// points we will use.
//
//------------------------------------------------------------
BOOLEAN LoadInetMibEntryPoints()
{
      HINSTANCE      hInetLib;

      if( !(hInetLib = LoadLibrary( "inetmib1.dll" ))) {

            return FALSE;
      }

      if( !(SnmpExtensionInit = (void *) GetProcAddress( hInetLib,
                  "SnmpExtensionInit" )) ) {

            return FALSE;
      }

      if( !(SnmpExtensionQuery = (void *) GetProcAddress( hInetLib,
                  "SnmpExtensionQuery" )) ) {

            return FALSE;
      }
      
      return TRUE;
}


//------------------------------------------------------------
//
// Main
//
// Do it all. Load and initialize the SNMP extension DLL and
// then build a table of TCP endpoints and UDP endpoints. After
// each table is built resolve addresses to names and print
// out the information
//
//------------------------------------------------------------
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                              wVersionRequested;
      WSADATA                              wsaData;
      char                              localname[HOSTNAMELEN], remotename[HOSTNAMELEN];
      char                              remoteport[PORTNAMELEN], localport[PORTNAMELEN];
      char                              localaddr[ADDRESSLEN], remoteaddr[ADDRESSLEN];

      //
      // Initialize winsock
      //
      wVersionRequested = MAKEWORD( 1, 1 );
      if( WSAStartup(  wVersionRequested, &wsaData ) ) {

            printf("Could not initialize Winsock.\n");
            return 1;
      }

      //
      // Locate and initialize INETMIB1
      //
      if( !LoadInetMibEntryPoints()) {

            printf("Could not load extension DLL.\n");
            return 1;
      }

      if( !SnmpExtensionInit( GetCurrentTime(), &hTrapEvent, &hIdentifier )) {

            printf("Could not initialize extension DLL.\n");
            return 1;
      }

      //
      // Initialize the query structure once
      //
      bindEntry.name.idLength = 0xA;
      bindEntry.name.ids = tcpidentifiers;
      bindList.list = &bindEntry;
      bindList.len  = 1;

      TcpInfoTable.prev = &TcpInfoTable;
      TcpInfoTable.next = &TcpInfoTable;

      //
      // Roll through TCP connections
      //
      currentIndex = 1;
      currentEntry = &TcpInfoTable;
      while(1) {

            if( !SnmpExtensionQuery( ASN_RFC1157_GETNEXTREQUEST,
                  &bindList, &errorStatus, &errorIndex )) {

                  return 1;
            }

            //
            // Terminate when we're no longer seeing TCP information
            //
            if( bindEntry.name.idLength < 0xA ) break;

            //
            // Go back to start of table if we're reading info
            // about the next byte
            //
            if( currentIndex != bindEntry.name.ids[9] ) {

                  currentEntry = TcpInfoTable.next;
                  currentIndex = bindEntry.name.ids[9];
            }

            //
            // Build our TCP information table
            //
            switch( bindEntry.name.ids[9] ) {

            case 1:
                  
                  //
                  // Always allocate a new structure
                  //
                  newEntry = (TCPINFO *) malloc( sizeof(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;
            }

      }
      
      //
      // Now print the connection information
      //
      printf("%7s %-30s %-30s %s\n", "Proto", "Local", "Remote", "State" );
      currentEntry = 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;
      }
      printf("\n");

      //
      // Initialize the query structure once
      //
      bindEntry.name.idLength = 0xA;
      bindEntry.name.ids = udpidentifiers;
      bindList.list = &bindEntry;
      bindList.len  = 1;

      UdpInfoTable.prev = &UdpInfoTable;
      UdpInfoTable.next = &UdpInfoTable;

      //
      // Roll through UDP endpoints
      //
      currentIndex = 1;
      currentEntry = &UdpInfoTable;
      while(1) {

            if( !SnmpExtensionQuery( ASN_RFC1157_GETNEXTREQUEST,
                  &bindList, &errorStatus, &errorIndex )) {

                  return 1;
            }

            //
            // Terminate when we're no longer seeing TCP information
            //
            if( bindEntry.name.idLength < 0xA ) break;

            //
            // Go back to start of table if we're reading info
            // about the next byte
            //
            if( currentIndex != bindEntry.name.ids[9] ) {

                  currentEntry = UdpInfoTable.next;
                  currentIndex = bindEntry.name.ids[9];
            }

            //
            // Build our TCP information table
            //
            switch( bindEntry.name.ids[9] ) {

            case 1:
                  
                  //
                  // Always allocate a new structure
                  //
                  newEntry = (TCPINFO *) malloc( sizeof(TCPINFO ));
                  newEntry->prev = currentEntry;
                  newEntry->next = &UdpInfoTable;
                  currentEntry->next = newEntry;
                  currentEntry = newEntry;

                  currentEntry->localip =
                        *(UINT *) bindEntry.value.asnValue.address.stream;
                  break;

            case 2:
                  
                  currentEntry->localport =
                        bindEntry.value.asnValue.number;
                  currentEntry = currentEntry->next;
                  break;
            }
      }
      
      //
      // Now print the connection information
      //
      currentEntry = UdpInfoTable.next;
      while( currentEntry != &UdpInfoTable ) {

            printf("%7s %s:%s\n", "UDP",
                        GetIpHostName( TRUE, currentEntry->localip, localname, HOSTNAMELEN),
                        GetPortName( currentEntry->localport, "udp", localport, PORTNAMELEN ) );
            
            currentEntry = currentEntry->next;
      }
      printf("\n");
}
0
 

Author Comment

by:alto
ID: 8077978
Hi, I see that sample uses the "inetmib1.dll" but only for query the local setting.
Is it also having a function to send traps?
0
 
LVL 86

Accepted Solution

by:
jkr earned 600 total points
ID: 8082294
I assume you are referring to inetmib1.dll's 'SnmpExtensionTrap()': http://msdn.microsoft.com/library/default.asp?url=/library/en-us/snmp/snmp/snmpextensiontrap.asp
0
 
LVL 86

Expert Comment

by:jkr
ID: 8138352
alto,

anything new on this one?
0
 

Author Comment

by:alto
ID: 8145822
Its not all the answer but it gave me a direction to work.

Thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question