Skip to content

Commit

Permalink
wasi-sockets: add service support for getaddrinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Henkru committed Sep 5, 2024
1 parent e8ee6b1 commit 6872c8e
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions libc-bottom-half/sources/netdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static int map_error(ip_name_lookup_error_code_t error)

static int add_addr(ip_name_lookup_option_ip_address_t address,
in_port_t port,
int socktype,
const struct addrinfo *restrict hint,
struct addrinfo **restrict current,
struct addrinfo **restrict res)
Expand Down Expand Up @@ -117,7 +118,7 @@ static int add_addr(ip_name_lookup_option_ip_address_t address,
*result = (struct addrinfo){
.ai_family = family,
.ai_flags = 0,
.ai_socktype = SOCK_STREAM,
.ai_socktype = socktype,
.ai_protocol = 0,
.ai_addrlen = addrlen,
.ai_addr = addr,
Expand Down Expand Up @@ -155,27 +156,39 @@ int getaddrinfo(const char *restrict host, const char *restrict serv,
ip_name_lookup_borrow_resolve_address_stream_t stream_borrow =
ip_name_lookup_borrow_resolve_address_stream(stream);
// The 'serv' parameter can be either a port number or a service name.
//
// TODO wasi-sockets: If the conversion of 'serv' to a valid port
// number fails, use getservbyname() to resolve the service name to
// its corresponding port number. This can be done after the
// getservbyname function is implemented.)
int port = 0;
uint16_t protocol = SERVICE_PROTOCOL_TCP;
if (serv != NULL) {
port = __wasi_sockets_utils__parse_port(serv);
if (port < 0) {
return EAI_NONAME;
const service_entry_t *service = __wasi_sockets_utils__get_service_entry_by_name(serv);
if (service) {
port = service->port;
protocol = service->protocol;
}
else {
return EAI_NONAME;
}
}
}
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, htons(port), hint,
&current, res);
if (error) {
return error;
if (protocol & SERVICE_PROTOCOL_TCP) {
int error = add_addr(address, htons(port), SOCK_STREAM,
hint, &current, res);
if (error) {
return error;
}
}
if (protocol & SERVICE_PROTOCOL_UDP) {
int error = add_addr(address, htons(port), SOCK_DGRAM,
hint, &current, res);
if (error) {
return error;
}
}
} else {
return 0;
Expand Down

0 comments on commit 6872c8e

Please sign in to comment.