This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 16c0c61e3e829a797bce279abc6908081dcc7a57
Author: chao.an <[email protected]>
AuthorDate: Mon Apr 13 15:40:01 2020 +0800

    netlink/route: reuse response terminator
    
    Change-Id: I19c3d97b088231d96909d415d286c728ffe83881
    Signed-off-by: chao.an <[email protected]>
---
 net/netlink/netlink_route.c | 117 +++++++++++++++-----------------------------
 1 file changed, 40 insertions(+), 77 deletions(-)

diff --git a/net/netlink/netlink_route.c b/net/netlink/netlink_route.c
index 6f356d7..741b6ba 100644
--- a/net/netlink/netlink_route.c
+++ b/net/netlink/netlink_route.c
@@ -83,14 +83,6 @@
  * Private Types
  ****************************************************************************/
 
-/* Used to send message done */
-
-struct nlroute_msgdone_rsplist_s
-{
-  sq_entry_t flink;
-  struct nlmsghdr payload;
-};
-
 /* RTM_GETLINK:  Enumerate network devices */
 
 struct getlink_recvfrom_response_s
@@ -262,32 +254,58 @@ static int netlink_device_callback(FAR struct 
net_driver_s *dev,
 #endif
 
 /****************************************************************************
- * Name: netlink_get_devlist
+ * Name: netlink_response_terminator
  *
  * Description:
  *   Dump a list of all network devices of the specified type.
  *
  ****************************************************************************/
 
-#ifndef CONFIG_NETLINK_DISABLE_GETLINK
-static int netlink_get_devlist(FAR struct socket *psock,
+static int netlink_response_terminator(FAR struct socket *psock,
                               FAR const struct nlroute_sendto_request_s *req)
 {
-  struct nlroute_info_s info;
-  FAR struct nlroute_msgdone_rsplist_s *alloc;
-  FAR struct nlmsghdr *resp;
-  int ret;
+  FAR struct netlink_response_s *resp;
+  FAR struct nlmsghdr *hdr;
 
-  /* Pre-allocate the list terminator */
+  /* Allocate the list terminator */
 
-  alloc = (FAR struct nlroute_msgdone_rsplist_s *)
-    kmm_zalloc(sizeof(struct nlroute_msgdone_rsplist_s));
-  if (alloc == NULL)
+  resp = kmm_zalloc(sizeof(struct netlink_response_s));
+  if (resp == NULL)
     {
       nerr("ERROR: Failed to allocate response terminator.\n");
       return -ENOMEM;
     }
 
+  /* Initialize and send the list terminator */
+
+  hdr              = &resp->msg;
+  hdr->nlmsg_len   = sizeof(struct nlmsghdr);
+  hdr->nlmsg_type  = NLMSG_DONE;
+  hdr->nlmsg_flags = req->hdr.nlmsg_flags;
+  hdr->nlmsg_seq   = req->hdr.nlmsg_seq;
+  hdr->nlmsg_pid   = req->hdr.nlmsg_pid;
+
+  /* Finally, add the response to the list of pending responses */
+
+  netlink_add_response(psock, resp);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: netlink_get_devlist
+ *
+ * Description:
+ *   Dump a list of all network devices of the specified type.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_NETLINK_DISABLE_GETLINK
+static int netlink_get_devlist(FAR struct socket *psock,
+                              FAR const struct nlroute_sendto_request_s *req)
+{
+  struct nlroute_info_s info;
+  int ret;
+
   /* Visit each device */
 
   info.psock = psock;
@@ -298,23 +316,10 @@ static int netlink_get_devlist(FAR struct socket *psock,
   net_unlock();
   if (ret < 0)
     {
-      kmm_free(alloc);
       return ret;
     }
 
-  /* Initialize and send the list terminator */
-
-  resp               = &alloc->payload;
-  resp->nlmsg_len    = sizeof(struct nlmsghdr);
-  resp->nlmsg_type   = NLMSG_DONE;
-  resp->nlmsg_flags  = req->hdr.nlmsg_flags;
-  resp->nlmsg_seq    = req->hdr.nlmsg_seq;
-  resp->nlmsg_pid    = req->hdr.nlmsg_pid;
-
-  /* Finally, add the data to the list of pending responses */
-
-  netlink_add_response(psock, (FAR struct netlink_response_s *)alloc);
-  return OK;
+  return netlink_response_terminator(psock, req);
 }
 #endif
 
@@ -485,48 +490,6 @@ static int netlink_get_nbtable(FAR struct socket *psock,
 #endif
 
 /****************************************************************************
- * Name: netlink_route_terminator
- *
- * Description:
- *   Dump a list of all network devices of the specified type.
- *
- ****************************************************************************/
-
-#ifndef CONFIG_NETLINK_DISABLE_GETROUTE
-static int
-netlink_route_terminator(FAR struct socket *psock,
-                         FAR const struct nlroute_sendto_request_s *req)
-{
-  FAR struct nlroute_msgdone_rsplist_s *alloc;
-  FAR struct nlmsghdr *resp;
-
-  /* Allocate the list terminator */
-
-  alloc = (FAR struct nlroute_msgdone_rsplist_s *)
-    kmm_zalloc(sizeof(struct nlroute_msgdone_rsplist_s));
-  if (alloc == NULL)
-    {
-      nerr("ERROR: Failed to allocate response terminator.\n");
-      return -ENOMEM;
-    }
-
-  /* Initialize and send the list terminator */
-
-  resp              = &alloc->payload;
-  resp->nlmsg_len   = sizeof(struct nlmsghdr);
-  resp->nlmsg_type  = NLMSG_DONE;
-  resp->nlmsg_flags = req->hdr.nlmsg_flags;
-  resp->nlmsg_seq   = req->hdr.nlmsg_seq;
-  resp->nlmsg_pid   = req->hdr.nlmsg_pid;
-
-  /* Finally, add the response to the list of pending responses */
-
-  netlink_add_response(psock, (FAR struct netlink_response_s *)alloc);
-  return OK;
-}
-#endif
-
-/****************************************************************************
  * Name: netlink_ipv4_route
  *
  * Description:
@@ -615,7 +578,7 @@ static int netlink_get_ipv4route(FAR struct socket *psock,
 
   /* Terminate the routing table */
 
-  return netlink_route_terminator(psock, req);
+  return netlink_response_terminator(psock, req);
 }
 #endif
 
@@ -708,7 +671,7 @@ static int netlink_get_ip6vroute(FAR struct socket *psock,
 
   /* Terminate the routing table */
 
-  return netlink_route_terminator(psock, req);
+  return netlink_response_terminator(psock, req);
 }
 #endif
 

Reply via email to