2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2018 The strace developers.
6 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include <arpa/inet.h>
13 #include "test_nlattr.h"
14 #ifdef HAVE_LINUX_IF_ADDR_H
15 # include <linux/if_addr.h>
17 #include <linux/rtnetlink.h>
21 #define SET_IFA_FAMILY(af) \
24 ifa_family_str = #af; \
29 const char *ifa_family_str;
32 init_ifaddrmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
34 SET_STRUCT(struct nlmsghdr, nlh,
36 .nlmsg_type = RTM_GETADDR,
37 .nlmsg_flags = NLM_F_DUMP
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()
50 print_ifaddrmsg(const unsigned int msg_len)
52 printf("{len=%u, type=RTM_GETADDR, flags=NLM_F_DUMP"
53 ", seq=0, pid=0}, {ifa_family=%s"
55 ", ifa_flags=IFA_F_SECONDARY"
56 ", ifa_scope=RT_SCOPE_UNIVERSE"
57 ", ifa_index=" IFINDEX_LO_STR "}",
58 msg_len, ifa_family_str);
64 skip_if_unavailable("/proc/self/fd/");
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,
77 const uint32_t ifa_flags = IFA_F_SECONDARY | IFA_F_PERMANENT;
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)));
84 static char pattern[4096];
85 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
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,
95 print_quoted_hex(pattern, 4));
97 TEST_NLATTR(fd, nlh0, hdrlen,
98 init_ifaddrmsg, print_ifaddrmsg,
99 IFA_ADDRESS, 4, pattern, 4,
100 print_quoted_hex(pattern, 4));
102 SET_IFA_FAMILY(AF_INET);
104 if (!inet_pton(AF_INET, address4, &a4))
105 perror_msg_and_skip("inet_pton");
107 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
108 init_ifaddrmsg, print_ifaddrmsg,
109 IFA_ADDRESS, pattern, a4,
110 printf("inet_addr(\"%s\")", address4));
112 SET_IFA_FAMILY(AF_INET6);
114 if (!inet_pton(AF_INET6, address6, &a6))
115 perror_msg_and_skip("inet_pton");
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));
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);
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"));
136 puts("+++ exited with 0 +++");