Now I'm working on decoding the communication on Netlink GENERIC
protocol.  For the task, many lines of receive_responses can be reused
with two minor modifications. This is the second one.

* socketutils.c (receive_responses): change the data type
of parameter `proto_name' to `void *' from `const char *'
to allow callee passing arbitrary data to the call backward
function `parser'. In addition the parameter name is
changed to `user_data'. To follow common convention, the
parameter is passed to `parser' as its last argument.
(unix_get, inet_get, netlink_get): Cast statically given
the protocol name to `void *' to pass it to the call
back function.
(inet_parse_response,unix_parse_response,netlink_parse_response):
Introduce a local variable `proto_name' to convert the data type
of argument from `void *' to `const char*'.

Signed-off-by: Masatake YAMATO <yam...@redhat.com>
---
 socketutils.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/socketutils.c b/socketutils.c
index 3292c2e..0016942 100644
--- a/socketutils.c
+++ b/socketutils.c
@@ -134,9 +134,10 @@ inet_send_query(const int fd, const int family, const int 
proto)
 }
 
 static int
-inet_parse_response(const char *const proto_name, const void *const data,
-                   const int data_len, const unsigned long inode)
+inet_parse_response(const void *const data,
+                   const int data_len, const unsigned long inode, void 
*user_data)
 {
+       const char *const proto_name = user_data;
        const struct inet_diag_msg *const diag_msg = data;
        static const char zero_addr[sizeof(struct in6_addr)];
        socklen_t addr_size, text_size;
@@ -190,9 +191,9 @@ inet_parse_response(const char *const proto_name, const 
void *const data,
 static bool
 receive_responses(const int fd, const unsigned long inode,
                  unsigned long expected_msg_type,
-                 const char *proto_name,
-                 int (* parser) (const char *, const void *,
-                                 int, unsigned long))
+                 void *user_data,
+                 int (* parser) (const void *,
+                                 int, unsigned long, void *))
 {
        static union {
                struct nlmsghdr hdr;
@@ -229,8 +230,8 @@ receive_responses(const int fd, const unsigned long inode,
                for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
                        if (h->nlmsg_type != expected_msg_type)
                                return false;
-                       const int rc = parser(proto_name, NLMSG_DATA(h),
-                                             h->nlmsg_len, inode);
+                       const int rc = parser(NLMSG_DATA(h),
+                                             h->nlmsg_len, inode, user_data);
                        if (rc > 0)
                                return true;
                        if (rc < 0)
@@ -263,9 +264,11 @@ unix_send_query(const int fd, const unsigned long inode)
 }
 
 static int
-unix_parse_response(const char *proto_name, const void *data,
-                   const int data_len, const unsigned long inode)
+unix_parse_response(const void *data,
+                   const int data_len, const unsigned long inode,
+                   void *user_data)
 {
+       const char *proto_name = user_data;
        const struct unix_diag_msg *diag_msg = data;
        struct rtattr *attr;
        int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
@@ -361,9 +364,11 @@ netlink_send_query(const int fd, const unsigned long inode)
 }
 
 static int
-netlink_parse_response(const char *proto_name, const void *data,
-                   const int data_len, const unsigned long inode)
+netlink_parse_response(const void *data,
+                   const int data_len, const unsigned long inode,
+                   void *user_data)
 {
+       const char *proto_name = user_data;
        const struct netlink_diag_msg *const diag_msg = data;
        const char *netlink_proto;
        char *details;
@@ -397,7 +402,7 @@ static const char *
 unix_get(const int fd, const unsigned long inode)
 {
        return unix_send_query(fd, inode)
-               && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, "UNIX", 
unix_parse_response)
+               && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, (void 
*)"UNIX", unix_parse_response)
                ? get_sockaddr_by_inode_cached(inode) : NULL;
 }
 
@@ -406,7 +411,7 @@ inet_get(const int fd, const int family, const int protocol,
         const unsigned long inode, const char *proto_name)
 {
        return inet_send_query(fd, family, protocol)
-               && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, 
proto_name, inet_parse_response)
+               && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, (void 
*)proto_name, inet_parse_response)
                ? get_sockaddr_by_inode_cached(inode) : NULL;
 }
 
@@ -438,7 +443,7 @@ static const char *
 netlink_get(const int fd, const unsigned long inode)
 {
        return netlink_send_query(fd, inode)
-               && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, "NETLINK",
+               && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, (void 
*)"NETLINK",
                                     netlink_parse_response)
                ? get_sockaddr_by_inode_cached(inode) : NULL;
 }
-- 
2.9.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Strace-devel mailing list
Strace-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/strace-devel

Reply via email to