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

📄 ncp.c

📁 PPP协议C语言源程序
💻 C
📖 第 1 页 / 共 5 页
字号:
                    if (num_dns_servers == 1)
                    {
#ifdef NU_DEBUG_PPP
                        _PRINT ("assigning client primary DNS\r\n");
#endif
                        /* We will NAK with our first DNS server. */

                        /* Put it in the NAK packet */
                        memcpy (ncp_nak_ptr, ncp_pkt_ptr,
                                            NCP_IP_ADDRESS_LENGTH);

                        /* Replace the zeros with the new IP address */
                        ncp_nak_ptr[LCP_CONFIG_OPTS]
                                                = temp_ip_address[0][0];
                        ncp_nak_ptr[LCP_CONFIG_OPTS + 1]
                                                = temp_ip_address[0][1];
                        ncp_nak_ptr[LCP_CONFIG_OPTS + 2]
                                                = temp_ip_address[0][2];
                        ncp_nak_ptr[LCP_CONFIG_OPTS + 3]
                                                = temp_ip_address[0][3];

                        /* Set the NAK flag and length */
                        options_ok = NU_FALSE;
                        nak_length += NCP_IP_ADDRESS_LENGTH;
                        ncp_nak_ptr += NCP_IP_ADDRESS_LENGTH;
                    }
                    else
                    {
                        /* We don't have a DNS server to assign so we must
                           reject this option. */

                        /* Put it in the reject packet */
                        memcpy (ncp_reject_ptr, ncp_pkt_ptr,
                                       NCP_IP_ADDRESS_LENGTH);

                        /* Update the length and the ptr */
                        ncp_reject_ptr += NCP_IP_ADDRESS_LENGTH;
                        reject_length += NCP_IP_ADDRESS_LENGTH;

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

                    }

                }

                /* Update the length and the ptr */
                ncp_pkt_ptr += NCP_IP_ADDRESS_LENGTH;
                length -= NCP_IP_ADDRESS_LENGTH;

                break;

            case NCP_SECONDARY_DNS_ADDRESS  :

                /* Put the address in a temp var */

                temp_ip_address[0][0] = ncp_pkt_ptr[LCP_CONFIG_OPTS];
                temp_ip_address[0][1] = ncp_pkt_ptr[LCP_CONFIG_OPTS + 1];
                temp_ip_address[0][2] = ncp_pkt_ptr[LCP_CONFIG_OPTS + 2];
                temp_ip_address[0][3] = ncp_pkt_ptr[LCP_CONFIG_OPTS + 3];

                /* If it is zero then the host is requesting a DNS
                   server address. */
                if ((temp_ip_address[0][0] == 0) && (temp_ip_address[0][1] == 0) &&
                        (temp_ip_address[0][2] == 0) && (temp_ip_address[0][3] == 0))
                {

                    /* Get the first two DNS servers in our DNS server list. */
                    num_dns_servers = NU_Get_DNS_Servers (temp_ip_address[0],
                                                        2 * IP_ADDR_LEN);

                    /* Make sure we got two. */
                    if (num_dns_servers == 2)
                    {
#ifdef NU_DEBUG_PPP
                        _PRINT ("assigning client secondary DNS\r\n");
#endif
                        /* We will NAK with our second DNS server. */

                        /* Put it in the NAK packet */
                        memcpy (ncp_nak_ptr, ncp_pkt_ptr,
                                            NCP_IP_ADDRESS_LENGTH);

                        /* Replace the zeros with the new IP address */
                        ncp_nak_ptr[LCP_CONFIG_OPTS]
                                                = temp_ip_address[1][0];
                        ncp_nak_ptr[LCP_CONFIG_OPTS + 1]
                                                = temp_ip_address[1][1];
                        ncp_nak_ptr[LCP_CONFIG_OPTS + 2]
                                                = temp_ip_address[1][2];
                        ncp_nak_ptr[LCP_CONFIG_OPTS + 3]
                                                = temp_ip_address[1][3];

                        /* Set the NAK flag and length */
                        options_ok = NU_FALSE;
                        nak_length += NCP_IP_ADDRESS_LENGTH;
                        ncp_nak_ptr += NCP_IP_ADDRESS_LENGTH;
                    }
                    else
                    {
                        /* We don't have a DNS server to assign so we must
                           reject this option. */

                        /* Put it in the reject packet */
                        memcpy (ncp_reject_ptr, ncp_pkt_ptr,
                                       NCP_IP_ADDRESS_LENGTH);

                        /* Update the length and the ptr */
                        ncp_reject_ptr += NCP_IP_ADDRESS_LENGTH;
                        reject_length += NCP_IP_ADDRESS_LENGTH;

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

                    }
                }

                /* Update the length and the ptr */
                ncp_pkt_ptr += NCP_IP_ADDRESS_LENGTH;
                length -= NCP_IP_ADDRESS_LENGTH;

                break;

            default                         :

                /* If we make it here then there is a protocol that we
                   do not understand, send a reject. */
                /* Put it in the reject packet */
                memcpy (ncp_reject_ptr, ncp_pkt_ptr,
                         (unsigned int)ncp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET]);

                /* Update the length and the ptr */
                ncp_reject_ptr += ncp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];
                reject_length += ncp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];

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

                /* The length is usually the second field in the protocol,
                   so minus it off and look at the reset of the pkt. */
                length -= ncp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];
                ncp_pkt_ptr += ncp_pkt_ptr[LCP_CONFIG_LENGTH_OFFSET];

                break;

        } /* switch */
    } /* while */

    /* See if all the options are ok, if so send an ack */
    if (options_ok && dont_need_to_reject)
    {
        /* Every option is ok. The ack pkt is the same as the config pkt.
           The only difference is that the pkt code is now config ack. */

        /* Change the pkt code and put in the same ID */
        ncp_ack_pkt->code = LCP_CONFIGURE_ACK;
        ncp_ack_pkt->identifier = identifier;

        /* Copy the pkt data over */
        memcpy (&ncp_ack_pkt->data, ncp_pkt + LCP_DATA_OFFSET,
                                    (unsigned int)INTSWAP(ncp_ack_pkt->length));

        /* Update the lengths for the buffer. */
        ack_buf_ptr->data_len = ack_buf_ptr->mem_total_data_len =
                INTSWAP(ncp_ack_pkt->length);

        /* Set the packet type. */
        ack_buf_ptr->mem_flags = NET_IPCP;

        /* Set the dlist for this buffer. */
        ack_buf_ptr->mem_dlist = &MEM_Buffer_Freelist;

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

        /* Send it */
        in_buf_ptr->mem_buf_device->dev_output  (ack_buf_ptr,
                        in_buf_ptr->mem_buf_device, NU_NULL, NU_NULL);

        /* Release the buffer for the NAK packet since it was not sent. */
        MEM_Buffer_Enqueue (&MEM_Buffer_Freelist, nak_buf_ptr);

        /* Release the buffer for the reject packet since it was not sent. */
        MEM_Buffer_Enqueue (&MEM_Buffer_Freelist, reject_buf_ptr);

    }
    else
    {
        /* Release the buffer for the ACK packet since it was not sent. */
        MEM_Buffer_Enqueue (&MEM_Buffer_Freelist, ack_buf_ptr);

        /* One or more options were not ok. We must nak or reject them. */

        /* See if we need to nak something. Only NAK if we do not have to
           reject. */
        if ((!options_ok) && (dont_need_to_reject))
        {
            /* Put in the pkt indentifier and type */
            ncp_nak_pkt->identifier     = identifier;
            ncp_nak_pkt->code           = LCP_CONFIGURE_NAK;
            ncp_nak_pkt->length         = INTSWAP (nak_length);

#ifdef NU_DEBUG_PPP
            _PRINT ("naking\r\n");
#endif
            /* Update the lengths for the buffer. */
            nak_buf_ptr->data_len = nak_buf_ptr->mem_total_data_len =
                nak_length;

            /* Set the packet type. */
            nak_buf_ptr->mem_flags = NET_IPCP;

            /* Set the dlist for this buffer. */
            nak_buf_ptr->mem_dlist = &MEM_Buffer_Freelist;

            /* Send the pkt */
            in_buf_ptr->mem_buf_device->dev_output (nak_buf_ptr,
                        in_buf_ptr->mem_buf_device, NU_NULL, NU_NULL);
        }
        else
            /* Release the buffer for the NAK packet since it was not sent. */
            MEM_Buffer_Enqueue (&MEM_Buffer_Freelist, nak_buf_ptr);


        /* See if we need to reject something */
        if (!dont_need_to_reject)
        {
            /* Put in the pkt indentifier and type */
            ncp_reject_pkt->identifier  = identifier;
            ncp_reject_pkt->code        = LCP_CONFIGURE_REJECT;
            ncp_reject_pkt->length      = INTSWAP (reject_length);

            /* Update the lengths for the buffer. */
            reject_buf_ptr->data_len = reject_buf_ptr->mem_total_data_len =
                reject_length;

            /* Set the packet type. */
            reject_buf_ptr->mem_flags = NET_IPCP;

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

            /* Set the dlist for this buffer. */
            reject_buf_ptr->mem_dlist = &MEM_Buffer_Freelist;

            /* Send the pkt */
            in_buf_ptr->mem_buf_device->dev_output (reject_buf_ptr,
                        in_buf_ptr->mem_buf_device, NU_NULL, NU_NULL);
        }
        else
            /* Release the buffer for the reject packet since it was not sent. */
            MEM_Buffer_Enqueue (&MEM_Buffer_Freelist, reject_buf_ptr);

    }

    return (options_ok && dont_need_to_reject);
}

/************************************************************************
* FUNCTION
*
*     NCP_Configure_Nak_Check
*
* DESCRIPTION
*
*     This function checks all naked options in a configure nak packet.
*     It is used to assign an address to a peer. If the packet contains
*     an unknown option this function will send a reject packet for that
*     option.
*
* AUTHOR
*
*     Uriah Pollock
*
* INPUTS
*
*     NET_BUFFER        *in_buf_ptr     Pointer to the incoming NCP
*                                        packet
*
* OUTPUTS
*
*     STATUS                            Did we get an IP address
*
************************************************************************/
STATUS NCP_Configure_Nak_Check (NET_BUFFER *in_buf_ptr)
{
    IPCP_LAYER          *ipcp;
    NET_BUFFER          *buf_ptr;
    LCP_HEADER          *ncp_reject_pkt;
    UINT8   HUGE        *ncp_pkt_ptr,
            HUGE        *ncp_reject_ptr;
    UINT8               ip_reply, dont_need_to_reject, identifier;
    UINT16              length, reject_length;
    UINT8               temp_ip_address[IP_ADDR_LEN];
    UINT8   HUGE        *ncp_pkt                = in_buf_ptr->data_ptr;

    /* This will be used to tell if all the config options are unacceptable */
    ip_reply = NU_FALSE;
    dont_need_to_reject = NU_TRUE;
    reject_length = 0;

    /* Allocate a buffer for the reject packet. */
    buf_ptr = MEM_Buffer_Dequeue (&MEM_Buffer_Freelist);

    /* Make sure a buffer was available */
    if (buf_ptr == NU_NULL)
    {
        NERRS_Log_Error (NERR_RECOVERABLE, __FILE__, __LINE__);

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

    /* Get a pointer to this packets IPCP structure. */
    ipcp = &(((LINK_LAYER *)in_buf_ptr->mem_buf_device->link_layer)->ipcp);

    /* Set the data pointer. */
    buf_ptr->data_ptr   = (buf_ptr->mem_parent_packet + in_buf_ptr->mem_buf_device->dev_hdrlen);
    ncp_reject_pkt      = (LCP_HEADER *)buf_ptr->data_ptr;

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

    /* The ID of this pkt must be the same as the ID of our
       config req pkt. */
    if (identifier == ipcp->identifier)
    {

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

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

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

        /* Point to the reject packet data in case we have to
           reject something */
        ncp_reject_ptr = &ncp_reject_pkt->data;
        reject_length = LCP_HEADER_LENGTH;

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

                    /* Put the address in a temp var */

                    temp_ip_address[0] = ncp_pkt_ptr[LCP_CONFIG_OPTS];
                    temp_ip_address[1] = ncp_pkt_ptr[LCP_CONFIG_OPTS + 1];
                    temp_ip_address[2] = ncp_pkt_ptr[LCP_CONFIG_OPTS + 2];
                    temp_ip_address[3] = ncp_pkt_ptr[LCP_CONFIG_OPTS + 3];

                    /* We can only be assigned an address if we are in client
                       mode. */
                    if (ipcp->mode == NCP_CLIENT)
                    {

                        /* Copy the address over to the IPCP structure. */
                        memcpy (&ipcp->local_ip_address, temp_ip_address,
                                    IP_ADDR_LEN);

                        /* Set the flag that signals we have been assigned an IP
                           address. */
                        ip_reply = NU_TRUE;

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

                        /* Bump the length and ptr */
                        ncp_pkt_ptr += NCP_IP_ADDRESS_LENGTH;
                        length -= NCP_IP_ADDRESS_LENGTH;
                    }
                    else
                    {
                        /* We should never get here. If we are in server mode
                           and the client NAKed us with an IP address then there
                           is probably an implementation problem with the
                           client. */

                        ncp_pkt_ptr += NCP_IP_ADDRESS_LENGTH;
                        length -= NCP_IP_ADDRESS_LENGTH;

                    }

                    break;

                case NCP_PRIMARY_DNS_ADDRESS    :

                    /* Put the address in a temp var */

               

⌨️ 快捷键说明

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