getifaddrs.c 6.5 KB

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