xref: /netbsd-src/external/mit/libuv/dist/src/unix/getaddrinfo.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
1*0e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2*0e552da7Schristos  * Permission is hereby granted, free of charge, to any person obtaining a copy
3*0e552da7Schristos  * of this software and associated documentation files (the "Software"), to
4*0e552da7Schristos  * deal in the Software without restriction, including without limitation the
5*0e552da7Schristos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6*0e552da7Schristos  * sell copies of the Software, and to permit persons to whom the Software is
7*0e552da7Schristos  * furnished to do so, subject to the following conditions:
8*0e552da7Schristos  *
9*0e552da7Schristos  * The above copyright notice and this permission notice shall be included in
10*0e552da7Schristos  * all copies or substantial portions of the Software.
11*0e552da7Schristos  *
12*0e552da7Schristos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13*0e552da7Schristos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14*0e552da7Schristos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15*0e552da7Schristos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16*0e552da7Schristos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17*0e552da7Schristos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18*0e552da7Schristos  * IN THE SOFTWARE.
19*0e552da7Schristos  */
20*0e552da7Schristos 
21*0e552da7Schristos /* Expose glibc-specific EAI_* error codes. Needs to be defined before we
22*0e552da7Schristos  * include any headers.
23*0e552da7Schristos  */
24*0e552da7Schristos 
25*0e552da7Schristos #include "uv.h"
26*0e552da7Schristos #include "internal.h"
27*0e552da7Schristos #include "idna.h"
28*0e552da7Schristos 
29*0e552da7Schristos #include <errno.h>
30*0e552da7Schristos #include <stddef.h> /* NULL */
31*0e552da7Schristos #include <stdlib.h>
32*0e552da7Schristos #include <string.h>
33*0e552da7Schristos #include <net/if.h> /* if_indextoname() */
34*0e552da7Schristos 
35*0e552da7Schristos /* EAI_* constants. */
36*0e552da7Schristos #include <netdb.h>
37*0e552da7Schristos 
38*0e552da7Schristos 
uv__getaddrinfo_translate_error(int sys_err)39*0e552da7Schristos int uv__getaddrinfo_translate_error(int sys_err) {
40*0e552da7Schristos   switch (sys_err) {
41*0e552da7Schristos   case 0: return 0;
42*0e552da7Schristos #if defined(EAI_ADDRFAMILY)
43*0e552da7Schristos   case EAI_ADDRFAMILY: return UV_EAI_ADDRFAMILY;
44*0e552da7Schristos #endif
45*0e552da7Schristos #if defined(EAI_AGAIN)
46*0e552da7Schristos   case EAI_AGAIN: return UV_EAI_AGAIN;
47*0e552da7Schristos #endif
48*0e552da7Schristos #if defined(EAI_BADFLAGS)
49*0e552da7Schristos   case EAI_BADFLAGS: return UV_EAI_BADFLAGS;
50*0e552da7Schristos #endif
51*0e552da7Schristos #if defined(EAI_BADHINTS)
52*0e552da7Schristos   case EAI_BADHINTS: return UV_EAI_BADHINTS;
53*0e552da7Schristos #endif
54*0e552da7Schristos #if defined(EAI_CANCELED)
55*0e552da7Schristos   case EAI_CANCELED: return UV_EAI_CANCELED;
56*0e552da7Schristos #endif
57*0e552da7Schristos #if defined(EAI_FAIL)
58*0e552da7Schristos   case EAI_FAIL: return UV_EAI_FAIL;
59*0e552da7Schristos #endif
60*0e552da7Schristos #if defined(EAI_FAMILY)
61*0e552da7Schristos   case EAI_FAMILY: return UV_EAI_FAMILY;
62*0e552da7Schristos #endif
63*0e552da7Schristos #if defined(EAI_MEMORY)
64*0e552da7Schristos   case EAI_MEMORY: return UV_EAI_MEMORY;
65*0e552da7Schristos #endif
66*0e552da7Schristos #if defined(EAI_NODATA)
67*0e552da7Schristos   case EAI_NODATA: return UV_EAI_NODATA;
68*0e552da7Schristos #endif
69*0e552da7Schristos #if defined(EAI_NONAME)
70*0e552da7Schristos # if !defined(EAI_NODATA) || EAI_NODATA != EAI_NONAME
71*0e552da7Schristos   case EAI_NONAME: return UV_EAI_NONAME;
72*0e552da7Schristos # endif
73*0e552da7Schristos #endif
74*0e552da7Schristos #if defined(EAI_OVERFLOW)
75*0e552da7Schristos   case EAI_OVERFLOW: return UV_EAI_OVERFLOW;
76*0e552da7Schristos #endif
77*0e552da7Schristos #if defined(EAI_PROTOCOL)
78*0e552da7Schristos   case EAI_PROTOCOL: return UV_EAI_PROTOCOL;
79*0e552da7Schristos #endif
80*0e552da7Schristos #if defined(EAI_SERVICE)
81*0e552da7Schristos   case EAI_SERVICE: return UV_EAI_SERVICE;
82*0e552da7Schristos #endif
83*0e552da7Schristos #if defined(EAI_SOCKTYPE)
84*0e552da7Schristos   case EAI_SOCKTYPE: return UV_EAI_SOCKTYPE;
85*0e552da7Schristos #endif
86*0e552da7Schristos #if defined(EAI_SYSTEM)
87*0e552da7Schristos   case EAI_SYSTEM: return UV__ERR(errno);
88*0e552da7Schristos #endif
89*0e552da7Schristos   }
90*0e552da7Schristos   assert(!"unknown EAI_* error code");
91*0e552da7Schristos   abort();
92*0e552da7Schristos #ifndef __SUNPRO_C
93*0e552da7Schristos   return 0;  /* Pacify compiler. */
94*0e552da7Schristos #endif
95*0e552da7Schristos }
96*0e552da7Schristos 
97*0e552da7Schristos 
uv__getaddrinfo_work(struct uv__work * w)98*0e552da7Schristos static void uv__getaddrinfo_work(struct uv__work* w) {
99*0e552da7Schristos   uv_getaddrinfo_t* req;
100*0e552da7Schristos   int err;
101*0e552da7Schristos 
102*0e552da7Schristos   req = container_of(w, uv_getaddrinfo_t, work_req);
103*0e552da7Schristos   err = getaddrinfo(req->hostname, req->service, req->hints, &req->addrinfo);
104*0e552da7Schristos   req->retcode = uv__getaddrinfo_translate_error(err);
105*0e552da7Schristos }
106*0e552da7Schristos 
107*0e552da7Schristos 
uv__getaddrinfo_done(struct uv__work * w,int status)108*0e552da7Schristos static void uv__getaddrinfo_done(struct uv__work* w, int status) {
109*0e552da7Schristos   uv_getaddrinfo_t* req;
110*0e552da7Schristos 
111*0e552da7Schristos   req = container_of(w, uv_getaddrinfo_t, work_req);
112*0e552da7Schristos   uv__req_unregister(req->loop, req);
113*0e552da7Schristos 
114*0e552da7Schristos   /* See initialization in uv_getaddrinfo(). */
115*0e552da7Schristos   if (req->hints)
116*0e552da7Schristos     uv__free(req->hints);
117*0e552da7Schristos   else if (req->service)
118*0e552da7Schristos     uv__free(req->service);
119*0e552da7Schristos   else if (req->hostname)
120*0e552da7Schristos     uv__free(req->hostname);
121*0e552da7Schristos   else
122*0e552da7Schristos     assert(0);
123*0e552da7Schristos 
124*0e552da7Schristos   req->hints = NULL;
125*0e552da7Schristos   req->service = NULL;
126*0e552da7Schristos   req->hostname = NULL;
127*0e552da7Schristos 
128*0e552da7Schristos   if (status == UV_ECANCELED) {
129*0e552da7Schristos     assert(req->retcode == 0);
130*0e552da7Schristos     req->retcode = UV_EAI_CANCELED;
131*0e552da7Schristos   }
132*0e552da7Schristos 
133*0e552da7Schristos   if (req->cb)
134*0e552da7Schristos     req->cb(req, req->retcode, req->addrinfo);
135*0e552da7Schristos }
136*0e552da7Schristos 
137*0e552da7Schristos 
uv_getaddrinfo(uv_loop_t * loop,uv_getaddrinfo_t * req,uv_getaddrinfo_cb cb,const char * hostname,const char * service,const struct addrinfo * hints)138*0e552da7Schristos int uv_getaddrinfo(uv_loop_t* loop,
139*0e552da7Schristos                    uv_getaddrinfo_t* req,
140*0e552da7Schristos                    uv_getaddrinfo_cb cb,
141*0e552da7Schristos                    const char* hostname,
142*0e552da7Schristos                    const char* service,
143*0e552da7Schristos                    const struct addrinfo* hints) {
144*0e552da7Schristos   char hostname_ascii[256];
145*0e552da7Schristos   size_t hostname_len;
146*0e552da7Schristos   size_t service_len;
147*0e552da7Schristos   size_t hints_len;
148*0e552da7Schristos   size_t len;
149*0e552da7Schristos   char* buf;
150*0e552da7Schristos   long rc;
151*0e552da7Schristos 
152*0e552da7Schristos   if (req == NULL || (hostname == NULL && service == NULL))
153*0e552da7Schristos     return UV_EINVAL;
154*0e552da7Schristos 
155*0e552da7Schristos   /* FIXME(bnoordhuis) IDNA does not seem to work z/OS,
156*0e552da7Schristos    * probably because it uses EBCDIC rather than ASCII.
157*0e552da7Schristos    */
158*0e552da7Schristos #ifdef __MVS__
159*0e552da7Schristos   (void) &hostname_ascii;
160*0e552da7Schristos #else
161*0e552da7Schristos   if (hostname != NULL) {
162*0e552da7Schristos     rc = uv__idna_toascii(hostname,
163*0e552da7Schristos                           hostname + strlen(hostname),
164*0e552da7Schristos                           hostname_ascii,
165*0e552da7Schristos                           hostname_ascii + sizeof(hostname_ascii));
166*0e552da7Schristos     if (rc < 0)
167*0e552da7Schristos       return rc;
168*0e552da7Schristos     hostname = hostname_ascii;
169*0e552da7Schristos   }
170*0e552da7Schristos #endif
171*0e552da7Schristos 
172*0e552da7Schristos   hostname_len = hostname ? strlen(hostname) + 1 : 0;
173*0e552da7Schristos   service_len = service ? strlen(service) + 1 : 0;
174*0e552da7Schristos   hints_len = hints ? sizeof(*hints) : 0;
175*0e552da7Schristos   buf = uv__malloc(hostname_len + service_len + hints_len);
176*0e552da7Schristos 
177*0e552da7Schristos   if (buf == NULL)
178*0e552da7Schristos     return UV_ENOMEM;
179*0e552da7Schristos 
180*0e552da7Schristos   uv__req_init(loop, req, UV_GETADDRINFO);
181*0e552da7Schristos   req->loop = loop;
182*0e552da7Schristos   req->cb = cb;
183*0e552da7Schristos   req->addrinfo = NULL;
184*0e552da7Schristos   req->hints = NULL;
185*0e552da7Schristos   req->service = NULL;
186*0e552da7Schristos   req->hostname = NULL;
187*0e552da7Schristos   req->retcode = 0;
188*0e552da7Schristos 
189*0e552da7Schristos   /* order matters, see uv_getaddrinfo_done() */
190*0e552da7Schristos   len = 0;
191*0e552da7Schristos 
192*0e552da7Schristos   if (hints) {
193*0e552da7Schristos     req->hints = memcpy(buf + len, hints, sizeof(*hints));
194*0e552da7Schristos     len += sizeof(*hints);
195*0e552da7Schristos   }
196*0e552da7Schristos 
197*0e552da7Schristos   if (service) {
198*0e552da7Schristos     req->service = memcpy(buf + len, service, service_len);
199*0e552da7Schristos     len += service_len;
200*0e552da7Schristos   }
201*0e552da7Schristos 
202*0e552da7Schristos   if (hostname)
203*0e552da7Schristos     req->hostname = memcpy(buf + len, hostname, hostname_len);
204*0e552da7Schristos 
205*0e552da7Schristos   if (cb) {
206*0e552da7Schristos     uv__work_submit(loop,
207*0e552da7Schristos                     &req->work_req,
208*0e552da7Schristos                     UV__WORK_SLOW_IO,
209*0e552da7Schristos                     uv__getaddrinfo_work,
210*0e552da7Schristos                     uv__getaddrinfo_done);
211*0e552da7Schristos     return 0;
212*0e552da7Schristos   } else {
213*0e552da7Schristos     uv__getaddrinfo_work(&req->work_req);
214*0e552da7Schristos     uv__getaddrinfo_done(&req->work_req, 0);
215*0e552da7Schristos     return req->retcode;
216*0e552da7Schristos   }
217*0e552da7Schristos }
218*0e552da7Schristos 
219*0e552da7Schristos 
uv_freeaddrinfo(struct addrinfo * ai)220*0e552da7Schristos void uv_freeaddrinfo(struct addrinfo* ai) {
221*0e552da7Schristos   if (ai)
222*0e552da7Schristos     freeaddrinfo(ai);
223*0e552da7Schristos }
224*0e552da7Schristos 
225*0e552da7Schristos 
uv_if_indextoname(unsigned int ifindex,char * buffer,size_t * size)226*0e552da7Schristos int uv_if_indextoname(unsigned int ifindex, char* buffer, size_t* size) {
227*0e552da7Schristos   char ifname_buf[UV_IF_NAMESIZE];
228*0e552da7Schristos   size_t len;
229*0e552da7Schristos 
230*0e552da7Schristos   if (buffer == NULL || size == NULL || *size == 0)
231*0e552da7Schristos     return UV_EINVAL;
232*0e552da7Schristos 
233*0e552da7Schristos   if (if_indextoname(ifindex, ifname_buf) == NULL)
234*0e552da7Schristos     return UV__ERR(errno);
235*0e552da7Schristos 
236*0e552da7Schristos   len = strnlen(ifname_buf, sizeof(ifname_buf));
237*0e552da7Schristos 
238*0e552da7Schristos   if (*size <= len) {
239*0e552da7Schristos     *size = len + 1;
240*0e552da7Schristos     return UV_ENOBUFS;
241*0e552da7Schristos   }
242*0e552da7Schristos 
243*0e552da7Schristos   memcpy(buffer, ifname_buf, len);
244*0e552da7Schristos   buffer[len] = '\0';
245*0e552da7Schristos   *size = len;
246*0e552da7Schristos 
247*0e552da7Schristos   return 0;
248*0e552da7Schristos }
249*0e552da7Schristos 
uv_if_indextoiid(unsigned int ifindex,char * buffer,size_t * size)250*0e552da7Schristos int uv_if_indextoiid(unsigned int ifindex, char* buffer, size_t* size) {
251*0e552da7Schristos   return uv_if_indextoname(ifindex, buffer, size);
252*0e552da7Schristos }
253