Skip to content

Commit e8ee6b1

Browse files
committed
wasi-sockets: add services table
1 parent 1b19fc6 commit e8ee6b1

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

expected/wasm32-wasip2/defined-symbols.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,13 @@ __wasi_sock_accept
301301
__wasi_sock_recv
302302
__wasi_sock_send
303303
__wasi_sock_shutdown
304+
__wasi_sockets_services_db
304305
__wasi_sockets_utils__any_addr
305306
__wasi_sockets_utils__borrow_network
306307
__wasi_sockets_utils__create_streams
307308
__wasi_sockets_utils__drop_streams
309+
__wasi_sockets_utils__get_service_entry_by_name
310+
__wasi_sockets_utils__get_service_entry_by_port
308311
__wasi_sockets_utils__map_error
309312
__wasi_sockets_utils__output_addr_validate
310313
__wasi_sockets_utils__output_addr_write

libc-bottom-half/headers/private/wasi/sockets_utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ typedef struct {
2626
};
2727
} output_sockaddr_t;
2828

29+
typedef struct {
30+
char *s_name;
31+
uint16_t port;
32+
uint16_t protocol;
33+
} service_entry_t;
34+
35+
typedef enum {
36+
SERVICE_PROTOCOL_TCP = 1,
37+
SERVICE_PROTOCOL_UDP = 2
38+
} service_protocol_e;
39+
2940
network_borrow_network_t __wasi_sockets_utils__borrow_network();
3041
int __wasi_sockets_utils__map_error(network_error_code_t wasi_error);
3142
bool __wasi_sockets_utils__parse_address(
@@ -50,5 +61,7 @@ bool __wasi_sockets_utils__stream(udp_socket_t *socket,
5061
network_error_code_t *error);
5162
void __wasi_sockets_utils__drop_streams(udp_socket_streams_t streams);
5263
int __wasi_sockets_utils__parse_port(const char *port);
64+
const service_entry_t *__wasi_sockets_utils__get_service_entry_by_name(const char *name);
65+
const service_entry_t *__wasi_sockets_utils__get_service_entry_by_port(const uint16_t port);
5366

5467
#endif

libc-bottom-half/sources/sockets_utils.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
#include <errno.h>
22
#include <stdlib.h>
33
#include <ctype.h>
4+
#include <string.h>
45

56
#include <wasi/sockets_utils.h>
67

78
static network_own_network_t global_network;
89
static bool global_network_initialized = false;
10+
static const service_entry_t global_services[] = {
11+
{ "domain", 53, SERVICE_PROTOCOL_TCP | SERVICE_PROTOCOL_UDP },
12+
{ "ftp", 21, SERVICE_PROTOCOL_TCP },
13+
{ "ftp-data", 20, SERVICE_PROTOCOL_TCP },
14+
{ "ftps", 990, SERVICE_PROTOCOL_TCP },
15+
{ "ftps-data", 989, SERVICE_PROTOCOL_TCP },
16+
{ "http", 80, SERVICE_PROTOCOL_TCP | SERVICE_PROTOCOL_UDP },
17+
{ "https", 443, SERVICE_PROTOCOL_TCP | SERVICE_PROTOCOL_UDP },
18+
{ "imap", 143, SERVICE_PROTOCOL_TCP },
19+
{ "imaps", 993, SERVICE_PROTOCOL_TCP },
20+
{ "ntp", 123, SERVICE_PROTOCOL_TCP },
21+
{ "pop3", 110, SERVICE_PROTOCOL_TCP },
22+
{ "pop3s", 995, SERVICE_PROTOCOL_TCP },
23+
{ "smtp", 25, SERVICE_PROTOCOL_TCP },
24+
{ "ssh", 22, SERVICE_PROTOCOL_TCP },
25+
{ "submission", 587, SERVICE_PROTOCOL_TCP },
26+
{ "submissions", 465, SERVICE_PROTOCOL_TCP },
27+
{ "telnet", 23, SERVICE_PROTOCOL_TCP },
28+
{ 0 },
29+
};
30+
weak_alias(global_services, __wasi_sockets_services_db);
931

1032
network_borrow_network_t __wasi_sockets_utils__borrow_network()
1133
{
@@ -482,3 +504,33 @@ int __wasi_sockets_utils__parse_port(const char *restrict port_str)
482504

483505
return (int)port;
484506
}
507+
508+
const service_entry_t *__wasi_sockets_utils__get_service_entry_by_name(const char *name)
509+
{
510+
if (!name) {
511+
return NULL;
512+
}
513+
514+
const service_entry_t *entry = __wasi_sockets_services_db;
515+
while(entry->s_name) {
516+
if (strcmp(name, entry->s_name) == 0) {
517+
return entry;
518+
}
519+
++entry;
520+
}
521+
522+
return NULL;
523+
}
524+
525+
const service_entry_t *__wasi_sockets_utils__get_service_entry_by_port(const uint16_t port)
526+
{
527+
const service_entry_t *entry = __wasi_sockets_services_db;
528+
while(entry->s_name) {
529+
if (entry->port == port) {
530+
return entry;
531+
}
532+
++entry;
533+
}
534+
535+
return NULL;
536+
}

0 commit comments

Comments
 (0)