Skip to content

Commit

Permalink
feat(*) wasm_socket unix domain socket support
Browse files Browse the repository at this point in the history
  • Loading branch information
casimiro committed Aug 11, 2023
1 parent d2da5e4 commit 254cd3b
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 68 deletions.
55 changes: 50 additions & 5 deletions src/common/ngx_wasm_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ ngx_wasm_socket_tcp_resume(ngx_wasm_socket_tcp_t *sock)

ngx_int_t
ngx_wasm_socket_tcp_init(ngx_wasm_socket_tcp_t *sock,
ngx_str_t *host, in_port_t port, unsigned tls,
ngx_wasm_subsys_env_t *env)
ngx_str_t *host, unsigned tls, ngx_wasm_subsys_env_t *env)
{
u_char *p, *last;
static ngx_str_t uds_prefix = ngx_string("unix:");

ngx_memzero(sock, sizeof(ngx_wasm_socket_tcp_t));

ngx_memcpy(&sock->env, env, sizeof(ngx_wasm_subsys_env_t));
Expand Down Expand Up @@ -165,7 +167,40 @@ ngx_wasm_socket_tcp_init(ngx_wasm_socket_tcp_t *sock,
sock->url.default_port = 80;
#endif
sock->url.url = sock->host;
sock->url.port = port;
sock->url.port = 0;

if (host->len > uds_prefix.len
&& ngx_memcmp(host->data, uds_prefix.data, uds_prefix.len) == 0)
{

#if (!NGX_HAVE_UNIX_DOMAIN)
ngx_wasm_log_error(NGX_LOG_ERR, sock->log, 0,
"host \"%V\" requires unix domain socket support",
host);
return NGX_ERROR;
#endif

} else {
/* extract port */

p = host->data;
last = p + host->len;

if (*p == '[') {
/* IPv6 */
p = ngx_strlchr(p, last, ']');
if (p == NULL) {
p = host->data;
}
}

p = ngx_strlchr(p, last, ':');

if (p) {
sock->url.port = ngx_atoi(p + 1, last - p);
}
}

sock->url.no_resolve = 1;

if (ngx_parse_url(sock->pool, &sock->url) != NGX_OK) {
Expand Down Expand Up @@ -208,6 +243,8 @@ ngx_wasm_socket_tcp_connect(ngx_wasm_socket_tcp_t *sock)
ngx_stream_session_t *s;
#endif

dd("enter");

if (sock->errlen) {
return NGX_ERROR;
}
Expand Down Expand Up @@ -284,7 +321,15 @@ ngx_wasm_socket_tcp_connect(ngx_wasm_socket_tcp_t *sock)
"wasm tcp socket no resolving: %V",
&sock->resolved.host);

return ngx_wasm_socket_tcp_connect_peer(sock);
rc = ngx_wasm_socket_tcp_connect_peer(sock);
if (sock->connected) {
#if (NGX_SSL)
if (sock->ssl_conf) {
return ngx_wasm_socket_tcp_ssl_handshake(sock);
}
#endif
}
return rc;
}

ngx_log_debug1(NGX_LOG_DEBUG_WASM, sock->log, 0,
Expand Down Expand Up @@ -1484,7 +1529,7 @@ ngx_wasm_socket_tcp_init_addr_text(ngx_peer_connection_t *pc)
break;
#endif

#if (NGX_HAVE_UNIX_DOMAIN && 0)
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
addr_text_max_len = NGX_UNIX_ADDRSTRLEN;
break;
Expand Down
3 changes: 1 addition & 2 deletions src/common/ngx_wasm_socket_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ struct ngx_wasm_socket_tcp_s {


ngx_int_t ngx_wasm_socket_tcp_init(ngx_wasm_socket_tcp_t *sock,
ngx_str_t *host, in_port_t port, unsigned tls,
ngx_wasm_subsys_env_t *env);
ngx_str_t *host, unsigned tls, ngx_wasm_subsys_env_t *env);
ngx_int_t ngx_wasm_socket_tcp_connect(ngx_wasm_socket_tcp_t *sock);
ngx_int_t ngx_wasm_socket_tcp_send(ngx_wasm_socket_tcp_t *sock,
ngx_chain_t *cl);
Expand Down
24 changes: 1 addition & 23 deletions src/http/proxy_wasm/ngx_http_proxy_wasm_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ ngx_http_proxy_wasm_dispatch(ngx_proxy_wasm_exec_t *pwexec,
{
static uint32_t callout_ids = 0;
size_t i;
in_port_t port = 0;
u_char *p, *last;
ngx_buf_t *buf;
ngx_event_t *ev;
ngx_table_elt_t *elts, *elt;
Expand Down Expand Up @@ -227,25 +225,6 @@ ngx_http_proxy_wasm_dispatch(ngx_proxy_wasm_exec_t *pwexec,
ngx_memcpy(call->host.data, host->data, host->len);
call->host.data[call->host.len] = '\0';

/* port */

p = host->data;
last = p + host->len;

if (*p == '[') {
/* IPv6 */
p = ngx_strlchr(p, last, ']');
if (p == NULL) {
p = host->data;
}
}

p = ngx_strlchr(p, last, ':');

if (p) {
port = ngx_atoi(p + 1, last - p);
}

/* headers/trailers */

if (ngx_proxy_wasm_pairs_unmarshal(pwexec, &call->headers, headers)
Expand Down Expand Up @@ -354,8 +333,7 @@ ngx_http_proxy_wasm_dispatch(ngx_proxy_wasm_exec_t *pwexec,

ngx_wasm_assert(rctx);

if (ngx_wasm_socket_tcp_init(sock, &call->host, port, enable_ssl,
&rctx->env)
if (ngx_wasm_socket_tcp_init(sock, &call->host, enable_ssl, &rctx->env)
!= NGX_OK)
{
dd("tcp init error");
Expand Down
Loading

0 comments on commit 254cd3b

Please sign in to comment.