Skip to content

Commit 6872c8e

Browse files
committed
wasi-sockets: add service support for getaddrinfo
1 parent e8ee6b1 commit 6872c8e

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

libc-bottom-half/sources/netdb.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static int map_error(ip_name_lookup_error_code_t error)
2727

2828
static int add_addr(ip_name_lookup_option_ip_address_t address,
2929
in_port_t port,
30+
int socktype,
3031
const struct addrinfo *restrict hint,
3132
struct addrinfo **restrict current,
3233
struct addrinfo **restrict res)
@@ -117,7 +118,7 @@ static int add_addr(ip_name_lookup_option_ip_address_t address,
117118
*result = (struct addrinfo){
118119
.ai_family = family,
119120
.ai_flags = 0,
120-
.ai_socktype = SOCK_STREAM,
121+
.ai_socktype = socktype,
121122
.ai_protocol = 0,
122123
.ai_addrlen = addrlen,
123124
.ai_addr = addr,
@@ -155,27 +156,39 @@ int getaddrinfo(const char *restrict host, const char *restrict serv,
155156
ip_name_lookup_borrow_resolve_address_stream_t stream_borrow =
156157
ip_name_lookup_borrow_resolve_address_stream(stream);
157158
// The 'serv' parameter can be either a port number or a service name.
158-
//
159-
// TODO wasi-sockets: If the conversion of 'serv' to a valid port
160-
// number fails, use getservbyname() to resolve the service name to
161-
// its corresponding port number. This can be done after the
162-
// getservbyname function is implemented.)
163159
int port = 0;
160+
uint16_t protocol = SERVICE_PROTOCOL_TCP;
164161
if (serv != NULL) {
165162
port = __wasi_sockets_utils__parse_port(serv);
166163
if (port < 0) {
167-
return EAI_NONAME;
164+
const service_entry_t *service = __wasi_sockets_utils__get_service_entry_by_name(serv);
165+
if (service) {
166+
port = service->port;
167+
protocol = service->protocol;
168+
}
169+
else {
170+
return EAI_NONAME;
171+
}
168172
}
169173
}
170174
while (true) {
171175
ip_name_lookup_option_ip_address_t address;
172176
if (ip_name_lookup_method_resolve_address_stream_resolve_next_address(
173177
stream_borrow, &address, &error)) {
174178
if (address.is_some) {
175-
int error = add_addr(address, htons(port), hint,
176-
&current, res);
177-
if (error) {
178-
return error;
179+
if (protocol & SERVICE_PROTOCOL_TCP) {
180+
int error = add_addr(address, htons(port), SOCK_STREAM,
181+
hint, &current, res);
182+
if (error) {
183+
return error;
184+
}
185+
}
186+
if (protocol & SERVICE_PROTOCOL_UDP) {
187+
int error = add_addr(address, htons(port), SOCK_DGRAM,
188+
hint, &current, res);
189+
if (error) {
190+
return error;
191+
}
179192
}
180193
} else {
181194
return 0;

0 commit comments

Comments
 (0)