From 69bfc89770152033d4aa0bc5673fc46e65d89838 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Sun, 31 Jan 2016 00:35:29 +0000 Subject: [PATCH] Change unix_diag requests to use exact match instead of full dump * socketutils.c (unix_send_query): Remove NLM_F_DUMP from nlmsg_flags, initialize udiag_cookie. * tests/netlink_unix_diag.c (send_query): Remove "family" and "proto" arguments, add "inode" argument, remove NLM_F_DUMP from nlmsg_flags, initialize udiag_ino and udiag_cookie. (check_responses): Add "inode" argument, check inode match. (main): Pass listening socket inode to send_query and check_responses. --- socketutils.c | 5 +++-- tests/netlink_unix_diag.c | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/socketutils.c b/socketutils.c index 6fbb8cfe..7046d298 100644 --- a/socketutils.c +++ b/socketutils.c @@ -209,13 +209,14 @@ unix_send_query(const int fd, const unsigned long inode) .nlh = { .nlmsg_len = sizeof(req), .nlmsg_type = SOCK_DIAG_BY_FAMILY, - .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST + .nlmsg_flags = NLM_F_REQUEST }, .udr = { .sdiag_family = AF_UNIX, .udiag_ino = inode, .udiag_states = -1, - .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER + .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER, + .udiag_cookie = { ~0U, ~0U } } }; struct iovec iov = { diff --git a/tests/netlink_unix_diag.c b/tests/netlink_unix_diag.c index 12033e0f..932a9e18 100644 --- a/tests/netlink_unix_diag.c +++ b/tests/netlink_unix_diag.c @@ -45,7 +45,7 @@ #endif static void -send_query(const int fd, const int family, const int proto) +send_query(const int fd, const unsigned int inode) { struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK @@ -57,13 +57,14 @@ send_query(const int fd, const int family, const int proto) .nlh = { .nlmsg_len = sizeof(req), .nlmsg_type = SOCK_DIAG_BY_FAMILY, - .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST + .nlmsg_flags = NLM_F_REQUEST }, .udr = { - .sdiag_family = family, - .sdiag_protocol = proto, + .sdiag_family = AF_UNIX, + .udiag_ino = inode, .udiag_states = -1, - .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER + .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER, + .udiag_cookie = { ~0U, ~0U } } }; struct iovec iov = { @@ -82,7 +83,7 @@ send_query(const int fd, const int family, const int proto) } static void -check_responses(const int fd) +check_responses(const int fd, const unsigned int inode) { static char buf[8192]; struct sockaddr_nl nladdr = { @@ -120,6 +121,8 @@ check_responses(const int fd) const struct unix_diag_msg *diag = NLMSG_DATA(h); if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*diag))) error_msg_and_skip("short response"); + if (diag->udiag_ino != inode) + error_msg_and_skip("inode mismatch"); } #define SUN_PATH "netlink_unix_diag_socket" @@ -147,7 +150,8 @@ int main(void) if (socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) != 1) perror_msg_and_skip("socket AF_NETLINK"); - send_query(1, AF_UNIX, 0); - check_responses(1); + unsigned int inode = inode_of_sockfd(0); + send_query(1, inode); + check_responses(1, inode); return 0; } -- 2.40.0