From b65c3dac0e391a24467e6ebe25fef3e53d470557 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 3 Nov 2019 19:29:20 -0600 Subject: [PATCH] netutils/netlib/netlib_getarptab.c: Misc fixes from initial testing with the 'arp -t' commeand. --- include/netutils/netlib.h | 2 +- netutils/netlib/netlib_getarptab.c | 40 ++++++++++++++++++++++++++++-- netutils/tftpc/tftpc_packets.c | 2 +- nshlib/nsh_command.c | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h index 2f3decf48..d6d71b03f 100644 --- a/include/netutils/netlib.h +++ b/include/netutils/netlib.h @@ -313,7 +313,7 @@ int netlib_get_arpmapping(FAR const struct sockaddr_in *inaddr, int netlib_set_arpmapping(FAR const struct sockaddr_in *inaddr, FAR const uint8_t *macaddr); #ifdef CONFIG_NETLINK_ROUTE -struct arp_enty_s; +struct arp_entry_s; ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab, unsigned int nentries); #endif diff --git a/netutils/netlib/netlib_getarptab.c b/netutils/netlib/netlib_getarptab.c index 87bb08b1a..14c43b70f 100644 --- a/netutils/netlib/netlib_getarptab.c +++ b/netutils/netlib/netlib_getarptab.c @@ -99,6 +99,9 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab, unsigned int nentrie { FAR struct netlib_recvfrom_response_s *resp; struct netlib_sendto_request_s req; + struct sockaddr_nl addr; + static unsigned int seqno = 0; + unsigned int thiseq; unsigned int allocsize; ssize_t nsent; ssize_t nrecvd; @@ -107,14 +110,14 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab, unsigned int nentrie int fd; int ret; - /* Allocate a buffer to hold the response */ + /* Pre-allocate a buffer to hold the response */ maxsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arp_entry_s); allocsize = SIZEOF_NETLIB_RECVFROM_RESPONSE_S(maxsize); resp = (FAR struct netlib_recvfrom_response_s *)malloc(allocsize); if (resp == NULL) { - fprintf(stderr, "ERROR: Faile to allocat response buffer\n"); + fprintf(stderr, "ERROR: Failed to allocate response buffer\n"); ret = -ENOMEM; return EXIT_FAILURE; } @@ -130,11 +133,29 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab, unsigned int nentrie goto errout_with_resp; } + /* Bind the socket so that we can use send() and receive() */ + + memset(&addr, 0, sizeof(struct sockaddr_nl)); + addr.nl_family = AF_NETLINK; + addr.nl_groups = RTM_GETNEIGH; + + ret = bind(fd, &addr, sizeof( struct sockaddr_nl)); + if (fd < 0) + { + int errcode = errno; + fprintf(stderr, "ERROR: bind() failed: %d\n", errcode); + ret = -errcode; + goto errout_with_socket; + } + /* Initialize the request */ + thiseq = ++seqno; + memset(&req, 0, sizeof(req)); req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT | NLM_F_REQUEST; + req.hdr.nlmsg_seq = thiseq; req.hdr.nlmsg_type = RTM_GETNEIGH; req.msg.ndm_family = AF_INET; @@ -147,6 +168,20 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab, unsigned int nentrie goto errout_with_socket; } + /* Initialize the response buffer. + * REVISIT: Linux examples that I have seen just pass a raw buffer. I am + * not sure how they associate the requested data to the recv() without a + * sequence number. + */ + + memset(&resp->hdr, 0, sizeof(resp->hdr)); + resp->hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); + resp->hdr.nlmsg_seq = thiseq; + resp->hdr.nlmsg_type = RTM_GETNEIGH; + + memset(&resp->msg, 0, sizeof(resp->msg)); + resp->msg.ndm_family = AF_INET; + nrecvd = recv(fd, resp, allocsize, 0); if (nrecvd < 0) { @@ -179,6 +214,7 @@ ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab, unsigned int nentrie errout_with_socket: close(fd); + errout_with_resp: free(resp); return ret; diff --git a/netutils/tftpc/tftpc_packets.c b/netutils/tftpc/tftpc_packets.c index 0dc6b3b79..8f0b4d38f 100644 --- a/netutils/tftpc/tftpc_packets.c +++ b/netutils/tftpc/tftpc_packets.c @@ -208,7 +208,7 @@ int tftp_parseerrpacket(const uint8_t *buffer) if (opcode == TFTP_ERR) { - ninfo("ERR message: %s (%d)\n", errmsg, errcode); + nwarn("WARNING: ERR message: %s (%d)\n", errmsg, errcode); return OK; } diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 52c8821a1..f04431f20 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/nshlib/nsh_command.c * - * Copyright (C) 2007-2018 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2019 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without