From: Masatake YAMATO Date: Sun, 11 Jun 2017 07:42:28 +0000 (+0900) Subject: socketeutils: extend receive_responses further X-Git-Tag: v4.18~97 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3a33709f40c61cdd0b291a9b24715159eb306b18;p=strace socketeutils: extend receive_responses further This is the second patch in series of implementing NETLINK_GENERIC protocol decoder. This change allows passing of opaque user data to the callback function. * socketutils.c (receive_responses): Change the type of proto_name argument from "const char *" to "void *" to allow passing of arbitrary data to the callback function. Rename proto_name to opaque_data, make it the last argument. Pass it to the callback function as the last argument. (inet_parse_response, unix_parse_response, netlink_parse_response): Change the type of proto_name argument from "const char *" to "void *", rename it to opaque_data, make it the last argument. Introduce a local variable proto_name to convert the type of opaque_data argument from "void *" to "const char *". (unix_get, inet_get, netlink_get): Cast the protocol name from "const char *" to "void *" for passing to the callback function. Signed-off-by: Masatake YAMATO Signed-off-by: Dmitry V. Levin --- diff --git a/socketutils.c b/socketutils.c index 01d369e1..6486e275 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 *opaque_data) { + const char *const proto_name = opaque_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, const unsigned long expected_msg_type, - const char *proto_name, - int (* parser) (const char *, const void *, - int, unsigned long)) + int (*parser)(const void *, int, + unsigned long, void *), + void *opaque_data) { 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, opaque_data); if (rc > 0) return true; if (rc < 0) @@ -263,9 +264,10 @@ 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 *opaque_data) { + const char *proto_name = opaque_data; const struct unix_diag_msg *diag_msg = data; struct rtattr *attr; int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg)); @@ -361,9 +363,10 @@ 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 *opaque_data) { + const char *proto_name = opaque_data; const struct netlink_diag_msg *const diag_msg = data; const char *netlink_proto; char *details; @@ -398,7 +401,7 @@ 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) + unix_parse_response, (void *) "UNIX") ? get_sockaddr_by_inode_cached(inode) : NULL; } @@ -408,7 +411,7 @@ inet_get(const int fd, const int family, const int protocol, { return inet_send_query(fd, family, protocol) && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, - proto_name, inet_parse_response) + inet_parse_response, (void *) proto_name) ? get_sockaddr_by_inode_cached(inode) : NULL; } @@ -441,7 +444,7 @@ netlink_get(const int fd, const unsigned long inode) { return netlink_send_query(fd, inode) && receive_responses(fd, inode, SOCK_DIAG_BY_FAMILY, - "NETLINK", netlink_parse_response) + netlink_parse_response, (void *) "NETLINK") ? get_sockaddr_by_inode_cached(inode) : NULL; }