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.
diff --git a/tests/netlink_unix_diag.c b/tests/netlink_unix_diag.c
index 12033e0..932a9e1 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 @@
.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 @@
}
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 @@
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 @@
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;
}