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 <stdio.h>
24 
25 #include "uv.h"
26 #include "internal.h"
27 #include "req-inl.h"
28 
29 #ifndef GetNameInfo
30 int WSAAPI GetNameInfoW(
31  const SOCKADDR *pSockaddr,
32  socklen_t SockaddrLength,
33  PWCHAR pNodeBuffer,
34  DWORD NodeBufferSize,
35  PWCHAR pServiceBuffer,
36  DWORD ServiceBufferSize,
37  INT Flags
38 );
39 #endif
40 
41 static void uv__getnameinfo_work(struct uv__work* w) {
43  WCHAR host[NI_MAXHOST];
44  WCHAR service[NI_MAXSERV];
45  int ret;
46 
47  req = container_of(w, uv_getnameinfo_t, work_req);
48  if (GetNameInfoW((struct sockaddr*)&req->storage,
49  sizeof(req->storage),
50  host,
51  ARRAY_SIZE(host),
52  service,
53  ARRAY_SIZE(service),
54  req->flags)) {
55  ret = WSAGetLastError();
56  req->retcode = uv__getaddrinfo_translate_error(ret);
57  return;
58  }
59 
60  ret = WideCharToMultiByte(CP_UTF8,
61  0,
62  host,
63  -1,
64  req->host,
65  sizeof(req->host),
66  NULL,
67  NULL);
68  if (ret == 0) {
69  req->retcode = uv_translate_sys_error(GetLastError());
70  return;
71  }
72 
73  ret = WideCharToMultiByte(CP_UTF8,
74  0,
75  service,
76  -1,
77  req->service,
78  sizeof(req->service),
79  NULL,
80  NULL);
81  if (ret == 0) {
82  req->retcode = uv_translate_sys_error(GetLastError());
83  }
84 }
85 
86 
87 /*
88 * Called from uv_run when complete.
89 */
90 static void uv__getnameinfo_done(struct uv__work* w, int status) {
92  char* host;
93  char* service;
94 
95  req = container_of(w, uv_getnameinfo_t, work_req);
96  uv__req_unregister(req->loop, req);
97  host = service = NULL;
98 
99  if (status == UV_ECANCELED) {
100  assert(req->retcode == 0);
101  req->retcode = UV_EAI_CANCELED;
102  } else if (req->retcode == 0) {
103  host = req->host;
104  service = req->service;
105  }
106 
107  if (req->getnameinfo_cb)
108  req->getnameinfo_cb(req, req->retcode, host, service);
109 }
110 
111 
112 /*
113 * Entry point for getnameinfo
114 * return 0 if a callback will be made
115 * return error code if validation fails
116 */
119  uv_getnameinfo_cb getnameinfo_cb,
120  const struct sockaddr* addr,
121  int flags) {
122  if (req == NULL || addr == NULL)
123  return UV_EINVAL;
124 
125  if (addr->sa_family == AF_INET) {
126  memcpy(&req->storage,
127  addr,
128  sizeof(struct sockaddr_in));
129  } else if (addr->sa_family == AF_INET6) {
130  memcpy(&req->storage,
131  addr,
132  sizeof(struct sockaddr_in6));
133  } else {
134  return UV_EINVAL;
135  }
136 
137  UV_REQ_INIT(req, UV_GETNAMEINFO);
139 
140  req->getnameinfo_cb = getnameinfo_cb;
141  req->flags = flags;
142  req->loop = loop;
143  req->retcode = 0;
144 
145  if (getnameinfo_cb) {
147  &req->work_req,
151  return 0;
152  } else {
153  uv__getnameinfo_work(&req->work_req);
154  uv__getnameinfo_done(&req->work_req, 0);
155  return req->retcode;
156  }
157 }
#define ARRAY_SIZE(a)
#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
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
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
#define NI_MAXHOST
Definition: unix.h:77
#define NI_MAXSERV
Definition: unix.h:81
#define UV_REQ_INIT(req, typ)
Definition: uv-common.h:322
@ UV__WORK_SLOW_IO
Definition: uv-common.h:194
#define uv__req_register(loop, req)
Definition: uv-common.h:224
#define uv__req_unregister(loop, req)
Definition: uv-common.h:230
UV_EXTERN int uv_translate_sys_error(int sys_errno)
Definition: core.c:1249
void(* uv_getnameinfo_cb)(uv_getnameinfo_t *req, int status, const char *hostname, const char *service)
Definition: uv.h:334
static void uv__getnameinfo_work(struct uv__work *w)
Definition: getnameinfo.c:41
int WSAAPI GetNameInfoW(const SOCKADDR *pSockaddr, socklen_t SockaddrLength, PWCHAR pNodeBuffer, DWORD NodeBufferSize, PWCHAR pServiceBuffer, DWORD ServiceBufferSize, INT Flags)
static void uv__getnameinfo_done(struct uv__work *w, int status)
Definition: getnameinfo.c:90
DWORD
static int addr
Definition: z80asm.c:58