2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2018 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
32 #include <arpa/inet.h>
33 #include "test_nlattr.h"
34 #ifdef HAVE_LINUX_IF_ADDR_H
35 # include <linux/if_addr.h>
37 #include <linux/rtnetlink.h>
41 #define SET_IFA_FAMILY(af) \
44 ifa_family_str = #af; \
49 const char *ifa_family_str;
52 init_ifaddrmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
54 SET_STRUCT(struct nlmsghdr, nlh,
56 .nlmsg_type = RTM_GETADDR,
57 .nlmsg_flags = NLM_F_DUMP
60 struct ifaddrmsg *const msg = NLMSG_DATA(nlh);
61 SET_STRUCT(struct ifaddrmsg, msg,
62 .ifa_family = ifa_family,
63 .ifa_flags = IFA_F_SECONDARY,
64 .ifa_scope = RT_SCOPE_UNIVERSE,
65 .ifa_index = ifindex_lo()
70 print_ifaddrmsg(const unsigned int msg_len)
72 printf("{len=%u, type=RTM_GETADDR, flags=NLM_F_DUMP"
73 ", seq=0, pid=0}, {ifa_family=%s"
75 ", ifa_flags=IFA_F_SECONDARY"
76 ", ifa_scope=RT_SCOPE_UNIVERSE"
77 ", ifa_index=" IFINDEX_LO_STR "}",
78 msg_len, ifa_family_str);
84 skip_if_unavailable("/proc/self/fd/");
86 static const char address4[] = "12.34.56.78";
87 static const char address6[] = "12:34:56:78:90:ab:cd:ef";
88 static const struct ifa_cacheinfo ci = {
89 .ifa_prefered = 0xabcdefac,
90 .ifa_valid = 0xbcdadbca,
97 const uint32_t ifa_flags = IFA_F_SECONDARY | IFA_F_PERMANENT;
99 const int fd = create_nl_socket(NETLINK_ROUTE);
100 const unsigned int hdrlen = sizeof(struct ifaddrmsg);
101 void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
102 NLA_HDRLEN + MAX(sizeof(ci), sizeof(a6)));
104 static char pattern[4096];
105 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
107 SET_IFA_FAMILY(AF_UNSPEC);
108 const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
109 char nla_type_str[256];
110 sprintf(nla_type_str, "%#x /* IFA_??? */", nla_type);
111 TEST_NLATTR_(fd, nlh0, hdrlen,
112 init_ifaddrmsg, print_ifaddrmsg,
113 nla_type, nla_type_str,
115 print_quoted_hex(pattern, 4));
117 TEST_NLATTR(fd, nlh0, hdrlen,
118 init_ifaddrmsg, print_ifaddrmsg,
119 IFA_ADDRESS, 4, pattern, 4,
120 print_quoted_hex(pattern, 4));
122 SET_IFA_FAMILY(AF_INET);
124 if (!inet_pton(AF_INET, address4, &a4))
125 perror_msg_and_skip("inet_pton");
127 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
128 init_ifaddrmsg, print_ifaddrmsg,
129 IFA_ADDRESS, pattern, a4,
130 printf("%s", address4));
132 SET_IFA_FAMILY(AF_INET6);
134 if (!inet_pton(AF_INET6, address6, &a6))
135 perror_msg_and_skip("inet_pton");
137 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
138 init_ifaddrmsg, print_ifaddrmsg,
139 IFA_ADDRESS, pattern, a6,
140 printf("%s", address6));
142 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
143 init_ifaddrmsg, print_ifaddrmsg,
144 IFA_CACHEINFO, pattern, ci,
145 PRINT_FIELD_U("{", ci, ifa_prefered);
146 PRINT_FIELD_U(", ", ci, ifa_valid);
147 PRINT_FIELD_U(", ", ci, cstamp);
148 PRINT_FIELD_U(", ", ci, tstamp);
151 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
152 init_ifaddrmsg, print_ifaddrmsg,
153 IFA_FLAGS, pattern, ifa_flags,
154 printf("IFA_F_SECONDARY|IFA_F_PERMANENT"));
156 puts("+++ exited with 0 +++");