blob: c88cc8f53ed1d6ac371eab90aef39a2669a26706 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * 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
27
28PJ_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 Prijono64df0eb2007-01-26 17:09:14 +000042 if (!he) {
Benny Prijono129287a2007-03-01 16:52:45 +000043 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 Prijono64df0eb2007-01-26 17:09:14 +000046 return pj_get_netos_error();
Benny Prijono129287a2007-03-01 16:52:45 +000047 */
Benny Prijono64df0eb2007-01-26 17:09:14 +000048 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +000049
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 Prijonoc16c6e32007-11-18 14:53:47 +000059/* Resolve IPv4/IPv6 address */
Benny Prijono62b86eb2007-12-01 08:52:57 +000060PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename,
Benny Prijonoc16c6e32007-11-18 14:53:47 +000061 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 Prijono80025db2007-12-02 15:36:46 +000065 struct addrinfo hint, *res, *orig_res;
Benny Prijonoc16c6e32007-11-18 14:53:47 +000066 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 Prijono62b86eb2007-12-01 08:52:57 +000071 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6 ||
72 af==PJ_AF_UNSPEC, PJ_EINVAL);
Benny Prijonoc16c6e32007-11-18 14:53:47 +000073
Benny Prijono43d4b6c2008-01-24 19:59:41 +000074 /* 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 Prijonoc16c6e32007-11-18 14:53:47 +000090 /* 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 Prijono80025db2007-12-02 15:36:46 +0000104 orig_res = res;
105
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000106 /* Enumerate each item in the result */
107 for (i=0; i<*count && res; res=res->ai_next) {
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000108 /* Ignore unwanted address families */
109 if (af!=PJ_AF_UNSPEC && res->ai_family != af)
110 continue;
111
Benny Prijono62b86eb2007-12-01 08:52:57 +0000112 /* Store canonical name (possibly truncating the name) */
Benny Prijono80025db2007-12-02 15:36:46 +0000113 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 Prijonoc16c6e32007-11-18 14:53:47 +0000120
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 Prijono80025db2007-12-02 15:36:46 +0000131 freeaddrinfo(orig_res);
132
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000133 /* Done */
134 return PJ_SUCCESS;
135
136#else /* PJ_SOCK_HAS_GETADDRINFO */
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000137
Benny Prijono43d4b6c2008-01-24 19:59:41 +0000138 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 Prijonoc16c6e32007-11-18 14:53:47 +0000155
Benny Prijono62b86eb2007-12-01 08:52:57 +0000156 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 Prijono0cc83f02008-01-19 09:34:30 +0000161 /* VC6 complains that "he" is uninitialized */
162 #ifdef _MSC_VER
163 pj_bzero(&he, sizeof(he));
164 #endif
165
Benny Prijono62b86eb2007-12-01 08:52:57 +0000166 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 Prijonoc16c6e32007-11-18 14:53:47 +0000195#endif /* PJ_SOCK_HAS_GETADDRINFO */
196}
197