修改net-snmp -snmptrap.c

/*
 * snmptrap.c - send snmp traps to a network entity.
 *
 */
/******************************************************************
	Copyright 1989, 1991, 1992 by Carnegie Mellon University

                      All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/
#include <net-snmp/net-snmp-config.h>

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <sys/types.h>
#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#if TIME_WITH_SYS_TIME
# ifdef WIN32
#  include <sys/timeb.h>
# else
#  include <sys/time.h>
# endif
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <stdio.h>
#if HAVE_WINSOCK_H
#include <winsock.h>
#endif
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if HAVE_NETDB_H
#include <netdb.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif

#include <net-snmp/net-snmp-includes.h>

oid             objid_enterprise[] = { 1, 3, 6, 1, 4, 1, 3, 1, 1 };
oid             objid_sysdescr[] = { 1, 3, 6, 1, 2, 1, 1, 1, 0 };
oid             objid_sysuptime[] = { 1, 3, 6, 1, 2, 1, 1, 3, 0 };
oid             objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
int             inform = 0;

#define BUF_SIZE 255
#define MAX_THREADS 100
typedef struct _MyData {
	int argc;
	char* argv[8];
} MYDATA, *PMYDATA;

void
usage(void)
{
    fprintf(stderr, "USAGE: %s ", inform ? "snmpinform" : "snmptrap");
    snmp_parse_args_usage(stderr);
    fprintf(stderr, " TRAP-PARAMETERS\n\n");
    snmp_parse_args_descriptions(stderr);
    fprintf(stderr,
            "  -C APPOPTS\t\tSet various application specific behaviour:\n");
    fprintf(stderr, "\t\t\t  i:  send an INFORM instead of a TRAP\n");
    fprintf(stderr,
            "\n  -v 1 TRAP-PARAMETERS:\n\t enterprise-oid agent trap-type specific-type uptime [OID TYPE VALUE]...\n");
    fprintf(stderr, "  or\n");
    fprintf(stderr,
            "  -v 2 TRAP-PARAMETERS:\n\t uptime trapoid [OID TYPE VALUE] ...\n");
}

int
snmp_input(int operation,
           netsnmp_session * session,
           int reqid, netsnmp_pdu *pdu, void *magic)
{
    return 1;
}

static void
optProc(int argc, char *const *argv, int opt)
{
     switch (opt) {
     case 'C':
         while (*optarg) {
             switch (*optarg++) {
             case 'i':
                 inform = 1;
                 break;
             default:
                 fprintf(stderr,
                         "Unknown flag passed to -C: %c\n", optarg[-1]);
                 exit(1);
             }
         }
         break;
     }
}

//void icmpSend(int argc, char *argv[])
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
	netsnmp_session session, *ss, *bakss;
    netsnmp_pdu    *pdu = NULL, *response = NULL, *bakpdu, *bakresponse;
    oid             name[MAX_OID_LEN];
    size_t          name_length;
    int             arg;
    int             status;
    char           *trap = NULL;
    char           *prognam;
    int             exitval = 0;
#ifndef NETSNMP_DISABLE_SNMPV1
    char           *specific = NULL, *description = NULL, *agent = NULL;
    in_addr_t      *pdu_in_addr_t;
#endif
	int nInterval = 0;
	int nTime     = 0;
	int num = 0;
	int tmpnum = 0;
	time_t time1 = {0} , time2 = {0};
	long            sysuptime;
	char            csysuptime[20];
	int addflag = 0;

	
	PMYDATA pData;
	pData = (PMYDATA)lpParam;

    prognam = strrchr(pData->argv[0], '\\');
    if (prognam)
        prognam++;
    else
        prognam = pData->argv[0];

    putenv(strdup("POSIXLY_CORRECT=1"));

    if (strcmp(prognam, "snmpinform") == 0)
        inform = 1;
	//pData->argv[4] = "public";
    switch (arg = snmp_parse_args(pData->argc, pData->argv, &session, "C:", optProc, &nInterval, &nTime)) {
    case -2:
        exit(0);
    case -1:
        usage();
        exit(1);
    default:
        break;
    }
	
    SOCK_STARTUP;

    session.callback = snmp_input;
    session.callback_magic = NULL;

    ss = snmp_add(&session,
                  netsnmp_transport_open_client("snmptrap", session.peername),
                  NULL, NULL);
    if (ss == NULL) {
        /*
         * diagnose netsnmp_transport_open_client and snmp_add errors with
         * the input netsnmp_session pointer
         */
        snmp_sess_perror("snmptrap", &session);
        SOCK_CLEANUP;
        exit(1);
    }
//     ss = snmp_sess_open(&session);
//     if (ss == NULL) {
//         snmp_sess_perror("snmpget", &session);
//         SOCK_CLEANUP;
//         exit(1);
//     }


    {

		 num = arg;
		 nTime  = 100;

		time(&time1);
		
RETY:
		tmpnum = num;
        pdu = snmp_pdu_create(inform ? SNMP_MSG_INFORM : SNMP_MSG_TRAP2);
        if ( !pdu ) {
            fprintf(stderr, "Failed to create notification PDU\n");
            SOCK_CLEANUP;
            exit(1);
        }
        if (tmpnum == pData->argc) {
            fprintf(stderr, "Missing up-time parameter\n");
            usage();
            SOCK_CLEANUP;
            exit(1);
        }
		if (addflag == 0) {
			addflag = 1;
		}
        trap = pData->argv[tmpnum];
		//num = arg;
        if (*trap == 0) {
            sysuptime = get_uptime();
            sprintf(csysuptime, "%ld", sysuptime);
            trap = csysuptime;
        }
        snmp_add_var(pdu, objid_sysuptime,
                     sizeof(objid_sysuptime) / sizeof(oid), 't', trap);
        if (++tmpnum == pData->argc) {
            fprintf(stderr, "Missing trap-oid parameter\n");
            usage();
            SOCK_CLEANUP;
            exit(1);
        }
         if (snmp_add_var
             (pdu, objid_snmptrap, sizeof(objid_snmptrap) / sizeof(oid),
              'o', pData->argv[tmpnum]) != 0) {
             snmp_perror(pData->argv[tmpnum]);
             SOCK_CLEANUP;
             exit(1);
         }
    }
    tmpnum++;

    while (tmpnum < pData->argc) {
        tmpnum += 3;
        if (tmpnum > pData->argc) {
            fprintf(stderr, "%s: Missing type/value for variable\n",
                    pData->argv[tmpnum - 3]);
            SOCK_CLEANUP;
            exit(1);
        }
        name_length = MAX_OID_LEN;
        if (!snmp_parse_oid(pData->argv[tmpnum - 3], name, &name_length)) {
            snmp_perror(pData->argv[tmpnum - 3]);
            SOCK_CLEANUP;
            exit(1);
        }
        if (snmp_add_var
            (pdu, name, name_length, pData->argv[tmpnum - 2][0],
             pData->argv[tmpnum - 1]) != 0) {
            snmp_perror(pData->argv[tmpnum - 3]);
            SOCK_CLEANUP;
            exit(1);
        }
    }

    if (inform)
        status = snmp_synch_response(ss, pdu, &response);
    else
        status = snmp_send(ss, pdu) == 0;
    if (status) {
        snmp_sess_perror(inform ? "snmpinform" : "snmptrap", ss);
        if (!inform)
        exitval = 1;
	}

 	if (response) {
 		snmp_free_pdu(response);
		response = NULL;
 	}

	if(nTime--)
		//Sleep(nInterval);
		goto RETY;

	time(&time2);
	time2 = time2-time1;
	printf("%ld\n",time2);
    snmp_close(ss);
    snmp_shutdown("snmpapp");
    SOCK_CLEANUP;
	printf("lpThreadId thread  OUT");
	return 0;

}

#define MAX_THREAD_COUNT 2

int
main(int argc, char *argv[])
{
	int nNumThread;
	HANDLE myThread[MAX_THREAD_COUNT];
	DWORD dwThreadId[MAX_THREAD_COUNT];
	int i,j, tmp;
	int s = sizeof(MYDATA);
// 	PMYDATA pmyData;
// 	pmyData = (PMYDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
// 		sizeof(MYDATA));
// 
// 	if (!pmyData) {
// 		exit(1);
// 	}

// 	pmyData->argv[0] = "snmptrap.exe";
// 	pmyData->argv[1] = "-v";
// 	pmyData->argv[2] = "2c";
// 	pmyData->argv[3] = "-c";
// 	pmyData->argv[4] = "public";
// 	pmyData->argv[5] = "172.140.28.64";
// 	pmyData->argv[6] = "";
// 	pmyData->argv[7] = "UCD-SNMP-MIB::ucdStart";

// 	pmyData->argc = argc;
// 	for (i = 0; argv[i]; i++) {
// 		for (j = 0; argv[i][j]; j++) {
// 			pmyData->argv[i][j] = argv[i][j];
// 		}
// 		pmyData->argv[i][j] = '\0';
// 	}

	static MYDATA pmyData[MAX_THREAD_COUNT] = {0};
	for (nNumThread = 0; nNumThread<MAX_THREAD_COUNT; nNumThread++) {
		pmyData[nNumThread].argc = argc;
		for (i = 0; argv[i]; i++) {
			pmyData[nNumThread].argv[i] = malloc(80);
			strcpy(pmyData[nNumThread].argv[i], argv[i]);
		}
	}
	for (nNumThread = 0; nNumThread<MAX_THREAD_COUNT; nNumThread++) {
		myThread[nNumThread] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE )ThreadProc, (LPVOID)&pmyData[nNumThread], 0, dwThreadId+nNumThread);
		if (NULL == myThread[nNumThread]) {
			exit(1);
		}		
	}

	WaitForMultipleObjects(MAX_THREAD_COUNT, myThread, TRUE, INFINITE);
	// Close all thread handles upon completion.
	for( j = MAX_THREAD_COUNT-1; j >= 0; j--)
	{
		CloseHandle(myThread[j]);
	}

	return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值