addr_resolv_sock.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/addr_resolv.h>
  20. #include <pj/assert.h>
  21. #include <pj/string.h>
  22. #include <pj/errno.h>
  23. #include <pj/ip_helper.h>
  24. #include <pj/compat/socket.h>
  25. #if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0
  26. # include <CoreFoundation/CFString.h>
  27. # include <CFNetwork/CFHost.h>
  28. #endif
  29. PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *hostname, pj_hostent *phe)
  30. {
  31. struct hostent *he;
  32. char copy[PJ_MAX_HOSTNAME];
  33. pj_assert(hostname && hostname ->slen < PJ_MAX_HOSTNAME);
  34. if (hostname->slen >= PJ_MAX_HOSTNAME)
  35. return PJ_ENAMETOOLONG;
  36. pj_memcpy(copy, hostname->ptr, hostname->slen);
  37. copy[ hostname->slen ] = '\0';
  38. he = gethostbyname(copy);
  39. if (!he) {
  40. return PJ_ERESOLVE;
  41. /* DO NOT use pj_get_netos_error() since host resolution error
  42. * is reported in h_errno instead of errno!
  43. return pj_get_netos_error();
  44. */
  45. }
  46. phe->h_name = he->h_name;
  47. phe->h_aliases = he->h_aliases;
  48. phe->h_addrtype = he->h_addrtype;
  49. phe->h_length = he->h_length;
  50. phe->h_addr_list = he->h_addr_list;
  51. return PJ_SUCCESS;
  52. }
  53. /* Resolve IPv4/IPv6 address */
  54. PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename,
  55. unsigned *count, pj_addrinfo ai[])
  56. {
  57. #if defined(PJ_SOCK_HAS_GETADDRINFO) && PJ_SOCK_HAS_GETADDRINFO!=0
  58. char nodecopy[PJ_MAX_HOSTNAME];
  59. pj_bool_t has_addr = PJ_FALSE;
  60. unsigned i;
  61. #if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0
  62. CFStringRef hostname;
  63. CFHostRef hostRef;
  64. pj_status_t status = PJ_SUCCESS;
  65. #else
  66. int rc;
  67. struct addrinfo hint, *res, *orig_res;
  68. #endif
  69. PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL);
  70. PJ_ASSERT_RETURN(nodename->ptr && nodename->slen, PJ_EINVAL);
  71. PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6 ||
  72. af==PJ_AF_UNSPEC, PJ_EINVAL);
  73. #if PJ_WIN32_WINCE
  74. /* Check if nodename is IP address */
  75. pj_bzero(&ai[0], sizeof(ai[0]));
  76. if ((af==PJ_AF_INET || af==PJ_AF_UNSPEC) &&
  77. pj_inet_pton(PJ_AF_INET, nodename,
  78. &ai[0].ai_addr.ipv4.sin_addr) == PJ_SUCCESS)
  79. {
  80. af = PJ_AF_INET;
  81. has_addr = PJ_TRUE;
  82. } else if ((af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) &&
  83. pj_inet_pton(PJ_AF_INET6, nodename,
  84. &ai[0].ai_addr.ipv6.sin6_addr) == PJ_SUCCESS)
  85. {
  86. af = PJ_AF_INET6;
  87. has_addr = PJ_TRUE;
  88. }
  89. if (has_addr) {
  90. pj_str_t tmp;
  91. tmp.ptr = ai[0].ai_canonname;
  92. pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME);
  93. ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
  94. *count = 1;
  95. return PJ_SUCCESS;
  96. }
  97. #else /* PJ_WIN32_WINCE */
  98. PJ_UNUSED_ARG(has_addr);
  99. #endif
  100. /* Copy node name to null terminated string. */
  101. if (nodename->slen >= PJ_MAX_HOSTNAME)
  102. return PJ_ENAMETOOLONG;
  103. pj_memcpy(nodecopy, nodename->ptr, nodename->slen);
  104. nodecopy[nodename->slen] = '\0';
  105. #if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0
  106. hostname = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, nodecopy,
  107. kCFStringEncodingASCII,
  108. kCFAllocatorNull);
  109. hostRef = CFHostCreateWithName(kCFAllocatorDefault, hostname);
  110. if (CFHostStartInfoResolution(hostRef, kCFHostAddresses, nil)) {
  111. CFArrayRef addrRef = CFHostGetAddressing(hostRef, nil);
  112. i = 0;
  113. if (addrRef != nil) {
  114. CFIndex idx, naddr;
  115. naddr = CFArrayGetCount(addrRef);
  116. for (idx = 0; idx < naddr && i < *count; idx++) {
  117. struct sockaddr *addr;
  118. size_t addr_size;
  119. addr = (struct sockaddr *)
  120. CFDataGetBytePtr(CFArrayGetValueAtIndex(addrRef, idx));
  121. /* This should not happen. */
  122. pj_assert(addr);
  123. /* Ignore unwanted address families */
  124. if (af!=PJ_AF_UNSPEC && addr->sa_family != af)
  125. continue;
  126. /* Store canonical name */
  127. pj_ansi_strxcpy(ai[i].ai_canonname, nodecopy,
  128. sizeof(ai[i].ai_canonname));
  129. /* Store address */
  130. addr_size = sizeof(*addr);
  131. if (addr->sa_family == PJ_AF_INET6) {
  132. addr_size = addr->sa_len;
  133. }
  134. PJ_ASSERT_ON_FAIL(addr_size <= sizeof(pj_sockaddr), continue);
  135. pj_memcpy(&ai[i].ai_addr, addr, addr_size);
  136. PJ_SOCKADDR_RESET_LEN(&ai[i].ai_addr);
  137. i++;
  138. }
  139. }
  140. *count = i;
  141. if (*count == 0)
  142. status = PJ_ERESOLVE;
  143. } else {
  144. status = PJ_ERESOLVE;
  145. }
  146. CFRelease(hostRef);
  147. CFRelease(hostname);
  148. return status;
  149. #else
  150. /* Call getaddrinfo() */
  151. pj_bzero(&hint, sizeof(hint));
  152. hint.ai_family = af;
  153. /* Zero value of ai_socktype means the implementation shall attempt
  154. * to resolve the service name for all supported socket types */
  155. hint.ai_socktype = 0;
  156. rc = getaddrinfo(nodecopy, NULL, &hint, &res);
  157. if (rc != 0)
  158. return PJ_ERESOLVE;
  159. orig_res = res;
  160. /* Enumerate each item in the result */
  161. for (i=0; i<*count && res; res=res->ai_next) {
  162. unsigned j;
  163. pj_bool_t duplicate_found = PJ_FALSE;
  164. /* Ignore unwanted address families */
  165. if (af!=PJ_AF_UNSPEC && res->ai_family != af)
  166. continue;
  167. if (res->ai_socktype != pj_SOCK_DGRAM() &&
  168. res->ai_socktype != pj_SOCK_STREAM() &&
  169. /* It is possible that the result's sock type
  170. * is unspecified.
  171. */
  172. res->ai_socktype != 0)
  173. {
  174. continue;
  175. }
  176. /* Add current address in the resulting list if there
  177. * is no duplicates only. */
  178. for (j = 0; j < i; j++) {
  179. if (!pj_sockaddr_cmp(&ai[j].ai_addr, res->ai_addr)) {
  180. duplicate_found = PJ_TRUE;
  181. break;
  182. }
  183. }
  184. if (duplicate_found) {
  185. continue;
  186. }
  187. /* Store canonical name (possibly truncating the name) */
  188. if (res->ai_canonname) {
  189. pj_ansi_strxcpy(ai[i].ai_canonname, res->ai_canonname,
  190. sizeof(ai[i].ai_canonname));
  191. } else {
  192. pj_ansi_strxcpy(ai[i].ai_canonname, nodecopy,
  193. sizeof(ai[i].ai_canonname));
  194. }
  195. /* Store address */
  196. PJ_ASSERT_ON_FAIL(res->ai_addrlen <= sizeof(pj_sockaddr), continue);
  197. pj_memcpy(&ai[i].ai_addr, res->ai_addr, res->ai_addrlen);
  198. PJ_SOCKADDR_RESET_LEN(&ai[i].ai_addr);
  199. /* Next slot */
  200. ++i;
  201. }
  202. *count = i;
  203. freeaddrinfo(orig_res);
  204. /* Done */
  205. return (*count > 0? PJ_SUCCESS : PJ_ERESOLVE);
  206. #endif
  207. #else /* PJ_SOCK_HAS_GETADDRINFO */
  208. pj_bool_t has_addr = PJ_FALSE;
  209. PJ_ASSERT_RETURN(count && *count, PJ_EINVAL);
  210. #if PJ_WIN32_WINCE
  211. /* Check if nodename is IP address */
  212. pj_bzero(&ai[0], sizeof(ai[0]));
  213. if ((af==PJ_AF_INET || af==PJ_AF_UNSPEC) &&
  214. pj_inet_pton(PJ_AF_INET, nodename,
  215. &ai[0].ai_addr.ipv4.sin_addr) == PJ_SUCCESS)
  216. {
  217. af = PJ_AF_INET;
  218. has_addr = PJ_TRUE;
  219. }
  220. else if ((af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) &&
  221. pj_inet_pton(PJ_AF_INET6, nodename,
  222. &ai[0].ai_addr.ipv6.sin6_addr) == PJ_SUCCESS)
  223. {
  224. af = PJ_AF_INET6;
  225. has_addr = PJ_TRUE;
  226. }
  227. if (has_addr) {
  228. pj_str_t tmp;
  229. tmp.ptr = ai[0].ai_canonname;
  230. pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME);
  231. ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
  232. *count = 1;
  233. return PJ_SUCCESS;
  234. }
  235. #else /* PJ_WIN32_WINCE */
  236. PJ_UNUSED_ARG(has_addr);
  237. #endif
  238. if (af == PJ_AF_INET || af == PJ_AF_UNSPEC) {
  239. pj_hostent he;
  240. unsigned i, max_count;
  241. pj_status_t status;
  242. /* VC6 complains that "he" is uninitialized */
  243. #ifdef _MSC_VER
  244. pj_bzero(&he, sizeof(he));
  245. #endif
  246. status = pj_gethostbyname(nodename, &he);
  247. if (status != PJ_SUCCESS)
  248. return status;
  249. max_count = *count;
  250. *count = 0;
  251. pj_bzero(ai, max_count * sizeof(pj_addrinfo));
  252. for (i=0; he.h_addr_list[i] && *count<max_count; ++i) {
  253. pj_ansi_strxcpy(ai[*count].ai_canonname, he.h_name,
  254. sizeof(ai[*count].ai_canonname));
  255. ai[*count].ai_addr.ipv4.sin_family = PJ_AF_INET;
  256. pj_memcpy(&ai[*count].ai_addr.ipv4.sin_addr,
  257. he.h_addr_list[i], he.h_length);
  258. PJ_SOCKADDR_RESET_LEN(&ai[*count].ai_addr);
  259. (*count)++;
  260. }
  261. return (*count > 0? PJ_SUCCESS : PJ_ERESOLVE);
  262. } else {
  263. /* IPv6 is not supported */
  264. *count = 0;
  265. return PJ_EIPV6NOTSUP;
  266. }
  267. #endif /* PJ_SOCK_HAS_GETADDRINFO */
  268. }