@@ -27,6 +27,7 @@ static int map_error(ip_name_lookup_error_code_t error)
27
27
28
28
static int add_addr (ip_name_lookup_option_ip_address_t address ,
29
29
in_port_t port ,
30
+ int socktype ,
30
31
const struct addrinfo * restrict hint ,
31
32
struct addrinfo * * restrict current ,
32
33
struct addrinfo * * restrict res )
@@ -117,7 +118,7 @@ static int add_addr(ip_name_lookup_option_ip_address_t address,
117
118
* result = (struct addrinfo ){
118
119
.ai_family = family ,
119
120
.ai_flags = 0 ,
120
- .ai_socktype = SOCK_STREAM ,
121
+ .ai_socktype = socktype ,
121
122
.ai_protocol = 0 ,
122
123
.ai_addrlen = addrlen ,
123
124
.ai_addr = addr ,
@@ -155,27 +156,39 @@ int getaddrinfo(const char *restrict host, const char *restrict serv,
155
156
ip_name_lookup_borrow_resolve_address_stream_t stream_borrow =
156
157
ip_name_lookup_borrow_resolve_address_stream (stream );
157
158
// 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.)
163
159
int port = 0 ;
160
+ uint16_t protocol = SERVICE_PROTOCOL_TCP ;
164
161
if (serv != NULL ) {
165
162
port = __wasi_sockets_utils__parse_port (serv );
166
163
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
+ }
168
172
}
169
173
}
170
174
while (true) {
171
175
ip_name_lookup_option_ip_address_t address ;
172
176
if (ip_name_lookup_method_resolve_address_stream_resolve_next_address (
173
177
stream_borrow , & address , & error )) {
174
178
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
+ }
179
192
}
180
193
} else {
181
194
return 0 ;
0 commit comments