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

📄 snmp_io.c

📁 wm PNE 3.3 source code, running at more than vxworks6.x version.
💻 C
📖 第 1 页 / 共 3 页
字号:
    {    (void) close (snmpSocket);#if INSTALL_SNMP_VXWORKS_IPV6    (void) close (snmpSocketV6);#endif    }    /********************************************************************************* snmpIoMain - main SNMP I/O routine* * This routine is invoked by the agent after it has successsfully* completed the preliminary initializations. In this routine, the user* is required to initialize the transport endpoint and the views, and* then complete initialization of the agent by calling* snmpdInitFinish(). Any configuration required for snmpIoTrapSend()* must be done before calling snmpdInitFinish() since it will be* invoked in there. Any hooks that are required by the user must be* passed to snmpdInitFinish().  The transport endpoint must be* initialized before snmpdInitFinish() is called; the main loop is* then invoked. Before calling snmpIoBody(), the routine which waits on * the socket, it calls snmpMonitorSpawn() which spawns the master-agent* task.** RETURNS:  This routine should never return, except on failure.** SEE ALSO* snmpdInitFinish()*/void snmpIoMain (void)    {    /* Initialize the endpoint and return if no endpoint is available */    if (snmpIoInit () == ERROR)        {        return;        }    /* Initialize the databases.  This can be done here or later.       If we have 2275 views or v3 installed we use the set specified       in nvhard.c, otherwise 1445 views are in use and we use the       views in snmpIoViewInit() which is later in this file */    ENVOY_SNMP_GET_WRITE_LOCK(SNMP_CoarseLock);#if (INSTALL_ENVOY_SNMP_VERSION_3 || INSTALL_ENVOY_SNMP_RFC2275_VIEWS)    SNMP_NV_Config(0);#else /* if it isn't 2275 it must be 1445 views */    snmpIoViewInit ();#endif    ENVOY_SNMP_RELEASE_WRITE_LOCK(SNMP_CoarseLock);    /* Complete the initialization of the agent. Trap configuration     * should be done by this point. Since we do not use any hooks by default     * we pass NULL pointers. All the hook routines expect a pointer to      * the SNMP packet.     */    snmpdInitFinish (NULL, NULL, NULL, NULL, NULL);        /* Invoke the main routine that waits on the AgentX socket */#if INSTALL_AXMASTER    DYNCFG_IFCFGVBL_BEGIN(agentx_master_component)    DYNCFG_FUNCALL(snmpAxMonitorSpawn) ();    DYNCFG_IFCFGVBL_END(agentx_master_component)#endif    /* Invoke the main routine that starts the AgentX subagent */#if INSTALL_AXSUB    DYNCFG_IFCFGVBL_BEGIN(agentx_subagent_component)    DYNCFG_FUNCALL(snmpAxSubagentSpawn) ();    DYNCFG_IFCFGVBL_END(agentx_subagent_component)#endif    /* Register vsSnmpUtil component */ #if INSTALL_SNMP_VXWORKS_VIRTUAL_STACK    /* If necessary start the virutal stack pieces */    if (usrVsSnmpUtilStart() != OK) {        return;        }#endif /* INSTALL_SNMP_VXWORKS_VIRTUAL_STACK */    /* We're ready to release the init semaphore */    snmpInitDone = 1;    semGive (snmpdInitFinishedSem);    /* Call the routine which waits on the socket */    snmpIoBody ();    }/********************************************************************************* snmpIoBody -  The top level loop of the snmp agent .** Waits on the endpoint till pkt is received and then invokes* the received pkt manager** RETURNS : N/A* NOMANUAL**/LOCAL void snmpIoBody (void)    {    int                       pktSize;           /* size of recvd packet */    struct sockaddr_in        remoteAddr;    /* remote transport address */    struct sockaddr_in        localAddr;     /* local transport address */#if INSTALL_SNMP_VXWORKS_IPV6    struct sockaddr_in6       remoteV6Addr;    struct sockaddr_in6       localV6Addr;    int                       sockV6Size;#endif /* #if INSTALL_SNMP_VXWORKS_IPV6 */    int                       maxSock;    char                      rcvBuf [SNMP_BUF_SIZE];  /* buffer for input */    int                       sockSize;    snmpdLog (SNMP_INFO, "Waiting for input...\n");    maxSock = snmpSocket + 1;#if INSTALL_SNMP_VXWORKS_IPV6    if (maxSock <= snmpSocketV6)        maxSock = snmpSocketV6 + 1;        MEMSET (&localV6Addr, 0, sizeof (localV6Addr));    sockV6Size = sizeof (struct sockaddr_in6);#endif /* #if INSTALL_SNMP_VXWORKS_IPV6 */    MEMSET (&localAddr, 0, sizeof (localAddr));        sockSize = sizeof (struct sockaddr_in);    while (1)        {        fd_set ready;        /* set up the select call and then wait for a packet */        FD_ZERO (&ready);        FD_SET (snmpSocket, &ready);#if INSTALL_SNMP_VXWORKS_IPV6        FD_SET (snmpSocketV6, &ready);#endif /* #if INSTALL_SNMP_VXWORKS_IPV6 */        if (select (maxSock, &ready, 0, 0, 0) < 0)            {            snmpdLog (SNMP_INFO, "select failed...\n");            break;            }        /* if it is a UDP packet it's an SNMP packet */        if (FD_ISSET (snmpSocket, &ready))            {            if ((pktSize = recvfrom (snmpSocket, rcvBuf, SNMP_BUF_SIZE, 0,                                     (struct sockaddr *) &remoteAddr,                                     &sockSize)) < 0)                {                if (errno == EWOULDBLOCK)                    continue;                break;                }            sockSize = sizeof (struct sockaddr_in);            getsockname (snmpSocket, (struct sockaddr *) &localAddr,                         &sockSize);            snmpdLog (SNMP_INFO, "Received packet...\n");            snmpdPktProcess (pktSize, rcvBuf, &remoteAddr,                             &localAddr, &snmpSocket);            }#if INSTALL_SNMP_VXWORKS_IPV6        /* if it is a UDP over IPV6 packet it's an SNMP packet */        if (FD_ISSET (snmpSocketV6, &ready))            {            if ((pktSize = recvfrom (snmpSocketV6, rcvBuf, SNMP_BUF_SIZE, 0,                                     (struct sockaddr *) &remoteV6Addr,                                     &sockV6Size)) < 0)                {                if (errno == EWOULDBLOCK)                    continue;                break;                }            sockV6Size = sizeof (struct sockaddr_in6);            getsockname (snmpSocketV6, (struct sockaddr *) &localV6Addr,                         &sockV6Size);            snmpdLog (SNMP_INFO, "Received IPV6 packet...\n");            snmpdPktProcess (pktSize, rcvBuf, &remoteV6Addr,                             &localV6Addr, &snmpSocketV6);            }#endif /* #if INSTALL_SNMP_VXWORKS_IPV6 */                }    }/********************************************************************************* snmpIoTrapSend - send a standard SNMP or MIB-II trap* * This routine sends a standard SNMP or MIB-II trap message to the* network.  It is called by the SNMP agent at startup (to indicate a* cold start) and when interface states change. It takes two* arguments: <trapType>, the trap type, and <trapSpecific>, the* user-defined specifics on this trap.** The agent designer must rewrite this according to specific transport needs.** RETURNS: N/A** SEE ALSO: snmpdTrapSend()*/void snmpIoTrapSend    (    int   trapType,            /* Type of the trap */    int   trapSpecific         /* User defined specifics */    )    {    void *                    pDestAddr;    struct sockaddr_in        destAddr;    u_long                    ipAddr;        /* ip address to transmit, presently 0  */    ipAddr = 0;        /*     * Set up the destination address table. Presently we just send     * the traps to the boot host. Rewrite according to your needs     */    destAddr.sin_family = AF_INET;    destAddr.sin_port = htons (TRAP_PORT);     destAddr.sin_addr.s_addr = hostGetByName (sysBootHost);    pDestAddr =  &destAddr;         snmpdTrapSend (&snmpSocket, 1, & pDestAddr, NULL, SNMP_VERSION_1,                    "trap community", snmpTrapMyOid, MYOIDLEN, &ipAddr,                    trapType, trapSpecific, 0, 0, 0);    }#if !(INSTALL_ENVOY_SNMP_VERSION_3 || INSTALL_ENVOY_SNMP_RFC2275_VIEWS)/******************************************************************************** snmpIoViewInit - sample view-initialization routine** This routine creates the initial view table at agent-startup time.* The agent designer should select the  views according to need.** RETURNS: N/A* NOMANUAL*/void snmpIoViewInit (void)    {    /* This entry restricts the view to the system group & snmpv3 subtree     * for communities which map to a view index of 1     */    snmpdViewEntrySet (viewTree1a, ARRAY_LEN (viewTree1a), 1, viewMask1,                        sizeof (viewMask1) , VIEW_INCLUDED );    snmpdViewEntrySet (viewTree1b, ARRAY_LEN (viewTree1b), 1, viewMask1,                        sizeof (viewMask1) , VIEW_INCLUDED );    /* This entry gives unrestricted access to the org sub tree for communities     * which map to a view index of 2     */    snmpdViewEntrySet (viewTree2, ARRAY_LEN (viewTree2), 2, viewMask2,                        sizeof (viewMask2) , VIEW_INCLUDED );    /* The next  entry  restrict the view to the icmp group for      * communities which map to a view index of 3     */    snmpdViewEntrySet (viewTree3, ARRAY_LEN (viewTree3), 3, viewMask3,                        sizeof (viewMask3) , VIEW_INCLUDED );    }#endif/******************************************************************************** snmpIoValidateCommon - common code for the various validation routines** This routine is used by all of the validation routines to do common* tasks such as copy the addresses and zero the private field** RETURNS: 0 if everything was acceptable, otherwise 1.* NOMANUAL*/static int snmpIoValidateCommon    (    SNMP_PKT_T *    pPkt,               /* ptr to snmp pkt */    SNMPADDR_T *    pRemoteAddr,        /* remote address */    SNMPADDR_T *    pLocalAddr          /* local address */    ){    int sizeSock = sizeof (struct sockaddr_in);    /* We don't use private memory so set this to NULL else the     *  hook to release this is invoked if non NULL      */    pPkt->user_private = (char *) 0;    /*      * Install the local and remote network addresses in the packet.     * These will be used later to respond to this pdu     */#if INSTALL_SNMP_VXWORKS_IPV6    if (((struct sockaddr *)pRemoteAddr)->sa_family == AF_INET6)        sizeSock = sizeof (struct sockaddr_in6);#endif /* #if INSTALL_SNMP_VXWORKS_IPV6 */    (void) memcpy ((char *) &(pPkt->pkt_src), (char *) pRemoteAddr,                    sizeSock);    (void) memcpy((char *) &(pPkt->pkt_dst), (char *) pLocalAddr,                    sizeSock);    return(0);}/******************************************************************************** snmpIoCommunityValidate - sample community validation routine* * This routine is used to set up the view-index field in the SNMP* packet.  There are two versions of this routine one for use with* a static community string table and one for use with a dynamic * community string table.  The static table is is shipped with defaults* such that the "priv" community is allowed to `set' variables, and* the "pub" community is allowed to `get' variables.  In the dynamic* version names for different virtual stacks (VS0, VS1) may be added** The agent designer is required to write this function according to* the design of the application.** RETURNS: 0 if the community is acceptable, otherwise 1.*/#if INSTALL_SNMP_VXWORKS_VIRTUAL_STACK/** In this copy of the routine we attempt to find the community* string in the dynamic table and if necessary set the virtual stack* number as specified.*/int snmpIoCommunityValidate    (    SNMP_PKT_T *    pPkt,               /* ptr to snmp pkt */    SNMPADDR_T *    pRemoteAddr,        /* remote address */    SNMPADDR_T *    pLocalAddr          /* local address */    )    {    int vsNum;  /* when virtual stacks are installed this variable                 * is used to pass the proper stack number around */    /* Assume that the packet will be found acceptable and do the     * common work. */    if (snmpIoValidateCommon(pPkt, pRemoteAddr, pLocalAddr) != 0) {        return(1);        }        vsNum = communityStringFind (pPkt->community,                                 ((pPkt->pdu_type == SET_REQUEST_PDU) ?                                  VIEW_TYPE_SET : VIEW_TYPE_GET),                                  &pPkt->view_name);    /* Check to see if we found a valid view.  If so and we have the     * virtual stack features turned on we attmpet to set the task     * id to the correct stack.  This allows the method routines to     * retrieve the MIB information from the proper stack.  If we     * don't have the virtual stack features enabled we simply return.     */    if ((vsNum != -1) && (SNMP_SET_VIRTUAL_STACK (vsNum) == OK)) {	return (0);        }    /* Got a bad community if we're here, so increment the     * snmpInBadCommunityNames counter     */    ++ snmp_stats.snmpInBadCommunityNames;            /* An auth fail trap may be sent here */        return (1);    }#else /* INSTALL_SNMP_VXWORKS_VIRTUAL_STACK *//* * The following is the static community string table.  It is not * used if SNMP_REGISTER_NAMING_SCOPES is installed.  In that case * the table is built dynamically.  See namingScope.c for more details. */ #if INSTALL_ENVOY_SNMP_RFC2275_VIEWS

⌨️ 快捷键说明

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