arp: change arptable get struct from arp_entry_s to arpreq

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu 2022-12-11 21:36:23 +08:00 committed by Xiang Xiao
parent df3121213e
commit c7e8ade590
4 changed files with 45 additions and 43 deletions

View File

@ -182,8 +182,8 @@ static void dump_neighbor(void)
#if defined(CONFIG_NET_ARP) && !defined(CONFIG_NETLINK_DISABLE_GETNEIGH)
static void dump_arp(void)
{
FAR struct arp_entry_s *arptab;
FAR char buffer[INET_ADDRSTRLEN];
FAR struct arpreq *arptab;
char buffer[INET_ADDRSTRLEN];
size_t allocsize;
ssize_t nentries;
int i;
@ -193,8 +193,8 @@ static void dump_arp(void)
/* Allocate a buffer to hold the ARP table */
allocsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arp_entry_s);
arptab = (FAR struct arp_entry_s *)malloc(allocsize);
allocsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arpreq);
arptab = (FAR struct arpreq *)malloc(allocsize);
if (arptab == NULL)
{
fprintf(stderr, "\nERROR: Failed to allocate ARP table\n");
@ -217,28 +217,23 @@ static void dump_arp(void)
for (i = 0; i < nentries; i++)
{
FAR struct arp_entry_s *arp = &arptab[i];
FAR struct arpreq *arp = &arptab[i];
FAR struct sockaddr_in *addr = (FAR struct sockaddr_in *)&arp->arp_pa;
inet_ntop(AF_INET, &arp->at_ipaddr, buffer, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &addr->sin_addr.s_addr, buffer, INET_ADDRSTRLEN);
printf(" Dest: %s MAC Addr: ", buffer);
for (j = 0; j < ETHER_ADDR_LEN; j++)
{
if (j == (ETHER_ADDR_LEN - 1))
{
printf("%02x", arp->at_ethaddr.ether_addr_octet[j]);
printf("%02x", (uint8_t)arp->arp_ha.sa_data[j]);
}
else
{
printf("%02x.", arp->at_ethaddr.ether_addr_octet[j]);
printf("%02x.", (uint8_t)arp->arp_ha.sa_data[j]);
}
}
#ifdef CONFIG_SYSTEM_TIME64
printf(" Time 0x%" PRIx64 "\n", arp->at_time);
#else
printf(" Time 0x%" PRIx32 "\n", arp->at_time);
#endif
}
free(arptab);

View File

@ -276,8 +276,8 @@ int netlib_set_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR const uint8_t *macaddr,
FAR const char *ifname);
#ifdef CONFIG_NETLINK_ROUTE
struct arp_entry_s;
ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,
struct arpreq;
ssize_t netlib_get_arptable(FAR struct arpreq *arptab,
unsigned int nentries);
#endif
#endif

View File

@ -72,7 +72,7 @@ struct netlib_recvfrom_response_s
* Parameters:
* arptab - The location to store the copy of the ARP table
* nentries - The size of the provided 'arptab' in number of entries each
* of size sizeof(struct arp_entry_s)
* of size sizeof(struct arpreq)
*
* Return:
* The number of ARP table entries read is returned on success; a negated
@ -80,7 +80,7 @@ struct netlib_recvfrom_response_s
*
****************************************************************************/
ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,
ssize_t netlib_get_arptable(FAR struct arpreq *arptab,
unsigned int nentries)
{
FAR struct netlib_recvfrom_response_s *resp;
@ -99,7 +99,7 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,
/* Pre-allocate a buffer to hold the response */
maxsize = nentries * sizeof(struct arp_entry_s);
maxsize = nentries * sizeof(struct arpreq);
allocsize = SIZEOF_NETLIB_RECVFROM_RESPONSE_S(maxsize);
resp = (FAR struct netlib_recvfrom_response_s *)malloc(allocsize);
if (resp == NULL)
@ -201,7 +201,7 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,
}
memcpy(arptab, resp->data, paysize);
ret = paysize / sizeof(struct arp_entry_s);
ret = paysize / sizeof(struct arpreq);
errout_with_socket:
close(fd);

View File

@ -1050,6 +1050,7 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
struct sockaddr_in inaddr;
struct ether_addr mac;
FAR const char *ifname = NULL;
bool badarg = false;
int option;
int ret;
@ -1083,19 +1084,22 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
ifname = optarg;
break;
case ':':
goto errout_missing;
case '?':
default:
goto errout_invalid;
badarg = true;
break;
}
}
if (badarg)
{
goto errout_invalid;
}
#ifdef CONFIG_NETLINK_ROUTE
if (opt_type == OPT_TYPE_ARP_LIST)
{
FAR struct arp_entry_s *arptab;
FAR struct arpreq *arptab;
size_t arpsize;
ssize_t nentries;
char ipaddr[16];
@ -1104,8 +1108,8 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
/* Allocate a buffer to hold the ARP table */
arpsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arp_entry_s);
arptab = (FAR struct arp_entry_s *)malloc(arpsize);
arpsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arpreq);
arptab = (FAR struct arpreq *)malloc(arpsize);
if (arptab == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
@ -1128,36 +1132,39 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
* xx.xx.xx.xx xx:xx:xx:xx:xx:xx xxxxxxxx[xxxxxxxx]
*/
nsh_output(vtbl, "%-12s %-17s Last Access Time\n",
"IP Address", "Ethernet Address");
nsh_output(vtbl, "%-12s %-17s %s\n",
"IP Address", "Ethernet Address", "Interface");
for (i = 0; i < nentries; i++)
{
FAR struct sockaddr_in *addr;
FAR uint8_t *ptr;
if (ifname != NULL &&
strcmp(ifname, (FAR char *)arptab[i].arp_dev) != 0)
{
continue;
}
/* Convert the IPv4 address to a string */
ptr = (FAR uint8_t *)&arptab[i].at_ipaddr;
addr = (FAR struct sockaddr_in *)&arptab[i].arp_pa;
ptr = (FAR uint8_t *)&addr->sin_addr.s_addr;
snprintf(ipaddr, 16, "%u.%u.%u.%u",
ptr[0], ptr[1], ptr[2], ptr[3]);
/* Convert the MAC address string to a binary */
snprintf(ethaddr, 24, "%02x:%02x:%02x:%02x:%02x:%02x",
arptab[i].at_ethaddr.ether_addr_octet[0],
arptab[i].at_ethaddr.ether_addr_octet[1],
arptab[i].at_ethaddr.ether_addr_octet[2],
arptab[i].at_ethaddr.ether_addr_octet[3],
arptab[i].at_ethaddr.ether_addr_octet[4],
arptab[i].at_ethaddr.ether_addr_octet[5]);
(uint8_t)arptab[i].arp_ha.sa_data[0],
(uint8_t)arptab[i].arp_ha.sa_data[1],
(uint8_t)arptab[i].arp_ha.sa_data[2],
(uint8_t)arptab[i].arp_ha.sa_data[3],
(uint8_t)arptab[i].arp_ha.sa_data[4],
(uint8_t)arptab[i].arp_ha.sa_data[5]);
#ifdef CONFIG_SYSTEM_TIME64
nsh_output(vtbl, "%-12s %-17s 0x%" PRIx64 "\n",
ipaddr, ethaddr, (uint64_t)arptab[i].at_time);
#else
nsh_output(vtbl, "%-12s %-17s 0x%" PRIx32 "\n",
ipaddr, ethaddr, (uint32_t)arptab[i].at_time);
#endif
nsh_output(vtbl, "%-12s %-17s %s\n",
ipaddr, ethaddr, arptab[i].arp_dev);
}
free(arptab);