s2_localinfo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2009 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@CFILE s2_localinfo.c
  25. *
  26. * @brief s2_localinfo() stub returning well-known addresses for testing.
  27. *
  28. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  29. */
  30. #include "config.h"
  31. #include <sofia-sip/hostdomain.h>
  32. #include <sofia-sip/su_string.h>
  33. #include <sofia-sip/su_alloc.h>
  34. #include <stdlib.h>
  35. #include <assert.h>
  36. #define SU_LOCALINFO_TEST 1
  37. #define su_getlocalinfo s2_getlocalinfo
  38. #define su_freelocalinfo s2_freelocalinfo
  39. #define su_gli_strerror s2_gli_strerror
  40. #define su_copylocalinfo s2_copylocalinfo
  41. #define su_sockaddr_scope s2_sockaddr_scope
  42. #define su_getlocalip s2_getlocalip
  43. #include "su_localinfo.c"
  44. static char const *default_ifaces[] = {
  45. "lo0\0" "127.0.0.1\0" "::1\0",
  46. NULL
  47. };
  48. static char const **ifaces = default_ifaces;
  49. void s2_localinfo_ifaces(char const **replace_ifaces)
  50. {
  51. ifaces = replace_ifaces;
  52. }
  53. int
  54. s2_getlocalinfo(su_localinfo_t const *hints,
  55. su_localinfo_t **return_localinfo)
  56. {
  57. int error = 0, ip4 = 0, ip6 = 0, i;
  58. su_localinfo_t *result = NULL, **rr = &result;
  59. su_localinfo_t hh[1] = {{ 0 }};
  60. assert(return_localinfo);
  61. *return_localinfo = NULL;
  62. if (hints) {
  63. /* Copy hints so that it can be modified */
  64. *hh = *hints;
  65. if (hh->li_canonname)
  66. hh->li_flags |= LI_CANONNAME;
  67. }
  68. hints = hh;
  69. switch (hh->li_family) {
  70. #if SU_HAVE_IN6
  71. case AF_INET6:
  72. if (hh->li_flags & LI_V4MAPPED)
  73. ip6 = ip4 = 1, hh->li_family = 0;
  74. else
  75. ip6 = 1;
  76. break;
  77. #endif
  78. case AF_INET:
  79. ip4 = 1;
  80. break;
  81. case 0:
  82. ip6 = ip4 = 1;
  83. break;
  84. default:
  85. return -1;
  86. }
  87. for (i = 0; ifaces[i]; i++) {
  88. char const *iface = ifaces[i], *address = iface;
  89. su_sockaddr_t su[1];
  90. for (;(address += strlen(address) + 1)[0];) {
  91. su_localinfo_t *li = NULL;
  92. int scope = 0;
  93. memset(su, 0, sizeof su);
  94. if (0)
  95. ;
  96. #if SU_HAVE_IN6
  97. else if (ip4 &&
  98. (hints->li_flags & LI_V4MAPPED) != 0 &&
  99. host_is_ip4_address(address)) {
  100. int32_t mapped;
  101. su->su_len = (sizeof su->su_sin6);
  102. su_inet_pton(su->su_family = AF_INET6,
  103. address,
  104. &mapped);
  105. ((int32_t*)&su->su_sin6.sin6_addr)[2] = htonl(0xffff);
  106. ((int32_t*)&su->su_sin6.sin6_addr)[3] = mapped;
  107. scope = li_scope4(mapped);
  108. }
  109. else if (ip6 && host_is_ip6_address(address)) {
  110. su->su_len = (sizeof su->su_sin6);
  111. su_inet_pton(su->su_family = AF_INET6,
  112. address,
  113. &su->su_sin6.sin6_addr);
  114. scope = li_scope6(&su->su_sin6.sin6_addr);
  115. }
  116. #endif
  117. else if (ip4 && host_is_ip4_address(address)) {
  118. su->su_len = (sizeof su->su_sin);
  119. su_inet_pton(su->su_family = AF_INET,
  120. address,
  121. &su->su_sin.sin_addr);
  122. scope = li_scope4(su->su_sin.sin_addr.s_addr);
  123. }
  124. else
  125. continue;
  126. if (scope == 0)
  127. continue;
  128. if (hints->li_scope && (hints->li_scope & scope) == 0)
  129. continue;
  130. if (hints->li_index && hints->li_index != i + 1)
  131. continue;
  132. if (hints->li_ifname && strcmp(hints->li_ifname, iface) != 0)
  133. continue;
  134. if (hints->li_family && hints->li_family != su->su_family)
  135. continue;
  136. if (hints->li_canonname && !su_casematch(address, hints->li_canonname))
  137. continue;
  138. li = calloc(1, (sizeof *li) + (sizeof *su) + strlen(iface) + 1);
  139. li->li_family = su->su_family;
  140. li->li_scope = scope;
  141. li->li_index = i + 1;
  142. li->li_addrlen = su_sockaddr_size(su);
  143. li->li_addr = memcpy((li + 1), su, (sizeof *su));
  144. if (hints->li_flags & LI_IFNAME)
  145. li->li_ifname = strcpy((char *)li->li_addr + li->li_addrlen, iface);
  146. if ((hints->li_flags & LI_CANONNAME) || hints->li_canonname) {
  147. li->li_flags |= LI_NUMERIC;
  148. li->li_canonname = su_strdup(NULL, address);
  149. }
  150. #define LI_MAPPED(li) \
  151. ((li)->li_family == AF_INET6 && \
  152. (IN6_IS_ADDR_V4MAPPED(&(li)->li_addr->su_sin6.sin6_addr) || \
  153. IN6_IS_ADDR_V4COMPAT(&(li)->li_addr->su_sin6.sin6_addr)))
  154. /* Insert according to scope, mappedness and family */
  155. for (rr = &result; *rr; rr = &(*rr)->li_next) {
  156. if ((*rr)->li_scope < li->li_scope)
  157. break;
  158. #if SU_HAVE_IN6
  159. if (LI_MAPPED(*rr) > LI_MAPPED(li))
  160. break;
  161. #endif
  162. if ((*rr)->li_family < li->li_family)
  163. break;
  164. }
  165. li->li_next = *rr;
  166. *rr = li;
  167. }
  168. }
  169. *return_localinfo = result;
  170. if (result == NULL)
  171. error = ELI_NOADDRESS;
  172. return error;
  173. }