[PATCH v6 08/18] tests: enhance netlink attribute test

JingPiao Chen chenjingpiao at gmail.com
Tue Jun 27 10:10:10 UTC 2017


* tests/nlattr.c: Include <arpa/inet.h> and <linux/inet_diag.h>.
(init_inet_diag_msg, test_nla_str, test_nla_u8,
test_nla_u32): New functions.
(main): Use test_nla_*.
---
 tests/nlattr.c | 209 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 209 insertions(+)

diff --git a/tests/nlattr.c b/tests/nlattr.c
index 15a0ad1..0cb6031 100644
--- a/tests/nlattr.c
+++ b/tests/nlattr.c
@@ -37,6 +37,8 @@
 #include <sys/socket.h>
 #include <netinet/tcp.h>
 #include "netlink.h"
+#include <arpa/inet.h>
+#include <linux/inet_diag.h>
 #include <linux/rtnetlink.h>
 #include <linux/sock_diag.h>
 #include <linux/unix_diag.h>
@@ -312,6 +314,210 @@ test_nla_type(const int fd)
 	       msg->nlh.nlmsg_len, sprintrc(rc));
 }
 
+static void
+init_inet_diag_msg(struct nlmsghdr *nlh, unsigned int msg_len,
+		   const char *address)
+{
+	struct inet_diag_msg *msg;
+
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	SET_STRUCT(struct inet_diag_msg, msg,
+		.idiag_family = AF_INET,
+		.idiag_state = TCP_LISTEN
+	);
+
+	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
+		perror_msg_and_skip("inet_pton");
+	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
+		perror_msg_and_skip("inet_pton");
+}
+
+static void
+test_nla_str(const int fd)
+{
+	const int hdrlen = sizeof(struct inet_diag_msg);
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct nlattr *nla;
+	unsigned int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+	long rc;
+
+	nla_len = NLA_HDRLEN + 4;
+	msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+	nlh = nlh0 - nla_len;
+	init_inet_diag_msg(nlh, msg_len, address);
+
+	nla = NLMSG_ATTR(nlh, hdrlen);
+	SET_STRUCT(struct nlattr, nla,
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_CONG
+	);
+	memcpy(RTA_DATA(nla), "123", 4);
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {idiag_family=AF_INET"
+	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
+	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
+	       ", idiag_if=0, idiag_cookie=[0, 0]}, idiag_expires=0"
+	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
+	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_CONG}"
+	       ", \"123\"}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address,
+	       nla_len, msg_len, sprintrc(rc));
+}
+
+static void
+test_nla_u8(const int fd)
+{
+	const int hdrlen = sizeof(struct inet_diag_msg);
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct nlattr *nla;
+	uint8_t *shutdown;
+	unsigned int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+	long rc;
+
+	nla_len = NLA_HDRLEN + sizeof(*shutdown);
+	msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+	nlh = nlh0 - nla_len;
+	init_inet_diag_msg(nlh, msg_len, address);
+
+	nla = NLMSG_ATTR(nlh, hdrlen);
+	SET_STRUCT(struct nlattr, nla,
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_SHUTDOWN
+	);
+	shutdown = RTA_DATA(nla);
+	*shutdown = 0xcd;
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {idiag_family=AF_INET"
+	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
+	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
+	       ", idiag_if=0, idiag_cookie=[0, 0]}, idiag_expires=0"
+	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
+	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_SHUTDOWN}"
+	       ", %u}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address, nla_len,
+	       *shutdown, msg_len, sprintrc(rc));
+}
+
+static void
+test_nla_u32(const int fd)
+{
+	const int hdrlen = sizeof(struct inet_diag_msg);
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct nlattr *nla;
+	uint32_t *mark;
+	unsigned int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+	long rc;
+
+	/* len < sizeof(uint32_t) */
+	nla_len = NLA_HDRLEN + 2;
+	msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+	nlh = nlh0 - nla_len;
+	init_inet_diag_msg(nlh, msg_len, address);
+
+	nla = NLMSG_ATTR(nlh, hdrlen);
+	SET_STRUCT(struct nlattr, nla,
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_MARK
+	);
+	memcpy(RTA_DATA(nla), "12", 2);
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {idiag_family=AF_INET"
+	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
+	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
+	       ", idiag_if=0, idiag_cookie=[0, 0]}, idiag_expires=0"
+	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
+	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_MARK}"
+	       ", \"12\"}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address, nla_len,
+	       msg_len, sprintrc(rc));
+
+	/* short read of mark */
+	nla_len = NLA_HDRLEN + sizeof(*mark);
+	msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+	nlh = nlh0 - (nla_len - 1);
+	init_inet_diag_msg(nlh, msg_len, address);
+
+	nla = NLMSG_ATTR(nlh, hdrlen);
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_MARK
+	};
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {idiag_family=AF_INET"
+	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
+	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
+	       ", idiag_if=0, idiag_cookie=[0, 0]}, idiag_expires=0"
+	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
+	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_MARK}"
+	       ", %p}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address, nla_len, RTA_DATA(nla),
+	       msg_len, sprintrc(rc));
+
+	/* mark */
+	nla_len = NLA_HDRLEN + sizeof(*mark);
+	msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+	nlh = nlh0 - nla_len;
+	init_inet_diag_msg(nlh, msg_len, address);
+
+	nla = NLMSG_ATTR(nlh, hdrlen);
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_MARK
+	};
+	mark = RTA_DATA(nla);
+	*mark = 0xabdfadca;
+
+	rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {idiag_family=AF_INET"
+	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
+	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
+	       ", idiag_if=0, idiag_cookie=[0, 0]}, idiag_expires=0"
+	       ", idiag_rqueue=0, idiag_wqueue=0, idiag_uid=0"
+	       ", idiag_inode=0}, {{nla_len=%u, nla_type=INET_DIAG_MARK}"
+	       ", %u}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address, nla_len, *mark,
+	       msg_len, sprintrc(rc));
+}
+
 int main(void)
 {
 	skip_if_unavailable("/proc/self/fd/");
@@ -320,6 +526,9 @@ int main(void)
 
 	test_nlattr(fd);
 	test_nla_type(fd);
+	test_nla_str(fd);
+	test_nla_u8(fd);
+	test_nla_u32(fd);
 
 	puts("+++ exited with 0 +++");
 
-- 
2.7.4





More information about the Strace-devel mailing list