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

📄 lcp.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 implicity accepts the terms
* of the license.
*
****************************************************************************/
/***************************************************************************
*
*   FILENAME                                                    VERSION
*
*       LCP.C                                                   2.4
*
*   COMPONENT
*
*       Link Control Protocol for PPP
*
*   DESCRIPTION
*
*       This file contains the link control protocol used by PPP to establish
*       and negotiate the configuration options that will be over the link.
*
*   DATA STRUCTURES
*
*       None
*
*   FUNCTIONS
*
*       LCP_Init
*       LCP_Reset
*       LCP_Interpret
*       LCP_Send_Config_Req
*       LCP_Random_Number
*       LCP_Random_Number32
*       LCP_Timer_Expire
*       LCP_Configure_Req_Check
*       LCP_Send_Terminate_Ack
*       LCP_Send_Terminate_Req
*       LCP_Configure_Reject_Check
*       LCP_Send_Code_Reject
*       LCP_Ack_Sent_State
*       LCP_Req_Sent_State
*       LCP_Ack_Rcvd_State
*       LCP_Open_State
*       LCP_Send_Echo_Reply
*       LCP_Closing_State
*       LCP_Code_Reject_Check
*       LCP_Configure_Nak_Check
*       LCP_Stopping_State
*       LCP_Echo_Expire
*       LCP_Send_Echo_Req
*       LCP_Send_Protocol_Reject
*
*   DEPENDENCIES
*
*       nucleus.h
*       externs.h
*       tcp_errs.h"
*       netevent.h
*       ncl.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
*
*       LCP_Init
*
*   DESCRIPTION
*
*       This function initializes the LCP layer of PPP. It creates the
*       needed timers and initializes the LCP structure for the passed
*       in device.
*
*   INPUTS
*
*       DV_DEVICE_ENTRY     *dev_ptr    Pointer to the deivce that this
*                                       LCP layer belongs to.
*
*   OUTPUTS
*
*       STATUS
*
***************************************************************************/
STATUS LCP_Init (DV_DEVICE_ENTRY *dev_ptr)
{
    STATUS      status;
    LCP_LAYER   *lcp;
    CHAR        timer_name[9]; /* max length for a PLUS timer name is 8 */
    CHAR        temp[5];       /* temp string to store the com port     */
    LINK_LAYER  *link_layer;

    link_layer = (LINK_LAYER*)dev_ptr->dev_ppp_layer;

#if (INCLUDE_PPP_MIB == NU_TRUE)
    INT32 retcode;
#endif

    /* Get a pointer to the lcp stucture for this device. */
    lcp = &link_layer->lcp;

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

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

    /* Create the restart timer, it will be used for LCP */
    status = NU_Create_Timer (&lcp->restart_timer, timer_name, LCP_Timer_Expire,
                   (UNSIGNED) dev_ptr, LCP_TIMEOUT_VALUE, LCP_TIMEOUT_VALUE,
                   NU_DISABLE_TIMER);

    if (status != NU_SUCCESS)
    {
        NERRS_Log_Error (TCP_FATAL, __FILE__, __LINE__);
        return(-1);
    }

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

    /* Build the LCP echo timer name */
    timer_name[0] = 'e';
    timer_name[1] = 'c';
    timer_name[2] = 'h';
    timer_name[3] = 'o';
    strcat (timer_name, (CHAR *)NU_ITOA ((INT)dev_ptr->dev_com_port, temp, 10));

    /* Create the echo timer, it will be used for checking if the link
       is still up. */
    status = NU_Create_Timer (&lcp->echo_timer, timer_name, LCP_Echo_Expire,
                   (UNSIGNED) dev_ptr , LCP_ECHO_VALUE, LCP_ECHO_VALUE,
                   NU_DISABLE_TIMER);

    if (status != NU_SUCCESS)
    {
        NERRS_Log_Error (TCP_FATAL, __FILE__, __LINE__);
        return(-1);
    }

#if (INCLUDE_PPP_MIB == NU_TRUE)
    /* Init the basic interface information. */
    SNMP_ifDescr(dev_ptr->dev_index, "PPP Protocol Driver");
    SNMP_ifType(dev_ptr->dev_index, 23);
    SNMP_ifMtu(dev_ptr->dev_index, dev_ptr->dev_mtu);
#endif
    
    return (NU_SUCCESS);
}

/***************************************************************************
*
*   FUNCTION
*
*       LCP_Reset
*
*   DESCRIPTION
*
*       Resets the LCP configuration options to their default values.
*       This is done in preparation for a new PPP session.
*
*   INPUTS
*
*       LCP_LAYER       *lcp_ptr    Pointer to the LCP structure to
*                                   be reset.
*
*   OUTPUTS
*
*       None
*
***************************************************************************/
VOID LCP_Reset(LCP_LAYER *lcp_ptr)
{
    /* Fill in the default TX LCP options into the structure that holds them.
       We don't fill them all in here because our peer will let us know
       which ones it can handle. */

    lcp_ptr->foreign_options.accm = lcp_ptr->foreign_options.Initial_accm;
    lcp_ptr->foreign_options.max_rx_unit =
        lcp_ptr->local_options.Initial_max_rx_unit;

    /* Reset the number of configure/terminate transmissions. */
    lcp_ptr->num_transmissions = LCP_MAX_CONFIGURE;

}

/***************************************************************************
*
*   FUNCTION
*
*       PPP_LCP_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.
*
*   INPUTS
*
*       NET_BUFFER      *buf_ptr    Pointer to the buffer that holds
*                                   the incoming packet
*
*   OUTPUTS
*
*       None
*
****************************************************************************/
VOID PPP_LCP_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 ("\nlcp interpret ");
#endif

    /* The current state of the LCP will determine what we will
       do with this packet. */
    switch (link_layer->lcp.state)
    {
        case INITIAL    :

#ifdef NU_DEBUG_PPP
                            _PRINT ("initial ");
#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. */

                            link_layer->lcp.echo_counter = INITIAL;

                            link_layer->disconnect(link_layer, NU_FALSE);

                            break;

        case STARTING   :

#ifdef NU_DEBUG_PPP
                            _PRINT ("initial ");
#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. */

                            link_layer->lcp.echo_counter = STARTING;

                            link_layer->disconnect(link_layer, NU_FALSE);

                            break;            /* just drop it for now */

        case CLOSED     :

#ifdef NU_DEBUG_PPP
                            _PRINT ("closed ");
#endif
                            /* This state is entered when the this layer
                               finished action is done. While PPP is waiting
                               for the modem to hangup it holds the semaphore
                               for the network stack. So if a packet comes in
                               during this state it will not get processed
                               until after the modem is hungup and the
                               semaphore is released. So this state is
                               useless in this implementation. */

                            break;

        case STOPPED    :

#ifdef NU_DEBUG_PPP
                            _PRINT ("initial ");
#endif
                            /* This state is entered when the this layer
                               finished action is done, in one place it is
                               entered when negotiation has failed.
                               While PPP is waiting for the modem to hangup
                               it holds the semaphore for the network stack.
                               So if a packet comes in during this state it
                               will not get processed until after the modem
                               is hungup and the semaphore is released. So
                               this state is useless in this implementation.
                               If it is entered after negotiation has failed
                               then the modem is going to hangup as well. */

                            break;

        case CLOSING    :

#ifdef NU_DEBUG_PPP
                            _PRINT ("closing ");
#endif

                            /* Call the routine to handle this state. */
                            LCP_Closing_State(buf_ptr);

                            break;

        case STOPPING   :

#ifdef NU_DEBUG_PPP
                            _PRINT ("stopping ");
#endif

                            /* Call the routine to handle this state. */
                            LCP_Stopping_State (buf_ptr);

                            break;

        case REQ_SENT   :   /* Call the routine to handle this state */
#ifdef NU_DEBUG_PPP
                            _PRINT ("req_sent ");
#endif
                            LCP_Req_Sent_State (buf_ptr);
                            break;

        case ACK_RCVD   :   /* Call the routine to handle this state */
#ifdef NU_DEBUG_PPP
                            _PRINT ("ack_rcvd ");
#endif
                            LCP_Ack_Rcvd_State (buf_ptr);
                            break;

        case ACK_SENT   :   /* Call the routine to handle this state */
#ifdef NU_DEBUG_PPP
                            _PRINT ("ack_sent ");
#endif
                            LCP_Ack_Sent_State (buf_ptr);
                            break;

        case OPENED     :   /* Call the open state routine */
#ifdef NU_DEBUG_PPP
                            _PRINT ("opened ");
#endif
                            LCP_Open_State (buf_ptr);
                            break;

    }

    /* Release the buffer space */
    MEM_Buffer_Chain_Free (&MEM_Buffer_List, &MEM_Buffer_Freelist);

}

/***************************************************************************
*
*   FUNCTION
*
*       LCP_Send_Config_Req
*
*   DESCRIPTION
*
*       Sends a configure request packet containing the current set of
*       configuration options and their appropriate settings.
*
*   INPUTS
*
*       *dev_ptr
*
*   OUTPUTS

⌨️ 快捷键说明

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