1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/* $OpenBSD: getnameinfo_async.c,v 1.4 2012/08/19 16:17:40 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "asr.h"
#include "asr_private.h"
static int getnameinfo_async_run(struct async *, struct async_res *);
static int _servname(struct async *);
static int _numerichost(struct async *);
struct async *
getnameinfo_async(const struct sockaddr *sa, socklen_t slen, char *host,
size_t hostlen, char *serv, size_t servlen, int flags, struct asr *asr)
{
struct asr_ctx *ac;
struct async *as;
ac = asr_use_resolver(asr);
if ((as = async_new(ac, ASR_GETNAMEINFO)) == NULL)
goto abort; /* errno set */
as->as_run = getnameinfo_async_run;
if (sa->sa_family == AF_INET)
memmove(&as->as.ni.sa.sa, sa, sizeof (as->as.ni.sa.sain));
else if (sa->sa_family == AF_INET6)
memmove(&as->as.ni.sa.sa, sa, sizeof (as->as.ni.sa.sain6));
as->as.ni.sa.sa.sa_len = slen;
as->as.ni.hostname = host;
as->as.ni.hostnamelen = hostlen;
as->as.ni.servname = serv;
as->as.ni.servnamelen = servlen;
as->as.ni.flags = flags;
asr_ctx_unref(ac);
return (as);
abort:
if (as)
async_free(as);
asr_ctx_unref(ac);
return (NULL);
}
static int
getnameinfo_async_run(struct async *as, struct async_res *ar)
{
void *addr;
socklen_t addrlen;
int r;
next:
switch(as->as_state) {
case ASR_STATE_INIT:
/* Make sure the parameters are all valid. */
if (as->as.ni.sa.sa.sa_family != AF_INET &&
as->as.ni.sa.sa.sa_family != AF_INET6) {
ar->ar_gai_errno = EAI_FAMILY;
async_set_state(as, ASR_STATE_HALT);
break;
}
if ((as->as.ni.sa.sa.sa_family == AF_INET &&
(as->as.ni.sa.sa.sa_len != sizeof (as->as.ni.sa.sain))) ||
(as->as.ni.sa.sa.sa_family == AF_INET6 &&
(as->as.ni.sa.sa.sa_len != sizeof (as->as.ni.sa.sain6)))) {
ar->ar_gai_errno = EAI_FAIL;
async_set_state(as, ASR_STATE_HALT);
break;
}
/* Set the service name first, if needed. */
if (_servname(as) == -1) {
ar->ar_gai_errno = EAI_OVERFLOW;
async_set_state(as, ASR_STATE_HALT);
break;
}
if (as->as.ni.hostname == NULL || as->as.ni.hostnamelen == 0) {
ar->ar_gai_errno = 0;
async_set_state(as, ASR_STATE_HALT);
break;
}
if (as->as.ni.flags & NI_NUMERICHOST) {
if (_numerichost(as) == -1) {
if (errno == ENOMEM)
ar->ar_gai_errno = EAI_MEMORY;
else if (errno == ENOSPC)
ar->ar_gai_errno = EAI_OVERFLOW;
else {
ar->ar_errno = errno;
ar->ar_gai_errno = EAI_SYSTEM;
}
} else
ar->ar_gai_errno = 0;
async_set_state(as, ASR_STATE_HALT);
break;
}
if (as->as.ni.sa.sa.sa_family == AF_INET) {
addrlen = sizeof(as->as.ni.sa.sain.sin_addr);
addr = &as->as.ni.sa.sain.sin_addr;
} else {
addrlen = sizeof(as->as.ni.sa.sain6.sin6_addr);
addr = &as->as.ni.sa.sain6.sin6_addr;
}
/*
* Create a subquery to lookup the address.
*/
as->as.ni.subq = gethostbyaddr_async_ctx(addr, addrlen,
as->as.ni.sa.sa.sa_family,
as->as_ctx);
if (as->as.ni.subq == NULL) {
ar->ar_gai_errno = EAI_MEMORY;
async_set_state(as, ASR_STATE_HALT);
break;
}
async_set_state(as, ASR_STATE_SUBQUERY);
break;
case ASR_STATE_SUBQUERY:
if ((r = async_run(as->as.ni.subq, ar)) == ASYNC_COND)
return (ASYNC_COND);
/*
* Request done.
*/
as->as.ni.subq = NULL;
if (ar->ar_hostent == NULL) {
if (as->as.ni.flags & NI_NAMEREQD) {
ar->ar_gai_errno = EAI_NONAME;
} else if (_numerichost(as) == -1) {
if (errno == ENOMEM)
ar->ar_gai_errno = EAI_MEMORY;
else if (errno == ENOSPC)
ar->ar_gai_errno = EAI_OVERFLOW;
else {
ar->ar_errno = errno;
ar->ar_gai_errno = EAI_SYSTEM;
}
} else
ar->ar_gai_errno = 0;
} else {
if (strlcpy(as->as.ni.hostname,
ar->ar_hostent->h_name,
as->as.ni.hostnamelen) >= as->as.ni.hostnamelen)
ar->ar_gai_errno = EAI_OVERFLOW;
else
ar->ar_gai_errno = 0;
free(ar->ar_hostent);
}
async_set_state(as, ASR_STATE_HALT);
break;
case ASR_STATE_HALT:
return (ASYNC_DONE);
default:
ar->ar_errno = EOPNOTSUPP;
ar->ar_gai_errno = EAI_SYSTEM;
async_set_state(as, ASR_STATE_HALT);
break;
}
goto next;
}
/*
* Set the service name on the result buffer is not NULL.
* return (-1) if the buffer is too small.
*/
static int
_servname(struct async *as)
{
struct servent s;
struct servent_data sd;
int port, r;
char *buf = as->as.ni.servname;
size_t buflen = as->as.ni.servnamelen;
if (as->as.ni.servname == NULL || as->as.ni.servnamelen == 0)
return (0);
if (as->as.ni.sa.sa.sa_family == AF_INET)
port = as->as.ni.sa.sain.sin_port;
else
port = as->as.ni.sa.sain6.sin6_port;
if (!(as->as.ni.flags & NI_NUMERICSERV)) {
memset(&sd, 0, sizeof (sd));
if (getservbyport_r(port,
(as->as.ni.flags & NI_DGRAM) ? "udp" : "tcp",
&s, &sd) != -1) {
r = strlcpy(buf, s.s_name, buflen) >= buflen;
endservent_r(&sd);
return (r ? -1 : 0);
}
}
r = snprintf(buf, buflen, "%u", ntohs(port));
if (r == -1 || r >= (int)buflen)
return (-1);
return (0);
}
/*
* Write the numeric address
*/
static int
_numerichost(struct async *as)
{
void *addr;
char *buf = as->as.ni.hostname;
size_t buflen = as->as.ni.hostnamelen;
if (as->as.ni.sa.sa.sa_family == AF_INET)
addr = &as->as.ni.sa.sain.sin_addr;
else
addr = &as->as.ni.sa.sain6.sin6_addr;
if (inet_ntop(as->as.ni.sa.sa.sa_family, addr, buf, buflen) == NULL)
/* errno set */
return (-1);
return (0);
}
|