2 * This file is part of inet-yy strace test.
4 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <netinet/in.h>
36 #include <linux/sock_diag.h>
37 #include <linux/inet_diag.h>
40 send_query(const int fd, const int family, const int proto)
42 struct sockaddr_nl nladdr = {
43 .nl_family = AF_NETLINK
47 struct inet_diag_req_v2 idr;
50 .nlmsg_len = sizeof(req),
51 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
52 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
55 .sdiag_family = family,
56 .sdiag_protocol = proto,
62 .iov_len = sizeof(req)
65 .msg_name = (void *) &nladdr,
66 .msg_namelen = sizeof(nladdr),
71 if (sendmsg(fd, &msg, 0) <= 0)
72 perror_msg_and_skip("sendmsg");
76 check_responses(const int fd)
80 long buf[8192 / sizeof(long)];
83 struct sockaddr_nl nladdr = {
84 .nl_family = AF_NETLINK
87 .iov_base = hdr_buf.buf,
88 .iov_len = sizeof(hdr_buf.buf)
91 .msg_name = (void *) &nladdr,
92 .msg_namelen = sizeof(nladdr),
97 ssize_t ret = recvmsg(fd, &msg, 0);
99 perror_msg_and_skip("recvmsg");
101 struct nlmsghdr *h = &hdr_buf.hdr;
102 if (!NLMSG_OK(h, ret))
103 error_msg_and_skip("!NLMSG_OK");
104 if (h->nlmsg_type == NLMSG_ERROR) {
105 const struct nlmsgerr *err = NLMSG_DATA(h);
106 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
107 error_msg_and_skip("NLMSG_ERROR");
109 perror_msg_and_skip("NLMSG_ERROR");
111 if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
112 error_msg_and_skip("unexpected nlmsg_type %u",
113 (unsigned) h->nlmsg_type);
115 const struct inet_diag_msg *diag = NLMSG_DATA(h);
116 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*diag)))
117 error_msg_and_skip("short response");
122 struct sockaddr_in addr;
123 socklen_t len = sizeof(addr);
125 memset(&addr, 0, sizeof(addr));
126 addr.sin_family = AF_INET;
127 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
132 if (socket(AF_INET, SOCK_STREAM, 0))
133 perror_msg_and_skip("socket AF_INET");
134 if (bind(0, (struct sockaddr *) &addr, len))
135 perror_msg_and_skip("bind");
137 perror_msg_and_skip("listen");
138 if (socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG) != 1)
139 perror_msg_and_skip("socket AF_NETLINK");
141 send_query(1, AF_INET, IPPROTO_TCP);