Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Nanang Izzuddin | a62ffc9 | 2011-05-05 06:14:19 +0000 | [diff] [blame^] | 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 5 | * |
| 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 Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 23 | #include <pj/errno.h> |
Benny Prijono | 4c8fb53 | 2007-11-26 05:36:18 +0000 | [diff] [blame] | 24 | #include <pj/ip_helper.h> |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 25 | #include <pj/compat/socket.h> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 26 | |
Sauw Ming | a4b628f | 2011-04-26 03:07:24 +0000 | [diff] [blame] | 27 | #if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0 |
| 28 | # include <CoreFoundation/CFString.h> |
| 29 | # include <CFNetwork/CFHost.h> |
| 30 | #endif |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 31 | |
| 32 | PJ_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 Prijono | 64df0eb | 2007-01-26 17:09:14 +0000 | [diff] [blame] | 46 | if (!he) { |
Benny Prijono | 129287a | 2007-03-01 16:52:45 +0000 | [diff] [blame] | 47 | 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 Prijono | 64df0eb | 2007-01-26 17:09:14 +0000 | [diff] [blame] | 50 | return pj_get_netos_error(); |
Benny Prijono | 129287a | 2007-03-01 16:52:45 +0000 | [diff] [blame] | 51 | */ |
Benny Prijono | 64df0eb | 2007-01-26 17:09:14 +0000 | [diff] [blame] | 52 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 53 | |
| 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 Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 63 | /* Resolve IPv4/IPv6 address */ |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 64 | PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename, |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 65 | 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 Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 69 | pj_bool_t has_addr = PJ_FALSE; |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 70 | unsigned i; |
Sauw Ming | a4b628f | 2011-04-26 03:07:24 +0000 | [diff] [blame] | 71 | #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 Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 76 | int rc; |
Sauw Ming | a4b628f | 2011-04-26 03:07:24 +0000 | [diff] [blame] | 77 | struct addrinfo hint, *res, *orig_res; |
| 78 | #endif |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 79 | |
| 80 | PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL); |
| 81 | PJ_ASSERT_RETURN(nodename->ptr && nodename->slen, PJ_EINVAL); |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 82 | PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6 || |
| 83 | af==PJ_AF_UNSPEC, PJ_EINVAL); |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 84 | |
Benny Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 85 | /* Check if nodename is IP address */ |
| 86 | pj_bzero(&ai[0], sizeof(ai[0])); |
Benny Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 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) |
| 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 Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 100 | |
Benny Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 101 | if (has_addr) { |
| 102 | pj_str_t tmp; |
Benny Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 103 | |
Benny Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 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; |
| 107 | *count = 1; |
Nanang Izzuddin | f366bf7 | 2010-01-04 16:54:50 +0000 | [diff] [blame] | 108 | |
Benny Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 109 | return PJ_SUCCESS; |
Benny Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 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'; |
| 117 | |
Sauw Ming | a4b628f | 2011-04-26 03:07:24 +0000 | [diff] [blame] | 118 | #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 Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 165 | /* 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 Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame] | 173 | orig_res = res; |
| 174 | |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 175 | /* Enumerate each item in the result */ |
| 176 | for (i=0; i<*count && res; res=res->ai_next) { |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 177 | /* Ignore unwanted address families */ |
| 178 | if (af!=PJ_AF_UNSPEC && res->ai_family != af) |
| 179 | continue; |
| 180 | |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 181 | /* Store canonical name (possibly truncating the name) */ |
Benny Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame] | 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'; |
| 186 | } else { |
| 187 | pj_ansi_strcpy(ai[i].ai_canonname, nodecopy); |
| 188 | } |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 189 | |
| 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 Izzuddin | f366bf7 | 2010-01-04 16:54:50 +0000 | [diff] [blame] | 193 | PJ_SOCKADDR_RESET_LEN(&ai[i].ai_addr); |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 194 | |
| 195 | /* Next slot */ |
| 196 | ++i; |
| 197 | } |
| 198 | |
| 199 | *count = i; |
| 200 | |
Benny Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame] | 201 | freeaddrinfo(orig_res); |
| 202 | |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 203 | /* Done */ |
| 204 | return PJ_SUCCESS; |
Sauw Ming | a4b628f | 2011-04-26 03:07:24 +0000 | [diff] [blame] | 205 | #endif |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 206 | |
| 207 | #else /* PJ_SOCK_HAS_GETADDRINFO */ |
Benny Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 208 | pj_bool_t has_addr = PJ_FALSE; |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 209 | |
Benny Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 210 | PJ_ASSERT_RETURN(count && *count, PJ_EINVAL); |
| 211 | |
| 212 | /* Check if nodename is IP address */ |
| 213 | pj_bzero(&ai[0], sizeof(ai[0])); |
Benny Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 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) |
| 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 Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 228 | |
Benny Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 229 | if (has_addr) { |
| 230 | pj_str_t tmp; |
Benny Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 231 | |
Benny Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 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; |
| 235 | *count = 1; |
Nanang Izzuddin | f366bf7 | 2010-01-04 16:54:50 +0000 | [diff] [blame] | 236 | |
Benny Prijono | 10d6243 | 2010-07-05 13:47:30 +0000 | [diff] [blame] | 237 | return PJ_SUCCESS; |
Benny Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 238 | } |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 239 | |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 240 | 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 Prijono | 0cc83f0 | 2008-01-19 09:34:30 +0000 | [diff] [blame] | 245 | /* VC6 complains that "he" is uninitialized */ |
| 246 | #ifdef _MSC_VER |
| 247 | pj_bzero(&he, sizeof(he)); |
| 248 | #endif |
| 249 | |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 250 | 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 Izzuddin | f366bf7 | 2010-01-04 16:54:50 +0000 | [diff] [blame] | 267 | PJ_SOCKADDR_RESET_LEN(&ai[*count].ai_addr); |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 268 | |
| 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 Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 280 | #endif /* PJ_SOCK_HAS_GETADDRINFO */ |
| 281 | } |
| 282 | |