blob: 342be072ae45380a808ac8997409b2a4cb0f07c7 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +00002 * This file is part of inet-yy strace test.
3 *
Dmitry V. Levin285c3f42016-01-06 15:53:45 +00004 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00005 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
17 *
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.
28 */
29
Dmitry V. Levin285c3f42016-01-06 15:53:45 +000030#include "tests.h"
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +000031#include <errno.h>
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000032#include <string.h>
33#include <unistd.h>
34#include <netinet/in.h>
35#include <linux/netlink.h>
36#include <linux/sock_diag.h>
37#include <linux/inet_diag.h>
38
Dmitry V. Levin285c3f42016-01-06 15:53:45 +000039static void
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000040send_query(const int fd, const int family, const int proto)
41{
Dmitry V. Levinaf534b82014-11-21 19:59:16 +000042 struct sockaddr_nl nladdr = {
43 .nl_family = AF_NETLINK
44 };
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000045 struct {
46 struct nlmsghdr nlh;
47 struct inet_diag_req_v2 idr;
Dmitry V. Levinaf534b82014-11-21 19:59:16 +000048 } req = {
49 .nlh = {
50 .nlmsg_len = sizeof(req),
51 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
52 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
53 },
54 .idr = {
55 .sdiag_family = family,
56 .sdiag_protocol = proto,
57 .idiag_states = -1
58 }
59 };
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000060 struct iovec iov = {
61 .iov_base = &req,
62 .iov_len = sizeof(req)
63 };
64 struct msghdr msg = {
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +000065 .msg_name = (void *) &nladdr,
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000066 .msg_namelen = sizeof(nladdr),
67 .msg_iov = &iov,
68 .msg_iovlen = 1
69 };
70
Dmitry V. Levin285c3f42016-01-06 15:53:45 +000071 if (sendmsg(fd, &msg, 0) <= 0)
72 perror_msg_and_skip("sendmsg");
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000073}
74
Dmitry V. Levin285c3f42016-01-06 15:53:45 +000075static void
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000076check_responses(const int fd)
77{
Dmitry V. Levinab263902016-02-24 02:51:09 +000078 static long buf[8192 / sizeof(long)];
Dmitry V. Levinaf534b82014-11-21 19:59:16 +000079 struct sockaddr_nl nladdr = {
80 .nl_family = AF_NETLINK
81 };
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000082 struct iovec iov = {
83 .iov_base = buf,
84 .iov_len = sizeof(buf)
85 };
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000086 struct msghdr msg = {
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +000087 .msg_name = (void *) &nladdr,
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000088 .msg_namelen = sizeof(nladdr),
89 .msg_iov = &iov,
Dmitry V. Levinaf534b82014-11-21 19:59:16 +000090 .msg_iovlen = 1
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000091 };
92
93 ssize_t ret = recvmsg(fd, &msg, 0);
94 if (ret <= 0)
Dmitry V. Levin285c3f42016-01-06 15:53:45 +000095 perror_msg_and_skip("recvmsg");
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000096
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +000097 struct nlmsghdr *h = (struct nlmsghdr *) buf;
Dmitry V. Levin285c3f42016-01-06 15:53:45 +000098 if (!NLMSG_OK(h, ret))
99 error_msg_and_skip("!NLMSG_OK");
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000100 if (h->nlmsg_type == NLMSG_ERROR) {
101 const struct nlmsgerr *err = NLMSG_DATA(h);
102 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
103 error_msg_and_skip("NLMSG_ERROR");
104 errno = -err->error;
105 perror_msg_and_skip("NLMSG_ERROR");
106 }
107 if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
108 error_msg_and_skip("unexpected nlmsg_type %u",
109 (unsigned) h->nlmsg_type);
110
111 const struct inet_diag_msg *diag = NLMSG_DATA(h);
112 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*diag)))
113 error_msg_and_skip("short response");
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +0000114}
115
116int main(void)
117{
118 struct sockaddr_in addr;
119 socklen_t len = sizeof(addr);
120
121 memset(&addr, 0, sizeof(addr));
122 addr.sin_family = AF_INET;
123 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
124
125 close(0);
126 close(1);
127
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +0000128 if (socket(AF_INET, SOCK_STREAM, 0))
129 perror_msg_and_skip("socket AF_INET");
Dmitry V. Levin285c3f42016-01-06 15:53:45 +0000130 if (bind(0, (struct sockaddr *) &addr, len))
131 perror_msg_and_skip("bind");
132 if (listen(0, 5))
133 perror_msg_and_skip("listen");
134 if (socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG) != 1)
135 perror_msg_and_skip("socket AF_NETLINK");
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +0000136
Dmitry V. Levin285c3f42016-01-06 15:53:45 +0000137 send_query(1, AF_INET, IPPROTO_TCP);
138 check_responses(1);
139 return 0;
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +0000140}