Exemplo n.º 1
0
int 
main(int argc, char** argv)
{

#ifdef _WIN32
  // save the size and other constraints
   char t[512];
   memset(t, 0, 512);
   GetConsoleTitle(t, 512);
   HWND h = FindWindow("ConsoleWindowClass", t);
   //  ShowWindow(h, SW_HIDE);
#endif   

  int retVal = 0;
  try
  {
    int limCount = min(argc, 4);
    for (int x = 1; x < limCount; x++) 
    {
      // check for an input file
      if (argv[x][0] == '-')
      {
	if (argv[x][1] == 'f' || argv[1][1] == 'F')
	  FileOpts(argv[x + 1]);
	else
	  // see if they need help
	  if (argv[x][1] == 'h')
	  {
#ifdef _WIN32
	    //	    ShowWindow(h, SW_SHOW);
#endif
	    Usage();
	    exit(0);
	  }
      }
      else
	// this is just for us
	if (!strcmp(argv[x], "dumpVer"))
	{
	  cout << "TrapGen from Network Computing Technologies, Inc.";
	  cout << endl << gProgramVersion << endl;
	  return 0;
	}
    } // for (int x = 1; x < limCount; x++) 

    // other command line arguments
    CmdLineOpts(argc, argv);

    if (gLogFileName != NULL)
    {
      gDoLogging = TRUE;
      gOfile.open(gLogFileName, ios::app);
    }

#ifdef _WIN32
    //    if (FALSE == gFreeConsole)
    //    {
    //      ShowWindow(h, SW_SHOW);
    if (TRUE == gFreeConsole)
    {
      ShowWindow(h, SW_HIDE);
    }
#endif


#ifdef _WIN32
    WSADATA wsaData;
    int err = WSAStartup(0x200, &wsaData);
    if (err == WSAVERNOTSUPPORTED)
      err = WSAStartup(0x101, &wsaData);
    if (gDoLogging)
      if (err)
	gOfile << "failed to initialize winsock, error is " << err << endl;
#endif

    ///////////////////////////////////////////////////
    // new to version 2.7
#ifdef _WIN32
    NtSysUpTime sysUpTime;
#endif
#ifdef _LINUX
    LinuxSysUpTime sysUpTime;
#endif
#ifdef _SOLARIS
    SolarisSysUpTime sysUpTime;
#endif
#ifdef _IRIX
    IrixSysUptime sysUpTime;
#endif
#ifdef _HPUX
    HpuxSysUpTime sysUpTime;
#endif
#ifdef _FREEBSD
    FreeBSDSysUptime sysUpTime;
#endif
    gTimeTicks = sysUpTime.SysUpTime();
    ///////////////////////////////////////////////////
    
    
    // figure out the ip address if not specified (V1 only)
    if (gSenderIP == NULL && gVersion == 0)
    {
      char buf[255];
      unsigned long len = 255;
      memset(buf, 0, len);
      if (gethostname(buf, len))
	if (gDoLogging)
	  gOfile << "gethostname failed with error " << WSAGetLastError() << endl;

      HOSTENT* h = gethostbyname(buf);
      if (h != NULL)
      {
	struct in_addr in;
	memcpy(&in.s_addr, *(h->h_addr_list), sizeof(in.s_addr));
	gSenderIP = inet_ntoa(in);
      }
      else
      {
	gSenderIP = "240.30.20.10";
	if (gDoLogging)
	  gOfile << "gethostbyname failed with error " << WSAGetLastError() << endl;
      }
    }

    // build the packet
    gPacket.Version(gVersion);
    gPacket.Community(gCommunity);
    if (gVersion == 0)
    {
      gPacket.Type(V1TRAP);
      gPacket.SenderOID(gSenderOID);
      gPacket.SenderIP(gSenderIP);
      gPacket.TimeTicks(gTimeTicks);
      gPacket.GenericTrapType(gGenericTrapType);
      gPacket.SpecificTrapType(gSpecificTrapType);
    }
    else
    {
      if (!gInform)
	gPacket.Type(V2TRAP);
      else
	gPacket.Type(INFORMPDU);

      gPacket.RequestId(gRequestId);
      gPacket.ErrorStatus(0);
      gPacket.ErrorIndex(0);
      gPacket.AddV2TrapVarbinds(gTimeTicks, 
				gSenderOID, 
				gGenericTrapType,
				gSpecificTrapType,
				gNoSubIds);
    }

    // send away
    retVal = Send();
    
  }
  catch (SnmpException* se)
  {
    retVal = -1;
  }
  catch (...)
  {
    retVal = -2;
  }

#ifdef _WIN32  
  WSACleanup();
#endif
  
  if (gDoLogging)
    gOfile.close();
  
  return retVal;
}
Exemplo n.º 2
0
void ReadInterfaceNamesBulk()
{
    BOOL                Done=FALSE;
    char				*OIDString;
    OidVarbind*         oid;
    VbPair*             vbp;
    const char			*RawRetP=NULL;
    Packet*             retP;
    DWORD				Retries;
    DWORD				x;						/* Counter */
    Packet				p(GETBULK, 1);			// create an SNMP v2c GETBULK command
    int					vbLen;
    int					vbCounter;
    int					len;

    unsigned int		IntNumber;

    /***************/
    /* Set up OIDs */
    /***************/
    OIDString = (char *)malloc(1000);
    sprintf(OIDString, "1.3.6.1.2.1.2.2.1.2");

    /*************************/
    /* Create GETBULK packet */
    /*************************/
    p.Community(Community);				// set the community string
    p.RequestId(123);					// use device number to track the request
    p.ErrorStatus(0);					// set non-repeaters to zero
    p.ErrorIndex(5);					// set number of max-repititions
    oid = new OidVarbind(OIDString);	// OID for first MAC address
    vbp = new VbPair(oid, NULL);		// Create vbpairs
    p.Add(vbp);							// Add vbpair to the packet
    UdpClient udp(161, Agent);			// Set to port 161 and IP address (or host name)
    udp.Timeout(5);						// Set timeout to PollDeviceTimeout
    udp.ReadBufferSize(65535);			// set buffer size to be large enough to handle response

    /*******************/
    /* Collection loop */
    /*******************/
    IntNumber=0;
    Done = FALSE;
    while (!Done)
    {
        Retries=0;

        do
        {
            dumpPacket(&p);
            udp.Send(&p);									// Send the packet
            Retries++;
            RawRetP = udp.Receive();						// Receive the reply
        } while ((!RawRetP) && (Retries < 3));

        /*************************************/
        /* If the packet is valid, save info */
        /*************************************/
        if ( RawRetP )
        {
            // Determine length and parse packet
            len = udp.ReadLength();
            SnmpParser parser((unsigned char*)RawRetP, len);
            retP = parser.packet();

            if (retP != NULL)
            {
                // Packet was parsed successfully
                if ( retP->RequestId() == 123 )
                {
                    // RequestID matches
                    if ( retP->ErrorStatus() == NOERROR )
                    {
                        // No SNMP error found, learn how many OIDs were returned
                        vbLen = retP->VbListLength();

                        // Loop through for each returned OID
                        for (vbCounter=1 ; vbCounter <= vbLen ; vbCounter++)
                        {
                            sprintf(OIDString, "%s", retP->VbOID(vbCounter));
                            if (strncmp(OIDString, "1.3.6.1.2.1.2.2.1.2.", 20) == 0)
                            {
                                // OID is still correct, print the results
                                printf("%s: %s\n", retP->VbOID(vbCounter), retP->VbData(vbCounter));
                            }
                            else
                            {
                                // OID does not match, we're done
                                Done=TRUE;
                                break;
                            }

                        }	// Loop through all OIDs

                        // Set up next packet
                        delete oid;
                        printf("assigning new oid of <%s>\r\n", OIDString);
                        oid = new OidVarbind(OIDString);
                        p.VbList()->FirstVbPair()->OIDVarbind(oid);
                    }
                    else
                    {
                        // ErrorStatus returned an SNMP error
                        printf("ERROR: ErrorStatus returned an SNMP error.\n");
                        break;
                    }
                }
                else
                {
                    // RequestID does not match
                    printf("ERROR: RequestID does not match.\n");
                    break;
                }
            }
            else
            {
                // Packet could not be parsed correctly, some error somewhere
                printf("ERROR: Packet could not be parsed.  Error code: %u\n", udp.ErrorCode());
                break;
            }

            /******************************/
            /* Delete packet if it exists */
            /******************************/
            if ( retP != NULL )
            {
                delete retP;
                retP=NULL;
            }
        }
        else
        {
            // No packet received
            printf("ERROR: No packet returned.  Error code: %u\n", udp.ErrorCode());
            break;
        }
    }

    free(OIDString);

    return;
}