]> granicus.if.org Git - strace/blob - tests/nlattr_inet_diag_msg.c
Introduce generic STRINGIFY and STRINGIFY_VAL macros
[strace] / tests / nlattr_inet_diag_msg.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 <arpa/inet.h>
34 #include <net/if.h>
35 #include <netinet/tcp.h>
36 #include "test_nlattr.h"
37 #include <linux/inet_diag.h>
38 #include <linux/sock_diag.h>
39
40 static const char address[] = "10.11.12.13";
41
42 #ifdef HAVE_IF_INDEXTONAME
43 # define IFINDEX_LO     (if_nametoindex("lo"))
44 #else
45 # define IFINDEX_LO     1
46 #endif
47
48 static void
49 init_inet_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
50 {
51         SET_STRUCT(struct nlmsghdr, nlh,
52                 .nlmsg_len = msg_len,
53                 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
54                 .nlmsg_flags = NLM_F_DUMP
55         );
56
57         struct inet_diag_msg *const msg = NLMSG_DATA(nlh);
58         SET_STRUCT(struct inet_diag_msg, msg,
59                 .idiag_family = AF_INET,
60                 .idiag_state = TCP_LISTEN,
61                 .id.idiag_if = IFINDEX_LO
62         );
63
64         if (!inet_pton(AF_INET, address, msg->id.idiag_src) ||
65             !inet_pton(AF_INET, address, msg->id.idiag_dst))
66                 perror_msg_and_skip("inet_pton");
67 }
68
69 static void
70 print_inet_diag_msg(const unsigned int msg_len)
71 {
72         printf("{len=%u, type=SOCK_DIAG_BY_FAMILY"
73                ", flags=NLM_F_DUMP, seq=0, pid=0}, {idiag_family=AF_INET"
74                ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
75                ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
76                ", inet_pton(AF_INET, \"%s\", &idiag_src)"
77                ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
78                ", idiag_if=if_nametoindex(\"lo\"), idiag_cookie=[0, 0]}"
79                ", idiag_expires=0, idiag_rqueue=0, idiag_wqueue=0"
80                ", idiag_uid=0, idiag_inode=0}",
81                msg_len, address, address);
82 }
83
84 static void
85 print_uint(const unsigned int *p)
86 {
87         printf("%u", *p);
88 }
89
90 int
91 main(void)
92 {
93         skip_if_unavailable("/proc/self/fd/");
94
95         const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
96         const unsigned int hdrlen = sizeof(struct inet_diag_msg);
97         void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
98
99 #define DEFAULT_STRLEN 32
100         static char pattern[DEFAULT_STRLEN];
101         fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
102
103         static const struct inet_diag_meminfo minfo = {
104                 .idiag_rmem = 0xfadcacdb,
105                 .idiag_wmem = 0xbdabcada,
106                 .idiag_fmem = 0xbadbfafb,
107                 .idiag_tmem = 0xfdacdadf
108         };
109         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
110                            init_inet_diag_msg, print_inet_diag_msg,
111                            INET_DIAG_MEMINFO, pattern, minfo,
112                            PRINT_FIELD_U("{", minfo, idiag_rmem);
113                            PRINT_FIELD_U(", ", minfo, idiag_wmem);
114                            PRINT_FIELD_U(", ", minfo, idiag_fmem);
115                            PRINT_FIELD_U(", ", minfo, idiag_tmem);
116                            printf("}"));
117
118         static const struct tcpvegas_info vegas = {
119                 .tcpv_enabled = 0xfadcacdb,
120                 .tcpv_rttcnt = 0xbdabcada,
121                 .tcpv_rtt = 0xbadbfafb,
122                 .tcpv_minrtt = 0xfdacdadf
123         };
124         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
125                            init_inet_diag_msg, print_inet_diag_msg,
126                            INET_DIAG_VEGASINFO, pattern, vegas,
127                            PRINT_FIELD_U("{", vegas, tcpv_enabled);
128                            PRINT_FIELD_U(", ", vegas, tcpv_rttcnt);
129                            PRINT_FIELD_U(", ", vegas, tcpv_rtt);
130                            PRINT_FIELD_U(", ", vegas, tcpv_minrtt);
131                            printf("}"));
132
133
134         static const struct tcp_dctcp_info dctcp = {
135                 .dctcp_enabled = 0xfdac,
136                 .dctcp_ce_state = 0xfadc,
137                 .dctcp_alpha = 0xbdabcada,
138                 .dctcp_ab_ecn = 0xbadbfafb,
139                 .dctcp_ab_tot = 0xfdacdadf
140         };
141         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
142                            init_inet_diag_msg, print_inet_diag_msg,
143                            INET_DIAG_DCTCPINFO, pattern, dctcp,
144                            PRINT_FIELD_U("{", dctcp, dctcp_enabled);
145                            PRINT_FIELD_U(", ", dctcp, dctcp_ce_state);
146                            PRINT_FIELD_U(", ", dctcp, dctcp_alpha);
147                            PRINT_FIELD_U(", ", dctcp, dctcp_ab_ecn);
148                            PRINT_FIELD_U(", ", dctcp, dctcp_ab_tot);
149                            printf("}"));
150
151         static const struct tcp_bbr_info bbr = {
152                 .bbr_bw_lo = 0xfdacdadf,
153                 .bbr_bw_hi = 0xfadcacdb,
154                 .bbr_min_rtt = 0xbdabcada,
155                 .bbr_pacing_gain = 0xbadbfafb,
156                 .bbr_cwnd_gain = 0xfdacdadf
157         };
158         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
159                            init_inet_diag_msg, print_inet_diag_msg,
160                            INET_DIAG_BBRINFO, pattern, bbr,
161                            PRINT_FIELD_X("{", bbr, bbr_bw_lo);
162                            PRINT_FIELD_X(", ", bbr, bbr_bw_hi);
163                            PRINT_FIELD_U(", ", bbr, bbr_min_rtt);
164                            PRINT_FIELD_U(", ", bbr, bbr_pacing_gain);
165                            PRINT_FIELD_U(", ", bbr, bbr_cwnd_gain);
166                            printf("}"));
167
168         static const uint32_t mem[] = { 0xaffacbad, 0xffadbcab };
169         TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
170                           init_inet_diag_msg, print_inet_diag_msg,
171                           INET_DIAG_SKMEMINFO, pattern, mem, print_uint);
172
173         static const uint32_t mark = 0xabdfadca;
174         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
175                            init_inet_diag_msg, print_inet_diag_msg,
176                            INET_DIAG_MARK, pattern, mark,
177                            printf("%u", mark));
178
179         static const uint8_t shutdown = 0xcd;
180         TEST_NLATTR(fd, nlh0, hdrlen,
181                     init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_SHUTDOWN,
182                     sizeof(shutdown), &shutdown, sizeof(shutdown),
183                     printf("%u", shutdown));
184
185         char *const str = tail_alloc(DEFAULT_STRLEN);
186         fill_memory_ex(str, DEFAULT_STRLEN, '0', 10);
187         TEST_NLATTR(fd, nlh0, hdrlen,
188                     init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_CONG,
189                     DEFAULT_STRLEN, str, DEFAULT_STRLEN,
190                     printf("\"%.*s\"...", DEFAULT_STRLEN, str));
191         str[DEFAULT_STRLEN - 1] = '\0';
192         TEST_NLATTR(fd, nlh0, hdrlen,
193                     init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_CONG,
194                     DEFAULT_STRLEN, str, DEFAULT_STRLEN,
195                     printf("\"%s\"", str));
196
197         puts("+++ exited with 0 +++");
198         return 0;
199 }