/************************************************************************
* NAME: fnet_arp_request
*
* DESCRIPTION: Sends ARP request.
*************************************************************************/
void fnet_arp_request( fnet_netif_t *netif, fnet_ip4_addr_t ipaddr )
{
    fnet_arp_header_t *arp_hdr;
    fnet_mac_addr_t sender_addr;

    fnet_netbuf_t *nb;

    if((nb = fnet_netbuf_new(sizeof(fnet_arp_header_t), FNET_TRUE)) != 0)
    {
        arp_hdr = nb->data_ptr;
        arp_hdr->hard_type = FNET_HTONS(FNET_ARP_HARD_TYPE); /* The type of hardware address (=1 for Ethernet).*/
        arp_hdr->prot_type = FNET_HTONS(FNET_ETH_TYPE_IP4);   /* The type of protocol address (=0x0800 for IP). */
        arp_hdr->hard_size = FNET_ARP_HARD_SIZE; /* The size in bytes of the hardware address (=6). */
        arp_hdr->prot_size = FNET_ARP_PROT_SIZE; /* The size in bytes of the protocol address (=4). */
        arp_hdr->op = FNET_HTONS(FNET_ARP_OP_REQUEST);       /* Opcode. */

        fnet_netif_get_hw_addr(netif, sender_addr, sizeof(fnet_mac_addr_t));

        fnet_memcpy(arp_hdr->target_hard_addr, fnet_eth_null_addr, sizeof(fnet_mac_addr_t));
        fnet_memcpy(arp_hdr->sender_hard_addr, sender_addr, sizeof(fnet_mac_addr_t));

        arp_hdr->targer_prot_addr = ipaddr;              /* Protocol address of target of this packet.*/
        arp_hdr->sender_prot_addr = netif->ip4_addr.address; /* Protocol address of sender of this packet.*/

        fnet_arp_trace("TX", arp_hdr); /* Print ARP header. */        
        
        ((fnet_eth_if_t *)(netif->if_ptr))->output(netif, FNET_ETH_TYPE_ARP, fnet_eth_broadcast, nb);
    }
}
/************************************************************************
* NAME: fnet_netif_ip6_addr_autoconf_set
*
* DESCRIPTION: Overwrite the last 64 bits with the interface ID.
*              "addr" contains prefix to form an address
*************************************************************************/
int fnet_netif_set_ip6_addr_autoconf(fnet_netif_t *netif, fnet_ip6_addr_t *ip_addr) //OK
{
    int result;
    unsigned char hw_addr[8];
    
    result = fnet_netif_get_hw_addr(netif, hw_addr, 8 );
    
    if(result == FNET_OK)
    {
        /* Build Interface identifier.*/
        /* Set the 8 last bytes of the IP address based on the Layer 2 identifier.*/
        switch(netif->api->hw_addr_size)
        {
            case 6: /* IEEE 48-bit MAC addresses. */
                fnet_memcpy(&(ip_addr->addr[8]), hw_addr,  3);
                ip_addr->addr[11] = 0xff;
                ip_addr->addr[12] = 0xfe;
                fnet_memcpy(&(ip_addr->addr[13]), &hw_addr[3],  3);
                ip_addr->addr[8] ^= 0x02; 
                break;
            case 8: /* IEEE EUI-64 identifier.*/
                fnet_memcpy(&(ip_addr->addr[8]), hw_addr,  8);
                ip_addr->addr[8] ^= 0x02; 
                break;
                /* TBD for others.*/
            default:
                result = FNET_ERR;
                break;
        }
    }
    
    return result;
}
Beispiel #3
0
static void fapp_get_cmd_mac(fnet_shell_desc_t desc)
{
    char mac_str[FNET_MAC_ADDR_STR_SIZE];
    fnet_mac_addr_t macaddr;

    fnet_netif_get_hw_addr(fapp_default_netif, macaddr, sizeof(fnet_mac_addr_t));
    fnet_mac_to_str(macaddr, mac_str);

    fnet_shell_println(desc, FAPP_GET_SOPT_FORMAT, mac_str);
}
Beispiel #4
0
/************************************************************************
* NAME: fapp_info_print
*
* DESCRIPTION: Display detailed information about the stack.
************************************************************************/
static void fapp_info_print( fnet_shell_desc_t desc )
{
    fnet_char_t                mac_str[FNET_MAC_ADDR_STR_SIZE];
    fnet_mac_addr_t     macaddr;
    fnet_netif_desc_t   netif = fnet_netif_get_default();         

    fapp_netif_info_print(desc, netif);

    /* HW address, if any */
    if(fnet_netif_get_hw_addr(netif, macaddr, sizeof(fnet_mac_addr_t)) == FNET_OK)
    {
        fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "MAC Address", fnet_mac_to_str(macaddr, mac_str));
    }    
    fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "MTU", fnet_netif_get_mtu(netif));    
    fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_S, "Link Status", fnet_netif_connected(netif) ? "connected" : "unconnected");
    fnet_shell_println(desc, FAPP_SHELL_INFO_FORMAT_D, "Free Heap", fnet_free_mem_status());

#if FAPP_CFG_HTTP_CMD && FNET_CFG_HTTP
    fapp_http_info(desc);
#endif

#if FAPP_CFG_DHCP_CMD && FNET_CFG_DHCP && FNET_CFG_IP4
    fapp_dhcp_info(desc);
#endif

#if FAPP_CFG_TELNET_CMD && FNET_CFG_TELNET
    fapp_telnet_info(desc);
#endif   

#if FAPP_CFG_TFTPS_CMD && FNET_CFG_TFTP_SRV
    fapp_tftps_info(desc);
#endif 

#if FAPP_CFG_LLMNR_CMD && FNET_CFG_LLMNR
    fapp_llmnr_info(desc);
#endif 
}
/************************************************************************
* NAME: fnet_arp_input
*
* DESCRIPTION: ARP input function.
*************************************************************************/
void fnet_arp_input( fnet_netif_t *netif, fnet_netbuf_t *nb )
{
	fnet_arp_if_t       *arpif = &(((fnet_eth_if_t *)(netif->if_ptr))->arp_if);
    fnet_arp_header_t   *arp_hdr = nb->data_ptr;
    fnet_mac_addr_t     local_addr;
    fnet_arp_entry_t    *entry;

    if(!((nb == 0) /* The packet is wrong. */
    || (nb->total_length < sizeof(fnet_arp_header_t)) || (arp_hdr->hard_type != FNET_HTONS(FNET_ARP_HARD_TYPE))
             || (arp_hdr->hard_size != FNET_ARP_HARD_SIZE) || (arp_hdr->prot_type != FNET_HTONS(FNET_ETH_TYPE_IP4))
             || (arp_hdr->prot_size != FNET_ARP_PROT_SIZE)))
    {

        if(nb->total_length > sizeof(fnet_arp_header_t)) 
        {
            /* Logical size and the physical size of the packet should be the same.*/
            fnet_netbuf_trim(&nb, (int)(sizeof(fnet_arp_header_t) - nb->total_length)); 
        }
        
        fnet_arp_trace("RX", arp_hdr); /* Print ARP header. */
        
        fnet_netif_get_hw_addr(netif, local_addr, sizeof(fnet_mac_addr_t));

        if(!(!fnet_memcmp(arp_hdr->sender_hard_addr, local_addr, sizeof(fnet_mac_addr_t)) /* It's from me => ignore it.*/
        || !fnet_memcmp(arp_hdr->sender_hard_addr, fnet_eth_broadcast, sizeof(fnet_mac_addr_t)))  /* It's broadcast=> error. */
        )
        {
            fnet_ip4_addr_t sender_prot_addr = arp_hdr->sender_prot_addr;
            fnet_ip4_addr_t targer_prot_addr = arp_hdr->targer_prot_addr;
           
            if(sender_prot_addr != netif->ip4_addr.address)     /* Check Duplicate IP address.*/
            {
                if(targer_prot_addr == netif->ip4_addr.address) /* It's for me.*/
                {
                    entry = fnet_arp_add_entry(netif, sender_prot_addr, arp_hdr->sender_hard_addr);
                }
                else
                {
                    entry = fnet_arp_update_entry(netif, sender_prot_addr, arp_hdr->sender_hard_addr);
                }

                if(entry && entry->hold)
                {
                    /* Send waiting data.*/
                    ((fnet_eth_if_t *)(netif->if_ptr))->output(netif, FNET_ETH_TYPE_IP4, entry->hard_addr, entry->hold);

                    entry->hold = 0;
                    entry->hold_time = 0;
                }
            }
            else
            {
                /* IP is duplicated. */
                fnet_event_raise(arpif->arp_event);
            }

            /* ARP request. If it asked for our address, we send out a reply.*/
            if((arp_hdr->op == FNET_HTONS(FNET_ARP_OP_REQUEST)) && (targer_prot_addr == netif->ip4_addr.address))
            {
                arp_hdr->op = FNET_HTONS(FNET_ARP_OP_REPLY); /* Opcode */

                fnet_memcpy(arp_hdr->target_hard_addr, arp_hdr->sender_hard_addr, sizeof(fnet_mac_addr_t));
                fnet_memcpy(arp_hdr->sender_hard_addr, local_addr, sizeof(fnet_mac_addr_t));

                arp_hdr->targer_prot_addr = arp_hdr->sender_prot_addr;
                arp_hdr->sender_prot_addr = netif->ip4_addr.address;
                
                fnet_arp_trace("TX Reply", arp_hdr); /* Print ARP header. */
                
                ((fnet_eth_if_t *)(netif->if_ptr))->output(netif, FNET_ETH_TYPE_ARP, fnet_eth_broadcast, nb);
                return;
            }
        }
    }

    fnet_netbuf_free_chain(nb);
}
Beispiel #6
0
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;
}