]> granicus.if.org Git - strace/blob - tests/nlattr_ndmsg.c
tests: change the license to GPL-2.0-or-later
[strace] / tests / nlattr_ndmsg.c
1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017-2018 The strace developers.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 #include "tests.h"
10
11 #include <stdio.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include "test_nlattr.h"
15 #ifdef HAVE_LINUX_NEIGHBOUR_H
16 # include <linux/neighbour.h>
17 #endif
18 #include <linux/rtnetlink.h>
19
20 #define NDA_PORT 6
21
22 static void
23 init_ndmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
24 {
25         SET_STRUCT(struct nlmsghdr, nlh,
26                 .nlmsg_len = msg_len,
27                 .nlmsg_type = RTM_GETNEIGH,
28                 .nlmsg_flags = NLM_F_DUMP
29         );
30
31         struct ndmsg *const msg = NLMSG_DATA(nlh);
32         SET_STRUCT(struct ndmsg, msg,
33                 .ndm_family = AF_UNIX,
34                 .ndm_ifindex = ifindex_lo(),
35                 .ndm_state = NUD_PERMANENT,
36                 .ndm_flags = NTF_PROXY,
37                 .ndm_type = RTN_UNSPEC
38         );
39 }
40
41 static void
42 print_ndmsg(const unsigned int msg_len)
43 {
44         printf("{len=%u, type=RTM_GETNEIGH, flags=NLM_F_DUMP"
45                ", seq=0, pid=0}, {ndm_family=AF_UNIX"
46                ", ndm_ifindex=" IFINDEX_LO_STR
47                ", ndm_state=NUD_PERMANENT"
48                ", ndm_flags=NTF_PROXY"
49                ", ndm_type=RTN_UNSPEC}",
50                msg_len);
51 }
52
53 int
54 main(void)
55 {
56         skip_if_unavailable("/proc/self/fd/");
57
58         const int fd = create_nl_socket(NETLINK_ROUTE);
59         const unsigned int hdrlen = sizeof(struct ndmsg);
60         void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
61                                    NLA_HDRLEN + sizeof(struct nda_cacheinfo));
62
63         static char pattern[4096];
64         fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
65
66         const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
67         char nla_type_str[256];
68         sprintf(nla_type_str, "%#x /* NDA_??? */", nla_type);
69         TEST_NLATTR_(fd, nlh0, hdrlen,
70                      init_ndmsg, print_ndmsg,
71                      nla_type, nla_type_str,
72                      4, pattern, 4,
73                      print_quoted_hex(pattern, 4));
74
75         TEST_NLATTR(fd, nlh0, hdrlen,
76                     init_ndmsg, print_ndmsg,
77                     NDA_DST, 4, pattern, 4,
78                     print_quoted_hex(pattern, 4));
79
80         static const struct nda_cacheinfo ci = {
81                 .ndm_confirmed = 0xabcdedad,
82                 .ndm_used = 0xbcdaedad,
83                 .ndm_updated = 0xcdbadeda,
84                 .ndm_refcnt = 0xdeadbeda
85         };
86
87         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
88                            init_ndmsg, print_ndmsg,
89                            NDA_CACHEINFO, pattern, ci,
90                            PRINT_FIELD_U("{", ci, ndm_confirmed);
91                            PRINT_FIELD_U(", ", ci, ndm_used);
92                            PRINT_FIELD_U(", ", ci, ndm_updated);
93                            PRINT_FIELD_U(", ", ci, ndm_refcnt);
94                            printf("}"));
95
96         const uint16_t port = 0xabcd;
97         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
98                            init_ndmsg, print_ndmsg,
99                            NDA_PORT, pattern, port,
100                            printf("htons(%u)", ntohs(port)));
101
102         puts("+++ exited with 0 +++");
103         return 0;
104 }