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
|
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <wasi/sockets_utils.h>
_Thread_local int h_errno = 0;
static int map_error(ip_name_lookup_error_code_t error)
{
switch (error) {
case NETWORK_ERROR_CODE_OUT_OF_MEMORY:
return EAI_MEMORY;
case NETWORK_ERROR_CODE_NAME_UNRESOLVABLE:
return EAI_NONAME;
case NETWORK_ERROR_CODE_TEMPORARY_RESOLVER_FAILURE:
return EAI_AGAIN;
case NETWORK_ERROR_CODE_PERMANENT_RESOLVER_FAILURE:
return EAI_FAIL;
default:
errno = __wasi_sockets_utils__map_error(error);
return EAI_SYSTEM;
}
}
static int add_addr(ip_name_lookup_option_ip_address_t address,
const struct addrinfo *restrict hint,
struct addrinfo **restrict current,
struct addrinfo **restrict res)
{
int family;
struct sockaddr *addr;
socklen_t addrlen;
switch (address.val.tag) {
case NETWORK_IP_ADDRESS_IPV4: {
if (hint && hint->ai_family != AF_UNSPEC &&
hint->ai_family != AF_INET) {
return 0;
}
network_ipv4_address_t ip = address.val.val.ipv4;
family = PF_INET;
addrlen = sizeof(struct sockaddr_in);
addr = malloc(addrlen);
if (addr == NULL) {
freeaddrinfo(*res);
return EAI_MEMORY;
}
struct sockaddr_in sockaddr = {
.sin_family = AF_INET,
.sin_port = 0,
.sin_addr = { .s_addr = ip.f0 | (ip.f1 << 8) |
(ip.f2 << 16) | (ip.f3 << 24) },
};
memcpy(addr, &sockaddr, addrlen);
break;
}
case NETWORK_IP_ADDRESS_IPV6: {
if (hint && hint->ai_family != AF_UNSPEC &&
hint->ai_family != AF_INET6) {
return 0;
}
network_ipv6_address_t ip = address.val.val.ipv6;
family = PF_INET6;
addrlen = sizeof(struct sockaddr_in6);
addr = malloc(addrlen);
if (addr == NULL) {
freeaddrinfo(*res);
return EAI_MEMORY;
}
struct sockaddr_in6 sockaddr = {
.sin6_family = AF_INET6,
.sin6_port = 0,
.sin6_addr = {
.s6_addr = {
ip.f0 >> 8,
ip.f0 & 0xFF,
ip.f1 >> 8,
ip.f1 & 0xFF,
ip.f2 >> 8,
ip.f2 & 0xFF,
ip.f3 >> 8,
ip.f3 & 0xFF,
ip.f4 >> 8,
ip.f4 & 0xFF,
ip.f5 >> 8,
ip.f5 & 0xFF,
ip.f6 >> 8,
ip.f6 & 0xFF,
ip.f7 >> 8,
ip.f7 & 0xFF,
} },
.sin6_flowinfo = 0,
.sin6_scope_id = 0,
};
memcpy(addr, &sockaddr, addrlen);
break;
}
default: /* unreachable */
abort();
}
struct addrinfo *result = malloc(sizeof(struct addrinfo));
if (result == NULL) {
freeaddrinfo(*res);
return EAI_MEMORY;
}
*result = (struct addrinfo){
.ai_family = family,
.ai_flags = 0,
.ai_socktype = SOCK_STREAM,
.ai_protocol = 0,
.ai_addrlen = addrlen,
.ai_addr = addr,
.ai_canonname = NULL,
.ai_next = NULL,
};
if (*current) {
(*current)->ai_next = result;
*current = result;
} else {
*current = result;
*res = result;
}
return 0;
}
int getaddrinfo(const char *restrict host, const char *restrict serv,
const struct addrinfo *restrict hint,
struct addrinfo **restrict res)
{
if (host == NULL) {
host = "localhost";
}
*res = NULL;
struct addrinfo *current = NULL;
wasip2_string_t name = { .ptr = (uint8_t *)host, .len = strlen(host) };
ip_name_lookup_own_resolve_address_stream_t stream;
ip_name_lookup_error_code_t error;
if (ip_name_lookup_resolve_addresses(
__wasi_sockets_utils__borrow_network(), &name, &stream,
&error)) {
ip_name_lookup_borrow_resolve_address_stream_t stream_borrow =
ip_name_lookup_borrow_resolve_address_stream(stream);
while (true) {
ip_name_lookup_option_ip_address_t address;
if (ip_name_lookup_method_resolve_address_stream_resolve_next_address(
stream_borrow, &address, &error)) {
if (address.is_some) {
int error = add_addr(address, hint,
¤t, res);
if (error) {
return error;
}
} else {
return 0;
}
} else if (error == NETWORK_ERROR_CODE_WOULD_BLOCK) {
ip_name_lookup_own_pollable_t pollable =
ip_name_lookup_method_resolve_address_stream_subscribe(
stream_borrow);
poll_borrow_pollable_t pollable_borrow =
poll_borrow_pollable(pollable);
poll_method_pollable_block(pollable_borrow);
poll_pollable_drop_own(pollable);
} else {
freeaddrinfo(*res);
return map_error(error);
}
}
} else {
return map_error(error);
}
}
void freeaddrinfo(struct addrinfo *p)
{
while (p) {
struct addrinfo *next = p->ai_next;
free(p->ai_addr);
free(p);
p = next;
}
}
int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen,
char *restrict host, socklen_t hostlen, char *restrict serv,
socklen_t servlen, int flags)
{
// TODO wasi-sockets
abort();
}
struct hostent *gethostbyname(const char *name)
{
// TODO wasi-sockets
return NULL;
}
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type)
{
// TODO wasi-sockets
return NULL;
}
const char *hstrerror(int err)
{
// TODO wasi-sockets
return "hstrerror: TODO";
}
struct servent *getservbyname(const char *name, const char *proto)
{
// TODO wasi-sockets
return NULL;
}
struct servent *getservbyport(int port, const char *proto)
{
// TODO wasi-sockets
return NULL;
}
struct protoent *getprotobyname(const char *name)
{
// TODO wasi-sockets
return NULL;
}
|