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

📄 ncp.c

📁 PPP协议C语言源程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/************************************************************************
*
*       Copyright (c) 1997-2001 by Accelerated Technology, Inc.
*
*  PROPRIETARY RIGHTS of Accelerated Technology are  involved in
*  the subject matter of this material.  All manufacturing,
*  reproduction, use, and sales rights pertaining to this subject
*  matter are  governed by the license agreement.  The recipient of
*     this software implicitly accepts the terms of the license.
*
*
*************************************************************************

*************************************************************************
* FILE NAME                                     VERSION
*
*     ncp.c                                       2.4
*
* COMPONENT
*
*     The Network Control Protocol for PPP. Specifically the
*     Internet Protocol Control Protocol (IPCP).
*
* DESCRIPTION
*
*     This file contains the network control protocols used by PPP to
*     negotiate network settings to be used over the link. This
*     implementation only contains the internet protocol control protocol
*     (IPCP). No other network protocols are suppported.
*
* DATA STRUCTURES
*
*     none
*
* FUNCTIONS
*
*     NCP_Init
*     PPP_NCP_IP_Interpret
*     NCP_IP_Send_Config_Req
*     NCP_IP_Req_Sent_State
*     NCP_IP_Ack_Rcvd_State
*     NCP_Timer_Expire
*     NCP_SnmpTimer_Expire
*     NCP_Change_IP_Mode
*     NCP_Configure_Req_Check
*     NCP_Configure_Nak_Check
*     NCP_IP_Ack_Sent_State
*     NCP_IP_Opened_State
*     NCP_IP_Code_Reject_Check
*     NCP_IP_Stopping_State
*     NCP_Set_Client_IP_Address
*
* DEPENDENCIES
*
*     nucleus.h
*     externs.h
*     tcp_errs.h
*     netevent.h
*     protocol.h
*     target.h
*     ppp.h
*
*
************************************************************************/

#include "plus/nucleus.h"
#include "net/inc/externs.h"
#include "net/inc/tcp_errs.h"
#include "net/inc/netevent.h"
#include "net/inc/ncl.h"
#include "net/target.h"        /* Compiler typedefs for data type sizes */

#include "ppp/inc/nu_ppp.h"

#if (INCLUDE_PPP_MIB == NU_TRUE)
#include SNMP_GLUE
#endif


/************************************************************************
* FUNCTION
*
*     NCP_Init
*
* DESCRIPTION
*
*     This function initializes the NCP layer of PPP. It creates the needed
*     timers and initializes the NCP structure for the passed in device.
*
* AUTHOR
*
*     Uriah Pollock
*
* INPUTS
*
*     DV_DEVICE_ENTRY     *dev_ptr     Pointer to the device that this
*                                       LCP layer belongs to.
*
* OUTPUTS
*
*     STATUS              NU_SUCCESS will be returned on if NCP is
*                         correctly initialized otherwise -1 will be
*                         returned or a Nucleus PLUS status will be
*                         returned.
*
************************************************************************/
STATUS NCP_Init (DV_DEVICE_ENTRY *dev_ptr)
{
    STATUS      ret_status;
    IPCP_LAYER  *ipcp;
    CHAR        timer_name[9]; /* max length for a PLUS timer name is 8 */
    CHAR        temp[5];       /* temp string to store the com port     */
#if (INCLUDE_PPP_MIB == NU_TRUE)
    INT32 retcode;
#endif

    /* Get a pointer to the lcp stucture for this device. */
    ipcp = &((LINK_LAYER *) dev_ptr->link_layer)->ipcp;

    /* Zero out the name */
    UTL_Zero (timer_name, 9);

    /* Build the NCP IPCP restart timer name */
    timer_name[0] = 'i';
    timer_name[1] = 'p';
    timer_name[2] = 'c';
    timer_name[3] = 'p';
    strcat (timer_name, (CHAR *)NU_ITOA ((INT)dev_ptr->dev_index, temp, 10));

    /* Create the restart timer, it will be used for NCP. */
    ret_status = NU_Create_Timer (&ipcp->restart_timer, timer_name,
                    NCP_Timer_Expire, (UNSIGNED) dev_ptr, LCP_TIMEOUT_VALUE,
                    LCP_TIMEOUT_VALUE, NU_DISABLE_TIMER);

    if (ret_status != NU_SUCCESS)
    {
        NERRS_Log_Error (NERR_RECOVERABLE, __FILE__, __LINE__);

        return(-1);
    }

    ipcp->network_options.use_primary_dns_server       = NCP_DEFAULT_PRI_DNS;
    ipcp->network_options.use_secondary_dns_server     = NCP_DEFAULT_SEC_DNS;
    ipcp->network_options.use_IpVanJacobsonCompression = NU_FALSE;
    
    
#if (INCLUDE_PPP_MIB == NU_TRUE) /* setup the values for this link from the PPP MIB */

    /* Create the SNMP periodic timer to report IPCP state to SNMP */
    ret_status = NU_Create_Timer (&ipcp->snmp_timer, "ipcpSNMP",
                    NCP_SnmpTimer_Expire, (UNSIGNED) dev_ptr,IPCP_SNMP_TIMEOUT_VALUE,
                    IPCP_SNMP_TIMEOUT_VALUE,NU_ENABLE_TIMER);

    if (ret_status != NU_SUCCESS)
    {
        NERRS_Log_Error (NERR_RECOVERABLE, __FILE__, __LINE__);

        return(-1);
    }


    /* create ppp status table entries first for PPP Mib */
    retcode = pppIpEntry_Add_Table_Entry (dev_ptr->dev_index);
    if (retcode == -2)
    {
        /* unable to create the table - no memory  */
        NERRS_Log_Error (TCP_FATAL, __FILE__, __LINE__);
        return(-1);
    }

    SNMP_SetpppIpStatusValues(dev_ptr->dev_index,
                              PPP_IP_OPER_STATUS_DOWN, NU_FALSE, NU_FALSE,
                              NU_FALSE, NU_FALSE);

    /* create ppp config table entries first for PPP Mib */
    retcode = pppIpConfigEntry_Add_Table_Entry (dev_ptr->dev_index);
     if (!retcode)  /* entry was created, so initialize everything  */
     {
        retcode = SNMP_SetpppIpConfigValues (dev_ptr->dev_index,
                              ipcp->network_options.use_IpVanJacobsonCompression);
        if (retcode == -2)
        {
            NERRS_Log_Error (TCP_FATAL, __FILE__, __LINE__);
            return(-1);
        }
     }
    else if (retcode == -1)
    {
        /* entry found, so use values from MIB table for this link */
        retcode = SNMP_GetpppIpConfigValues(dev_ptr->dev_index,
                           &ipcp->network_options.use_IpVanJacobsonCompression);
        if (retcode == -2)
        {
            NERRS_Log_Error (TCP_FATAL, __FILE__, __LINE__);
            return(-1);
        }
     }
     else
     {
        /* unable to create the table - no memory  */
        NERRS_Log_Error (TCP_FATAL, __FILE__, __LINE__);
        return(-1);
     }

#endif

    return (NU_SUCCESS);

} /* NCP_Init */

/************************************************************************
* FUNCTION
*
*     NCP_Reset
*
* DESCRIPTION
*
*     Resets the NCP DNS server addresses. This is done in preparation
*     for a new PPP session.
*
* AUTHOR
*
*     Uriah Pollock
*
* INPUTS
*
*     IPCP_LAYER        *ipcp_ptr    Pointer to the IPCP structure to
*                                      be reset.
*
* OUTPUTS
*
*     none
*
************************************************************************/
VOID NCP_Reset (IPCP_LAYER *ipcp_ptr)
{
    /* Zero out the primary and secondary DNS servers. */
    UTL_Zero (ipcp_ptr->primary_dns_server, IP_ADDRESS_LENGTH);
    UTL_Zero (ipcp_ptr->secondary_dns_server, IP_ADDRESS_LENGTH);

}

/************************************************************************
* FUNCTION
*
*     NCP_InjectSnmpEvent
*
* DESCRIPTION
*
*     API to injects an open or close event to the NCP Layer (for SNMP)
*
* AUTHOR
*
*     Raj Johnson
*
* INPUTS
*
*     INT32 NCP_State  - PPP_IP_OPER_STATUS_UP or PPP_IP_OPER_STATUS_DOWN
*     INT32 PPP_Link   - for specific PPP link
*                        PPP Link and device index are same
*
* OUTPUTS
*
*     none
*
************************************************************************/

#if (INCLUDE_PPP_MIB == NU_TRUE)
VOID NCP_InjectSnmpEvent (INT32 NCP_State, INT32 dev_index)
{
    DV_DEVICE_ENTRY *dev_ptr;
    IPCP_LAYER      *ipcp;

    dev_ptr = DEV_Get_Dev_By_Index( dev_index );

    /* Grab a pointer to the IPCP layer stucture. */
    ipcp = &((LINK_LAYER *)dev_ptr->link_layer)->ipcp;

    /* Reset the NCP */
    NCP_Reset (ipcp);

    if( (NCP_State == PPP_IP_OPER_STATUS_UP) && (ipcp->state != OPENED) )
    {
        /* We need to send another configure request with the newest
           current set of configuration options. */
        UTL_Timerset (NCP_SEND_CONFIG, (UNSIGNED) dev_ptr, 0, 0);
        ipcp->state = REQ_SENT;
#ifdef NU_DEBUG_PPP
        _PRINT ("IPCP State: REQ_SENT ");
#endif
    }
    else if ( (NCP_State == PPP_IP_OPER_STATUS_DOWN) && (ipcp->state == OPENED) )
    {
         ipcp->state = STOPPING;
#ifdef NU_DEBUG_PPP
         _PRINT ("IPCP State: STOPPING ");
#endif
         
    }

}
#endif

/************************************************************************
* FUNCTION
*
*     PPP_NCP_IP_Interpret
*
* DESCRIPTION
*
*     Based on the current state of PPP, this function passes the
*     incoming packet to the state function. After the packet has been
*     processed it is deallocated from the buffer list.
*
* AUTHOR
*
*     Uriah Pollock
*
* INPUTS
*
*     NET_BUFFER        *buf_ptr        Pointer to the incoming NCP
*                                        packet
*
* OUTPUTS
*
*     none
*
************************************************************************/
void PPP_NCP_IP_Interpret (NET_BUFFER *buf_ptr)
{
    LINK_LAYER      *link_layer;
    link_layer = (LINK_LAYER *)buf_ptr->mem_buf_device->dev_ppp_layer;

#ifdef NU_DEBUG_PPP
    _PRINT ("\nncp interpret\r\n");
#endif

    /* LCP must be in the opened state for NCP negotiation */
    if (link_layer->lcp.state != OPENED)
    {
#ifdef NU_DEBUG_PPP
        _PRINT ("discarded - lcp not open\r\n");
#endif
        link_layer->silent_discards++;
    }
    else
    {
        /* What we do will depends on the state of the NCP */
        switch (link_layer->ipcp.state)
        {
            case INITIAL    :

#ifdef NU_DEBUG_PPP
                _PRINT ("initial\r\n");
#endif
                /* This state not used. Execution will never
                   make it here. */

                break;

            case STARTING   :

#ifdef NU_DEBUG_PPP
                _PRINT ("starting\r\n");
#endif

                /* This state is ONLY entered after the
                   modem has been hung up. If we ever
                   reach this point then there must be a problem
                   with the modem. So we will try to hang it
                   up again and ignore this packet. */

                /* Set the next state to goto after the modem hangsup */
                link_layer->lcp.echo_counter = STARTING;

                link_layer->disconnect(link_layer, NU_FALSE);

                break;

            case CLOSED     :

#ifdef NU_DEBUG_PPP
                _PRINT ("closed\r\n");
#endif

                /* This state not used. Execution will never
                   make it here. */

                break;

            case STOPPED    :

#ifdef NU_DEBUG_PPP
                _PRINT ("stopped\r\n");
#endif

                /* This state not used. Execution will never
                   make it here. */

                break;

            case CLOSING    :

⌨️ 快捷键说明

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