📄 sendtrap.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/snmptalk/sendtrap.c,v 1.4 2003/01/15 14:04:55 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1991-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: sendtrap.c,v $ * Revision 1.4 2003/01/15 14:04:55 josh * directory structure shifting * * Revision 1.3 2002/03/11 19:17:09 josh * snmptalk's destination address is now stored and passed around * as an ipaddr_t. This required changes to a lot of internal function * calls that used to expect an inaddr_t to be passed around. Also, * snmptalk is now capable of using IPv6 sockets * * Revision 1.2 2001/11/08 16:47:30 tneale * Updated for newset file layout * * Revision 1.1.1.1 2001/11/05 17:49:12 tneale * Tornado shuffle * * Revision 1.7 2001/04/13 21:10:53 josh * move the trap-sending code off of the kingfisher branch * and onto the trunk * * Revision 1.6 2001/01/19 22:24:49 paul * Update copyright. * * Revision 1.5.2.2 2001/03/12 22:11:16 tneale * Updated copyright * * Revision 1.5.2.1 2001/01/19 21:38:28 josh * a new mechanism for hardcoding traps with arbitrary varbind lists * into an agent. useful for testing purposes * * Revision 1.5 2000/03/17 00:14:35 meister * Update copyright message * * Revision 1.4 2000/02/02 17:36:28 josh * use VBList_Free() API call * * Revision 1.3 1999/11/05 20:59:36 josh * fixing an unaligned access problem with SNMPADDR_Ts * * Revision 1.2 1999/11/04 23:31:12 josh * fixing memory leaks found by Purify * * Revision 1.1 1999/10/22 21:14:44 josh * quick-n-dirty code to send & receive notifies over Attache only * * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include <wrn/wm/common/install.h>#include <wrn/wm/common/config.h>#include <snmptalk.h>#include <stdio.h>#include <wrn/wm/attache/config.h>#include <wrn/wm/common/types.h>#include <wrn/wm/attache/mib.h>#include <wrn/wm/attache/timer.h>#include <wrn/wm/attache/packet.h>#include <wrn/wm/attache/net.h>#include <wrn/wm/attache/route.h>#include <wrn/wm/attache/ip.h>#include <wrn/wm/attache/glue.h>#include <wrn/wm/attache/arp.h>#include <wrn/wm/attache/udp.h>#include <wrn/wm/attache/slowtime.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/localio.h>#include <wrn/wm/snmp/engine/buffer.h>#include <wrn/wm/snmp/engine/objectid.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/wkobj.h>#include <wrn/wm/snmp/engine/v3_eng.h>#include <wrn/wm/snmp/engine/v3_ntfy.h>#include <wrn/wm/snmp/engine/sendntfy.h>#include "snmpint.h"#include "md.h"#include "readmib.h"#include "mibutils.h"#include <wrn/wm/demo/snarklib.h>#include <wrn/wm/demo/snmpconf.h>#include <wrn/wm/demo/strdup.h>#include <wrn/wm/common/glue.h>enum help_level { help_none, help_short, help_long };/* envoy completion routine. send the response back where the request * came from */void notify_complete(SNMPADDR_T *for_addr, SNMPADDR_T *loc_addr, PTR_T pktp, ALENGTH_T need, PTR_T cookie){struct udp_conn udpc;EBUFFER_T ebuff;packet *p;bits32_t for_ip, loc_ip;EBufferInitialize(&ebuff);p = udp_alloc(need, 0);if (!p) return;MEMSET(&udpc, 0, sizeof(struct udp_conn));MEMCPY(&for_ip, for_addr->snmp_data, 4);MEMCPY(&loc_ip, loc_addr->snmp_data, 4);ip_conn_set_dst(udp_conn_get_ip_conn(&udpc), for_ip); ip_conn_set_src(udp_conn_get_ip_conn(&udpc), loc_ip);EBufferSetup(BFL_IS_STATIC, &ebuff, p->pkt_data, p->pkt_datalen);if (SNMP_Process_Finish((SNMP_PKT_T *)pktp, &ebuff, need) == 0) { /* build a control block to send a response back where the * request came from */ udp_conn_set_dst(&udpc, for_addr->snmp_port); udp_conn_set_src(&udpc, loc_addr->snmp_port); udp_send_conn(p, &udpc); EBufferClean(&ebuff); }}void notify_err_complete(SNMPADDR_T *for_addr, SNMPADDR_T *loc_addr, int error_code, PTR_T cookie){ return;}/* just need to release the packet if there's an error */void notify_cleanup(PTR_T cookie){if (cookie) SNMP_memory_free(cookie);}boolean_t do_sendtrap(struct sty *sty, enum help_level help, int argc, char *argv[]){bits8_t context[] = "";SNMPADDR_T loc_addr;SNMPV3_TRAP_T *trap;switch (help) { case help_short: sty_puts(sty, "send_trap <trap_name> <notify_name> - send out a trap\n"); break; case help_long: sty_puts(sty, "\The 'send_trap' command builds and sends a trap packet using the SNMPv3\n\Target and Notify MIBs from RFC2573.\n"); break; case help_none: if (argc < 3) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Need to specify <trap_name> <notify_name>\n"); return(1); } MEMSET(&loc_addr, 0, sizeof(SNMPADDR_T)); loc_addr.snmp_port = 161; for (trap = trap_root; trap; trap = trap->next) { if (STRNCMP(argv[1], EBufferStart(&trap->trap_name), EBufferUsed(&trap->trap_name)) == 0) break; } SNMP_Send_Notify_Name(argv[2], STRLEN(argv[2]), trap->vblp, &loc_addr, context, 0, notify_complete, notify_err_complete, notify_cleanup, 0); }return 1;}void notify_rcv(packet *p, inaddr_t from, bits16_t port, void *hint){SNMPADDR_T faddr, laddr;SNMP_PKT_T *rp;int error_code; MEMSET(&faddr, 0, sizeof(SNMPADDR_T));MEMSET(&laddr, 0, sizeof(SNMPADDR_T));MEMCPY(faddr.snmp_data, &from, sizeof(inaddr_t));faddr.snmp_port = port;laddr.snmp_port = 162;rp = SNMP_Decode_Packet_WER((unsigned char *)p->pkt_data, (int)p->pkt_datalen, &faddr, &laddr, &error_code);udp_free(p);if (rp == 0) return; if (rp->maxpkt > SNMP_MAX_PACKET_SIZE) rp->maxpkt = SNMP_MAX_PACKET_SIZE;rp->io_complete = notify_complete;rp->error_complete = notify_err_complete;rp->async_cookie = 0;rp->cont = 0;if (error_code != 0) { ENVOY_Send_SNMP_Packet(rp); SNMP_Free(rp); return; }/* We're just blackholing everything we receive, except for Informs, * which we respond to. */if (rp->pdu_type == INFORM_REQUEST_PDU) { rp->pdu_type = GET_RESPONSE_PDU; ENVOY_Send_SNMP_Packet(rp);}SNMP_Free(rp);}void notify_init(){ udp_listen(162, notify_rcv, (void (*)(inaddr_t, bits16_t, void *)) 0, (bits32_t)0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -