📄 snmp_io.c
字号:
/* snmpIoLib.c - default transport routines for SNMP *//* * Copyright 1984-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. */#include <copyright_wrs.h>/* modification history -------------------------01n,28jun05,job need some locking controls around socket creation01m,17may05,job don't choke on a 0-length packet01l,19apr05,job update copyright notices01k,01dec03,job update copyright information01j,13aug03,job a new round of virtual stack modification01i,23jul03,job virtual stack fixes01h,22jul03,job add include file to eliminate warning01g,22apr03,job add RFC 3419 support to conversion routines01f,05dec00,ten added calls for initializing and starting up an AgentX subagent.01e,19apr98,ann added routine call to snmpMonitorSpawn(), masterIoInit() and masterQueCleanup(). Header files added. Added log statement in snmpIoWrite() (SPR# 21171)01d,12aug96,rjc memory allocation funcs moved in from snmpdLib.c. No more memory partition options.01d,18jun96,rjc fixes for SPR 6759 and 676101d,14jun96,jmo 3rd update man pages01c,11jun96,jmo 2nd update man pages01b,23may96,jmo updated man pages01a,10mar96,rjc written*//* * $Log: snmp_io.c,v $ * Revision 1.12 2003/01/15 14:05:12 josh * directory structure shifting * * Revision 1.11 2002/06/05 19:49:20 tneale * Added dynamic configuration of AgentX subagent * * Revision 1.10 2002/05/31 20:58:40 josh * some preliminary changes to allow the vxWorks SNMP agent to run * over a standard IPv6 sockets layer * * Revision 1.9 2002/05/23 19:42:44 josh * updates to support the entity MIB * * Revision 1.8 2002/05/01 19:09:17 josh * adding a new call snmpdShutdown() * * Revision 1.7 2001/11/15 21:53:50 josh * populate the proxy table a little * * Revision 1.6 2001/11/12 22:53:17 meister * Changed some DYNCFG_EXTERN_FUNCPTRS's to DYNCFG_FUNCPTR's * * Revision 1.5 2001/11/08 22:18:33 meister * rework pathnames * * Revision 1.4 2001/11/07 22:15:55 meister * rework pathnames again * * Revision 1.3 2001/11/06 21:50:53 josh * second (and hopefully final) pass of new path hacking * * Revision 1.2 2001/11/06 21:20:40 josh * revised new path hacking * * Revision 1.1.1.1 2001/11/05 17:47:49 tneale * Tornado shuffle * * Revision 1.8.4.5 2001/10/29 21:42:49 josh * adding a spot for the proxy forwarding routine * * Revision 1.8.4.4 2001/10/16 20:49:25 meister * added back dependencies on INSTALL_AXMASTER so that we can build * without dynamic config as well * * Revision 1.8.4.3 2001/10/16 18:30:08 meister * component file shuffling, use snmpdLog instead of raw printfs * * Revision 1.8.4.2 2001/09/27 13:12:48 meister * dynamic configuration rototill * * Revision 1.8.4.1 2001/09/25 17:11:07 josh * general cleanup and improvement of some default behavior * * Revision 1.8 2001/05/07 14:39:24 markadam * Changed the default view for "public" access to be the same as for "private" * access. Note, this only affects GETs. * * Revision 1.7 2001/04/16 19:38:42 josh * merging the kingfisher branch onto the trunk * * Revision 1.6.2.4 2001/03/12 22:11:32 tneale * Updated copyright * * Revision 1.6.2.3 2000/12/20 23:11:55 tneale * Changed preprocessor flags for AgentX to allow demo users to set values * independently * * Revision 1.6.2.2 2000/12/05 22:02:22 tneale * Added calls to start up AgentX subagent * * Revision 1.6.2.1 2000/10/13 21:26:59 josh * Adding AgentX master agent support to vxWorks agent * * Revision 1.6 2000/03/13 17:17:01 josh * cleanup to remove some warnings * * Revision 1.5 2000/03/09 13:55:02 josh * add some demo code for sending SNMPv3 notifies * * Revision 1.4 2000/03/08 20:09:17 josh * Adding V3 support to vxWorks port * * Revision 1.3 2000/03/07 21:17:25 josh * fixing log comments * * Revision 1.2 2000/03/07 20:58:35 josh * putting CVS headers and logs into files that were lacking * *//*DESCRIPTIONThis module implements the SNMP v1/v2c/v3 transport-dependent routines.INCLUDE FILES: snmpdIoLib.h*/#include <vxWorks.h>#include <selectLib.h>#include <stdio.h>#include <sys/socket.h>#include <sockLib.h>#include <sys/types.h>#include <netinet/in.h>#include <ioLib.h>#include <taskLib.h>#include <wrn/wm/snmp/vxagent/snmpdLib.h>#include <wrn/wm/snmp/vxagent/snmpIoLib.h>#include <wrn/wm/snmp/engine/view.h>#include <wrn/wm/snmp/engine/snmpstat.h>#include <wrn/wm/snmp/engine/auxfuncs.h>#include <hostLib.h>#include <inetLib.h>#include <wrn/wm/snmp/engine/buildpkt.h>#include <wrn/wm/snmp/engine/objectid.h>#include <m2Lib.h>#include <semLib.h>#if (INSTALL_ENVOY_SNMP_VERSION_3 || INSTALL_ENVOY_SNMP_RFC2275_VIEWS)#include <wrn/wm/demo/nvutils.h>#endif#if (INSTALL_ENVOY_SNMP_VERSION_3 && INSTALL_ENVOY_SNMP_V3_NOTIFY)#include <wrn/wm/snmp/engine/v3_ntfy.h>#include <wrn/wm/snmp/engine/sendntfy.h>#include <wrn/wm/snmp/engine/wkobj.h>#endif#include <wrn/wm/snmp/vxagent/axCfg.h>#include <wrn/wm/common/dyncfg.h>DYNCFG_VBL_DECLARE_EXTERN(agentx_master_component)DYNCFG_FUNCPTR0(axMasterIoInit,STATUS)DYNCFG_FUNCPTR0(snmpAxMonitorSpawn,void)DYNCFG_VBL_DECLARE_EXTERN(agentx_subagent_component)DYNCFG_FUNCPTR0(axSubagentIoInit,STATUS)DYNCFG_FUNCPTR0(snmpAxSubagentSpawn,void)#if INSTALL_SNMP_VXWORKS_VIRTUAL_STACK#include <netinet/vsLib.h>#include <wrn/wm/snmp/vxagent/namingScopes.h>#endif /* INSTALL_SNMP_VXWORKS_VIRTUAL_STACK *//* defines */#define SNMP_PORT 161 /* port for snmp */#define TRAP_PORT 162 /* port for snmp traps */#define ARRAY_LEN(X) (sizeof (X) / sizeof X[0])#define MYOIDLEN ARRAY_LEN (snmpTrapMyOid) /* length of agent oid */#define SNMP_BUF_SIZE 2048 /* size of SNMP buffer must be * >= SNMP_MIN_PACKET_SIZE and * <= SNMP_MAX_PACKET_SIZE in envoy.h *//* typedefs */#if ((CPU_FAMILY==I960) && (defined __GNUC__))#pragma align 1 /* tell gcc960 not to optimize alignments */#endif /* CPU_FAMILY==I960 *//* struct for community to view index mapping */#if INSTALL_ENVOY_SNMP_RFC2275_VIEWStypedef struct { char * Cmnty; char * viewIndex; } CMNTY_TO_VIEW_T;#else /* if not 2275 then we are using 1445 */typedef struct { char * Cmnty; int viewIndex; } CMNTY_TO_VIEW_T;#endif#if ((CPU_FAMILY==I960) && (defined __GNUC__))#pragma align 0 /* turn off alignment requirement */#endif /* CPU_FAMILY==I960 *//* externs */IMPORT int snmpSocket;IMPORT SNMP_STATS_T snmp_stats;IMPORT char sysBootHost [];IMPORT BOOL snmpInitDone;IMPORT SEM_ID snmpdInitFinishedSem;IMPORT SEM_ID snmpdSocketControlSem;#if INSTALL_SNMP_VXWORKS_VIRTUAL_STACKIMPORT int snmpdStackNum;#endif /* INSTALL_SNMP_VXWORKS_VIRTUAL_STACK *//* locals */#if INSTALL_ENVOY_SNMP_RFC1445_VIEWS/* definitions used for view table sample function */LOCAL OIDC_T viewTree1a [] = {1, 3, 6, 1, 2, 1, 1}; /* system subtree */ LOCAL OIDC_T viewTree1b [] = {1, 3, 6, 1, 6, 3}; /* snmpv3 subtree */ LOCAL uchar_t viewMask1 [] = {0xff, 0xff, 0xff, 0xff}; /* subtree mask */LOCAL OIDC_T viewTree2 [] = {1, 3}; /* iso subtree */LOCAL uchar_t viewMask2 [] = {0xff}; /* subtree mask */LOCAL OIDC_T viewTree3 [] = {1, 3, 6, 1, 2, 1, 5}; /* icmp subtree */ LOCAL uchar_t viewMask3 [] = {0xff, 0xff, 0xff, 0xff}; /* subtree mask */#endifOIDC_T snmpTrapMyOid [] = {1,2,3,4,5,6}; /* agent oid used in trap * example , must be specified * by user */OIDC_T udpdomain[] = { 1, 3, 6, 1, 6, 1, 1 }; /* udp domain oid used when * creating taddress/tdomain * pairs */OIDC_T udpipv6domain[] = { 1, 3, 6, 1, 2, 1, 100, 1, 2 }; /* domain oid used for UDP over IPv6 *//* globals */int snmpSocket; /* socket descriptor for snmp */#if INSTALL_SNMP_VXWORKS_IPV6int snmpSocketV6;#endif /* #if INSTALL_SNMP_VXWORKS_IPV6 *//* forward declarations */LOCAL void snmpIoBody (void);void sendSampleNotify (char *, char *);#if INSTALL_SNMP_VXWORKS_VIRTUAL_STACKIMPORT STATUS usrVsSnmpUtilStart (void);#endif /* INSTALL_SNMP_VXWORKS_VIRTUAL_STACK *//********************************************************************************* snmpIoInit - initialization routine for SNMP transport endpoint** This is the routine to be called to initialize the transport endpoint* used by the SNMP agent. This routine is invoked from snmpIoMain().* This implementation is for a socket based system. It will also call the* masterIoInit() routine to create the IPC mechanism for the master agent.** GLOBALS* snmpSocket** RETURNS: ERROR if unable to create bound socket else OK.* * SEE ALSO: snmpIoMain()*/STATUS snmpIoInit (void) { struct sockaddr_in snmpSrvr;#if INSTALL_SNMP_VXWORKS_IPV6 struct sockaddr_in6 snmpSrvrV6;#endif /* #if INSTALL_SNMP_VXWORKS_IPV6 */ int maxPktSize = SNMP_MAX_PACKET_SIZE; /* Initialize the socket for interaction with manager */ if ( (snmpSocket = socket(PF_INET, SOCK_DGRAM, 0)) == ERROR) { snmpdLog (SNMP_ERROR, "Unable to open socket\n"); return (ERROR); } memset (&snmpSrvr, 0, sizeof (snmpSrvr)); snmpSrvr.sin_family = AF_INET; snmpSrvr.sin_port = htons (SNMP_PORT); snmpSrvr.sin_addr.s_addr = htonl (INADDR_ANY); if ( bind (snmpSocket, (struct sockaddr *)&snmpSrvr, sizeof(snmpSrvr)) == ERROR) { snmpdLog (SNMP_ERROR, "Can't bind server socket\n"); return (ERROR); } if (setsockopt (snmpSocket, SOL_SOCKET, SO_SNDBUF, (char*) &maxPktSize, sizeof (maxPktSize)) == ERROR) { snmpdLog (SNMP_WARN, "setsockopt failed\n"); }#if INSTALL_SNMP_VXWORKS_IPV6 if ((snmpSocketV6 = socket(PF_INET6, SOCK_DGRAM, 0)) == ERROR) { snmpdLog (SNMP_ERROR, "Unable to open IPV6 socket\n"); return (ERROR); } memset (&snmpSrvrV6, 0, sizeof (snmpSrvrV6)); snmpSrvrV6.sin6_family = AF_INET6; snmpSrvrV6.sin6_flowinfo = 0; snmpSrvrV6.sin6_port = htons (SNMP_PORT); snmpSrvrV6.sin6_addr = in6addr_any; if (bind(snmpSocketV6, (struct sockaddr *) &snmpSrvrV6, sizeof (snmpSrvrV6)) == ERROR) { snmpdLog (SNMP_ERROR, "Can't bind IPV6 server socket\n"); return (ERROR); }#endif /* #if INSTALL_SNMP_VXWORKS_IPV6 */ #if INSTALL_AXMASTER DYNCFG_IFCFGVBL_BEGIN(agentx_master_component) snmpdLog (SNMP_INFO, "Initializing AgentX listener\n"); if (DYNCFG_FUNCALL(axMasterIoInit) () == ERROR) snmpdLog (SNMP_WARN, "Error: could not initialize AgentX listener\n"); DYNCFG_IFCFGVBL_END(agentx_master_component)#endif #if INSTALL_AXSUB DYNCFG_IFCFGVBL_BEGIN(agentx_subagent_component) snmpdLog (SNMP_INFO, "Initializing AgentX subagent\n"); if (DYNCFG_FUNCALL(axSubagentIoInit) () == ERROR) snmpdLog (SNMP_WARN, "Error: could not initialize AgentX subagent\n"); DYNCFG_IFCFGVBL_END(agentx_subagent_component)#endif semGive(snmpdSocketControlSem); return (OK); } /******************************************************************************* ** snmpIoWrite - write a packet to the transport** This routine writes a datagram to the socket. The routine calls* sendto() with flags always set to 0. The <local> parameter is not* used in this case, but exists for conformance with our * transport-independent interface.** RETURNS: N/A** SEE ALSO: sendto() * .I (VxWorks Reference Manual)*/void snmpIoWrite ( void * pSocket, /* Socket descriptor for SNMP */ char * pBuf, /* Encoded SNMP packet */ int bufSize, /* Size of the packet */ void * pRemote, /* Remote address of manager */ void * pLocal /* Local address */ ) { struct sockaddr *forSock = (struct sockaddr *) pRemote; int szFrom;#if INSTALL_SNMP_VXWORKS_IPV6 if (forSock->sa_family == AF_INET6) szFrom = sizeof (struct sockaddr_in6); else szFrom = sizeof (struct sockaddr_in);#else szFrom = sizeof (struct sockaddr_in);#endif /* #if INSTALL_SNMP_VXWORKS_IPV6 */ (void) sendto (* (int *) pSocket, pBuf, bufSize, 0, forSock, szFrom); snmpdLog (SNMP_INFO, "Packet transmitted...\n"); } /******************************************************************************* * snmpIoClose - close the transport endpoint.** This routine is invoked to deallocate the transport endpoint.* It is invoked from the task deletion hook and also from snmpdExit().** RETURNS: N/A*/void snmpIoClose (void)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -