Skip to content

Commit

Permalink
refactor: migrate fds pos and evlib into desc
Browse files Browse the repository at this point in the history
We have to do several refactors to prepare for the ability for client connections
to have multiple wsis attempting the same connection via different paths /
dest addresses.

This starts to prepare for that by expending wsi->desc from being a simple
union to being a struct containing the union, moving the position_in_fds_table from
the wsi into the desc object.

I started off intending to take this further and have the desc usable as a
standalone object on the fds list, but considering event libs the amount of
churn will be much too huge.  So wsis remain the unit of currency for
connections.

As it is, this should be a NOP.
  • Loading branch information
lws-team committed May 4, 2022
1 parent 31ff36e commit 96a548c
Show file tree
Hide file tree
Showing 48 changed files with 244 additions and 240 deletions.
10 changes: 10 additions & 0 deletions include/libwebsockets/lws-adopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,21 @@ typedef enum {
LWS_ADOPT_RAW_SOCKET_UDP = LWS_ADOPT_SOCKET | LWS_ADOPT_FLAG_UDP,
} lws_adoption_type;

#define LWS_NO_FDS_POS ((int32_t)-1)

typedef union {
lws_sockfd_type sockfd;
lws_filefd_type filefd;
} lws_sock_file_fd_type;

typedef struct {
lws_sock_file_fd_type u;
#if defined(LWS_WITH_EVENT_LIBS)
void *evlib_desc; /* overallocated */
#endif
int32_t pos_in_fds_table;
} lws_desc_t;

#if defined(LWS_ESP_PLATFORM)
#include <lwip/sockets.h>
#endif
Expand Down
8 changes: 4 additions & 4 deletions lib/core-net/adopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ lws_adopt_ss_server_accept(struct lws *new_wsi)
h->policy = new_wsi->a.vhost->ss_handle->policy;

/* apply requested socket options */
if (lws_plat_set_socket_options_ip(new_wsi->desc.sockfd,
if (lws_plat_set_socket_options_ip(new_wsi->desc.u.sockfd,
h->policy->priority,
(LCCSCF_IP_LOW_LATENCY *
!!(h->policy->flags & LWSSSPOLF_ATTR_LOW_LATENCY)) |
Expand Down Expand Up @@ -373,7 +373,7 @@ lws_adopt_descriptor_vhost2(struct lws *new_wsi, lws_adoption_type type,
}
#endif

new_wsi->desc = fd;
new_wsi->desc.u = fd;

if (!LWS_SSL_ENABLED(new_wsi->a.vhost) ||
!(type & LWS_ADOPT_SOCKET))
Expand Down Expand Up @@ -579,7 +579,7 @@ adopt_socket_readbuf(struct lws *wsi, const char *readbuf, size_t len)
if (!readbuf || len == 0)
return wsi;

if (wsi->position_in_fds_table == LWS_NO_FDS_POS)
if (wsi->desc.pos_in_fds_table == LWS_NO_FDS_POS)
return wsi;

pt = &wsi->a.context->pt[(int)wsi->tsi];
Expand Down Expand Up @@ -611,7 +611,7 @@ adopt_socket_readbuf(struct lws *wsi, const char *readbuf, size_t len)
* libuv won't come back and service us without a network
* event, so we need to do the header service right here.
*/
pfd = &pt->fds[wsi->position_in_fds_table];
pfd = &pt->fds[wsi->desc.pos_in_fds_table];
pfd->revents |= LWS_POLLIN;
lwsl_err("%s: calling service\n", __func__);
if (lws_service_fd_tsi(wsi->a.context, pfd, wsi->tsi))
Expand Down
2 changes: 1 addition & 1 deletion lib/core-net/client/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ lws_client_connect_via_info(const struct lws_client_connect_info *i)

wsi->user_space = NULL;
wsi->pending_timeout = NO_PENDING_TIMEOUT;
wsi->position_in_fds_table = LWS_NO_FDS_POS;
wsi->desc.pos_in_fds_table = LWS_NO_FDS_POS;
wsi->ocport = wsi->c_port = (uint16_t)(unsigned int)i->port;
wsi->sys_tls_client_cert = i->sys_tls_client_cert;

Expand Down
42 changes: 21 additions & 21 deletions lib/core-net/client/connect3.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ lws_client_connect_check(struct lws *wsi, int *real_errno)
*/

#if !defined(WIN32)
if (!getsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_ERROR, &e, &sl)) {
if (!getsockopt(wsi->desc.u.sockfd, SOL_SOCKET, SO_ERROR, &e, &sl)) {

en = LWS_ERRNO;
if (!e) {
Expand All @@ -100,7 +100,7 @@ lws_client_connect_check(struct lws *wsi, int *real_errno)
return LCCCR_CONNECTED;
}

lwsl_wsi_notice(wsi, "getsockopt fd %d says %s", wsi->desc.sockfd,
lwsl_wsi_notice(wsi, "getsockopt fd %d says %s", wsi->desc.u.sockfd,
lws_errno_describe(e, t16, sizeof(t16)));

*real_errno = e;
Expand All @@ -110,7 +110,7 @@ lws_client_connect_check(struct lws *wsi, int *real_errno)

#else

if (!connect(wsi->desc.sockfd, (const struct sockaddr *)&wsi->sa46_peer.sa4, 0))
if (!connect(wsi->desc.u.sockfd, (const struct sockaddr *)&wsi->sa46_peer.sa4, 0))
return LCCCR_CONNECTED;

en = LWS_ERRNO;
Expand Down Expand Up @@ -208,8 +208,8 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,

if (n && /* calling back with a problem */
!wsi->dns_sorted_list.count && /* there's no results */
!lws_socket_is_valid(wsi->desc.sockfd) && /* no attempt ongoing */
!wsi->speculative_connect_owner.count /* no spec attempt */ ) {
!lws_socket_is_valid(wsi->desc.u.sockfd) /* no attempt ongoing */
) {
lwsl_wsi_notice(wsi, "dns lookup failed %d", n);

/*
Expand All @@ -234,7 +234,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
*/

if (lwsi_state(wsi) == LRS_WAITING_CONNECT &&
lws_socket_is_valid(wsi->desc.sockfd)) {
lws_socket_is_valid(wsi->desc.u.sockfd)) {
if (!wsi->dns_sorted_list.count &&
!wsi->sul_connect_timeout.list.owner)
/* no dns results and no ongoing timeout for one */
Expand Down Expand Up @@ -343,7 +343,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
* socket and add to the fds
*/

if (!lws_socket_is_valid(wsi->desc.sockfd)) {
if (!lws_socket_is_valid(wsi->desc.u.sockfd)) {

if (wsi->a.context->event_loop_ops->check_client_connect_ok &&
wsi->a.context->event_loop_ops->check_client_connect_ok(wsi)
Expand All @@ -356,17 +356,17 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
af = 0;
if (wsi->unix_skt) {
af = AF_UNIX;
wsi->desc.sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
wsi->desc.u.sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
}
else
#endif
{
af = wsi->sa46_peer.sa4.sin_family;
wsi->desc.sockfd = socket(wsi->sa46_peer.sa4.sin_family,
wsi->desc.u.sockfd = socket(wsi->sa46_peer.sa4.sin_family,
SOCK_STREAM, 0);
}

if (!lws_socket_is_valid(wsi->desc.sockfd)) {
if (!lws_socket_is_valid(wsi->desc.u.sockfd)) {
en = LWS_ERRNO;

lws_snprintf(dcce, sizeof(dcce),
Expand All @@ -377,7 +377,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
goto try_next_dns_result;
}

if (lws_plat_set_socket_options(wsi->a.vhost, wsi->desc.sockfd,
if (lws_plat_set_socket_options(wsi->a.vhost, wsi->desc.u.sockfd,
#if defined(LWS_WITH_UNIX_SOCK)
wsi->unix_skt)) {
#else
Expand All @@ -394,7 +394,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
}

/* apply requested socket options */
if (lws_plat_set_socket_options_ip(wsi->desc.sockfd,
if (lws_plat_set_socket_options_ip(wsi->desc.u.sockfd,
wsi->c_pri, wsi->flags))
lwsl_wsi_warn(wsi, "unable to set ip options");

Expand Down Expand Up @@ -451,7 +451,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
_WSI_TOKEN_CLIENT_LOCALPORT);

if ((iface && *iface) || (local_port && atoi(local_port))) {
m = lws_socket_bind(wsi->a.vhost, wsi, wsi->desc.sockfd,
m = lws_socket_bind(wsi->a.vhost, wsi, wsi->desc.u.sockfd,
(local_port ? atoi(local_port) : 0), iface, af);
if (m < 0) {
lws_snprintf(dcce, sizeof(dcce),
Expand Down Expand Up @@ -506,7 +506,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
if (lws_fi(&wsi->fic, "conn_cb_rej") ||
user_callback_handle_rxflow(wsi->a.protocol->callback, wsi,
LWS_CALLBACK_CONNECTING, wsi->user_space,
(void *)(intptr_t)wsi->desc.sockfd, 0)) {
(void *)(intptr_t)wsi->desc.u.sockfd, 0)) {
lwsl_wsi_info(wsi, "CONNECTION CB closed");
goto failed1;
}
Expand All @@ -524,7 +524,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
m = -1;
else
#endif
m = connect(wsi->desc.sockfd, (const struct sockaddr *)psa,
m = connect(wsi->desc.u.sockfd, (const struct sockaddr *)psa,
(socklen_t)n);

#if defined(LWS_WITH_CONMON)
Expand All @@ -549,7 +549,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
#endif

lwsl_wsi_debug(wsi, "connect: fd %d, %s",
wsi->desc.sockfd,
wsi->desc.u.sockfd,
lws_errno_describe(errno_copy, t16, sizeof(t16)));

if (errno_copy &&
Expand Down Expand Up @@ -649,7 +649,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
#if defined(_DEBUG)
char buf[64];
#endif
if (getsockname((int)wsi->desc.sockfd,
if (getsockname((int)wsi->desc.u.sockfd,
(struct sockaddr *)&wsi->sa46_local,
&salen) == -1) {
en = LWS_ERRNO;
Expand Down Expand Up @@ -692,7 +692,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
lws_inform_client_conn_fail(wsi,(void *)cce, strlen(cce));

/* take care that we might be inserted in fds already */
if (wsi->position_in_fds_table != LWS_NO_FDS_POS)
if (wsi->desc.pos_in_fds_table != LWS_NO_FDS_POS)
/* do the full wsi close flow */
goto failed1;

Expand All @@ -707,7 +707,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
*/
{
struct lws_vhost *vhost = wsi->a.vhost;
lws_sockfd_type sfd = wsi->desc.sockfd;
lws_sockfd_type sfd = wsi->desc.u.sockfd;

//lws_vhost_lock(vhost);
__lws_free_wsi(wsi); /* acquires vhost lock in wsi reset */
Expand All @@ -734,8 +734,8 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
/*
* We are killing the socket but leaving
*/
compatible_close(wsi->desc.sockfd);
wsi->desc.sockfd = LWS_SOCK_INVALID;
compatible_close(wsi->desc.u.sockfd);
wsi->desc.u.sockfd = LWS_SOCK_INVALID;

try_next_dns_result:
lws_sul_cancel(&wsi->sul_connect_timeout);
Expand Down
10 changes: 5 additions & 5 deletions lib/core-net/client/connect4.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ lws_client_connect_4_established(struct lws *wsi, struct lws *wsi_piggyback,
goto failed;
wsi->c_port = (uint16_t)wsi->a.vhost->http.http_proxy_port;

n = (int)send(wsi->desc.sockfd, (char *)pt->serv_buf,
n = (int)send(wsi->desc.u.sockfd, (char *)pt->serv_buf,
(unsigned int)plen,
MSG_NOSIGNAL);
if (n < 0) {
Expand Down Expand Up @@ -272,9 +272,9 @@ lws_client_connect_4_established(struct lws *wsi, struct lws *wsi_piggyback,
PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
(int)wsi->a.context->timeout_secs);

assert(lws_socket_is_valid(wsi->desc.sockfd));
assert(lws_socket_is_valid(wsi->desc.u.sockfd));

pfd.fd = wsi->desc.sockfd;
pfd.fd = wsi->desc.u.sockfd;
pfd.events = LWS_POLLIN;
pfd.revents = LWS_POLLOUT;

Expand Down Expand Up @@ -312,9 +312,9 @@ lws_client_connect_4_established(struct lws *wsi, struct lws *wsi_piggyback,
lws_set_timeout(wsi, PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
(int)wsi->a.context->timeout_secs);

assert(lws_socket_is_valid(wsi->desc.sockfd));
assert(lws_socket_is_valid(wsi->desc.u.sockfd));

pfd.fd = wsi->desc.sockfd;
pfd.fd = wsi->desc.u.sockfd;
pfd.events = LWS_POLLIN;
pfd.revents = LWS_POLLIN;

Expand Down
30 changes: 15 additions & 15 deletions lib/core-net/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,14 +561,14 @@ __lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason,
lws_rops_func_fidx(wsi->role_ops, LWS_ROPS_close_via_role_protocol).
close_via_role_protocol(wsi, reason)) {
lwsl_wsi_info(wsi, "close_via_role took over (sockfd %d)",
wsi->desc.sockfd);
wsi->desc.u.sockfd);
return;
}

just_kill_connection:

lwsl_wsi_debug(wsi, "real just_kill_connection A: (sockfd %d)",
wsi->desc.sockfd);
wsi->desc.u.sockfd);

#if defined(LWS_WITH_THREADPOOL) && defined(LWS_HAVE_PTHREAD_H)
lws_threadpool_wsi_closing(wsi);
Expand Down Expand Up @@ -671,12 +671,12 @@ __lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason,
#endif
{
lwsl_info("%s: shutdown conn: %s (sk %d, state 0x%x)\n",
__func__, lws_wsi_tag(wsi), (int)(lws_intptr_t)wsi->desc.sockfd,
__func__, lws_wsi_tag(wsi), (int)(lws_intptr_t)wsi->desc.u.sockfd,
lwsi_state(wsi));
if (!wsi->socket_is_permanently_unusable &&
lws_socket_is_valid(wsi->desc.sockfd)) {
lws_socket_is_valid(wsi->desc.u.sockfd)) {
wsi->socket_is_permanently_unusable = 1;
n = shutdown(wsi->desc.sockfd, SHUT_WR);
n = shutdown(wsi->desc.u.sockfd, SHUT_WR);
}
}
if (n)
Expand All @@ -693,7 +693,7 @@ __lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason,
#if defined(LWS_WITH_CLIENT)
!wsi->close_is_redirect &&
#endif
lws_socket_is_valid(wsi->desc.sockfd) &&
lws_socket_is_valid(wsi->desc.u.sockfd) &&
lwsi_state(wsi) != LRS_SHUTDOWN &&
(context->event_loop_ops->flags & LELOF_ISPOLL)) {
__lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
Expand All @@ -707,7 +707,7 @@ __lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason,
}

lwsl_wsi_info(wsi, "real just_kill_connection: sockfd %d\n",
wsi->desc.sockfd);
wsi->desc.u.sockfd);

#ifdef LWS_WITH_HUBBUB
if (wsi->http.rw) {
Expand Down Expand Up @@ -888,21 +888,21 @@ __lws_close_free_wsi_final(struct lws *wsi)
int n;

if (!wsi->shadow &&
lws_socket_is_valid(wsi->desc.sockfd) && !lws_ssl_close(wsi)) {
lwsl_wsi_debug(wsi, "fd %d", wsi->desc.sockfd);
n = compatible_close(wsi->desc.sockfd);
lws_socket_is_valid(wsi->desc.u.sockfd) && !lws_ssl_close(wsi)) {
lwsl_wsi_debug(wsi, "fd %d", wsi->desc.u.sockfd);
n = compatible_close(wsi->desc.u.sockfd);
if (n)
lwsl_wsi_debug(wsi, "closing: close ret %d", LWS_ERRNO);

__remove_wsi_socket_from_fds(wsi);
if (lws_socket_is_valid(wsi->desc.sockfd))
delete_from_fd(wsi->a.context, wsi->desc.sockfd);
if (lws_socket_is_valid(wsi->desc.u.sockfd))
delete_from_fd(wsi->a.context, wsi->desc.u.sockfd);

#if !defined(LWS_PLAT_FREERTOS) && !defined(WIN32) && !defined(LWS_PLAT_OPTEE)
delete_from_fdwsi(wsi->a.context, wsi);
#endif

sanity_assert_no_sockfd_traces(wsi->a.context, wsi->desc.sockfd);
sanity_assert_no_sockfd_traces(wsi->a.context, wsi->desc.u.sockfd);
}

/* ... if we're closing the cancel pipe, account for it */
Expand All @@ -913,11 +913,11 @@ __lws_close_free_wsi_final(struct lws *wsi)

if (pt->pipe_wsi == wsi)
pt->pipe_wsi = NULL;
if (pt->dummy_pipe_fds[0] == wsi->desc.sockfd)
if (pt->dummy_pipe_fds[0] == wsi->desc.u.sockfd)
pt->dummy_pipe_fds[0] = LWS_SOCK_INVALID;
}

wsi->desc.sockfd = LWS_SOCK_INVALID;
wsi->desc.u.sockfd = LWS_SOCK_INVALID;

#if defined(LWS_WITH_CLIENT)
lws_free_set_NULL(wsi->cli_hostname_copy);
Expand Down
6 changes: 3 additions & 3 deletions lib/core-net/dummy-callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ lws_callback_http_dummy(struct lws *wsi, enum lws_callback_reasons reason,
sizeof(wsi->http.cgi->inflate_buf)) {
int written;

written = (int)write(args->stdwsi[LWS_STDIN]->desc.filefd,
written = (int)write(args->stdwsi[LWS_STDIN]->desc.u.filefd,
wsi->http.cgi->inflate_buf,
sizeof(wsi->http.cgi->inflate_buf) -
wsi->http.cgi->inflate.avail_out);
Expand Down Expand Up @@ -808,7 +808,7 @@ lws_callback_http_dummy(struct lws *wsi, enum lws_callback_reasons reason,
lwsl_wsi_info(wsi, "proxied %d bytes", n);

if (wsi->http.cgi->post_in_expected && args->stdwsi[LWS_STDIN] &&
args->stdwsi[LWS_STDIN]->desc.filefd > 0) {
args->stdwsi[LWS_STDIN]->desc.u.filefd > 0) {
wsi->http.cgi->post_in_expected -= (unsigned int)n;

if (!wsi->http.cgi->post_in_expected) {
Expand All @@ -824,7 +824,7 @@ lws_callback_http_dummy(struct lws *wsi, enum lws_callback_reasons reason,

lwsl_wsi_info(siwsi, "expected POST in end: "
"closing stdin fd %d",
siwsi->desc.sockfd);
siwsi->desc.u.sockfd);

/*
* We don't want the child / parent relationship
Expand Down
2 changes: 1 addition & 1 deletion lib/core-net/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const char *
lws_get_peer_simple(struct lws *wsi, char *name, size_t namelen)
{
wsi = lws_get_network_wsi(wsi);
return lws_get_peer_simple_fd(wsi->desc.sockfd, name, namelen);
return lws_get_peer_simple_fd(wsi->desc.u.sockfd, name, namelen);
}
#endif

Expand Down
Loading

0 comments on commit 96a548c

Please sign in to comment.