1 /* 2 * Check decoding of SIOCGIFCONF command of ioctl syscall. 3 * 4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr (at) gmail.com> 5 * Copyright (c) 2016-2017 The strace developers. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 18 * 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. 29 */ 30 31 #include "tests.h" 32 33 #include <stdbool.h> 34 #include <stdio.h> 35 #include <string.h> 36 37 #include <arpa/inet.h> 38 #include <net/if.h> 39 #include <sys/ioctl.h> 40 #include <sys/socket.h> 41 42 #define MAX_STRLEN 1 43 44 static void 45 print_ifc_len(int val) 46 { 47 if (val % (int) sizeof(struct ifreq)) 48 printf("%d", val); 49 else 50 printf("%d * sizeof(struct ifreq)", 51 val / (int) sizeof(struct ifreq)); 52 } 53 54 static void 55 print_ifconf(struct ifconf *ifc, int in_len, char *in_buf, long rc) 56 { 57 if (in_buf) { 58 printf("{ifc_len="); 59 print_ifc_len(in_len); 60 61 if (in_len != ifc->ifc_len) { 62 printf(" => "); 63 print_ifc_len(ifc->ifc_len); 64 } 65 } else { 66 printf("{ifc_len="); 67 print_ifc_len(ifc->ifc_len); 68 } 69 70 printf(", ifc_buf="); 71 72 if ((rc < 0) || !in_buf) { 73 if (in_buf) 74 printf("%p", in_buf); 75 else 76 printf("NULL"); 77 } else { 78 int i; 79 80 printf("["); 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); 87 88 if (i) 89 printf(", "); 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)); 94 } 95 96 if ((size_t) (ifc->ifc_len - i) >= sizeof(struct ifreq)) 97 printf(", ..."); 98 99 printf("]"); 100 } 101 102 printf("}"); 103 } 104 105 static void 106 gifconf_ioctl(int fd, struct ifconf *ifc, bool ifc_valid) 107 { 108 const char *errstr; 109 int in_len; 110 char *in_buf; 111 long rc; 112 113 if (ifc_valid) { 114 in_len = ifc->ifc_len; 115 in_buf = ifc->ifc_buf; 116 } 117 118 rc = ioctl(fd, SIOCGIFCONF, ifc); 119 errstr = sprintrc(rc); 120 121 printf("ioctl(%d, SIOCGIFCONF, ", fd); 122 if (ifc_valid) { 123 print_ifconf(ifc, in_len, in_buf, rc); 124 } else { 125 if (ifc) 126 printf("%p", ifc); 127 else 128 printf("NULL"); 129 } 130 131 printf(") = %s\n", errstr); 132 } 133 134 int 135 main(int argc, char *argv[]) 136 { 137 struct ifreq *ifr = tail_alloc(2 * sizeof(*ifr)); 138 TAIL_ALLOC_OBJECT_CONST_PTR(struct ifconf, ifc); 139 140 struct sockaddr_in addr; 141 int fd; 142 143 memset(&addr, 0, sizeof(addr)); 144 addr.sin_family = AF_INET; 145 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 146 147 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 148 perror_msg_and_skip("socket AF_INET"); 149 150 gifconf_ioctl(fd, NULL, false); 151 gifconf_ioctl(fd, ifc + 1, false); 152 153 ifc->ifc_len = 3141592653U; 154 ifc->ifc_buf = NULL; 155 gifconf_ioctl(fd, ifc, true); 156 157 ifc->ifc_len = 0; 158 ifc->ifc_buf = (char *) (ifr + 2); 159 gifconf_ioctl(fd, ifc, true); 160 161 ifc->ifc_len = 1; 162 ifc->ifc_buf = (char *) (ifr + 1); 163 gifconf_ioctl(fd, ifc, true); 164 165 ifc->ifc_len = 1 * sizeof(*ifr); 166 ifc->ifc_buf = (char *) (ifr + 1); 167 gifconf_ioctl(fd, ifc, true); 168 169 ifc->ifc_len = 2 * sizeof(*ifr); 170 ifc->ifc_buf = (char *) (ifr + 1); 171 gifconf_ioctl(fd, ifc, true); 172 173 ifc->ifc_len = 2 * sizeof(*ifr) + 2; 174 ifc->ifc_buf = (char *) ifr; 175 gifconf_ioctl(fd, ifc, true); 176 177 ifc->ifc_len = 3 * sizeof(*ifr) + 4; 178 ifc->ifc_buf = (char *) ifr; 179 gifconf_ioctl(fd, ifc, true); 180 181 puts("+++ exited with 0 +++"); 182 return 0; 183 } 184