[PATCH v3 16/16] tests: enhance netlink attribute test
JingPiao Chen
chenjingpiao at gmail.com
Mon Jun 19 01:15:40 UTC 2017
* tests/netlink_attribute.c: Include <arpa/inet.h>
and <linux/inet_diag.h>.
(test_nla_str, test_nla_u8, test_nla_u32): New functions.
(main): Use them.
---
tests/netlink_attribute.c | 224 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 224 insertions(+)
diff --git a/tests/netlink_attribute.c b/tests/netlink_attribute.c
index 61c75a0..8451fbe 100644
--- a/tests/netlink_attribute.c
+++ b/tests/netlink_attribute.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>
@@ -311,6 +313,225 @@ 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;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+ long rc;
+
+ nla_len = NLA_HDRLEN + 4;
+ nlh = nlh0 - nla_len;
+ *nlh = (struct nlmsghdr) {
+ .nlmsg_len = NLMSG_SPACE(sizeof(*msg)) + nla_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, nlh->nlmsg_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, nlh->nlmsg_len, address, address, nla->nla_len,
+ nlh->nlmsg_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;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+ long rc;
+
+ nla_len = NLA_HDRLEN + sizeof(*shutdown);
+ nlh = nlh0 - nla_len;
+ *nlh = (struct nlmsghdr) {
+ .nlmsg_len = NLMSG_SPACE(sizeof(*msg)) + nla_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, nlh->nlmsg_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, nlh->nlmsg_len, address, address, nla->nla_len, 0xcd,
+ nlh->nlmsg_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;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+ long rc;
+
+ /* len < sizeof(*mark) */
+ nla_len = NLA_HDRLEN + 2;
+ nlh = nlh0 - nla_len;
+ *nlh = (struct nlmsghdr) {
+ .nlmsg_len = NLMSG_SPACE(sizeof(*msg)) + nla_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, nlh->nlmsg_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, nlh->nlmsg_len, address, address, nla->nla_len,
+ nlh->nlmsg_len, sprintrc(rc));
+
+ /* short read of mark */
+ nla_len = NLA_HDRLEN + sizeof(*mark);
+ memmove(nlh0 - (nla_len - 1), nlh, NLMSG_SPACE(sizeof(*msg)));
+ nlh = nlh0 - (nla_len - 1);
+ nlh->nlmsg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_MARK
+ };
+
+ rc = sendto(fd, nlh, nlh->nlmsg_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, nlh->nlmsg_len, address, address, nla->nla_len,
+ RTA_DATA(nla), nlh->nlmsg_len, sprintrc(rc));
+
+ /* mark */
+ nla_len = NLA_HDRLEN + sizeof(*mark);
+ memmove(nlh0 - nla_len, nlh, NLMSG_SPACE(sizeof(*msg)));
+ nlh = nlh0 - nla_len;
+ nlh->nlmsg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+
+ 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;
+
+ rc = sendto(fd, nlh, nlh->nlmsg_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, nlh->nlmsg_len, address, address, nla->nla_len,
+ 0xabdfadca, nlh->nlmsg_len, sprintrc(rc));
+}
+
int main(void)
{
skip_if_unavailable("/proc/self/fd/");
@@ -319,6 +540,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