3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <pj/addr_resolv.h>
21 #include <pj/assert.h>
22 #include <pj/string.h>
24 #include <pj/ip_helper.h>
25 #include <pj/compat/socket.h>
27 #if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0
28 # include <CoreFoundation/CFString.h>
29 # include <CFNetwork/CFHost.h>
32 PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *hostname, pj_hostent *phe)
35 char copy[PJ_MAX_HOSTNAME];
37 pj_assert(hostname && hostname ->slen < PJ_MAX_HOSTNAME);
39 if (hostname->slen >= PJ_MAX_HOSTNAME)
40 return PJ_ENAMETOOLONG;
42 pj_memcpy(copy, hostname->ptr, hostname->slen);
43 copy[ hostname->slen ] = '\0';
45 he = gethostbyname(copy);
48 /* DO NOT use pj_get_netos_error() since host resolution error
49 * is reported in h_errno instead of errno!
50 return pj_get_netos_error();
54 phe->h_name = he->h_name;
55 phe->h_aliases = he->h_aliases;
56 phe->h_addrtype = he->h_addrtype;
57 phe->h_length = he->h_length;
58 phe->h_addr_list = he->h_addr_list;
63 /* Resolve IPv4/IPv6 address */
64 PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename,
65 unsigned *count, pj_addrinfo ai[])
67 #if defined(PJ_SOCK_HAS_GETADDRINFO) && PJ_SOCK_HAS_GETADDRINFO!=0
68 char nodecopy[PJ_MAX_HOSTNAME];
69 pj_bool_t has_addr = PJ_FALSE;
71 #if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0
74 pj_status_t status = PJ_SUCCESS;
77 struct addrinfo hint, *res, *orig_res;
80 PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL);
81 PJ_ASSERT_RETURN(nodename->ptr && nodename->slen, PJ_EINVAL);
82 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6 ||
83 af==PJ_AF_UNSPEC, PJ_EINVAL);
85 /* Check if nodename is IP address */
86 pj_bzero(&ai[0], sizeof(ai[0]));
87 if ((af==PJ_AF_INET || af==PJ_AF_UNSPEC) &&
88 pj_inet_pton(PJ_AF_INET, nodename,
89 &ai[0].ai_addr.ipv4.sin_addr) == PJ_SUCCESS)
93 } else if ((af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) &&
94 pj_inet_pton(PJ_AF_INET6, nodename,
95 &ai[0].ai_addr.ipv6.sin6_addr) == PJ_SUCCESS)
104 tmp.ptr = ai[0].ai_canonname;
105 pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME);
106 ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
112 /* Copy node name to null terminated string. */
113 if (nodename->slen >= PJ_MAX_HOSTNAME)
114 return PJ_ENAMETOOLONG;
115 pj_memcpy(nodecopy, nodename->ptr, nodename->slen);
116 nodecopy[nodename->slen] = '\0';
118 #if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0
119 hostname = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, nodecopy,
120 kCFStringEncodingASCII,
122 hostRef = CFHostCreateWithName(kCFAllocatorDefault, hostname);
123 if (CFHostStartInfoResolution(hostRef, kCFHostAddresses, nil)) {
124 CFArrayRef addrRef = CFHostGetAddressing(hostRef, nil);
126 if (addrRef != nil) {
129 naddr = CFArrayGetCount(addrRef);
130 for (idx = 0; idx < naddr && i < *count; idx++) {
131 struct sockaddr *addr;
133 addr = (struct sockaddr *)
134 CFDataGetBytePtr(CFArrayGetValueAtIndex(addrRef, idx));
135 /* This should not happen. */
138 /* Ignore unwanted address families */
139 if (af!=PJ_AF_UNSPEC && addr->sa_family != af)
142 /* Store canonical name */
143 pj_ansi_strcpy(ai[i].ai_canonname, nodecopy);
146 PJ_ASSERT_ON_FAIL(sizeof(*addr) <= sizeof(pj_sockaddr),
148 pj_memcpy(&ai[i].ai_addr, addr, sizeof(*addr));
149 PJ_SOCKADDR_RESET_LEN(&ai[i].ai_addr);
157 status = PJ_ERESOLVE;
165 /* Call getaddrinfo() */
166 pj_bzero(&hint, sizeof(hint));
169 rc = getaddrinfo(nodecopy, NULL, &hint, &res);
175 /* Enumerate each item in the result */
176 for (i=0; i<*count && res; res=res->ai_next) {
177 /* Ignore unwanted address families */
178 if (af!=PJ_AF_UNSPEC && res->ai_family != af)
181 /* Store canonical name (possibly truncating the name) */
182 if (res->ai_canonname) {
183 pj_ansi_strncpy(ai[i].ai_canonname, res->ai_canonname,
184 sizeof(ai[i].ai_canonname));
185 ai[i].ai_canonname[sizeof(ai[i].ai_canonname)-1] = '\0';
187 pj_ansi_strcpy(ai[i].ai_canonname, nodecopy);
191 PJ_ASSERT_ON_FAIL(res->ai_addrlen <= sizeof(pj_sockaddr), continue);
192 pj_memcpy(&ai[i].ai_addr, res->ai_addr, res->ai_addrlen);
193 PJ_SOCKADDR_RESET_LEN(&ai[i].ai_addr);
201 freeaddrinfo(orig_res);
207 #else /* PJ_SOCK_HAS_GETADDRINFO */
208 pj_bool_t has_addr = PJ_FALSE;
210 PJ_ASSERT_RETURN(count && *count, PJ_EINVAL);
212 /* Check if nodename is IP address */
213 pj_bzero(&ai[0], sizeof(ai[0]));
214 if ((af==PJ_AF_INET || af==PJ_AF_UNSPEC) &&
215 pj_inet_pton(PJ_AF_INET, nodename,
216 &ai[0].ai_addr.ipv4.sin_addr) == PJ_SUCCESS)
221 else if ((af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) &&
222 pj_inet_pton(PJ_AF_INET6, nodename,
223 &ai[0].ai_addr.ipv6.sin6_addr) == PJ_SUCCESS)
232 tmp.ptr = ai[0].ai_canonname;
233 pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME);
234 ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af;
240 if (af == PJ_AF_INET || af == PJ_AF_UNSPEC) {
242 unsigned i, max_count;
245 /* VC6 complains that "he" is uninitialized */
247 pj_bzero(&he, sizeof(he));
250 status = pj_gethostbyname(nodename, &he);
251 if (status != PJ_SUCCESS)
257 pj_bzero(ai, max_count * sizeof(pj_addrinfo));
259 for (i=0; he.h_addr_list[i] && *count<max_count; ++i) {
260 pj_ansi_strncpy(ai[*count].ai_canonname, he.h_name,
261 sizeof(ai[*count].ai_canonname));
262 ai[*count].ai_canonname[sizeof(ai[*count].ai_canonname)-1] = '\0';
264 ai[*count].ai_addr.ipv4.sin_family = PJ_AF_INET;
265 pj_memcpy(&ai[*count].ai_addr.ipv4.sin_addr,
266 he.h_addr_list[i], he.h_length);
267 PJ_SOCKADDR_RESET_LEN(&ai[*count].ai_addr);
275 /* IPv6 is not supported */
278 return PJ_EIPV6NOTSUP;
280 #endif /* PJ_SOCK_HAS_GETADDRINFO */