]> granicus.if.org Git - strace/blob - tests/nlattr_inet_diag_req_compat.c
tests: extend TEST_NETLINK_OBJECT macro
[strace] / tests / nlattr_inet_diag_req_compat.c
1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "tests.h"
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
35 #include <net/if.h>
36 #include <netinet/tcp.h>
37 #include "test_nlattr.h"
38 #include <linux/inet_diag.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/sock_diag.h>
41
42 static const char address[] = "10.11.12.13";
43
44 #ifdef HAVE_IF_INDEXTONAME
45 # define IFINDEX_LO     (if_nametoindex("lo"))
46 #else
47 # define IFINDEX_LO     1
48 #endif
49
50 static void
51 init_inet_diag_req(struct nlmsghdr *const nlh, const unsigned int msg_len)
52 {
53         SET_STRUCT(struct nlmsghdr, nlh,
54                 .nlmsg_len = msg_len,
55                 .nlmsg_type = TCPDIAG_GETSOCK,
56                 .nlmsg_flags = NLM_F_REQUEST
57         );
58
59         struct inet_diag_req *const req = NLMSG_DATA(nlh);
60         SET_STRUCT(struct inet_diag_req, req,
61                 .idiag_family = AF_INET,
62                 .idiag_ext = 1 << (INET_DIAG_TOS - 1),
63                 .idiag_states = 1 << TCP_LAST_ACK,
64                 .id.idiag_if = IFINDEX_LO
65         );
66
67         if (!inet_pton(AF_INET, address, req->id.idiag_src) ||
68             !inet_pton(AF_INET, address, req->id.idiag_dst))
69                 perror_msg_and_skip("inet_pton");
70 }
71
72 static void
73 print_inet_diag_req(const unsigned int msg_len)
74 {
75         printf("{len=%u, type=TCPDIAG_GETSOCK, flags=NLM_F_REQUEST"
76                ", seq=0, pid=0}, {idiag_family=AF_INET"
77                ", idiag_src_len=0, idiag_dst_len=0"
78                ", idiag_ext=1<<(INET_DIAG_TOS-1)"
79                ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
80                ", idiag_src=inet_addr(\"%s\")"
81                ", idiag_dst=inet_addr(\"%s\")"
82                ", idiag_if=if_nametoindex(\"lo\")"
83                ", idiag_cookie=[0, 0]}"
84                ", idiag_states=1<<TCP_LAST_ACK, idiag_dbs=0}",
85                msg_len, address, address);
86 }
87
88 int
89 main(void)
90 {
91         skip_if_unavailable("/proc/self/fd/");
92
93         int fd = create_nl_socket(NETLINK_SOCK_DIAG);
94         const unsigned int hdrlen = sizeof(struct inet_diag_req);
95         void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
96
97         static char pattern[4096];
98         fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
99
100         char nla_type_str[256];
101         sprintf(nla_type_str, "%#x /* INET_DIAG_REQ_??? */",
102                 INET_DIAG_REQ_BYTECODE + 1);
103         TEST_NLATTR_(fd, nlh0, hdrlen,
104                      init_inet_diag_req, print_inet_diag_req,
105                      INET_DIAG_REQ_BYTECODE + 1, nla_type_str,
106                      4, pattern, 4,
107                      print_quoted_hex(pattern, 4));
108
109         puts("+++ exited with 0 +++");
110         return 0;
111 }