[PATCH v4 10/10] tests: enhance netlink attribute test

JingPiao Chen chenjingpiao at gmail.com
Tue Jun 20 09:23:55 UTC 2017


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

diff --git a/tests/nlattr.c b/tests/nlattr.c
index 60cc3d7..baa82d9 100644
--- a/tests/nlattr.c
+++ b/tests/nlattr.c
@@ -35,6 +35,8 @@
 #include <unistd.h>
 #include <sys/socket.h>
 #include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <linux/inet_diag.h>
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
 #include <linux/sock_diag.h>
@@ -44,6 +46,12 @@
 # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
 #endif
 
+#define INIT_STRUCT(type, name, ...)			\
+	do {						\
+		type tmp = { __VA_ARGS__ };		\
+		memcpy(name, &tmp, sizeof(tmp));	\
+	} while (0)
+
 static void
 test_nlattr(const int fd)
 {
@@ -294,6 +302,261 @@ test_nla_type(const int fd)
 	       msg->nlh.nlmsg_len, sprintrc(rc));
 }
 
+static void
+test_nla_str(const int fd)
+{
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct inet_diag_msg *msg;
+	struct nlattr *nla;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	nla_len = NLA_HDRLEN + 4;
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	INIT_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct inet_diag_msg) {
+		.idiag_family = AF_INET,
+		.idiag_state = TCP_LISTEN,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_CONG
+	};
+	memcpy(RTA_DATA(nla), "123", 4);
+
+	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
+		perror_msg_and_skip("sendto");
+	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
+		perror_msg_and_skip("sendto");
+
+	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->nla_len,
+	       msg_len, sprintrc(rc));
+}
+
+static void
+test_nla_u8(const int fd)
+{
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct inet_diag_msg *msg;
+	struct nlattr *nla;
+	uint8_t *shutdown;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	nla_len = NLA_HDRLEN + sizeof(*shutdown);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	INIT_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct inet_diag_msg) {
+		.idiag_family = AF_INET,
+		.idiag_state = TCP_LISTEN,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_SHUTDOWN
+	};
+	shutdown = RTA_DATA(nla);
+	*shutdown = 0xcd;
+
+	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
+		perror_msg_and_skip("sendto");
+	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
+		perror_msg_and_skip("sendto");
+
+	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->nla_len, 0xcd,
+	       msg_len, sprintrc(rc));
+}
+
+static void
+test_nla_u32(const int fd)
+{
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct inet_diag_msg *msg;
+	struct nlattr *nla;
+	uint32_t *mark;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	/* len < sizeof(*mark) */
+	nla_len = NLA_HDRLEN + 2;
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	INIT_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct inet_diag_msg) {
+		.idiag_family = AF_INET,
+		.idiag_state = TCP_LISTEN,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_MARK
+	};
+	memcpy(RTA_DATA(nla), "12", 2);
+
+	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
+		perror_msg_and_skip("sendto");
+	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
+		perror_msg_and_skip("sendto");
+
+	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->nla_len,
+	       msg_len, sprintrc(rc));
+
+	/* short read of mark */
+	nla_len = NLA_HDRLEN + sizeof(*mark);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - (nla_len - 1);
+	INIT_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct inet_diag_msg) {
+		.idiag_family = AF_INET,
+		.idiag_state = TCP_LISTEN,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_MARK
+	};
+
+	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
+		perror_msg_and_skip("sendto");
+	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
+		perror_msg_and_skip("sendto");
+
+	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->nla_len, RTA_DATA(nla),
+	       msg_len, sprintrc(rc));
+
+	/* mark */
+	nla_len = NLA_HDRLEN + sizeof(*mark);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	INIT_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP
+	);
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct inet_diag_msg) {
+		.idiag_family = AF_INET,
+		.idiag_state = TCP_LISTEN,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = INET_DIAG_MARK
+	};
+	mark = RTA_DATA(nla);
+	*mark = 0xabdfadca;
+
+	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
+		perror_msg_and_skip("sendto");
+	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
+		perror_msg_and_skip("sendto");
+
+	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->nla_len, 0xabdfadca,
+	       msg_len, sprintrc(rc));
+}
+
 int main(void)
 {
 	skip_if_unavailable("/proc/self/fd/");
@@ -302,6 +565,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