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

📄 lcp.c

📁 PPP协议C语言源程序
💻 C
📖 第 1 页 / 共 5 页
字号:
                

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

            }

            break;

        case ACK_RCVD   :
#ifdef NU_DEBUG_PPP
            _PRINT ("ack_rcvd ");
#endif
            /* See if we get another try at configuring. */
            if (lcp->num_transmissions-- > 0)
            {

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

                UTL_Timerset (LCP_SEND_CONFIG, dev_ptr, NU_NULL, NU_NULL);

                lcp->state = REQ_SENT;
#ifdef NU_DEBUG_PPP
                _PRINT ("LCP State: REQ_SENT ");
#endif
                

            }
            else
            {
                /* Stop any PPP timer that may still be running */
                PPP_Stop_All_Timers(device);

                /* Change states */
                lcp->state = STOPPED;
#ifdef NU_DEBUG_PPP
                _PRINT ("LCP 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_layer->negotiation_progression, LCP_CONFIG_FAIL, NU_OR);
            }

            break;

        case ACK_SENT   :
#ifdef NU_DEBUG_PPP
            _PRINT ("ack_sent ");
#endif
            /* See if we get another try at configuring. */
            if (lcp->num_transmissions-- > 0)
            {

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

                UTL_Timerset (LCP_SEND_CONFIG, dev_ptr, NU_NULL, NU_NULL);
            }
            else
            {
                /* Stop any PPP timer that may still be running */
                PPP_Stop_All_Timers(device);

                /* Change states */
                lcp->state = STOPPED;
#ifdef NU_DEBUG_PPP
                _PRINT ("LCP 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_layer->negotiation_progression, LCP_CONFIG_FAIL, NU_OR);
            }

            break;
    }
}


/***************************************************************************
*
*   FUNCTION
*
*       LCP_Configure_Req_Check
*
*   DESCRIPTION
*
*       This function checks all configuration options in the incoming
*       configure request packet. If everything is ok then it will send an
*       ACK to the peer.  If any one option is not ok it will NAK those
*       options. If an option is unknown it will reject that option.
*
*   INPUTS
*
*       NET_BUFFER      *buf_ptr    Pointer to the incoming
*                                   configure request packet
*
*   OUTPUTS
*
*       STATUS
*
****************************************************************************/
STATUS LCP_Configure_Req_Check (NET_BUFFER *buf_ptr)
{
    NET_BUFFER          *reject_buf_ptr, *nak_buf_ptr, *ack_buf_ptr;
    LCP_HEADER          *lcp_reject_pkt, *lcp_nak_pkt, *lcp_ack_pkt;
    LCP_LAYER           *lcp;
    UINT8   HUGE        *lcp_pkt_ptr, *lcp_reject_ptr, *lcp_nak_ptr;
    UINT8               options_ok, dont_need_to_reject, identifier;
    UINT16              length, reject_length, nak_length, mru;
    UINT8   HUGE        *lcp_pkt = buf_ptr->data_ptr;
    LINK_LAYER  *link_layer;

    link_layer = (LINK_LAYER*)buf_ptr->mem_buf_device->link_layer;

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

    /* Allocate a buffer for each one of the packets that we may need to send. */
    reject_buf_ptr = MEM_Buffer_Dequeue (&MEM_Buffer_Freelist);
    nak_buf_ptr    = MEM_Buffer_Dequeue (&MEM_Buffer_Freelist);
    ack_buf_ptr    = MEM_Buffer_Dequeue (&MEM_Buffer_Freelist);

    /* Make sure a buffer was available */
    if ((nak_buf_ptr == NU_NULL) || (ack_buf_ptr == NU_NULL) ||
            (reject_buf_ptr == NU_NULL))
    {
        NERRS_Log_Error (TCP_SEVERE, __FILE__, __LINE__);

        /* If any buffer were allocated then we need to put them back
           before we return. */
        if (reject_buf_ptr)
            MEM_Buffer_Enqueue (&MEM_Buffer_Freelist, reject_buf_ptr);

        if (nak_buf_ptr)
            MEM_Buffer_Enqueue (&MEM_Buffer_Freelist, nak_buf_ptr);

        if (ack_buf_ptr)
            MEM_Buffer_Enqueue (&MEM_Buffer_Freelist, ack_buf_ptr);

        /* Get out */
        return (NU_FALSE);
    }


    /* Set the data pointer to give room for the PPP header. */
    reject_buf_ptr->data_ptr = (reject_buf_ptr->mem_parent_packet + buf_ptr->mem_buf_device->dev_hdrlen);
    nak_buf_ptr->data_ptr    = (nak_buf_ptr->mem_parent_packet + buf_ptr->mem_buf_device->dev_hdrlen);
    ack_buf_ptr->data_ptr    = (ack_buf_ptr->mem_parent_packet + buf_ptr->mem_buf_device->dev_hdrlen);

    /* Set the lcp layer pointers to point at the data section of the
       packet. */
    lcp_reject_pkt = (LCP_HEADER *) reject_buf_ptr->data_ptr;
    lcp_nak_pkt    = (LCP_HEADER *) nak_buf_ptr->data_ptr;
    lcp_ack_pkt    = (LCP_HEADER *) ack_buf_ptr->data_ptr;

    /* Point to the nak and reject packet data in case we have to
       reject or nak something */
    lcp_nak_ptr = &lcp_nak_pkt->data;
    nak_length  = LCP_HEADER_LENGTH;

    lcp_reject_ptr  = &lcp_reject_pkt->data;
    reject_length   = LCP_HEADER_LENGTH;

    /* This will be used to tell if all the config options are unacceptable */
    options_ok          = NU_TRUE;
    dont_need_to_reject = NU_TRUE;

    /* Get the ID of this pkt */
    identifier  = lcp_pkt[LCP_ID_OFFSET];

    /* Put the LCP structure on the packet and get the length */
    lcp_pkt_ptr = lcp_pkt;
    length      = (UINT16) ( (*(lcp_pkt_ptr + LCP_LENGTH_OFFSET)) << 8 );
    length      =  length | *(lcp_pkt_ptr + LCP_LENGTH_OFFSET + 1);

    /* Save the length before we take off the header. This will be used
       if the options are ok */
    lcp_ack_pkt->length = INTSWAP (length);

    /* Now remove the header */
    length     -= LCP_HEADER_LENGTH;

    /* Get a pointer to the data inside the LCP pkt */
    lcp_pkt_ptr = lcp_pkt_ptr + LCP_DATA_OFFSET;

    while (length > 0)
    {
        /* Check out what the option is and see if we can handle it */
        switch (lcp_pkt_ptr[0])
        {
            case LCP_MAX_RX_UNIT            :

                /* Pull the MRU from the packet. First push the pointer
                   to the MRU option. */
                lcp_pkt_ptr += LCP_CONFIG_OPTS;

                /* Now pull out the MSB. */
                mru = (UINT8) lcp_pkt_ptr[0];

                /* Shift it to the correct location. */
                mru <<= 8;

                /* Get the LSB and OR it in with the MSB */
                mru |= (UINT8) lcp_pkt_ptr[1];

                /* Update the MTU for this device so that we do not
                   attempt to send packets larger than our peer
                   can handle. */
                buf_ptr->mem_buf_device->dev_mtu = mru;

                /* Update the pkt pointer and the packet size */
                lcp_pkt_ptr += (LCP_MRU_LENGTH - LCP_CONFIG_OPTS);
                length -= LCP_MRU_LENGTH;

                break;


            case LCP_ASYNC_CONTROL_CHAR_MAP :

                /* Store off the char map for our peer. */

                /* Get the high bits */
                lcp->foreign_options.accm = *(lcp_pkt_ptr + LCP_CONFIG_OPTS) << 8;
                lcp->foreign_options.accm = lcp->foreign_options.accm |
                               *(lcp_pkt_ptr + LCP_CONFIG_OPTS + 1);

                /* Move them to the high side of our ACCM var */
                lcp->foreign_options.accm = lcp->foreign_options.accm << 16;

                /* Now get the low bits */
                lcp->foreign_options.accm = lcp->foreign_options.accm |
                               (*(lcp_pkt_ptr + LCP_CONFIG_OPTS + 2) << 8);
                lcp->foreign_options.accm = lcp->foreign_options.accm |
                               *(lcp_pkt_ptr + LCP_CONFIG_OPTS + 3);

                /* Update the pkt pointer and the packet size */
                lcp_pkt_ptr += LCP_ACCM_LENGTH;
                length -= LCP_ACCM_LENGTH;

                break;

            case LCP_AUTHENTICATION_PROTOCOL :

                /* Check to see if the peer is trying to use an authentication
                   protocol that we support. */

                /* Is is chap? */
                if ((lcp_pkt_ptr [LCP_CONFIG_OPTS] ==
                    ((PPP_CHAP_PROTOCOL >> 8) & PPP_BIT_MASK))
                    && ((lcp_pkt_ptr [LCP_CONFIG_OPTS + 1] ==
                    (PPP_CHAP_PROTOCOL & PPP_BIT_MASK))))

                    /* Yes it is, make sure it is with MD5? */
                    if (lcp_pkt_ptr [LCP_DATA_OFFSET] != LCP_CHAP_MD5)
                    {
                        /* At this point the peer is using CHAP but not with
                        MD5. We must NAK and state that we must use MD5. */

                        lcp_pkt_ptr[LCP_DATA_OFFSET] = LCP_CHAP_MD5;

                        /* Put it in the NAK packet */
                        memcpy (lcp_nak_ptr, lcp_pkt_ptr, LCP_CHAP_LENGTH);

                        /* Update the length and the ptr */
                        lcp_nak_ptr += lcp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];
                        nak_length += lcp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];

                        /* Mark that we need to NAK something */
                        options_ok = NU_FALSE;

                    }
                    else
                    {
                        /* If we make it here then our peer is using
                           MD5-CHAP, which is fine with us. Put this in
                           in the options structure. */
                        lcp->local_options.authentication_protocol =
                                                PPP_CHAP_PROTOCOL;
                    }
                else

                /* See if it is PAP */
                if ((lcp_pkt_ptr [LCP_CONFIG_OPTS] ==
                    ((PPP_PAP_PROTOCOL >> 8) & PPP_BIT_MASK))
                    && ((lcp_pkt_ptr [LCP_CONFIG_OPTS + 1] ==
                    (PPP_PAP_PROTOCOL & PPP_BIT_MASK))))
                {
                    /* This is fine. Put it in the options structure. */
                    lcp->local_options.authentication_protocol = PPP_PAP_PROTOCOL;
                }
                else
                {
                    /* At this point the peer does not request CHAP or PAP.
                       We must NAK the authentication protocol and
                       suggest that we use our default protocol.  */

                    lcp_pkt_ptr[LCP_CONFIG_OPTS]  = (UINT8)
                            ((lcp->local_options.authentication_protocol >> 8)
                                    & PPP_BIT_MASK);
                    lcp_pkt_ptr[LCP_CONFIG_OPTS + 1]  = (UINT8)
                            (lcp->local_options.authentication_protocol
                            & PPP_BIT_MASK);

                    /* If the default protocol is CHAP, we must add MD5 as
                       the encryption algoritm used. */
                    if (lcp->local_options.authentication_protocol ==
                                PPP_CHAP_PROTOCOL)
                    {

                        /* Put in the type of algorithm used, ours will be MD5 */
                        lcp_pkt_ptr[LCP_DATA_OFFSET] = LCP_CHAP_MD5;

                        /* Put it in the NAK packet */
                        memcpy (lcp_nak_ptr, lcp_pkt_ptr, LCP_CHAP_LENGTH);

                        /* Add the correct length */
                        lcp_nak_ptr [LCP_CONFIG_LENGTH_OFFSET] = LCP_CHAP_LENGTH;

                    }
                    else
                    {

                        /* Put it in the NAK packet */
                        memcpy (lcp_nak_ptr, lcp_pkt_ptr, LCP_PAP_LENGTH);

                        /* Add the correct length */
                        lcp_nak_ptr [LCP_CONFIG_LENGTH_OFFSET] = LCP_PAP_LENGTH;

                    }

                    /* Update the length and the ptr */
                    nak_length += lcp_nak_ptr[LCP_CONFIG_LENGTH_OFFSET];
                    lcp_nak_ptr += lcp_nak_ptr[LCP_CONFIG_LENGTH_OFFSET];

                    /* Mark that we need to NAK something */
                    options_ok = NU_FALSE;

                }

                length -= lcp_pkt_ptr [LCP_CONFIG_LENGTH_OFFSET];
                lcp_pkt_ptr += lcp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];

                break;

            case LCP_QUALITY_PROTOCOL       :

                /* There is no support for this at this time so reject
                   it */

                /* Put it in the reject packet */
                memcpy (lcp_reject_ptr, lcp_pkt_ptr,
                          (unsigned int)lcp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET]);

                /* Update the length and the ptr */
                lcp_reject_ptr += lcp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];
                reject_length += lcp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];

                /* Mark that we need to reject something */
                dont_need_to_reject = NU_FALSE;

                length -= lcp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];
                lcp_pkt_ptr += lcp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];

                break;

            case LCP_MAGIC_NUMBER           :

                /* Extract the magic number from the packet */

                /* Get the high bits */
                lcp->foreign_options.magic_number = *(lcp_pkt_ptr + LCP_CONFIG_OPTS) << 8;
                lcp->foreign_options.magic_number = lcp->foreign_options.magic_number |
                               *(lcp_pkt_ptr + LCP_CONFIG_OPTS + 1);

                /* Move them to the high side of our magic num var */
                lcp->foreign_options.magic_number = lcp->foreign_options.magic_number << 16;

                /* Now get the low bits */
                lcp->foreign_options.magic_number = lcp->foreign_options.magic_number |
                    (LCP_CLEAR_HIGH & (*(lcp_pkt_ptr + LCP_CONFIG_OPTS + 2) << 8));
                lcp->foreign_options.magic_number = lcp->foreign_options.magic_number |

⌨️ 快捷键说明

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