Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 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 | |
| 27 | |
| 28 | PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *hostname, pj_hostent *phe) |
| 29 | { |
| 30 | struct hostent *he; |
| 31 | char copy[PJ_MAX_HOSTNAME]; |
| 32 | |
| 33 | pj_assert(hostname && hostname ->slen < PJ_MAX_HOSTNAME); |
| 34 | |
| 35 | if (hostname->slen >= PJ_MAX_HOSTNAME) |
| 36 | return PJ_ENAMETOOLONG; |
| 37 | |
| 38 | pj_memcpy(copy, hostname->ptr, hostname->slen); |
| 39 | copy[ hostname->slen ] = '\0'; |
| 40 | |
| 41 | he = gethostbyname(copy); |
Benny Prijono | 64df0eb | 2007-01-26 17:09:14 +0000 | [diff] [blame] | 42 | if (!he) { |
Benny Prijono | 129287a | 2007-03-01 16:52:45 +0000 | [diff] [blame] | 43 | return PJ_ERESOLVE; |
| 44 | /* DO NOT use pj_get_netos_error() since host resolution error |
| 45 | * is reported in h_errno instead of errno! |
Benny Prijono | 64df0eb | 2007-01-26 17:09:14 +0000 | [diff] [blame] | 46 | return pj_get_netos_error(); |
Benny Prijono | 129287a | 2007-03-01 16:52:45 +0000 | [diff] [blame] | 47 | */ |
Benny Prijono | 64df0eb | 2007-01-26 17:09:14 +0000 | [diff] [blame] | 48 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 49 | |
| 50 | phe->h_name = he->h_name; |
| 51 | phe->h_aliases = he->h_aliases; |
| 52 | phe->h_addrtype = he->h_addrtype; |
| 53 | phe->h_length = he->h_length; |
| 54 | phe->h_addr_list = he->h_addr_list; |
| 55 | |
| 56 | return PJ_SUCCESS; |
| 57 | } |
| 58 | |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 59 | /* Resolve IPv4/IPv6 address */ |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 60 | 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] | 61 | unsigned *count, pj_addrinfo ai[]) |
| 62 | { |
| 63 | #if defined(PJ_SOCK_HAS_GETADDRINFO) && PJ_SOCK_HAS_GETADDRINFO!=0 |
| 64 | char nodecopy[PJ_MAX_HOSTNAME]; |
Benny Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame] | 65 | struct addrinfo hint, *res, *orig_res; |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 66 | unsigned i; |
| 67 | int rc; |
| 68 | |
| 69 | PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL); |
| 70 | PJ_ASSERT_RETURN(nodename->ptr && nodename->slen, PJ_EINVAL); |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 71 | PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6 || |
| 72 | af==PJ_AF_UNSPEC, PJ_EINVAL); |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 73 | |
Benny Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 74 | /* Check if nodename is IP address */ |
| 75 | pj_bzero(&ai[0], sizeof(ai[0])); |
| 76 | ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af; |
| 77 | if (pj_inet_pton(af, nodename, pj_sockaddr_get_addr(&ai[0].ai_addr)) |
| 78 | == PJ_SUCCESS) |
| 79 | { |
| 80 | pj_str_t tmp; |
| 81 | |
| 82 | tmp.ptr = ai[0].ai_canonname; |
| 83 | pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME); |
| 84 | ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af; |
| 85 | *count = 1; |
| 86 | |
| 87 | return PJ_SUCCESS; |
| 88 | } |
| 89 | |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 90 | /* Copy node name to null terminated string. */ |
| 91 | if (nodename->slen >= PJ_MAX_HOSTNAME) |
| 92 | return PJ_ENAMETOOLONG; |
| 93 | pj_memcpy(nodecopy, nodename->ptr, nodename->slen); |
| 94 | nodecopy[nodename->slen] = '\0'; |
| 95 | |
| 96 | /* Call getaddrinfo() */ |
| 97 | pj_bzero(&hint, sizeof(hint)); |
| 98 | hint.ai_family = af; |
| 99 | |
| 100 | rc = getaddrinfo(nodecopy, NULL, &hint, &res); |
| 101 | if (rc != 0) |
| 102 | return PJ_ERESOLVE; |
| 103 | |
Benny Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame] | 104 | orig_res = res; |
| 105 | |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 106 | /* Enumerate each item in the result */ |
| 107 | for (i=0; i<*count && res; res=res->ai_next) { |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 108 | /* Ignore unwanted address families */ |
| 109 | if (af!=PJ_AF_UNSPEC && res->ai_family != af) |
| 110 | continue; |
| 111 | |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 112 | /* Store canonical name (possibly truncating the name) */ |
Benny Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame] | 113 | if (res->ai_canonname) { |
| 114 | pj_ansi_strncpy(ai[i].ai_canonname, res->ai_canonname, |
| 115 | sizeof(ai[i].ai_canonname)); |
| 116 | ai[i].ai_canonname[sizeof(ai[i].ai_canonname)-1] = '\0'; |
| 117 | } else { |
| 118 | pj_ansi_strcpy(ai[i].ai_canonname, nodecopy); |
| 119 | } |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 120 | |
| 121 | /* Store address */ |
| 122 | PJ_ASSERT_ON_FAIL(res->ai_addrlen <= sizeof(pj_sockaddr), continue); |
| 123 | pj_memcpy(&ai[i].ai_addr, res->ai_addr, res->ai_addrlen); |
| 124 | |
| 125 | /* Next slot */ |
| 126 | ++i; |
| 127 | } |
| 128 | |
| 129 | *count = i; |
| 130 | |
Benny Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame] | 131 | freeaddrinfo(orig_res); |
| 132 | |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 133 | /* Done */ |
| 134 | return PJ_SUCCESS; |
| 135 | |
| 136 | #else /* PJ_SOCK_HAS_GETADDRINFO */ |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 137 | |
Benny Prijono | 43d4b6c | 2008-01-24 19:59:41 +0000 | [diff] [blame] | 138 | PJ_ASSERT_RETURN(count && *count, PJ_EINVAL); |
| 139 | |
| 140 | /* Check if nodename is IP address */ |
| 141 | pj_bzero(&ai[0], sizeof(ai[0])); |
| 142 | ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af; |
| 143 | if (pj_inet_pton(af, nodename, pj_sockaddr_get_addr(&ai[0].ai_addr)) |
| 144 | == PJ_SUCCESS) |
| 145 | { |
| 146 | pj_str_t tmp; |
| 147 | |
| 148 | tmp.ptr = ai[0].ai_canonname; |
| 149 | pj_strncpy_with_null(&tmp, nodename, PJ_MAX_HOSTNAME); |
| 150 | ai[0].ai_addr.addr.sa_family = (pj_uint16_t)af; |
| 151 | *count = 1; |
| 152 | |
| 153 | return PJ_SUCCESS; |
| 154 | } |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 155 | |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 156 | if (af == PJ_AF_INET || af == PJ_AF_UNSPEC) { |
| 157 | pj_hostent he; |
| 158 | unsigned i, max_count; |
| 159 | pj_status_t status; |
| 160 | |
Benny Prijono | 0cc83f0 | 2008-01-19 09:34:30 +0000 | [diff] [blame] | 161 | /* VC6 complains that "he" is uninitialized */ |
| 162 | #ifdef _MSC_VER |
| 163 | pj_bzero(&he, sizeof(he)); |
| 164 | #endif |
| 165 | |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 166 | status = pj_gethostbyname(nodename, &he); |
| 167 | if (status != PJ_SUCCESS) |
| 168 | return status; |
| 169 | |
| 170 | max_count = *count; |
| 171 | *count = 0; |
| 172 | |
| 173 | pj_bzero(ai, max_count * sizeof(pj_addrinfo)); |
| 174 | |
| 175 | for (i=0; he.h_addr_list[i] && *count<max_count; ++i) { |
| 176 | pj_ansi_strncpy(ai[*count].ai_canonname, he.h_name, |
| 177 | sizeof(ai[*count].ai_canonname)); |
| 178 | ai[*count].ai_canonname[sizeof(ai[*count].ai_canonname)-1] = '\0'; |
| 179 | |
| 180 | ai[*count].ai_addr.ipv4.sin_family = PJ_AF_INET; |
| 181 | pj_memcpy(&ai[*count].ai_addr.ipv4.sin_addr, |
| 182 | he.h_addr_list[i], he.h_length); |
| 183 | |
| 184 | (*count)++; |
| 185 | } |
| 186 | |
| 187 | return PJ_SUCCESS; |
| 188 | |
| 189 | } else { |
| 190 | /* IPv6 is not supported */ |
| 191 | *count = 0; |
| 192 | |
| 193 | return PJ_EIPV6NOTSUP; |
| 194 | } |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 195 | #endif /* PJ_SOCK_HAS_GETADDRINFO */ |
| 196 | } |
| 197 | |