// // Created by epagris on 2022.12.10.. // #include #include "arp_cache.h" #include "dynmem.h" #include "utils.h" ArpCache *arpc_new(uint16_t size) { ArpCache * arcp = (ArpCache *) dynmem_alloc(2 * sizeof(uint16_t) + size * sizeof(ArpEntry)); arcp->size = size; arcp->fill = 0; return arcp; } void arpc_free(ArpCache *aprc) { dynmem_free(aprc); } void arpc_learn(ArpCache *arpc, const ArpEntry *newEntry) { // TODO: nagyon dummy... for (uint16_t i = 0; i < arpc->fill; i++) { ArpEntry * entry = arpc->entries + i; if (!memcmp(entry->eth, newEntry->eth, ETH_HW_ADDR_LEN) && (entry->ip == newEntry->ip)) { return; } } // if not found... arpc->entries[arpc->fill++] = *newEntry; } const ArpEntry *arpc_get(ArpCache *arpc, ip4_addr ip) { for (uint16_t i = 0; i < arpc->fill; i++) { ArpEntry * entry = arpc->entries + i; if (entry->ip == ip) { return entry; } } return NULL; } void arpc_dump(ArpCache *arpc) { for (uint16_t i = 0; i < arpc->fill; i++) { ArpEntry * entry = arpc->entries + i; PRINT_IPv4(entry->ip); MSG(" -> %02x:%02x:%02x:%02x:%02x:%02x\n", entry->eth[0], entry->eth[1], entry->eth[2], entry->eth[3], entry->eth[4], entry->eth[5]); } MSG("\n"); }