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

📄 ncp.c

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

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

                break;

            case STOPPING   :

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

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

                break;

            case REQ_SENT   :

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

                /* Call the routine to handle this state */
                NCP_IP_Req_Sent_State (buf_ptr);
                break;

            case ACK_RCVD   :

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

                /* Call the routine to handle this state */
                NCP_IP_Ack_Rcvd_State (buf_ptr);
                break;

            case ACK_SENT   :

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

                /* Call the routine to handle this state */
                NCP_IP_Ack_Sent_State (buf_ptr);
                break;

            case OPENED     :

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

                /* Call the routine to handle this state */
                NCP_IP_Opened_State (buf_ptr);
                break;

        } /* switch */

    } /* else */

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

}

/************************************************************************
* FUNCTION
*
*     NCP_IP_Send_Config_Req
*
* DESCRIPTION
*
*     Sends a configure request packet to the peer. This packet contains
*     either an IP address that is being assigned to the peer or a IP
*     address of zero, stating that we need to be assigned an IP address.
*
* AUTHOR
*
*     Uriah Pollock
*
* INPUTS
*
*     UINT8             ip_address [4]  IP address to be assigned to the
*                                          peer. If we are in client mode
*                                          this will be zero, stating that
*                                          we need to be assigned an ip
*                                          address.
*     DV_DEVICE_ENTRY   *dev_ptr        Pointer to the PPP device
*
* OUTPUTS
*
*     none
*
************************************************************************/
VOID NCP_IP_Send_Config_Req (UINT8 ip_address[4], DV_DEVICE_ENTRY *dev_ptr)
{

    NET_BUFFER          *buf_ptr;
    IPCP_LAYER          *ipcp;
    LCP_HEADER  HUGE    *ncp_pkt;
    UINT8       HUGE    *ncp_pkt_ptr;
    UINT16              len;

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

    len = 0;

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

    /* Is there a negotiation packet that can be reused? */
    if (ipcp->negotiation_pkt != NU_NULL)
        buf_ptr = ipcp->negotiation_pkt;
    else
        /* Allocate a buffer for the LCP packet. */
        ipcp->negotiation_pkt = 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;
    }

    /* Set the data ptr */
    buf_ptr->data_ptr = buf_ptr->mem_parent_packet + dev_ptr->dev_hdrlen;
    ncp_pkt = (LCP_HEADER *) buf_ptr->data_ptr;

    /* Set up the configure pkt. */
    ncp_pkt->code        = LCP_CONFIGURE_REQUEST;
    ncp_pkt->identifier  = LCP_Random_Number();

    /* Get a pointer to the data part of the NCP packet. */
    ncp_pkt_ptr         = &ncp_pkt->data;

    /* NCP IP option */
    ncp_pkt_ptr[len++]  = NCP_IP_ADDRESS;
    ncp_pkt_ptr[len++]  = NCP_IP_ADDRESS_LENGTH;

    /* If we are in client mode we will ask for an IP address by sending
       all zeros. */

    /* Fill in the address */
    ncp_pkt_ptr[len++] = ip_address[0];
    ncp_pkt_ptr[len++] = ip_address[1];
    ncp_pkt_ptr[len++] = ip_address[2];
    ncp_pkt_ptr[len++] = ip_address[3];

    /* If we are a client then we may want to get some DNS
       server addresses. */
    if (ipcp->mode == NCP_CLIENT)
    {
        /* Do we want to request a primary DNS server address? */
        if (ipcp->network_options.use_primary_dns_server == NU_TRUE)
        {
            /* NCP IP option */
            ncp_pkt_ptr[len++]  = NCP_PRIMARY_DNS_ADDRESS;
            ncp_pkt_ptr[len++]  = NCP_IP_ADDRESS_LENGTH;

            /* Fill in zeros for the address, this will make
               the server NAK us with a good address. */
            ncp_pkt_ptr[len++] = ipcp->primary_dns_server[0];
            ncp_pkt_ptr[len++] = ipcp->primary_dns_server[1];
            ncp_pkt_ptr[len++] = ipcp->primary_dns_server[2];
            ncp_pkt_ptr[len++] = ipcp->primary_dns_server[3];

        }

        /* Do we want to request a secondary DNS server address? */
        if (ipcp->network_options.use_secondary_dns_server == NU_TRUE)
        {
            /* NCP IP option */
            ncp_pkt_ptr[len++]  = NCP_SECONDARY_DNS_ADDRESS;
            ncp_pkt_ptr[len++]  = NCP_IP_ADDRESS_LENGTH;

            /* Fill in zeros for the address, this will make
               the server NAK us with a good address. */
            ncp_pkt_ptr[len++] = ipcp->secondary_dns_server[0];
            ncp_pkt_ptr[len++] = ipcp->secondary_dns_server[1];
            ncp_pkt_ptr[len++] = ipcp->secondary_dns_server[2];
            ncp_pkt_ptr[len++] = ipcp->secondary_dns_server[3];

        }
    }

    /* Fill in the length for the ncp packet. */
    len                     += LCP_HEADER_LENGTH;
    ncp_pkt->length          = INTSWAP(len);

    /* Set the length for the buffer. */
    buf_ptr->data_len = buf_ptr->mem_total_data_len = len;

    /* Store this identifier for checking receive pkts. */
    ipcp->identifier     = ncp_pkt->identifier;

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

    /* Set the dlist to NULL so this buffer will not be
       put on the free list. */
    buf_ptr->mem_dlist = NU_NULL;

    /* Ok, now send the pkt and let the automaton begin. */

    /* Bump the transmission count. */
    ipcp->num_transmissions--;

    /* Send the configure request packet. */
    dev_ptr->dev_output (buf_ptr, dev_ptr, NU_NULL, NU_NULL);

}

/************************************************************************
* FUNCTION
*
*     NCP_IP_Req_Sent_State
*
* DESCRIPTION
*
*     This functions handles processing of an incoming NCP packet.
*
* AUTHOR
*
*     Uriah Pollock
*
* INPUTS
*
*     NET_BUFFER        *buf_ptr    Pointer to the incoming NCP
*                                    packet
*
* OUTPUTS
*
*     none
*
************************************************************************/
void NCP_IP_Req_Sent_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 */
            status = NCP_Configure_Req_Check (buf_ptr);

            /* Check the status to see what state we need to switch
               to. */
            if (status == NU_TRUE)  /* all options were ok */
            {
                ipcp->state = ACK_SENT;
#ifdef NU_DEBUG_PPP
                _PRINT ("IPCP State: ACK_SENT ");
#endif
            }

            break;

        case LCP_CONFIGURE_ACK :
#ifdef NU_DEBUG_PPP
            _PRINT ("config_ack\r\n");
#endif
            /* All we need to do is reset the
               restart counter and change
               states */
            ipcp->num_transmissions = LCP_MAX_CONFIGURE;
            ipcp->state = ACK_RCVD;
#ifdef NU_DEBUG_PPP
            _PRINT ("IPCP State: ACK_RCVD ");
#endif

            break;

        case LCP_CONFIGURE_NAK :
#ifdef NU_DEBUG_PPP
            _PRINT ("config_nak\r\n");
#endif
            /* Init the restart counter and call the routine to
               handle this state. */

            ipcp->num_transmissions = LCP_MAX_CONFIGURE;

            /* 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);
            }

            break;

        case LCP_TERMINATE_REQUEST :

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

            break;

        case LCP_TERMINATE_ACK :

            /* do nothing */

            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

            }

            break;

        default :

            /* If we get here then an un-interpretable
               packet was received, send a
               code-reject */

            LCP_Send_Code_Reject (buf_ptr, PPP_IP_CONTROL_PROTOCOL);
            break;

    } /* switch */

}

/************************************************************************
* FUNCTION
*
*     NCP_IP_Ack_Rcvd_State
*
* DESCRIPTION
*
*     This functions handles processing of an incoming NCP packet.
*
* AUTHOR
*
*     Uriah Pollock

⌨️ 快捷键说明

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