Rizin
unix-like reverse engineering framework and cli tools
getnameinfo.c
Go to the documentation of this file.
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21 
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "uv.h"
28 #include "internal.h"
29 
30 
31 static void uv__getnameinfo_work(struct uv__work* w) {
33  int err;
34  socklen_t salen;
35 
36  req = container_of(w, uv_getnameinfo_t, work_req);
37 
38  if (req->storage.ss_family == AF_INET)
39  salen = sizeof(struct sockaddr_in);
40  else if (req->storage.ss_family == AF_INET6)
41  salen = sizeof(struct sockaddr_in6);
42  else
43  abort();
44 
45  err = getnameinfo((struct sockaddr*) &req->storage,
46  salen,
47  req->host,
48  sizeof(req->host),
49  req->service,
50  sizeof(req->service),
51  req->flags);
53 }
54 
55 static void uv__getnameinfo_done(struct uv__work* w, int status) {
57  char* host;
58  char* service;
59 
60  req = container_of(w, uv_getnameinfo_t, work_req);
61  uv__req_unregister(req->loop, req);
62  host = service = NULL;
63 
64  if (status == UV_ECANCELED) {
65  assert(req->retcode == 0);
66  req->retcode = UV_EAI_CANCELED;
67  } else if (req->retcode == 0) {
68  host = req->host;
69  service = req->service;
70  }
71 
72  if (req->getnameinfo_cb)
73  req->getnameinfo_cb(req, req->retcode, host, service);
74 }
75 
76 /*
77 * Entry point for getnameinfo
78 * return 0 if a callback will be made
79 * return error code if validation fails
80 */
83  uv_getnameinfo_cb getnameinfo_cb,
84  const struct sockaddr* addr,
85  int flags) {
86  if (req == NULL || addr == NULL)
87  return UV_EINVAL;
88 
89  if (addr->sa_family == AF_INET) {
90  memcpy(&req->storage,
91  addr,
92  sizeof(struct sockaddr_in));
93  } else if (addr->sa_family == AF_INET6) {
94  memcpy(&req->storage,
95  addr,
96  sizeof(struct sockaddr_in6));
97  } else {
98  return UV_EINVAL;
99  }
100 
101  uv__req_init(loop, (uv_req_t*)req, UV_GETNAMEINFO);
102 
103  req->getnameinfo_cb = getnameinfo_cb;
104  req->flags = flags;
105  req->type = UV_GETNAMEINFO;
106  req->loop = loop;
107  req->retcode = 0;
108 
109  if (getnameinfo_cb) {
111  &req->work_req,
115  return 0;
116  } else {
117  uv__getnameinfo_work(&req->work_req);
118  uv__getnameinfo_done(&req->work_req, 0);
119  return req->retcode;
120  }
121 }
static bool err
Definition: armass.c:435
#define NULL
Definition: cris-opc.c:27
#define w
Definition: crypto_rc6.c:13
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec req
Definition: sflib.h:128
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
assert(limit<=UINT32_MAX/2)
#define container_of(ptr, type, member)
Definition: rz_types.h:650
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
unsigned int socklen_t
Definition: sftypes.h:219
#define AF_INET
Definition: sftypes.h:287
#define AF_INET6
Definition: sftypes.h:295
Definition: uv.h:1780
Definition: uv.h:407
uv_loop_t * loop
Definition: main.c:7
void uv__work_submit(uv_loop_t *loop, struct uv__work *w, enum uv__work_kind kind, void(*work)(struct uv__work *w), void(*done)(struct uv__work *w, int status))
Definition: threadpool.c:256
int uv__getaddrinfo_translate_error(int sys_err)
Definition: getaddrinfo.c:42
static void uv__getnameinfo_work(struct uv__work *w)
Definition: getnameinfo.c:31
int uv_getnameinfo(uv_loop_t *loop, uv_getnameinfo_t *req, uv_getnameinfo_cb getnameinfo_cb, const struct sockaddr *addr, int flags)
Definition: getnameinfo.c:81
static void uv__getnameinfo_done(struct uv__work *w, int status)
Definition: getnameinfo.c:55
@ UV__WORK_SLOW_IO
Definition: uv-common.h:194
#define uv__req_init(loop, req, typ)
Definition: uv-common.h:329
#define uv__req_unregister(loop, req)
Definition: uv-common.h:230
void(* uv_getnameinfo_cb)(uv_getnameinfo_t *req, int status, const char *hostname, const char *service)
Definition: uv.h:334
static int addr
Definition: z80asm.c:58