/************************************************************************ * NAME: fnet_arp_lookup * * DESCRIPTION: This function looks up an entry corresponding to * the destination IP address *************************************************************************/ fnet_mac_addr_t *fnet_arp_lookup(fnet_netif_t *netif, fnet_ip4_addr_t ipaddr) { fnet_arp_if_t *arpif = netif->arp_if_ptr; /* PFI */ fnet_index_t i; fnet_mac_addr_t *result = FNET_NULL; /* Find an entry. */ for (i = 0U; i < FNET_CFG_ARP_TABLE_SIZE; ++i) { if (ipaddr == arpif->arp_table[i].prot_addr) { if (fnet_memcmp(arpif->arp_table[i].hard_addr, fnet_eth_null_addr, sizeof(fnet_mac_addr_t))) { result = &arpif->arp_table[i].hard_addr; } /* Else => not found */ break; } } return result; }
/************************************************************************ * NAME: fnet_arp_lookup * * DESCRIPTION: This function looks up an entry corresponding to * the destination IP address *************************************************************************/ fnet_mac_addr_t *fnet_arp_lookup( fnet_netif_t *netif, fnet_ip4_addr_t ipaddr ) { fnet_arp_if_t *arpif = &(((fnet_eth_if_t *)(netif->if_ptr))->arp_if); //PFI int i; fnet_mac_addr_t *result = FNET_NULL; /* Find an entry. */ for (i = 0; i < FNET_ARP_TABLE_SIZE; ++i) { if(ipaddr == arpif->arp_table[i].prot_addr) { if(fnet_memcmp(arpif->arp_table[i].hard_addr, fnet_eth_null_addr, sizeof(fnet_mac_addr_t))) { result = &arpif->arp_table[i].hard_addr; } /* Else => not found */ break; } } //NOTFOUND: return result; }
/************************************************************************ * NAME: fapp_mem_memcpy * * DESCRIPTION: ************************************************************************/ fnet_return_t fapp_mem_memcpy (fnet_shell_desc_t desc, void *dest, const void *src, fnet_size_t n ) { fnet_size_t i; const struct fapp_mem_region *region; if(fapp_mem_region_is_protected( (fnet_uint32_t)dest, n) == FNET_FALSE) { /* Find memory region.*/ region = fapp_mem_region_find( (fnet_uint32_t)dest, n); if(region && (region->memcpy)) { #if FAPP_CFG_CHECK_FLASH_BEFORE_WRITE if(region->erase) { /* Check if memory is erased.*/ for(i=0u; i<n; i++) { if(((fnet_uint8_t *)dest)[i] != 0xFFu) { fnet_shell_println(desc, FAPP_MEM_ERROR_NOTERASED); goto FAIL; } } } #endif /* Write. */ region->memcpy(dest, src, n); /* Verify result. */ #if 0 /* Old code.*/ for(i=0; i<n; i++) { if(((volatile fnet_uint8_t *)dest)[i] !=((fnet_uint8_t *)src)[i]) { goto FAIL; } } #else /* Workaround of the Flash cache issue discovered on K60.*/ /* Delay. fnet_timer_delay(1); is too big - 100ms.*/ for(i=0u; i<100000000u; i++) { if(fnet_memcmp(dest, src, n) == 0) { break; } } if(i==100000000u) { goto FAIL; } #endif return FNET_OK; } } FAIL: fnet_shell_println(desc, FAPP_MEM_ERROR_WRITEFAILED, n, dest); return FNET_ERR; }
/************************************************************************ * 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); }