1e66f31c5Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2e66f31c5Sopenharmony_ci*
3e66f31c5Sopenharmony_ci* Permission is hereby granted, free of charge, to any person obtaining a copy
4e66f31c5Sopenharmony_ci* of this software and associated documentation files (the "Software"), to
5e66f31c5Sopenharmony_ci* deal in the Software without restriction, including without limitation the
6e66f31c5Sopenharmony_ci* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7e66f31c5Sopenharmony_ci* sell copies of the Software, and to permit persons to whom the Software is
8e66f31c5Sopenharmony_ci* furnished to do so, subject to the following conditions:
9e66f31c5Sopenharmony_ci*
10e66f31c5Sopenharmony_ci* The above copyright notice and this permission notice shall be included in
11e66f31c5Sopenharmony_ci* all copies or substantial portions of the Software.
12e66f31c5Sopenharmony_ci*
13e66f31c5Sopenharmony_ci* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14e66f31c5Sopenharmony_ci* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15e66f31c5Sopenharmony_ci* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16e66f31c5Sopenharmony_ci* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17e66f31c5Sopenharmony_ci* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18e66f31c5Sopenharmony_ci* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19e66f31c5Sopenharmony_ci* IN THE SOFTWARE.
20e66f31c5Sopenharmony_ci*/
21e66f31c5Sopenharmony_ci
22e66f31c5Sopenharmony_ci#include <assert.h>
23e66f31c5Sopenharmony_ci#include <stdlib.h>
24e66f31c5Sopenharmony_ci#include <stdio.h>
25e66f31c5Sopenharmony_ci#include <string.h>
26e66f31c5Sopenharmony_ci
27e66f31c5Sopenharmony_ci#include "uv.h"
28e66f31c5Sopenharmony_ci#include "internal.h"
29e66f31c5Sopenharmony_ci
30e66f31c5Sopenharmony_ci
31e66f31c5Sopenharmony_cistatic void uv__getnameinfo_work(struct uv__work* w) {
32e66f31c5Sopenharmony_ci  uv_getnameinfo_t* req;
33e66f31c5Sopenharmony_ci  int err;
34e66f31c5Sopenharmony_ci  socklen_t salen;
35e66f31c5Sopenharmony_ci
36e66f31c5Sopenharmony_ci  req = container_of(w, uv_getnameinfo_t, work_req);
37e66f31c5Sopenharmony_ci
38e66f31c5Sopenharmony_ci  if (req->storage.ss_family == AF_INET)
39e66f31c5Sopenharmony_ci    salen = sizeof(struct sockaddr_in);
40e66f31c5Sopenharmony_ci  else if (req->storage.ss_family == AF_INET6)
41e66f31c5Sopenharmony_ci    salen = sizeof(struct sockaddr_in6);
42e66f31c5Sopenharmony_ci  else
43e66f31c5Sopenharmony_ci    abort();
44e66f31c5Sopenharmony_ci
45e66f31c5Sopenharmony_ci  err = getnameinfo((struct sockaddr*) &req->storage,
46e66f31c5Sopenharmony_ci                    salen,
47e66f31c5Sopenharmony_ci                    req->host,
48e66f31c5Sopenharmony_ci                    sizeof(req->host),
49e66f31c5Sopenharmony_ci                    req->service,
50e66f31c5Sopenharmony_ci                    sizeof(req->service),
51e66f31c5Sopenharmony_ci                    req->flags);
52e66f31c5Sopenharmony_ci  req->retcode = uv__getaddrinfo_translate_error(err);
53e66f31c5Sopenharmony_ci}
54e66f31c5Sopenharmony_ci
55e66f31c5Sopenharmony_cistatic void uv__getnameinfo_done(struct uv__work* w, int status) {
56e66f31c5Sopenharmony_ci  uv_getnameinfo_t* req;
57e66f31c5Sopenharmony_ci  char* host;
58e66f31c5Sopenharmony_ci  char* service;
59e66f31c5Sopenharmony_ci
60e66f31c5Sopenharmony_ci  req = container_of(w, uv_getnameinfo_t, work_req);
61e66f31c5Sopenharmony_ci  uv__req_unregister(req->loop, req);
62e66f31c5Sopenharmony_ci  host = service = NULL;
63e66f31c5Sopenharmony_ci
64e66f31c5Sopenharmony_ci  if (status == UV_ECANCELED) {
65e66f31c5Sopenharmony_ci    assert(req->retcode == 0);
66e66f31c5Sopenharmony_ci    req->retcode = UV_EAI_CANCELED;
67e66f31c5Sopenharmony_ci  } else if (req->retcode == 0) {
68e66f31c5Sopenharmony_ci    host = req->host;
69e66f31c5Sopenharmony_ci    service = req->service;
70e66f31c5Sopenharmony_ci  }
71e66f31c5Sopenharmony_ci
72e66f31c5Sopenharmony_ci  if (req->getnameinfo_cb)
73e66f31c5Sopenharmony_ci    req->getnameinfo_cb(req, req->retcode, host, service);
74e66f31c5Sopenharmony_ci}
75e66f31c5Sopenharmony_ci
76e66f31c5Sopenharmony_ci/*
77e66f31c5Sopenharmony_ci* Entry point for getnameinfo
78e66f31c5Sopenharmony_ci* return 0 if a callback will be made
79e66f31c5Sopenharmony_ci* return error code if validation fails
80e66f31c5Sopenharmony_ci*/
81e66f31c5Sopenharmony_ciint uv_getnameinfo(uv_loop_t* loop,
82e66f31c5Sopenharmony_ci                   uv_getnameinfo_t* req,
83e66f31c5Sopenharmony_ci                   uv_getnameinfo_cb getnameinfo_cb,
84e66f31c5Sopenharmony_ci                   const struct sockaddr* addr,
85e66f31c5Sopenharmony_ci                   int flags) {
86e66f31c5Sopenharmony_ci  if (req == NULL || addr == NULL)
87e66f31c5Sopenharmony_ci    return UV_EINVAL;
88e66f31c5Sopenharmony_ci
89e66f31c5Sopenharmony_ci  if (addr->sa_family == AF_INET) {
90e66f31c5Sopenharmony_ci    memcpy(&req->storage,
91e66f31c5Sopenharmony_ci           addr,
92e66f31c5Sopenharmony_ci           sizeof(struct sockaddr_in));
93e66f31c5Sopenharmony_ci  } else if (addr->sa_family == AF_INET6) {
94e66f31c5Sopenharmony_ci    memcpy(&req->storage,
95e66f31c5Sopenharmony_ci           addr,
96e66f31c5Sopenharmony_ci           sizeof(struct sockaddr_in6));
97e66f31c5Sopenharmony_ci  } else {
98e66f31c5Sopenharmony_ci    return UV_EINVAL;
99e66f31c5Sopenharmony_ci  }
100e66f31c5Sopenharmony_ci
101e66f31c5Sopenharmony_ci  uv__req_init(loop, (uv_req_t*)req, UV_GETNAMEINFO);
102e66f31c5Sopenharmony_ci
103e66f31c5Sopenharmony_ci  req->getnameinfo_cb = getnameinfo_cb;
104e66f31c5Sopenharmony_ci  req->flags = flags;
105e66f31c5Sopenharmony_ci  req->type = UV_GETNAMEINFO;
106e66f31c5Sopenharmony_ci  req->loop = loop;
107e66f31c5Sopenharmony_ci  req->retcode = 0;
108e66f31c5Sopenharmony_ci
109e66f31c5Sopenharmony_ci  if (getnameinfo_cb) {
110e66f31c5Sopenharmony_ci    uv__work_submit(loop,
111e66f31c5Sopenharmony_ci#ifdef USE_FFRT
112e66f31c5Sopenharmony_ci                    (uv_req_t*)req,
113e66f31c5Sopenharmony_ci#endif
114e66f31c5Sopenharmony_ci                    &req->work_req,
115e66f31c5Sopenharmony_ci                    UV__WORK_SLOW_IO,
116e66f31c5Sopenharmony_ci                    uv__getnameinfo_work,
117e66f31c5Sopenharmony_ci                    uv__getnameinfo_done);
118e66f31c5Sopenharmony_ci    return 0;
119e66f31c5Sopenharmony_ci  } else {
120e66f31c5Sopenharmony_ci    uv__getnameinfo_work(&req->work_req);
121e66f31c5Sopenharmony_ci    uv__getnameinfo_done(&req->work_req, 0);
122e66f31c5Sopenharmony_ci    return req->retcode;
123e66f31c5Sopenharmony_ci  }
124e66f31c5Sopenharmony_ci}
125