getifaddrs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2000 - 2001 Kungliga Tekniska H�gskolan
  3. * (Royal Institute of Technology, Stockholm, Sweden).
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  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. *
  17. * 3. Neither the name of the Institute nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * originally downloaded from
  34. *
  35. * http://ftp.uninett.no/pub/OpenBSD/src/kerberosV/src/lib/roken/getifaddrs.c
  36. */
  37. #include <errno.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <net/if.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <sys/ioctl.h>
  44. #include <unistd.h>
  45. #include "core/private.h"
  46. #ifdef LWS_HAVE_SYS_SOCKIO_H
  47. #include <sys/sockio.h>
  48. #endif
  49. #ifdef LWS_HAVE_NETINET_IN6_VAR_H
  50. #include <netinet/in6_var.h>
  51. #endif
  52. #ifndef max
  53. #define max(a, b) ((a) > (b) ? (a) : (b))
  54. #endif
  55. #include "getifaddrs.h"
  56. static int
  57. getifaddrs2(struct ifaddrs **ifap, int af, int siocgifconf, int siocgifflags,
  58. size_t ifreq_sz)
  59. {
  60. int ret;
  61. int fd;
  62. size_t buf_size;
  63. char *buf;
  64. struct ifconf ifconf;
  65. char *p;
  66. size_t sz;
  67. struct sockaddr sa_zero;
  68. struct ifreq *ifr;
  69. struct ifaddrs *start, **end = &start;
  70. buf = NULL;
  71. memset(&sa_zero, 0, sizeof(sa_zero));
  72. fd = socket(af, SOCK_DGRAM, 0);
  73. if (fd < 0)
  74. return -1;
  75. buf_size = 8192;
  76. for (;;) {
  77. buf = lws_zalloc(buf_size, "getifaddrs2");
  78. if (buf == NULL) {
  79. ret = ENOMEM;
  80. goto error_out;
  81. }
  82. ifconf.ifc_len = buf_size;
  83. ifconf.ifc_buf = buf;
  84. /*
  85. * Solaris returns EINVAL when the buffer is too small.
  86. */
  87. if (ioctl(fd, siocgifconf, &ifconf) < 0 && errno != EINVAL) {
  88. ret = errno;
  89. goto error_out;
  90. }
  91. /*
  92. * Can the difference between a full and a overfull buf
  93. * be determined?
  94. */
  95. if (ifconf.ifc_len < (int)buf_size)
  96. break;
  97. lws_free(buf);
  98. buf_size *= 2;
  99. }
  100. for (p = ifconf.ifc_buf; p < ifconf.ifc_buf + ifconf.ifc_len; p += sz) {
  101. struct ifreq ifreq;
  102. struct sockaddr *sa;
  103. size_t salen;
  104. ifr = (struct ifreq *)p;
  105. sa = &ifr->ifr_addr;
  106. sz = ifreq_sz;
  107. salen = sizeof(struct sockaddr);
  108. #ifdef LWS_HAVE_STRUCT_SOCKADDR_SA_LEN
  109. salen = sa->sa_len;
  110. sz = max(sz, sizeof(ifr->ifr_name) + sa->sa_len);
  111. #endif
  112. #ifdef SA_LEN
  113. salen = SA_LEN(sa);
  114. sz = max(sz, sizeof(ifr->ifr_name) + SA_LEN(sa));
  115. #endif
  116. memset(&ifreq, 0, sizeof(ifreq));
  117. memcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name));
  118. if (ioctl(fd, siocgifflags, &ifreq) < 0) {
  119. ret = errno;
  120. goto error_out;
  121. }
  122. *end = lws_malloc(sizeof(**end), "getifaddrs");
  123. (*end)->ifa_next = NULL;
  124. (*end)->ifa_name = strdup(ifr->ifr_name);
  125. (*end)->ifa_flags = ifreq.ifr_flags;
  126. (*end)->ifa_addr = lws_malloc(salen, "getifaddrs");
  127. memcpy((*end)->ifa_addr, sa, salen);
  128. (*end)->ifa_netmask = NULL;
  129. #if 0
  130. /* fix these when we actually need them */
  131. if (ifreq.ifr_flags & IFF_BROADCAST) {
  132. (*end)->ifa_broadaddr =
  133. lws_malloc(sizeof(ifr->ifr_broadaddr), "getifaddrs");
  134. memcpy((*end)->ifa_broadaddr, &ifr->ifr_broadaddr,
  135. sizeof(ifr->ifr_broadaddr));
  136. } else if (ifreq.ifr_flags & IFF_POINTOPOINT) {
  137. (*end)->ifa_dstaddr =
  138. lws_malloc(sizeof(ifr->ifr_dstaddr), "getifaddrs");
  139. memcpy((*end)->ifa_dstaddr, &ifr->ifr_dstaddr,
  140. sizeof(ifr->ifr_dstaddr));
  141. } else
  142. (*end)->ifa_dstaddr = NULL;
  143. #else
  144. (*end)->ifa_dstaddr = NULL;
  145. #endif
  146. (*end)->ifa_data = NULL;
  147. end = &(*end)->ifa_next;
  148. }
  149. *ifap = start;
  150. close(fd);
  151. lws_free(buf);
  152. return 0;
  153. error_out:
  154. close(fd);
  155. lws_free(buf);
  156. errno = ret;
  157. return -1;
  158. }
  159. int
  160. getifaddrs(struct ifaddrs **ifap)
  161. {
  162. int ret = -1;
  163. errno = ENXIO;
  164. #if defined(AF_INET6) && defined(SIOCGIF6CONF) && defined(SIOCGIF6FLAGS)
  165. if (ret)
  166. ret = getifaddrs2(ifap, AF_INET6, SIOCGIF6CONF, SIOCGIF6FLAGS,
  167. sizeof(struct in6_ifreq));
  168. #endif
  169. #if defined(LWS_HAVE_IPV6) && defined(SIOCGIFCONF)
  170. if (ret)
  171. ret = getifaddrs2(ifap, AF_INET6, SIOCGIFCONF, SIOCGIFFLAGS,
  172. sizeof(struct ifreq));
  173. #endif
  174. #if defined(AF_INET) && defined(SIOCGIFCONF) && defined(SIOCGIFFLAGS)
  175. if (ret)
  176. ret = getifaddrs2(ifap, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
  177. sizeof(struct ifreq));
  178. #endif
  179. return ret;
  180. }
  181. void
  182. freeifaddrs(struct ifaddrs *ifp)
  183. {
  184. struct ifaddrs *p, *q;
  185. for (p = ifp; p; ) {
  186. lws_free(p->ifa_name);
  187. lws_free(p->ifa_addr);
  188. lws_free(p->ifa_dstaddr);
  189. lws_free(p->ifa_netmask);
  190. lws_free(p->ifa_data);
  191. q = p;
  192. p = p->ifa_next;
  193. lws_free(q);
  194. }
  195. }
  196. #ifdef TEST
  197. void
  198. print_addr(const char *s, struct sockaddr *sa)
  199. {
  200. int i;
  201. printf(" %s=%d/", s, sa->sa_family);
  202. #ifdef LWS_HAVE_STRUCT_SOCKADDR_SA_LEN
  203. for (i = 0;
  204. i < sa->sa_len - ((lws_intptr_t)sa->sa_data - (lws_intptr_t)&sa->sa_family); i++)
  205. printf("%02x", ((unsigned char *)sa->sa_data)[i]);
  206. #else
  207. for (i = 0; i < sizeof(sa->sa_data); i++)
  208. printf("%02x", ((unsigned char *)sa->sa_data)[i]);
  209. #endif
  210. printf("\n");
  211. }
  212. void
  213. print_ifaddrs(struct ifaddrs *x)
  214. {
  215. struct ifaddrs *p;
  216. for (p = x; p; p = p->ifa_next) {
  217. printf("%s\n", p->ifa_name);
  218. printf(" flags=%x\n", p->ifa_flags);
  219. if (p->ifa_addr)
  220. print_addr("addr", p->ifa_addr);
  221. if (p->ifa_dstaddr)
  222. print_addr("dstaddr", p->ifa_dstaddr);
  223. if (p->ifa_netmask)
  224. print_addr("netmask", p->ifa_netmask);
  225. printf(" %p\n", p->ifa_data);
  226. }
  227. }
  228. int
  229. main()
  230. {
  231. struct ifaddrs *a = NULL, *b;
  232. getifaddrs2(&a, AF_INET, SIOCGIFCONF, SIOCGIFFLAGS,
  233. sizeof(struct ifreq));
  234. print_ifaddrs(a);
  235. printf("---\n");
  236. getifaddrs(&b);
  237. print_ifaddrs(b);
  238. return 0;
  239. }
  240. #endif