⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendtrap.c

📁 wm PNE 3.3 source code, running at more than vxworks6.x version.
💻 C
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/unxagent/sun/sendtrap.c,v 1.3 2002/03/11 19:11:29 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 1988-1997 Epilogue Technology Corporation. *  Copyright 1998 Integrated Systems, Inc. *  All rights reserved. ****************************************************************************//* * $Log: sendtrap.c,v $ * Revision 1.3  2002/03/11 19:11:29  josh * add v3-specific userexits to unxagent so that it links with * full set of libraries * * Revision 1.2  2001/11/09 21:49:03  josh * unxagent demo path adjustment, first pass * * Revision 1.1.1.1  2001/11/05 17:49:17  tneale * Tornado shuffle * * Revision 7.9  2001/01/19 22:25:03  paul * Update copyright. * * Revision 7.8  2000/03/17 00:15:31  meister * Update copyright message * * Revision 7.7  1998/06/28 21:55:52  sar * #ifdef some static defs to avoid compiler warnings * * Revision 7.6  1998/06/07 03:04:44  sar * Moved the global variables for the non-volatile configuration stuff * and the community strings to the snark library. * Also changed the names of the configuration variables * * Revision 7.5  1998/02/25 04:58:22  sra * Update copyrights. * * Revision 7.4  1997/03/20 06:54:13  sra * DFARS-safe copyright text.  Zap! * * Revision 7.3  1997/02/25 10:58:16  sra * Update copyright notice, dust under the bed. * * Revision 7.2  1997/01/08 23:01:49  sar * Updated copyright and modified include files to use envoy/h as * appropriate * * Revision 7.1  1996/12/04  18:53:10  sar * Corrected the var bind count when sending a v2 trap * * Revision 7.0  1996/03/15  22:07:57  sar * Updated revision to 7.0 and copyright to 96 * * Revision 6.1  1995/11/01  01:07:10  sar * added pp style argument lists * modified the v2 traps to the current scheme (community based) * * Revision 6.0  1995/05/31  21:49:48  sra * Release 6.0. * * Revision 5.4  1995/05/02  23:25:51  sar * Minor cleanup to make compilers happier. * * Revision 5.3  1995/03/21  19:40:54  sar * Use sysuptime instead of get_sysUpTime se we don't have to deal with * getsysUpTime's argument list. * * Revision 5.2  1994/11/01  16:00:05  sar * moved the defs for some oidcs around to make some compilers happy. * * Revision 5.1  1994/10/14  19:34:02  sar * Added a routine to send out v2 traps using the create_and_send * (actually create_encode_and_send) routine. * * Revision 5.0  1994/05/16  16:20:43  sar * Updated revision to 5.0 and copyright to include 1994 * * Revision 4.0  1993/06/24  17:31:48  sar * Updated rev to 4.0 copyright to 93 * and made the if defined(install..) checks be if install * * Revision 3.5  1993/06/09  21:16:10  sar * modified call to get_sysUpTime to use the correct arguments. * * Revision 3.4  1993/05/13  22:22:28  sar * Added some defines, includes (<sysent.h> <errno.h> <libc.h>), casts * (struct sockaddr *) and changed memfoo to MEMFOO and objidcmp to * llist_cmp to get rid of warnings from the compiler. * * Revision 3.3  1993/04/26  20:20:12  sar * Added ifdefs to allow clean version 1 and version 2 makes. * * Revision 3.2  1993/04/22  20:23:42  sar * Updated macros and defines to use the naming scheme. * For example we now use INSTALL_ENVOY_SNMP_VERSION_1 or _2. * * Revision 3.1  1993/02/18  20:29:44  sar * Updated check on SGRP to be either a check on SGRPv1 or (SGRPv1 || SGRPv2) * * Revision 3.0  1992/04/03  19:53:44  dab * Release 3.0 * * Revision 2.101  92/02/04  10:47:29  dab * Updated for release 3.0 of SNMP. *  * Revision 2.100  92/02/03  16:46:15  dab * Generic unix SNMP agent. *  *  *    Rev 2.1   28 May 1990 16:07:46 * Corrected error in which address of Get_sysUptime was being passed * rather then the result of that routine. *  *    Rev 2.0   31 Mar 1990 15:34:14 * Initial revision.*//* [clearcase]modification history-------------------01a,19apr05,job  update copyright notices*/#include <sys/types.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/snmpstat.h>#include <wrn/wm/snmp/engine/buildpkt.h>#include <wrn/wm/snmp/engine/auxfuncs.h>#include <wrn/wm/snmp/engine/wkobj.h>#include <wrn/wm/snmp/engine/objectid.h>#include "snmpvars.h"#include <sys/socket.h>#include <netinet/in.h>#include "../agent.h"#include <wrn/wm/demo/snmpfunc.h>extern int errno;extern UINT_32_T	sysuptime();#if INSTALL_ENVOY_SNMP_VERSION_1static	void	send_a_trap __((int, int, u_long));#endif#if INSTALL_ENVOY_SNMP_VERSION_2static  void    send_v2_traps __((int, int, u_long));#endifvoid  send_traps(int fromsock,	     int ttype){int	thnum;for (thnum = 0; thnum < trap_2_cnt; thnum++) {#if INSTALL_ENVOY_SNMP_VERSION_1   send_a_trap(fromsock, ttype, traplist[thnum]);#endif#if INSTALL_ENVOY_SNMP_VERSION_2   send_v2_traps(fromsock, ttype, traplist[thnum]);#endif   }}#if INSTALL_ENVOY_SNMP_VERSION_1static void  send_a_trap(int    fromsock,	      int    ttype,	      u_long host_addr){EBUFFER_T		ebuff;struct sockaddr_in	traps_to;SNMP_PKT_T *trap_pkt;EBufferInitialize(&ebuff);/* Generate and send a generic trap. */trap_pkt = SNMP_Create_Trap(SNMP_VERSION_1, strlen(snmp_trap_community),			    snmp_trap_community, snmp_product_id_count,			    snmp_product_id,			    (OCTET_T *)snmp_local_ip_address,			    ttype, 0, sysuptime(), 0);if (trap_pkt == 0)  return;if (SNMP_Encode_Packet(trap_pkt, &ebuff) == -1) {  SNMP_Free(trap_pkt);  printf("Failure encoding startup trap\n");  return;  }SNMP_Free(trap_pkt);/* Transmit the Trap PDU to the trap port on the trap   catching machine.  Note that in real life traps may   be sent to more than one catcher.   */traps_to.sin_family = AF_INET;traps_to.sin_port = htons(162);traps_to.sin_addr.s_addr = host_addr; (void) sendto(fromsock, ebuff.start_bp, EBufferUsed(&ebuff),	       0, (struct sockaddr *)&traps_to, sizeof(traps_to));SGRPv1_INC_COUNTER(snmp_stats.snmpOutPkts);SGRPv1_INC_COUNTER(snmp_stats.snmpOutTraps);EBufferClean(&ebuff);}#endif /* INSTALL_ENVOY_SNMP_VERSION_1 */#if INSTALL_ENVOY_SNMP_VERSION_2OIDC_T csoidc[] = {SNMP_COLDSTART};OIDC_T wsoidc[] = {SNMP_WARMSTART};OIDC_T ldoidc[] = {SNMP_LINKDOWN};OIDC_T luoidc[] = {SNMP_LINKUP};OIDC_T afoidc[] = {SNMP_AUTHENTICATIONFAILURE};OIDC_T stoidc[] = {SNMP_SNMPTRAPOID, 0};OIDC_T sysupoidc[] = {1, 3, 6, 1, 2, 1, 1, 3, 0};/* routine to handle traps for v2 */static void  send_v2_traps(int    fromsock,		int    ttype,		u_long host_addr){EBUFFER_T		ebuff;struct sockaddr_in	traps_to;SNMP_PKT_T *trap_pkt;OBJ_ID_T trapoid;EBufferInitialize(&ebuff);switch (ttype) {    case COLD_START:	trapoid.component_list = csoidc;	trapoid.num_components = sizeof(csoidc)/sizeof(OIDC_T);	break;    case WARM_START:	trapoid.component_list = wsoidc;	trapoid.num_components = sizeof(wsoidc)/sizeof(OIDC_T);	break;    case LINK_DOWN:		trapoid.component_list = ldoidc;	trapoid.num_components = sizeof(ldoidc)/sizeof(OIDC_T);	break;    case LINK_UP:	trapoid.component_list = luoidc;	trapoid.num_components = sizeof(luoidc)/sizeof(OIDC_T);	break;    case AUTH_FAILURE:	trapoid.component_list = afoidc;	trapoid.num_components = sizeof(afoidc)/sizeof(OIDC_T);	break;    default:	return;    }trap_pkt = SNMP_Create_Request2(TRAP2_PDU, SNMP_VERSION_2,				strlen(snmp_trap_community),				snmp_trap_community, 0, 2, 0, 0);if (trap_pkt == 0)    return;SNMP_Bind_Timeticks(trap_pkt, 0, sizeof(sysupoidc)/sizeof(OIDC_T), sysupoidc,		    sysuptime());SNMP_Bind_Object_ID(trap_pkt, 1, sizeof(stoidc)/sizeof(OIDC_T), stoidc,		    trapoid.num_components, trapoid.component_list);if (SNMP_Encode_Packet(trap_pkt, &ebuff) == -1) {  SNMP_Free(trap_pkt);  printf("Failure encoding startup trap\n");  return;  }SNMP_Free(trap_pkt);/* Transmit the Trap PDU to the trap port on the trap   catching machine.  Note that in real life traps may   be sent to more than one catcher.   */traps_to.sin_family = AF_INET;traps_to.sin_port = htons(162);traps_to.sin_addr.s_addr = host_addr; (void) sendto(fromsock, ebuff.start_bp, EBufferUsed(&ebuff),	       0, (struct sockaddr *)&traps_to, sizeof(traps_to));SGRPv1_INC_COUNTER(snmp_stats.snmpOutPkts);SGRPv1_INC_COUNTER(snmp_stats.snmpOutTraps);EBufferClean(&ebuff);}#endif /* INSTALL_ENVOY_SNMP_VERSION_2 */#if INSTALL_ENVOY_SNMP_VERSION_3#if INSTALL_ENVOY_SNMP_V3_TARGET/******************************************************************************** envoy_taddress_to_snmpaddr** This routine and its companion envoy_snmpaddr_to_taddress will be* used to convert between an SNMPADDR_T and a TAddress/TDomain pair.* In the case of these demos, the SNMPADDR_T is an opaque structure* equivalent to a (struct sockaddr_in).  For SNMP over UDP, the* TDomain will always be 1.3.6.1.6.1.1, and the TAddress will be 6* bytes long: 4 bytes of address and 2 bytes of port, all in* network order.** RETURNS: 0 on success, -1 on failure.** SEE ALSO: envoy_snmpaddr_to_taddress()*/int   envoy_taddress_to_snmpaddr(SNMPADDR_T *addr,			     OBJ_ID_T   *tdomain,			     EBUFFER_T  *taddress){struct sockaddr_in sock;bits16_t port;bits32_t ipaddr;OIDC_T udpdomain[] = {1, 3, 6, 1, 6, 1, 1}; if (oidcmp(tdomain->num_components, tdomain->component_list,	   sizeof(udpdomain)/sizeof(OIDC_T), udpdomain) == 1) {    sock.sin_family = AF_INET;    MEMCPY(&ipaddr, EBufferStart(taddress), 4);    MEMCPY(&port, (EBufferStart(taddress) + 4), 2);    sock.sin_port = port;    sock.sin_addr.s_addr = ipaddr;    MEMCPY(addr, &sock, sizeof(sock));    return 0;    }return -1;}/******************************************************************************** envoy_snmpaddr_to_taddress** This routine and its companion envoy_taddress_to_snmpaddr will be* used to convert between an SNMPADDR_T and a TAddress/TDomain pair.* In the case of these demos, the SNMPADDR_T is an opaque structure* equivalent to a (struct sockaddr_in).  For SNMP over UDP, the* TDomain will always be 1.3.6.1.6.1.1, and the TAddress will be 6* bytes long: 4 bytes of address and 2 bytes of port, all in* network order.** RETURNS: 0 on success, -1 on failure.** SEE ALSO: envoy_snmpaddr_to_taddress()*/int  envoy_snmpaddr_to_taddress(SNMPADDR_T *addr,			     OBJ_ID_T   *tdomain,			     EBUFFER_T  *taddress){struct sockaddr *sock = (struct sockaddr *)addr;struct sockaddr_in sock_in;bits16_t port;bits32_t ipaddr;bits8_t tadd[6];OIDC_T udpdomain[] = {1, 3, 6, 1, 6, 1, 1};if (sock->sa_family == AF_INET) {  (void) memcpy((char *)(&sock_in), (char *)addr, sizeof(struct sockaddr_in));  if (build_object_id(sizeof(udpdomain)/sizeof(OIDC_T), udpdomain, tdomain))    return -1;  ipaddr = sock_in.sin_addr.s_addr;  port = sock_in.sin_port;  MEMCPY(tadd, &ipaddr, 4);  MEMCPY(tadd + 4, &port, 2);  if (EBufferAllocateLoad(BFL_IS_ALLOC, taddress, tadd, 6)) {    Clean_Obj_ID(tdomain);    return -1;    }  return 0;  }return -1;}#endif /* #if INSTALL_ENVOY_SNMP_V3_TARGET */#endif /* #if INSTALL_ENVOY_SNMP_VERSION_3 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -