1 /* Tests for ioctl wrappers. 2 More complicated ones than just trivial ones in scalar_ioctl. */ 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <strings.h> 7 #include <unistd.h> 8 #include <net/if.h> 9 #include <sys/socket.h> 10 #include <sys/sockio.h> 11 12 /* sockio */ 13 __attribute__((noinline)) 14 static int test_SIOCGIFCONF(void) 15 { 16 int fd = socket(AF_INET, SOCK_DGRAM, 0); 17 if (fd < 0) 18 perror("socket"); 19 20 int n_ifs; 21 if (ioctl(fd, SIOCGIFNUM, &n_ifs) < 0) 22 perror("ioctl(SIOCGIFNUM)"); 23 24 struct ifconf ifc; 25 ifc.ifc_len = (n_ifs + 1) * sizeof(struct ifreq); 26 ifc.ifc_buf = malloc((n_ifs + 1) * sizeof(struct ifreq)); 27 if (ifc.ifc_buf == NULL) 28 perror("malloc"); 29 30 if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) 31 perror("ioctl(SIOCGIFCONF)"); 32 33 /* Check definedness of ifc attributes ... */ 34 int x = 0; 35 if (ifc.ifc_len != 0) x = -1; else x = -2; 36 if (ifc.ifc_req != NULL) x = -3; else x = -4; 37 if (strcmp(ifc.ifc_req[0].ifr_name, "") != 0) x = -5; else x = -6; 38 /* ... and now one which is not defined. */ 39 if (strcmp(ifc.ifc_req[n_ifs].ifr_name, "") != 0) x = -7; else x = -8; 40 41 free(ifc.ifc_buf); 42 close(fd); 43 return x; 44 } 45 46 __attribute__((noinline)) 47 static int test_SIOCGLIFCONF(void) 48 { 49 int fd = socket(AF_INET, SOCK_DGRAM, 0); 50 if (fd < 0) 51 perror("socket"); 52 53 struct lifnum lifn; 54 lifn.lifn_family = AF_INET; 55 lifn.lifn_flags = 0; 56 if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) 57 perror("ioctl(SIOCGLIFNUM)"); 58 59 struct lifconf lifc; 60 lifc.lifc_family = AF_INET; 61 lifc.lifc_flags = 0; 62 lifc.lifc_len = (lifn.lifn_count + 1) * sizeof(struct lifreq); 63 lifc.lifc_buf = malloc((lifn.lifn_count + 1) * sizeof(struct lifreq)); 64 if (lifc.lifc_buf == NULL) 65 perror("malloc"); 66 67 if (ioctl(fd, SIOCGLIFCONF, &lifc) < 0) 68 perror("ioctl(SIOCGLIFCONF)"); 69 70 /* Check definedness of lifc attributes ... */ 71 int x = 0; 72 if (lifc.lifc_len != 0) x = -1; else x = -2; 73 if (lifc.lifc_req != NULL) x = -3; else x = -4; 74 if (strcmp(lifc.lifc_req[0].lifr_name, "") != 0) x = -5; else x = -6; 75 /* ... and now one which is not defined. */ 76 if (strcmp(lifc.lifc_req[lifn.lifn_count].lifr_name, "") != 0) 77 x = -7; else x = -8; 78 79 free(lifc.lifc_buf); 80 close(fd); 81 return x; 82 } 83 84 int main(void) 85 { 86 /* sockio */ 87 test_SIOCGIFCONF(); 88 test_SIOCGLIFCONF(); 89 90 return 0; 91 } 92