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.
37 #include <arpa/inet.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
45 print_ifc_len(int val)
47 if (val % (int) sizeof(struct ifreq))
50 printf("%d * sizeof(struct ifreq)",
51 val / (int) sizeof(struct ifreq));
55 print_ifconf(struct ifconf *ifc, int in_len, char *in_buf, long rc)
59 print_ifc_len(in_len);
61 if (in_len != ifc->ifc_len) {
63 print_ifc_len(ifc->ifc_len);
67 print_ifc_len(ifc->ifc_len);
72 if ((rc < 0) || !in_buf) {
81 for (i = 0; i < (ifc->ifc_len) &&
82 i < (int) (MAX_STRLEN * sizeof(struct ifreq));
83 i += sizeof(struct ifreq)) {
84 struct ifreq *ifr = (struct ifreq *) (ifc->ifc_buf + i);
85 struct sockaddr_in *const sa_in =
86 (struct sockaddr_in *) &(ifr->ifr_addr);
90 printf("{ifr_name=\"%s\", ifr_addr={sa_family=AF_INET, "
91 "sin_port=htons(%u), sin_addr=inet_addr(\"%s\")}"
92 "}", ifr->ifr_name, ntohs(sa_in->sin_port),
93 inet_ntoa(sa_in->sin_addr));
96 if ((size_t) (ifc->ifc_len - i) >= sizeof(struct ifreq))
106 gifconf_ioctl(int fd, struct ifconf *ifc, bool ifc_valid)
114 in_len = ifc->ifc_len;
115 in_buf = ifc->ifc_buf;
118 rc = ioctl(fd, SIOCGIFCONF, ifc);
119 errstr = sprintrc(rc);
121 printf("ioctl(%d, SIOCGIFCONF, ", fd);
123 print_ifconf(ifc, in_len, in_buf, rc);
131 printf(") = %s\n", errstr);
135 main(int argc, char *argv[])
137 struct ifreq *ifr = tail_alloc(2 * sizeof(*ifr));
138 TAIL_ALLOC_OBJECT_CONST_PTR(struct ifconf, ifc);
140 struct sockaddr_in addr;
143 memset(&addr, 0, sizeof(addr));
144 addr.sin_family = AF_INET;
145 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
147 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
148 perror_msg_and_skip("socket AF_INET");
150 gifconf_ioctl(fd, NULL, false);
151 gifconf_ioctl(fd, ifc + 1, false);
153 ifc->ifc_len = 3141592653U;
155 gifconf_ioctl(fd, ifc, true);
158 ifc->ifc_buf = (char *) (ifr + 2);
159 gifconf_ioctl(fd, ifc, true);
162 ifc->ifc_buf = (char *) (ifr + 1);
163 gifconf_ioctl(fd, ifc, true);
165 ifc->ifc_len = 1 * sizeof(*ifr);
166 ifc->ifc_buf = (char *) (ifr + 1);
167 gifconf_ioctl(fd, ifc, true);
169 ifc->ifc_len = 2 * sizeof(*ifr);
170 ifc->ifc_buf = (char *) (ifr + 1);
171 gifconf_ioctl(fd, ifc, true);
173 ifc->ifc_len = 2 * sizeof(*ifr) + 2;
174 ifc->ifc_buf = (char *) ifr;
175 gifconf_ioctl(fd, ifc, true);
177 ifc->ifc_len = 3 * sizeof(*ifr) + 4;
178 ifc->ifc_buf = (char *) ifr;
179 gifconf_ioctl(fd, ifc, true);
181 puts("+++ exited with 0 +++");