2 * Check decoding of SIOCGIFCONF command of ioctl syscall.
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5 * Copyright (c) 2016-2017 The strace developers.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <arpa/inet.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
44 print_ifc_len(int val)
46 if (val % (int) sizeof(struct ifreq))
49 printf("%d * sizeof(struct ifreq)",
50 val / (int) sizeof(struct ifreq));
54 print_ifconf(struct ifconf *ifc, int in_len, char *in_buf, long rc)
58 print_ifc_len(in_len);
60 if (in_len != ifc->ifc_len) {
62 print_ifc_len(ifc->ifc_len);
66 print_ifc_len(ifc->ifc_len);
71 if ((rc < 0) || !in_buf) {
80 for (i = 0; i < (ifc->ifc_len) &&
81 i < (int) (MAX_STRLEN * sizeof(struct ifreq));
82 i += sizeof(struct ifreq)) {
83 struct ifreq *ifr = (struct ifreq *) (ifc->ifc_buf + i);
84 struct sockaddr_in *const sa_in =
85 (struct sockaddr_in *) &(ifr->ifr_addr);
89 printf("{ifr_name=\"%s\", ifr_addr={sa_family=AF_INET, "
90 "sin_port=htons(%u), sin_addr=inet_addr(\"%s\")}"
91 "}", ifr->ifr_name, ntohs(sa_in->sin_port),
92 inet_ntoa(sa_in->sin_addr));
95 if ((size_t) (ifc->ifc_len - i) >= sizeof(struct ifreq))
105 gifconf_ioctl(int fd, struct ifconf *ifc, bool ifc_valid)
113 in_len = ifc->ifc_len;
114 in_buf = ifc->ifc_buf;
117 rc = ioctl(fd, SIOCGIFCONF, ifc);
118 errstr = sprintrc(rc);
120 printf("ioctl(%d, SIOCGIFCONF, ", fd);
122 print_ifconf(ifc, in_len, in_buf, rc);
130 printf(") = %s\n", errstr);
134 main(int argc, char *argv[])
136 struct ifreq *ifr = tail_alloc(2 * sizeof(*ifr));
137 TAIL_ALLOC_OBJECT_CONST_PTR(struct ifconf, ifc);
139 struct sockaddr_in addr;
142 memset(&addr, 0, sizeof(addr));
143 addr.sin_family = AF_INET;
144 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
146 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
147 perror_msg_and_skip("socket AF_INET");
149 gifconf_ioctl(fd, NULL, false);
150 gifconf_ioctl(fd, ifc + 1, false);
152 ifc->ifc_len = 3141592653U;
154 gifconf_ioctl(fd, ifc, true);
157 ifc->ifc_buf = (char *) (ifr + 2);
158 gifconf_ioctl(fd, ifc, true);
161 ifc->ifc_buf = (char *) (ifr + 1);
162 gifconf_ioctl(fd, ifc, true);
164 ifc->ifc_len = 1 * sizeof(*ifr);
165 ifc->ifc_buf = (char *) (ifr + 1);
166 gifconf_ioctl(fd, ifc, true);
168 ifc->ifc_len = 2 * sizeof(*ifr);
169 ifc->ifc_buf = (char *) (ifr + 1);
170 gifconf_ioctl(fd, ifc, true);
172 ifc->ifc_len = 2 * sizeof(*ifr) + 2;
173 ifc->ifc_buf = (char *) ifr;
174 gifconf_ioctl(fd, ifc, true);
176 ifc->ifc_len = 3 * sizeof(*ifr) + 4;
177 ifc->ifc_buf = (char *) ifr;
178 gifconf_ioctl(fd, ifc, true);
180 puts("+++ exited with 0 +++");