示例#1
0
文件: fapp_dhcp.c 项目: 8bitgeek/fnet
/************************************************************************
* NAME: fapp_dhcp_cmd
*
* DESCRIPTION: Enable DHCP client. 
************************************************************************/
void fapp_dhcp_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t ** argv )
{
    struct              fnet_dhcp_params dhcp_params;
    fnet_dhcp_desc_t    dhcp_desc;
    fnet_netif_desc_t   netif = fnet_netif_get_default();

    if(argc == 1u    /* By default is "init".*/
#if 0 /* DHCP reboot feature not used too much. */
    || fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0
#endif    
    ) /* [reboot] */
    {
               
        fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params));

        fapp_dhcp_discover_counter = FAPP_CFG_DHCP_CMD_DISCOVER_MAX; /* reset counter.*/
        
#if 0 /* DHCP reboot feature not used too much. */
        if(fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0) /* [reboot] */
            dhcp_params.requested_ip_address.s_addr = fnet_netif_get_ip4_addr(netif);
#endif            

        fapp_dhcp_ip_old = fnet_netif_get_ip4_addr(netif); /* Save ip to restore if cancelled. */
        
        /* Enable DHCP client */
        dhcp_desc = fnet_dhcp_init(netif, &dhcp_params);
        if(dhcp_desc != FNET_ERR)
        {
            fapp_dhcp_desc = dhcp_desc;

            /* Register DHCP event handlers. */
            fnet_dhcp_handler_updated_set(fapp_dhcp_desc, fapp_dhcp_handler_updated, (void *)desc);
            fnet_dhcp_handler_discover_set(fapp_dhcp_desc, fapp_dhcp_handler_discover, (void *)desc);
            
            fnet_shell_println(desc, FAPP_TOCANCEL_STR);
            fnet_shell_block(desc, fapp_dhcp_on_ctrlc); /* Block shell. */
        }
        else
        {
            fnet_shell_println(desc, FAPP_INIT_ERR, "DHCP");
        }
    }
    else if((argc == 2u) && (fnet_strcasecmp(&FAPP_COMMAND_RELEASE[0], argv[1]) == 0)) /* [release] */
    {
        fapp_dhcp_release();
    }
    else
    {
        fnet_shell_println(desc, FAPP_PARAM_ERR, argv[1]);
    }
}
示例#2
0
/************************************************************************
* NAME: fapp_netif_addr_print
*
* DESCRIPTION: Print Interface IP addresses. 
************************************************************************/
void fapp_netif_addr_print(fnet_shell_desc_t desc, fnet_address_family_t family, fnet_netif_desc_t netif, fnet_bool_t print_type)
{
	fnet_char_t    ip_str[FNET_IP_ADDR_STR_SIZE]={0};
    
#if FNET_CFG_IP4
    if((family & AF_INET)==AF_INET)
    {
        fnet_ip4_addr_t local_ip;
        
        local_ip = fnet_netif_get_ip4_addr(netif);
        fnet_shell_printf(desc, FAPP_SHELL_INFO_FORMAT_S, "IPv4 Address", fnet_inet_ntoa(*(struct in_addr *)(&local_ip), ip_str) ); 
        if(print_type)
        {
            fnet_shell_println(desc," <%s>", fnet_netif_get_ip4_addr_automatic(netif) ? "automatic" : "manual");
        }
        else
        {
            fnet_shell_println(desc, "");  
        }
    }
#endif /* FNET_CFG_IP4 */
#if FNET_CFG_IP6
    if((family & AF_INET6)==AF_INET6)
    {
        fnet_bool_t                 result;
        fnet_index_t                n;
        fnet_netif_ip6_addr_info_t  addr_info;
        
        /* Print all assigned IPv6 addreses.*/
        n=0u;
        for(;;) 
        {
            result = fnet_netif_get_ip6_addr (netif, n, &addr_info);
            
            if(result == FNET_TRUE)
            {
                fnet_inet_ntop(AF_INET6, (fnet_uint8_t*)(&addr_info.address), ip_str, sizeof(ip_str));
                            
                fnet_shell_printf(desc, FAPP_SHELL_INFO_FORMAT_S, "IPv6 Address", ip_str);
                if(print_type)
                {
                    fnet_shell_println(desc," <%s> ScopeID:%d", (addr_info.type == FNET_NETIF_IP6_ADDR_TYPE_AUTOCONFIGURABLE)  ? "autoconfigurable" : "manual", fnet_netif_get_scope_id(netif)); 
                }
                else
                {
                    fnet_shell_println(desc,"");    
                }
            }
            else
            {
               break;
            }

            n++;
        }
    }
#endif /* FNET_CFG_IP6 */
}
示例#3
0
static void fapp_dup_ip_handler( fnet_netif_desc_t netif )
{
    fnet_char_t     name[FNET_NETIF_NAMELEN];
    fnet_char_t     ip_str[FNET_IP4_ADDR_STR_SIZE];
    fnet_ip4_addr_t addr;
    
    fnet_netif_get_name( netif, name, sizeof(name) );
    addr = fnet_netif_get_ip4_addr( netif );
    fnet_inet_ntoa(*(struct in_addr *)( &addr), ip_str);

    fnet_printf(FAPP_DUP_IP_WARN, name, ip_str);
}
示例#4
0
/************************************************************************
* NAME: fapp_dhcp_cmd
*
* DESCRIPTION: Enable DHCP client.
************************************************************************/
void fapp_dhcp_cmd( fnet_shell_desc_t desc, fnet_index_t argc, fnet_char_t **argv )
{
    struct              fnet_dhcp_params dhcp_params;
    fnet_dhcp_desc_t    dhcp_desc;
    fnet_netif_desc_t   netif = fnet_netif_get_default();

    if(argc == 1u    /* By default is "init".*/
#if 0 /* DHCP reboot feature not used too much. */
        ||((argc == 2u) && (fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0))  /* [reboot] */
#endif
        ||((argc == 2u) && (fnet_strcasecmp(&FAPP_DHCP_COMMAND_AUTOIP[0], argv[1]) == 0)) /* [autoip] */
      ) 
    {

        fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params));

        fapp_dhcp_discover_counter = FAPP_CFG_DHCP_CMD_DISCOVER_MAX; /* reset counter.*/

#if 0 /* DHCP reboot feature not used too much. */
        if(fnet_strcasecmp(&FAPP_DHCP_COMMAND_REBOOT[0], argv[1]) == 0) /* [reboot] */
            dhcp_params.requested_ip_address.s_addr = fnet_netif_get_ip4_addr(netif);
#endif
        /* Check Auto-IP flag.*/
        if((argc == 2u) && (fnet_strcasecmp(&FAPP_DHCP_COMMAND_AUTOIP[0], argv[1]) == 0)) 
        {
            fapp_dhcp_autoip = FNET_TRUE;
        }
        else
        {
            fapp_dhcp_autoip = FNET_FALSE;
        }

        dhcp_params.probe_addr = FNET_TRUE; /* Enable probing of the newly received address.*/

        /* Save current IP address only if it was allocated manually/statically. */
        if(fnet_netif_get_ip4_addr_type(netif) == FNET_NETIF_IP_ADDR_TYPE_MANUAL)
        {
            fapp_dhcp_ip_old = fnet_netif_get_ip4_addr(netif); /* Save ip to restore if cancelled. */
        }
        else
        {
            fapp_dhcp_ip_old = 0;
        }

        /* Start DHCP client */
        dhcp_desc = fnet_dhcp_init(netif, &dhcp_params);
        if(dhcp_desc)
        {
            fapp_dhcp_desc = dhcp_desc;

            /* Register DHCP event handler callbacks. */
            fnet_dhcp_set_callback_updated(fapp_dhcp_desc, fapp_dhcp_callback_updated, (void *)desc);
            fnet_dhcp_set_callback_discover(fapp_dhcp_desc, fapp_dhcp_callback_discover, (void *)desc);

            fnet_shell_println(desc, FAPP_DELIMITER_STR);
            fnet_shell_println(desc, " DHCPv4 Client started.");
            fnet_shell_println(desc, FAPP_DELIMITER_STR);

            fnet_shell_println(desc, FAPP_TOCANCEL_STR);
            fnet_shell_block(desc, fapp_dhcp_on_ctrlc); /* Block shell. */
        }
        else
        {
            fnet_shell_println(desc, FAPP_INIT_ERR, "DHCP");
        }
    }
    else if((argc == 2u) && (fnet_strcasecmp(&FAPP_COMMAND_RELEASE[0], argv[1]) == 0)) /* [release] */
    {
        fapp_dhcp_release();
    }
    else
    {
        fnet_shell_println(desc, FAPP_PARAM_ERR, argv[1]);
    }
}
示例#5
0
文件: fapp_http.c 项目: ErikZalm/fnet
static int fapp_http_ssi_echo_handle(char * query, long *cookie)
{
    int result = FNET_OK;
    const struct fapp_http_echo_variable *  echo_var_ptr;
    fnet_netif_desc_t netif = fapp_default_netif;
    
    const char *ssi_buffer_ptr = 0;
    
    /* Find static echo value. */
	for(echo_var_ptr = fapp_http_echo_variables; echo_var_ptr->variable && echo_var_ptr->value; echo_var_ptr++)
	{
        if (!fnet_strcmp( query, echo_var_ptr->variable))                    
        {				 
            ssi_buffer_ptr = echo_var_ptr->value;
            break;
        }
    }
   
    /* Find run-time echo values. */
    if(ssi_buffer_ptr == 0)
    {
    #if FNET_CFG_IP4
        char ip_str[FNET_IP4_ADDR_STR_SIZE];
    #endif

        ssi_buffer_ptr = fapp_http_ssi_buffer; 
        if (!fnet_strcmp( query, "IP_ADDRESS"))
        {
        #if FNET_CFG_IP4
            fnet_ip4_addr_t ip_adr = fnet_netif_get_ip4_addr(netif);
            fnet_inet_ntoa(*(struct in_addr *)( &ip_adr), ip_str);
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "%s", ip_str);
        #else
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "...");            
        #endif /* FNET_CFG_IP4 */
        }
        else if (!fnet_strcmp( query, "SUBNET_MASK"))
        {
        #if FNET_CFG_IP4
            fnet_ip4_addr_t ip_adr = fnet_netif_get_ip4_subnet_mask(netif);
            fnet_inet_ntoa(*(struct in_addr *)( &ip_adr), ip_str);
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "%s", ip_str);
        #else
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "...");            
        #endif /* FNET_CFG_IP4 */            
        }
        else if (!fnet_strcmp( query, "GATEWAY"))
        {
        #if FNET_CFG_IP4
            fnet_ip4_addr_t ip_adr = fnet_netif_get_ip4_gateway(netif);
            fnet_inet_ntoa(*(struct in_addr *)( &ip_adr), ip_str);
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "%s", ip_str);
        #else
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "...");            
        #endif /* FNET_CFG_IP4 */            
        }
        else if (!fnet_strcmp( query, "MAC"))
        {
            fnet_mac_addr_t macaddr;
            char mac_str[FNET_MAC_ADDR_STR_SIZE];
            fnet_netif_get_hw_addr(netif, macaddr, sizeof(fnet_mac_addr_t));
            fnet_mac_to_str(macaddr, mac_str);
            fnet_snprintf(fapp_http_ssi_buffer, sizeof(fapp_http_ssi_buffer), "%s", mac_str);
        }
        else
        {
            result = FNET_ERR;
        }
    }
    
    *cookie = (long)ssi_buffer_ptr; /* Save ssi_buffer_ptr as cookie.*/
    
    return result;
}