]> granicus.if.org Git - strace/blob - tests/nlattr_ifaddrmsg.c
tests: change the license to GPL-2.0-or-later
[strace] / tests / nlattr_ifaddrmsg.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 <arpa/inet.h>
13 #include "test_nlattr.h"
14 #ifdef HAVE_LINUX_IF_ADDR_H
15 # include <linux/if_addr.h>
16 #endif
17 #include <linux/rtnetlink.h>
18
19 #define IFA_FLAGS 8
20
21 #define SET_IFA_FAMILY(af)              \
22         do {                            \
23                 ifa_family = af;        \
24                 ifa_family_str = #af;   \
25         }                               \
26         while (0)
27
28 uint8_t ifa_family;
29 const char *ifa_family_str;
30
31 static void
32 init_ifaddrmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
33 {
34         SET_STRUCT(struct nlmsghdr, nlh,
35                 .nlmsg_len = msg_len,
36                 .nlmsg_type = RTM_GETADDR,
37                 .nlmsg_flags = NLM_F_DUMP
38         );
39
40         struct ifaddrmsg *const msg = NLMSG_DATA(nlh);
41         SET_STRUCT(struct ifaddrmsg, msg,
42                 .ifa_family = ifa_family,
43                 .ifa_flags = IFA_F_SECONDARY,
44                 .ifa_scope = RT_SCOPE_UNIVERSE,
45                 .ifa_index = ifindex_lo()
46         );
47 }
48
49 static void
50 print_ifaddrmsg(const unsigned int msg_len)
51 {
52         printf("{len=%u, type=RTM_GETADDR, flags=NLM_F_DUMP"
53                ", seq=0, pid=0}, {ifa_family=%s"
54                ", ifa_prefixlen=0"
55                ", ifa_flags=IFA_F_SECONDARY"
56                ", ifa_scope=RT_SCOPE_UNIVERSE"
57                ", ifa_index=" IFINDEX_LO_STR "}",
58                msg_len, ifa_family_str);
59 }
60
61 int
62 main(void)
63 {
64         skip_if_unavailable("/proc/self/fd/");
65
66         static const char address4[] = "12.34.56.78";
67         static const char address6[] = "12:34:56:78:90:ab:cd:ef";
68         static const struct ifa_cacheinfo ci = {
69                 .ifa_prefered = 0xabcdefac,
70                 .ifa_valid = 0xbcdadbca,
71                 .cstamp = 0xcdabedba,
72                 .tstamp = 0xdebabdac
73         };
74
75         struct in_addr a4;
76         struct in6_addr a6;
77         const uint32_t ifa_flags = IFA_F_SECONDARY | IFA_F_PERMANENT;
78
79         const int fd = create_nl_socket(NETLINK_ROUTE);
80         const unsigned int hdrlen = sizeof(struct ifaddrmsg);
81         void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
82                                    NLA_HDRLEN + MAX(sizeof(ci), sizeof(a6)));
83
84         static char pattern[4096];
85         fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
86
87         SET_IFA_FAMILY(AF_UNSPEC);
88         const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
89         char nla_type_str[256];
90         sprintf(nla_type_str, "%#x /* IFA_??? */", nla_type);
91         TEST_NLATTR_(fd, nlh0, hdrlen,
92                      init_ifaddrmsg, print_ifaddrmsg,
93                      nla_type, nla_type_str,
94                      4, pattern, 4,
95                      print_quoted_hex(pattern, 4));
96
97         TEST_NLATTR(fd, nlh0, hdrlen,
98                     init_ifaddrmsg, print_ifaddrmsg,
99                     IFA_ADDRESS, 4, pattern, 4,
100                     print_quoted_hex(pattern, 4));
101
102         SET_IFA_FAMILY(AF_INET);
103
104         if (!inet_pton(AF_INET, address4, &a4))
105                 perror_msg_and_skip("inet_pton");
106
107         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
108                            init_ifaddrmsg, print_ifaddrmsg,
109                            IFA_ADDRESS, pattern, a4,
110                            printf("inet_addr(\"%s\")", address4));
111
112         SET_IFA_FAMILY(AF_INET6);
113
114         if (!inet_pton(AF_INET6, address6, &a6))
115                 perror_msg_and_skip("inet_pton");
116
117         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
118                            init_ifaddrmsg, print_ifaddrmsg,
119                            IFA_ADDRESS, pattern, a6,
120                            printf("inet_pton(AF_INET6, \"%s\")", address6));
121
122         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
123                            init_ifaddrmsg, print_ifaddrmsg,
124                            IFA_CACHEINFO, pattern, ci,
125                            PRINT_FIELD_U("{", ci, ifa_prefered);
126                            PRINT_FIELD_U(", ", ci, ifa_valid);
127                            PRINT_FIELD_U(", ", ci, cstamp);
128                            PRINT_FIELD_U(", ", ci, tstamp);
129                            printf("}"));
130
131         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
132                            init_ifaddrmsg, print_ifaddrmsg,
133                            IFA_FLAGS, pattern, ifa_flags,
134                            printf("IFA_F_SECONDARY|IFA_F_PERMANENT"));
135
136         puts("+++ exited with 0 +++");
137         return 0;
138 }