* tests/netlink_sock_diag.c: Include <arpa/inet.h>
and <linux/inet_diag.h>.
(test_inet_diag_req, test_inet_diag_req_v2)
(test_inet_diag_msg): New functions.
(mian): Use them.

Co-authored-by: Fabien Siron <fabien.si...@epita.fr>
---
 tests/netlink_sock_diag.c | 397 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 397 insertions(+)

diff --git a/tests/netlink_sock_diag.c b/tests/netlink_sock_diag.c
index 17fd5f4..8171e2e 100644
--- a/tests/netlink_sock_diag.c
+++ b/tests/netlink_sock_diag.c
@@ -33,8 +33,10 @@
 #include <stdint.h>
 #include <unistd.h>
 #include <sys/socket.h>
+#include <arpa/inet.h>
 #include <netinet/tcp.h>
 #include <linux/if_ether.h>
+#include <linux/inet_diag.h>
 #include <linux/netlink.h>
 #include <linux/netlink_diag.h>
 #include <linux/packet_diag.h>
@@ -792,6 +794,398 @@ test_packet_diag_msg(const int fd)
               sprintrc(rc));
 }
 
+static void
+test_inet_diag_req(const int fd)
+{
+       const char address[] = "8.8.8.8";
+       struct nlmsghdr *nlh;
+       struct inet_diag_req *req;
+       uint8_t *family;
+       void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+       long rc;
+
+       /* print family only */
+       nlh = nlh0 - sizeof(*family);
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
+               .nlmsg_type = TCPDIAG_GETSOCK,
+               .nlmsg_flags = NLM_F_REQUEST,
+       };
+
+       family = NLMSG_DATA(nlh);
+       *family = AF_INET;
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_INET}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              sprintrc(rc));
+
+       /* print unknown family */
+       *family = 0xff;
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=0xff /* AF_??? 
*/}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              sprintrc(rc));
+
+       /* short read of family */
+       memmove(nlh0, nlh, NLMSG_HDRLEN);
+       nlh = nlh0;
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}, %p}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              NLMSG_DATA(nlh),
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              sprintrc(rc));
+
+       /* print family and string */
+       nlh = nlh0 - (sizeof(*family) + 4);
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
+               .nlmsg_type = TCPDIAG_GETSOCK,
+               .nlmsg_flags = NLM_F_REQUEST,
+       };
+
+       family = NLMSG_DATA(nlh);
+       *family = AF_INET;
+       memcpy(family + 1, "1234", 4);
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+              ", {family=AF_INET, \"1234\"}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+              sprintrc(rc));
+
+       /* print inet_diag_req */
+       nlh = nlh0 - sizeof(*req);
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
+               .nlmsg_type = TCPDIAG_GETSOCK,
+               .nlmsg_flags = NLM_F_REQUEST,
+       };
+
+       req = NLMSG_DATA(nlh);
+       *req = (struct inet_diag_req) {
+               .idiag_family = AF_INET,
+               .idiag_ext = 1 << (INET_DIAG_TOS - 1),
+               .idiag_states = 1 << TCP_LAST_ACK,
+       };
+
+       if (!inet_pton(AF_INET, address, req->id.idiag_src))
+               perror_msg_and_skip("sendto");
+       if (!inet_pton(AF_INET, address, req->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=TCPDIAG_GETSOCK"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}, {idiag_family=AF_INET"
+              ", idiag_src_len=0, idiag_dst_len=0"
+              ", idiag_ext=(1<<(INET_DIAG_TOS-1)), 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_states=1<<TCP_LAST_ACK"
+              ", idiag_dbs=0}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, nlh->nlmsg_len, address, address, nlh->nlmsg_len,
+              sprintrc(rc));
+
+       /* short read of inet_diag_req */
+       nlh = nlh0 - sizeof(*family);
+       memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*family));
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_INET, %p}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
+              NLMSG_DATA(nlh) + 1,
+              NLMSG_HDRLEN + (unsigned int) sizeof(*req),
+              sprintrc(rc));
+}
+
+static void
+test_inet_diag_req_v2(const int fd)
+{
+       const char address[] = "8.8.8.8";
+       struct nlmsghdr *nlh;
+       struct inet_diag_req_v2 *req;
+       uint8_t *family;
+       void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+       long rc;
+
+       /* print family only */
+       nlh = nlh0 - sizeof(*family);
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
+               .nlmsg_type = SOCK_DIAG_BY_FAMILY,
+               .nlmsg_flags = NLM_F_REQUEST,
+       };
+
+       family = NLMSG_DATA(nlh);
+       *family = AF_INET;
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_INET}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              sprintrc(rc));
+
+       /* print unknown family */
+       *family = 0xff;
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=0xff /* AF_??? 
*/}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              sprintrc(rc));
+
+       /* short read of family */
+       memmove(nlh0, nlh, NLMSG_HDRLEN);
+       nlh = nlh0;
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}, %p}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              NLMSG_DATA(nlh),
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              sprintrc(rc));
+
+       /* print family and string */
+       nlh = nlh0 - sizeof(*family) - 4;
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
+               .nlmsg_type = SOCK_DIAG_BY_FAMILY,
+               .nlmsg_flags = NLM_F_REQUEST,
+       };
+
+       family = NLMSG_DATA(nlh);
+       *family = AF_INET;
+       memcpy(family + 1, "1234", 4);
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+              ", {family=AF_INET, \"1234\"}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+              sprintrc(rc));
+
+       /* print inet_diag_req_v2 */
+       nlh = nlh0 - sizeof(*req);
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
+               .nlmsg_type = SOCK_DIAG_BY_FAMILY,
+               .nlmsg_flags = NLM_F_REQUEST,
+       };
+
+       req = NLMSG_DATA(nlh);
+       *req = (struct inet_diag_req_v2) {
+               .sdiag_family = AF_INET,
+               .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+               .sdiag_protocol = IPPROTO_TCP,
+               .idiag_states = 1 << TCP_CLOSE,
+       };
+
+       if (!inet_pton(AF_INET, address, req->id.idiag_src))
+               perror_msg_and_skip("sendto");
+       if (!inet_pton(AF_INET, address, req->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_REQUEST, seq=0, pid=0}, {sdiag_family=AF_INET"
+              ", sdiag_protocol=IPPROTO_TCP, idiag_ext=(1<<(INET_DIAG_CONG-1))"
+              ", idiag_states=1<<TCP_CLOSE, 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]}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, nlh->nlmsg_len, address, address,
+              nlh->nlmsg_len, sprintrc(rc));
+
+       /* short read of inet_diag_req_v2 */
+       nlh = nlh0 - sizeof(*family);
+       memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*family));
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
+                   NULL, 0);
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_INET, %p}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
+              NLMSG_DATA(nlh) + 1,
+              NLMSG_HDRLEN + (unsigned int) sizeof(*req),
+              sprintrc(rc));
+}
+
+static void
+test_inet_diag_msg(const int fd)
+{
+       const char address[] = "8.8.8.8";
+       struct nlmsghdr *nlh;
+       struct inet_diag_msg *msg;
+       uint8_t *family;
+       void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+       long rc;
+
+       /* print family only */
+       nlh = nlh0 - sizeof(*family);
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
+               .nlmsg_type = SOCK_DIAG_BY_FAMILY,
+               .nlmsg_flags = NLM_F_DUMP,
+       };
+
+       family = NLMSG_DATA(nlh);
+       *family = AF_INET;
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_INET}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              sprintrc(rc));
+
+       /* print unknown family */
+       *family = 0xff;
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=0xff /* AF_??? */}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              sprintrc(rc));
+
+       /* short read of family */
+       memmove(nlh0, nlh, NLMSG_HDRLEN);
+       nlh = nlh0;
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_DUMP, seq=0, pid=0}, %p}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              NLMSG_DATA(nlh),
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+              sprintrc(rc));
+
+       /* print family and string */
+       nlh = nlh0 - sizeof(*family) - 4;
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
+               .nlmsg_type = SOCK_DIAG_BY_FAMILY,
+               .nlmsg_flags = NLM_F_DUMP,
+       };
+
+       family = NLMSG_DATA(nlh);
+       *family = AF_INET;
+       memcpy(family + 1, "1234", 4);
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
+                   NULL, 0);
+
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_DUMP, seq=0, pid=0}"
+              ", {family=AF_INET, \"1234\"}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+              NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+              sprintrc(rc));
+
+       /* print inet_diag_msg */
+       nlh = nlh0 - sizeof(*msg);
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(*msg),
+               .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,
+       };
+
+       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}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, nlh->nlmsg_len, address, address,
+              nlh->nlmsg_len, sprintrc(rc));
+
+       /* short read of inet_diag_msg */
+       nlh = nlh0 - sizeof(*family);
+       memmove(nlh, nlh0 - sizeof(*msg), NLMSG_HDRLEN + sizeof(*family));
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
+                   NULL, 0);
+       printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+              ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_INET, %p}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
+              NLMSG_DATA(nlh) + 1,
+              NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
+              sprintrc(rc));
+}
+
 int main(void)
 {
        skip_if_unavailable("/proc/self/fd/");
@@ -806,6 +1200,9 @@ int main(void)
        test_netlink_diag_msg(fd);
        test_packet_diag_req(fd);
        test_packet_diag_msg(fd);
+       test_inet_diag_req(fd);
+       test_inet_diag_req_v2(fd);
+       test_inet_diag_msg(fd);
 
        printf("+++ exited with 0 +++\n");
 
-- 
2.7.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Strace-devel mailing list
Strace-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/strace-devel

Reply via email to