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

📄 ncp.c

📁 PPP协议C语言源程序
💻 C
📖 第 1 页 / 共 5 页
字号:
*
* INPUTS
*
*     NET_BUFFER        *buf_ptr        Pointer to the incoming NCP
*                                        packet
*
* OUTPUTS
*
*     none
*
************************************************************************/
void NCP_IP_Ack_Rcvd_State (NET_BUFFER *buf_ptr)
{
    IPCP_LAYER  *ipcp;
    STATUS      status;
    LINK_LAYER      *link_layer;
    link_layer = (LINK_LAYER *)buf_ptr->mem_buf_device->dev_ppp_layer;

#if (INCLUDE_PPP_MIB == NU_TRUE)
    DV_DEVICE_ENTRY *dev_ptr;
    dev_ptr = buf_ptr->mem_buf_device;
#endif

    /* Get a pointer to the IPCP structure for this packet */
    ipcp = &link_layer->ipcp;

    /* Now see what kind of pkt it is and deal
       with it. */

    switch (*buf_ptr->data_ptr)
    {
        case LCP_CONFIGURE_REQUEST :

#ifdef NU_DEBUG_PPP
            _PRINT ("config_req\r\n");
#endif
            /* Determine if the options are ok
               with our side. This function will take care of
               sending the ACK or NAK. */
            status = NCP_Configure_Req_Check (buf_ptr);

            /* Check the status to see what we need do. */
            if (status == NU_TRUE)  /* all options were ok */
            {
                /* Change states and notify the upper layer that
                   the application can now have a shot. */
                ipcp->state = OPENED;
#ifdef NU_DEBUG_PPP
                _PRINT ("IPCP State: OPENED ");
#endif

                NU_Set_Events (&link_layer->negotiation_progression,
                                NCP_CONFIG_SUCCESS, NU_OR);
            }

            break;

        case LCP_CONFIGURE_ACK      :

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

            /* Send a configure req */
            NCP_IP_Send_Config_Req (ipcp->local_ip_address,
                                    buf_ptr->mem_buf_device);

            /* Change states */
            ipcp->state = REQ_SENT;
#ifdef NU_DEBUG_PPP
            _PRINT ("IPCP State: REQ_SENT ");
#endif

            break;

        case LCP_CONFIGURE_NAK      :

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

            /* Check the NAK pkt. If we get a true back then this pkt contains
               our IP address. */
            status = NCP_Configure_Nak_Check (buf_ptr);

            if (status == NU_TRUE)
            {
                /* We need to send a request with our new IP address. */
                NCP_IP_Send_Config_Req (ipcp->local_ip_address,
                                        buf_ptr->mem_buf_device);
            }

            /* Change states */
            ipcp->state = REQ_SENT;
#ifdef NU_DEBUG_PPP
            _PRINT ("IPCP State: REQ_SENT ");
#endif

            break;

            case LCP_TERMINATE_REQUEST  :

            /* Send a terminate ack */
            LCP_Send_Terminate_Ack (buf_ptr, PPP_IP_CONTROL_PROTOCOL);

            /* Change states */
            ipcp->state = REQ_SENT;
#ifdef NU_DEBUG_PPP
            _PRINT ("IPCP State: REQ_SENT ");
#endif

            break;

        case LCP_TERMINATE_ACK          :

            /* All we need to do is change states. */
            ipcp->state = REQ_SENT;
#ifdef NU_DEBUG_PPP
            _PRINT ("IPCP State: REQ_SENT ");
#endif

            break;

        case LCP_CONFIGURE_REJECT       :

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

            /* We need to check what was rejected and make sure we can do
               without it. */
            status = NCP_IP_Configure_Reject_Check (buf_ptr);

            /* If we can not do without it. */
            if (status != NU_TRUE)
            {
                /* Change states. */
                ipcp->state = STOPPED;
                link_layer->lcp.state = STOPPED;
#ifdef NU_DEBUG_PPP
                _PRINT ("IPCP State: STOPPED ");
                _PRINT ("LCP State: STOPPED ");
#endif

#if (INCLUDE_PPP_MIB == NU_TRUE)
                SNMP_ifOperStatus(dev_ptr->dev_index, RFC1213_IF_DOWN);
#endif

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

                /* Hang the modem up. This layer finished. */
                link_layer->disconnect(link_layer, NU_FALSE);

                /* Change states. */
                ipcp->state = STARTING;
#ifdef NU_DEBUG_PPP
                _PRINT ("IPCP State: STARTING ");
#endif

            }
            else
            {
                ipcp->state = REQ_SENT;
#ifdef NU_DEBUG_PPP
                _PRINT ("IPCP State: REQ_SENT ");
#endif
            }

            break;

        default :

            /* If we get here then we have RX an unknown code. So
               send a reject pkt for that code. */

            LCP_Send_Code_Reject (buf_ptr, PPP_IP_CONTROL_PROTOCOL);

            break;

    } /* switch */
}

/************************************************************************
* FUNCTION
*
*     NCP_Timer_Expire
*
* DESCRIPTION
*
*     This is the expiration routine for the NCP negotiation timer. It
*     will retransmit a NCP packet or signal PPP that negotiation failed.
*     All is based on the current state of NCP.
*
* AUTHOR
*
*     Uriah Pollock
*
* INPUTS
*
*     UNSIGNED          dev_ptr         The address of the PPP device
*                                       for which the NCP timer expired.
*
* OUTPUTS
*
*     none
*
************************************************************************/
void NCP_Timer_Expire(UNSIGNED dev_ptr)
{
    DV_DEVICE_ENTRY *device;
    LCP_LAYER       *lcp;
    IPCP_LAYER      *ipcp;
    LINK_LAYER      *link;

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

    /* Make a pointer to the device for this timer. */
    device = (DV_DEVICE_ENTRY *)dev_ptr;

    /* Get a pointer to the ppp link stucture for this device. */
    link = (LINK_LAYER *)device->link_layer;

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

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

    /* Check the current state to see what we can do. */
    switch (ipcp->state)
    {
        case CLOSING    :
#ifdef NU_DEBUG_PPP
            _PRINT ("closing\r\n");
#endif

            /* See if we get another try at terminating. */
            if (ipcp->num_transmissions-- > 0)
            {
#ifdef NU_DEBUG_PPP
                _PRINT ("send term req again\r\n");
#endif

                /* Set the event to send the packet again. */
                UTL_Timerset (NCP_RESEND, dev_ptr, 0, 0);
            }
            else
            {
                /* Stop the restart timer. */
                NU_Control_Timer (&ipcp->restart_timer, NU_DISABLE_TIMER);

                /* Stop the echo timer since we are leaving the open state */
                NU_Control_Timer (&lcp->echo_timer, NU_DISABLE_TIMER);

                /* Change states */
                ipcp->state = CLOSED;
#ifdef NU_DEBUG_PPP
                _PRINT ("IPCP State: CLOSED ");
#endif

                /* Now let LCP close the link. */

                /* Reset the reset counter */
                lcp->num_transmissions = LCP_MAX_TERMINATE;

                /* Reset the reset timer */
                NU_Control_Timer (&lcp->restart_timer, NU_DISABLE_TIMER);

                /* Set the event that will get the link closed. */
                UTL_Timerset (LCP_CLOSE_LINK, dev_ptr, 0, 0);

                lcp->state = CLOSING;

            }


        break;

        case STOPPING   :
#ifdef NU_DEBUG_PPP
            _PRINT ("stopping\n");
#endif

            /* See if we get another try at terminating. */
            if (ipcp->num_transmissions-- > 0)
            {
#ifdef NU_DEBUG_PPP
                _PRINT ("send term req again\r\n");
#endif

                /* Set the event to send the packet again. */
                UTL_Timerset (NCP_RESEND, dev_ptr, 0, 0);
            }
            else
            {
                /* Stop the restart timer. */
                NU_Control_Timer (&ipcp->restart_timer, NU_DISABLE_TIMER);

                /* Stop the echo timer since we are leaving the open state */
                NU_Control_Timer (&lcp->echo_timer, NU_DISABLE_TIMER);

                /* Otherwise we have tried to many times, set the event
                   that will tell initialization we could not configure. Do
                   this only if LCP is in the open state, this will
                   suggest that we are still negotiating the link and this
                   terminate is probaly because we could not configure. */

                if (lcp->state == OPENED)
                    NU_Set_Events (&link->negotiation_progression, LCP_CONFIG_FAIL, NU_OR);

                /* Change states for both LCP */
                ipcp->state = STOPPED;
#ifdef NU_DEBUG_PPP
                _PRINT ("IPCP State: STOPPED ");
#endif

                /* Now let LCP close the link. */

                /* Reset the reset counter */
                lcp->num_transmissions = LCP_MAX_TERMINATE;

                /* Reset the reset timer */
                NU_Control_Timer (&lcp->restart_timer, NU_DISABLE_TIMER);

                /* Set the event that will get the link closed. */
                UTL_Timerset (LCP_CLOSE_LINK, dev_ptr, 0, 0);

                lcp->state = CLOSING;

            }

        break;

        case REQ_SENT   :
#ifdef NU_DEBUG_PPP
            _PRINT ("req_sent\r\n");
#endif

            /* See if we get another try at configuring. */
            if (ipcp->num_transmissions-- > 0)
            {
#ifdef NU_DEBUG_PPP
                _PRINT ("send config req again\r\n");
#endif

                /* Set the event to send the packet again. */
                UTL_Timerset (NCP_RESEND, dev_ptr, 0, 0);
            }
            else
            {
                /* Stop the restart timer. */
                NU_Control_Timer (&ipcp->restart_timer, NU_DISABLE_TIMER);

                /* Stop the echo timer since we are leaving the open state */
                NU_Control_Timer (&lcp->echo_timer, NU_DISABLE_TIMER);

                /* Change states */
                ipcp->state = STOPPED;
#ifdef NU_DEBUG_PPP
                _PRINT ("IPCP State: STOPPED ");
#endif

                /* Otherwise we have tried to many times, set the event
                   that will tell initialization we could not configure. */
                NU_Set_Events (&link->negotiation_progression, LCP_CONFIG_FAIL, NU_OR);

            }

            break;

        case ACK_RCVD   :
#ifdef NU_DEBUG_PPP
            _PRINT ("ack_rcvd\r\n");
#endif

            /* See if we get another try at configuring. */
            if (ipcp->num_transmissions--)
            {

                /* We need to send another configure request with the newest
                   current set of configuration options. */

                UTL_Timerset (NCP_SEND_CONFIG, dev_ptr, 0, 0);

                ipcp->state = REQ_SENT;
#ifdef NU_DEBUG_PPP
                _PRINT ("IPCP State: REQ_SENT ");
#endif
            }
            else
            {
                /* Stop the restart timer. */
                NU_Control_Timer (&ipcp->restart_timer, NU_DISABLE_TIMER);

                /* Stop the echo timer since we are leaving the open state */
                NU_Control_Timer (&lcp->echo_timer, NU_DISABLE_TIMER);

                /* Change states */
                ipcp->state = STOPPED;

⌨️ 快捷键说明

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