blob: fb730fd4891e0a7eb17a0f0222467bd5edd9dd53 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono844653c2008-12-23 17:27:53 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00005 *
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.
10 *
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.
15 *
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
19 */
20#include <pj/addr_resolv.h>
21#include <pj/assert.h>
22#include <pj/string.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000023#include <pj/errno.h>
Benny Prijono4c8fb532007-11-26 05:36:18 +000024#include <pj/ip_helper.h>
Benny Prijono594e4c52006-09-14 18:51:01 +000025#include <pj/compat/socket.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000026
Sauw Minga4b628f2011-04-26 03:07:24 +000027#if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0
28# include <CoreFoundation/CFString.h>
29# include <CFNetwork/CFHost.h>
30#endif
Benny Prijono5dcb38d2005-11-21 01:55:47 +000031
32PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *hostname, pj_hostent *phe)
33{
34 struct hostent *he;
35 char copy[PJ_MAX_HOSTNAME];
36
37 pj_assert(hostname && hostname ->slen < PJ_MAX_HOSTNAME);
38
39 if (hostname->slen >= PJ_MAX_HOSTNAME)
40 return PJ_ENAMETOOLONG;
41
42 pj_memcpy(copy, hostname->ptr, hostname->slen);
43 copy[ hostname->slen ] = '\0';
44
45 he = gethostbyname(copy);
Benny Prijono64df0eb2007-01-26 17:09:14 +000046 if (!he) {
Benny Prijono129287a2007-03-01 16:52:45 +000047 return PJ_ERESOLVE;
48 /* DO NOT use pj_get_netos_error() since host resolution error
49 * is reported in h_errno instead of errno!
Benny Prijono64df0eb2007-01-26 17:09:14 +000050 return pj_get_netos_error();
Benny Prijono129287a2007-03-01 16:52:45 +000051 */
Benny Prijono64df0eb2007-01-26 17:09:14 +000052 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +000053
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;
59
60 return PJ_SUCCESS;
61}
62
Benny Prijonoc16c6e32007-11-18 14:53:47 +000063/* Resolve IPv4/IPv6 address */
Benny Prijono62b86eb2007-12-01 08:52:57 +000064PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename,
Benny Prijonoc16c6e32007-11-18 14:53:47 +000065 unsigned *count, pj_addrinfo ai[])
66{
67#if defined(PJ_SOCK_HAS_GETADDRINFO) && PJ_SOCK_HAS_GETADDRINFO!=0
68 char nodecopy[PJ_MAX_HOSTNAME];
Benny Prijono10d62432010-07-05 13:47:30 +000069 pj_bool_t has_addr = PJ_FALSE;
Benny Prijonoc16c6e32007-11-18 14:53:47 +000070 unsigned i;
Sauw Minga4b628f2011-04-26 03:07:24 +000071#if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0
72 CFStringRef hostname;
73 CFHostRef hostRef;
74 pj_status_t status = PJ_SUCCESS;
75#else
Benny Prijonoc16c6e32007-11-18 14:53:47 +000076 int rc;
Sauw Minga4b628f2011-04-26 03:07:24 +000077 struct addrinfo hint, *res, *orig_res;
78#endif
Benny Prijonoc16c6e32007-11-18 14:53:47 +000079
80 PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL);
81 PJ_ASSERT_RETURN(nodename->ptr && nodename->slen, PJ_EINVAL);
Benny Prijono62b86eb2007-12-01 08:52:57 +000082 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6 ||
83 af==PJ_AF_UNSPEC, PJ_EINVAL);
Benny Prijonoc16c6e32007-11-18 14:53:47 +000084
Benny Prijono43d4b6c2008-01-24 19:59:41 +000085 /* Check if nodename is IP address */
86 pj_bzero(&ai[0], sizeof(ai[0]));
Benny Prijono10d62432010-07-05 13:47:30 +000087 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)
90 {
91 af = PJ_AF_INET;
92 has_addr = PJ_TRUE;
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)
96 {
97 af = PJ_AF_INET6;
98 has_addr = PJ_TRUE;
99 }
Benny Prijono43d4b6c2008-01-24 19:59:41 +0000100
Benny Prijono10d62432010-07-05 13:47:30 +0000101 if (has_addr) {
102 pj_str_t tmp;
Benny Prijono43d4b6c2008-01-24 19:59:41 +0000103
Benny Prijono10d62432010-07-05 13:47:30 +0000104 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;
107 *count = 1;
Nanang Izzuddinf366bf72010-01-04 16:54:50 +0000108
Benny Prijono10d62432010-07-05 13:47:30 +0000109 return PJ_SUCCESS;
Benny Prijono43d4b6c2008-01-24 19:59:41 +0000110 }
111
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000112 /* 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';
117
Sauw Minga4b628f2011-04-26 03:07:24 +0000118#if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0
119 hostname = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, nodecopy,
120 kCFStringEncodingASCII,
121 kCFAllocatorNull);
122 hostRef = CFHostCreateWithName(kCFAllocatorDefault, hostname);
123 if (CFHostStartInfoResolution(hostRef, kCFHostAddresses, nil)) {
124 CFArrayRef addrRef = CFHostGetAddressing(hostRef, nil);
125 i = 0;
126 if (addrRef != nil) {
127 CFIndex idx, naddr;
128
129 naddr = CFArrayGetCount(addrRef);
130 for (idx = 0; idx < naddr && i < *count; idx++) {
131 struct sockaddr *addr;
132
133 addr = (struct sockaddr *)
134 CFDataGetBytePtr(CFArrayGetValueAtIndex(addrRef, idx));
135 /* This should not happen. */
136 pj_assert(addr);
137
138 /* Ignore unwanted address families */
139 if (af!=PJ_AF_UNSPEC && addr->sa_family != af)
140 continue;
141
142 /* Store canonical name */
143 pj_ansi_strcpy(ai[i].ai_canonname, nodecopy);
144
145 /* Store address */
146 PJ_ASSERT_ON_FAIL(sizeof(*addr) <= sizeof(pj_sockaddr),
147 continue);
148 pj_memcpy(&ai[i].ai_addr, addr, sizeof(*addr));
149 PJ_SOCKADDR_RESET_LEN(&ai[i].ai_addr);
150
151 i++;
152 }
153 }
154
155 *count = i;
156 } else {
157 status = PJ_ERESOLVE;
158 }
159
160 CFRelease(hostRef);
161 CFRelease(hostname);
162
163 return status;
164#else
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000165 /* Call getaddrinfo() */
166 pj_bzero(&hint, sizeof(hint));
167 hint.ai_family = af;
168
169 rc = getaddrinfo(nodecopy, NULL, &hint, &res);
170 if (rc != 0)
171 return PJ_ERESOLVE;
172
Benny Prijono80025db2007-12-02 15:36:46 +0000173 orig_res = res;
174
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000175 /* Enumerate each item in the result */
176 for (i=0; i<*count && res; res=res->ai_next) {
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000177 /* Ignore unwanted address families */
178 if (af!=PJ_AF_UNSPEC && res->ai_family != af)
179 continue;
180
Benny Prijono62b86eb2007-12-01 08:52:57 +0000181 /* Store canonical name (possibly truncating the name) */
Benny Prijono80025db2007-12-02 15:36:46 +0000182 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';
186 } else {
187 pj_ansi_strcpy(ai[i].ai_canonname, nodecopy);
188 }
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000189
190 /* Store address */
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);
Nanang Izzuddinf366bf72010-01-04 16:54:50 +0000193 PJ_SOCKADDR_RESET_LEN(&ai[i].ai_addr);
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000194
195 /* Next slot */
196 ++i;
197 }
198
199 *count = i;
200
Benny Prijono80025db2007-12-02 15:36:46 +0000201 freeaddrinfo(orig_res);
202
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000203 /* Done */
204 return PJ_SUCCESS;
Sauw Minga4b628f2011-04-26 03:07:24 +0000205#endif
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000206
207#else /* PJ_SOCK_HAS_GETADDRINFO */
Benny Prijono10d62432010-07-05 13:47:30 +0000208 pj_bool_t has_addr = PJ_FALSE;
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000209
Benny Prijono43d4b6c2008-01-24 19:59:41 +0000210 PJ_ASSERT_RETURN(count && *count, PJ_EINVAL);
211
212 /* Check if nodename is IP address */
213 pj_bzero(&ai[0], sizeof(ai[0]));
Benny Prijono10d62432010-07-05 13:47:30 +0000214 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)
217 {
218 af = PJ_AF_INET;
219 has_addr = PJ_TRUE;
220 }
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)
224 {
225 af = PJ_AF_INET6;
226 has_addr = PJ_TRUE;
227 }
Benny Prijono43d4b6c2008-01-24 19:59:41 +0000228
Benny Prijono10d62432010-07-05 13:47:30 +0000229 if (has_addr) {
230 pj_str_t tmp;
Benny Prijono43d4b6c2008-01-24 19:59:41 +0000231
Benny Prijono10d62432010-07-05 13:47:30 +0000232 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;
235 *count = 1;
Nanang Izzuddinf366bf72010-01-04 16:54:50 +0000236
Benny Prijono10d62432010-07-05 13:47:30 +0000237 return PJ_SUCCESS;
Benny Prijono43d4b6c2008-01-24 19:59:41 +0000238 }
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000239
Benny Prijono62b86eb2007-12-01 08:52:57 +0000240 if (af == PJ_AF_INET || af == PJ_AF_UNSPEC) {
241 pj_hostent he;
242 unsigned i, max_count;
243 pj_status_t status;
244
Benny Prijono0cc83f02008-01-19 09:34:30 +0000245 /* VC6 complains that "he" is uninitialized */
246 #ifdef _MSC_VER
247 pj_bzero(&he, sizeof(he));
248 #endif
249
Benny Prijono62b86eb2007-12-01 08:52:57 +0000250 status = pj_gethostbyname(nodename, &he);
251 if (status != PJ_SUCCESS)
252 return status;
253
254 max_count = *count;
255 *count = 0;
256
257 pj_bzero(ai, max_count * sizeof(pj_addrinfo));
258
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';
263
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);
Nanang Izzuddinf366bf72010-01-04 16:54:50 +0000267 PJ_SOCKADDR_RESET_LEN(&ai[*count].ai_addr);
Benny Prijono62b86eb2007-12-01 08:52:57 +0000268
269 (*count)++;
270 }
271
272 return PJ_SUCCESS;
273
274 } else {
275 /* IPv6 is not supported */
276 *count = 0;
277
278 return PJ_EIPV6NOTSUP;
279 }
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000280#endif /* PJ_SOCK_HAS_GETADDRINFO */
281}
282