Update Files
This commit is contained in:
@ -0,0 +1,853 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2019 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
*
|
||||
* Serialized Secure Streams deserializer for Proxy side
|
||||
*/
|
||||
|
||||
#include <private-lib-core.h>
|
||||
|
||||
/*
|
||||
* event loop is consuming dsh-buffered, already-serialized tx from the
|
||||
* foreign side
|
||||
*/
|
||||
|
||||
int
|
||||
lws_ss_deserialize_tx_payload(struct lws_dsh *dsh, struct lws *wsi,
|
||||
lws_ss_tx_ordinal_t ord, uint8_t *buf,
|
||||
size_t *len, int *flags)
|
||||
{
|
||||
uint8_t *p;
|
||||
size_t si;
|
||||
|
||||
if (lws_dsh_get_head(dsh, KIND_C_TO_P, (void **)&p, &si)) {
|
||||
*len = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The packet in the dsh has a proxying serialization header, process
|
||||
* and strip it so we just forward the payload
|
||||
*/
|
||||
|
||||
if (*len <= si - 23 || si < 23) {
|
||||
/*
|
||||
* What comes out of the dsh needs to fit in the tx buffer...
|
||||
* we have arrangements at the proxy rx of the client UDS to
|
||||
* chop chunks larger than 1380 into seuqential lumps of 1380
|
||||
*/
|
||||
lwsl_err("%s: *len = %d, si = %d\n", __func__, (int)*len, (int)si);
|
||||
assert(0);
|
||||
return 1;
|
||||
}
|
||||
if (p[0] != LWSSS_SER_TXPRE_TX_PAYLOAD) {
|
||||
assert(0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
*len = (size_t)(lws_ser_ru16be(&p[1]) - (23 - 3));
|
||||
if (*len != si - 23) {
|
||||
/*
|
||||
* We cannot accept any length that doesn't reflect the actual
|
||||
* length of what came in from the dsh, either something nasty
|
||||
* happened with truncation or we are being attacked
|
||||
*/
|
||||
assert(0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
memcpy(buf, p + 23, si - 23);
|
||||
|
||||
*flags = (int)lws_ser_ru32be(&p[3]);
|
||||
|
||||
lws_dsh_free((void **)&p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* event loop side is consuming serialized data from the client via dsh, parse
|
||||
* it using a bytewise parser for the serialization header(s)...
|
||||
* it's possibly coalesced
|
||||
*
|
||||
* client: pss is pointing to the start of userdata. We can use
|
||||
* pss_to_sspc_h(_pss, _ssi) to convert that to a pointer to the sspc
|
||||
* handle
|
||||
*
|
||||
* proxy: pss is pointing to &conn->ss, a pointer to the ss handle
|
||||
*
|
||||
* Returns one of
|
||||
*
|
||||
* LWSSSSRET_OK
|
||||
* LWSSSSRET_DISCONNECT_ME
|
||||
* LWSSSSRET_DESTROY_ME
|
||||
*/
|
||||
|
||||
/* convert userdata ptr _pss to handle pointer, allowing for any layout in
|
||||
* userdata */
|
||||
#define client_pss_to_sspc_h(_pss, _ssi) (*((lws_sspc_handle_t **) \
|
||||
((uint8_t *)_pss) + _ssi->handle_offset))
|
||||
/* client pss to sspc userdata */
|
||||
#define client_pss_to_userdata(_pss) ((void *)_pss)
|
||||
/* proxy convert pss to ss handle */
|
||||
#define proxy_pss_to_ss_h(_pss) (*_pss)
|
||||
|
||||
/* convert userdata ptr _pss to handle pointer, allowing for any layout in
|
||||
* userdata */
|
||||
#define client_pss_to_sspc_h(_pss, _ssi) (*((lws_sspc_handle_t **) \
|
||||
((uint8_t *)_pss) + _ssi->handle_offset))
|
||||
/* client pss to sspc userdata */
|
||||
#define client_pss_to_userdata(_pss) ((void *)_pss)
|
||||
/* proxy convert pss to ss handle */
|
||||
#define proxy_pss_to_ss_h(_pss) (*_pss)
|
||||
|
||||
int
|
||||
lws_ss_proxy_deserialize_parse(struct lws_ss_serialization_parser *par,
|
||||
struct lws_context *context,
|
||||
struct lws_dsh *dsh, const uint8_t *cp,
|
||||
size_t len, lws_ss_conn_states_t *state,
|
||||
void *parconn, lws_ss_handle_t **pss,
|
||||
lws_ss_info_t *ssi)
|
||||
{
|
||||
lws_ss_state_return_t r;
|
||||
lws_ss_metadata_t *pm;
|
||||
uint8_t pre[23];
|
||||
uint32_t flags;
|
||||
lws_usec_t us;
|
||||
uint8_t *p;
|
||||
int n;
|
||||
|
||||
while (len--) {
|
||||
|
||||
switch (par->ps) {
|
||||
case RPAR_TYPE:
|
||||
par->type = *cp++;
|
||||
par->ps++;
|
||||
break;
|
||||
|
||||
case RPAR_LEN_MSB: /* this is remaining frame length */
|
||||
par->rem = (uint16_t)((*cp++) << 8);
|
||||
par->ps++;
|
||||
break;
|
||||
|
||||
case RPAR_LEN_LSB:
|
||||
par->rem = (uint16_t)(par->rem | *cp++);
|
||||
switch (par->type) {
|
||||
|
||||
/* event loop side */
|
||||
|
||||
case LWSSS_SER_TXPRE_TX_PAYLOAD:
|
||||
|
||||
if (*state != LPCSPROX_OPERATIONAL)
|
||||
goto hangup;
|
||||
|
||||
par->ps = RPAR_FLAG_B3;
|
||||
break;
|
||||
|
||||
case LWSSS_SER_TXPRE_DESTROYING:
|
||||
|
||||
par->ps = RPAR_TYPE;
|
||||
lwsl_cx_notice(context, "DESTROYING");
|
||||
goto hangup;
|
||||
|
||||
case LWSSS_SER_TXPRE_ONWARD_CONNECT:
|
||||
|
||||
|
||||
if (*state != LPCSPROX_OPERATIONAL)
|
||||
goto hangup;
|
||||
|
||||
par->ps = RPAR_TYPE;
|
||||
lwsl_cx_notice(context, "ONWARD_CONNECT");
|
||||
|
||||
/*
|
||||
* Shrug it off if we are already connecting or
|
||||
* connected
|
||||
*/
|
||||
|
||||
if (!proxy_pss_to_ss_h(pss) ||
|
||||
proxy_pss_to_ss_h(pss)->wsi)
|
||||
break;
|
||||
|
||||
/*
|
||||
* We're going to try to do the onward connect
|
||||
*/
|
||||
|
||||
if ((proxy_pss_to_ss_h(pss) &&
|
||||
lws_fi(&proxy_pss_to_ss_h(pss)->fic,
|
||||
"ssproxy_onward_conn_fail")) ||
|
||||
_lws_ss_client_connect(proxy_pss_to_ss_h(pss),
|
||||
0, parconn) ==
|
||||
LWSSSSRET_DESTROY_ME)
|
||||
goto hangup;
|
||||
break;
|
||||
|
||||
case LWSSS_SER_TXPRE_STREAMTYPE:
|
||||
|
||||
if (*state != LPCSPROX_WAIT_INITIAL_TX)
|
||||
goto hangup;
|
||||
if (par->rem < 1 + 4 + 1)
|
||||
goto hangup;
|
||||
par->ps = RPAR_INIT_PROVERS;
|
||||
break;
|
||||
|
||||
case LWSSS_SER_TXPRE_METADATA:
|
||||
|
||||
if (par->rem < 3)
|
||||
goto hangup;
|
||||
par->ctr = 0;
|
||||
par->ps = RPAR_METADATA_NAMELEN;
|
||||
break;
|
||||
|
||||
case LWSSS_SER_TXPRE_TXCR_UPDATE:
|
||||
par->ps = RPAR_TXCR0;
|
||||
par->ctr = 0;
|
||||
break;
|
||||
|
||||
case LWSSS_SER_TXPRE_TIMEOUT_UPDATE:
|
||||
|
||||
if (par->rem != 4)
|
||||
goto hangup;
|
||||
par->ps = RPAR_TIMEOUT0;
|
||||
par->ctr = 0;
|
||||
break;
|
||||
|
||||
case LWSSS_SER_TXPRE_PAYLOAD_LENGTH_HINT:
|
||||
|
||||
if (par->rem != 4)
|
||||
goto hangup;
|
||||
par->ps = RPAR_PAYLEN0;
|
||||
par->ctr = 0;
|
||||
break;
|
||||
|
||||
/* client side */
|
||||
|
||||
case LWSSS_SER_RXPRE_RX_PAYLOAD:
|
||||
case LWSSS_SER_RXPRE_CREATE_RESULT:
|
||||
case LWSSS_SER_RXPRE_CONNSTATE:
|
||||
case LWSSS_SER_RXPRE_METADATA:
|
||||
goto hangup;
|
||||
|
||||
case LWSSS_SER_RXPRE_TXCR_UPDATE:
|
||||
par->ctr = 0;
|
||||
par->ps = RPAR_RX_TXCR_UPDATE;
|
||||
break;
|
||||
|
||||
case LWSSS_SER_RXPRE_PERF:
|
||||
par->ctr = 0;
|
||||
if (!par->rem)
|
||||
goto hangup;
|
||||
par->ps = RPAR_PERF;
|
||||
break;
|
||||
|
||||
default:
|
||||
lwsl_cx_notice(context, "bad type 0x%x",
|
||||
par->type);
|
||||
goto hangup;
|
||||
}
|
||||
break;
|
||||
|
||||
case RPAR_FLAG_B3:
|
||||
case RPAR_FLAG_B2:
|
||||
case RPAR_FLAG_B1:
|
||||
case RPAR_FLAG_B0:
|
||||
par->flags <<= 8;
|
||||
par->flags |= *cp++;
|
||||
par->ps++;
|
||||
if (!par->rem--)
|
||||
goto hangup;
|
||||
break;
|
||||
|
||||
case RPAR_LATA3:
|
||||
case RPAR_LATA2:
|
||||
case RPAR_LATA1:
|
||||
case RPAR_LATA0:
|
||||
par->usd_phandling <<= 8;
|
||||
par->usd_phandling |= *cp++;
|
||||
par->ps++;
|
||||
if (!par->rem--)
|
||||
goto hangup;
|
||||
break;
|
||||
|
||||
case RPAR_LATB7:
|
||||
case RPAR_LATB6:
|
||||
case RPAR_LATB5:
|
||||
case RPAR_LATB4:
|
||||
case RPAR_LATB3:
|
||||
case RPAR_LATB2:
|
||||
case RPAR_LATB1:
|
||||
case RPAR_LATB0:
|
||||
par->ust_pwait <<= 8;
|
||||
par->ust_pwait |= *cp++;
|
||||
par->ps++;
|
||||
par->frag1 = 1;
|
||||
if (!par->rem--)
|
||||
goto hangup;
|
||||
|
||||
if (par->ps == RPAR_RIDESHARE_LEN &&
|
||||
!(par->flags & LWSSS_FLAG_RIDESHARE))
|
||||
par->ps = RPAR_PAYLOAD;
|
||||
|
||||
if (par->rem)
|
||||
break;
|
||||
|
||||
/* fallthru - handle 0-length payload */
|
||||
|
||||
if (!(par->flags & LWSSS_FLAG_RIDESHARE))
|
||||
goto payload_ff;
|
||||
goto hangup;
|
||||
|
||||
/*
|
||||
* Inbound rideshare info is provided on the RX packet
|
||||
* itself
|
||||
*/
|
||||
|
||||
case RPAR_RIDESHARE_LEN:
|
||||
par->slen = *cp++;
|
||||
par->ctr = 0;
|
||||
par->ps++;
|
||||
if (par->rem-- < par->slen)
|
||||
goto hangup;
|
||||
break;
|
||||
|
||||
case RPAR_PERF:
|
||||
n = (int)len + 1;
|
||||
if (n > par->rem)
|
||||
n = par->rem;
|
||||
|
||||
if (n) {
|
||||
cp += n;
|
||||
par->rem = (uint16_t)(par->rem -
|
||||
(uint16_t)(unsigned int)n);
|
||||
len = (len + 1) - (unsigned int)n;
|
||||
}
|
||||
if (!par->rem)
|
||||
par->ps = RPAR_TYPE;
|
||||
break;
|
||||
|
||||
case RPAR_RIDESHARE:
|
||||
par->rideshare[par->ctr++] = (char)*cp++;
|
||||
if (!par->rem--)
|
||||
goto hangup;
|
||||
if (par->ctr != par->slen)
|
||||
break;
|
||||
par->ps = RPAR_PAYLOAD;
|
||||
if (par->rem)
|
||||
break;
|
||||
|
||||
/* fallthru - handle 0-length payload */
|
||||
|
||||
case RPAR_PAYLOAD:
|
||||
payload_ff:
|
||||
n = (int)len + 1;
|
||||
if (n > par->rem)
|
||||
n = par->rem;
|
||||
/*
|
||||
* We get called with a serialized buffer of a size
|
||||
* chosen by the client. We can only create dsh entries
|
||||
* with up to 1380 payload, to guarantee we can emit
|
||||
* them on the onward connection atomically.
|
||||
*
|
||||
* If 1380 isn't enough to cover what was handed to us,
|
||||
* we'll stop at 1380 and go around again and create
|
||||
* more dsh entries for the rest, with their own
|
||||
* headers.
|
||||
*/
|
||||
|
||||
if (n > 1380)
|
||||
n = 1380;
|
||||
|
||||
/*
|
||||
* Since we're in the business of fragmenting client
|
||||
* serialized payloads at 1380, we have to deal with
|
||||
* refragmenting the SOM / EOM flags that covered the
|
||||
* whole client serialized packet, so they apply to
|
||||
* each dsh entry we split it into correctly
|
||||
*/
|
||||
|
||||
flags = par->flags & LWSSS_FLAG_RELATED_START;
|
||||
if (par->frag1)
|
||||
/*
|
||||
* Only set the first time we came to this
|
||||
* state after deserialization of the header
|
||||
*/
|
||||
flags |= par->flags &
|
||||
(LWSSS_FLAG_SOM | LWSSS_FLAG_POLL);
|
||||
|
||||
if (par->rem == n)
|
||||
/*
|
||||
* We are going to complete the advertised
|
||||
* payload length from the client on this dsh,
|
||||
* so give him the EOM type flags if any
|
||||
*/
|
||||
flags |= par->flags & (LWSSS_FLAG_EOM |
|
||||
LWSSS_FLAG_RELATED_END);
|
||||
|
||||
par->frag1 = 0;
|
||||
us = lws_now_usecs();
|
||||
|
||||
{
|
||||
lws_ss_handle_t *hss;
|
||||
|
||||
/*
|
||||
* Proxy - we received some serialized tx from
|
||||
* the client.
|
||||
*
|
||||
* The header for buffering private to the
|
||||
* proxy is 23 bytes vs 19, so we can hold the
|
||||
* current time when it was buffered
|
||||
* additionally
|
||||
*/
|
||||
|
||||
hss = proxy_pss_to_ss_h(pss);
|
||||
if (hss)
|
||||
lwsl_ss_info(hss, "C2P RX: len %d", (int)n);
|
||||
|
||||
p = pre;
|
||||
pre[0] = LWSSS_SER_TXPRE_TX_PAYLOAD;
|
||||
lws_ser_wu16be(&p[1], (uint16_t)((unsigned int)n + 23 - 3));
|
||||
lws_ser_wu32be(&p[3], flags);
|
||||
/* us held at client before written */
|
||||
lws_ser_wu32be(&p[7], par->usd_phandling);
|
||||
/* us taken for transit to proxy */
|
||||
lws_ser_wu32be(&p[11], (uint32_t)(us -
|
||||
(lws_usec_t)par->ust_pwait));
|
||||
/* time used later to find proxy hold time */
|
||||
lws_ser_wu64be(&p[15], (uint64_t)us);
|
||||
|
||||
if ((hss &&
|
||||
lws_fi(&hss->fic, "ssproxy_dsh_c2p_pay_oom")) ||
|
||||
lws_dsh_alloc_tail(dsh, KIND_C_TO_P, pre,
|
||||
23, cp, (unsigned int)n)) {
|
||||
lwsl_ss_err(hss, "unable to alloc in dsh 3");
|
||||
|
||||
return LWSSSSRET_DISCONNECT_ME;
|
||||
}
|
||||
|
||||
lwsl_notice("%s: dsh c2p %d, p2c %d\n", __func__,
|
||||
(int)lws_dsh_get_size(dsh, KIND_C_TO_P),
|
||||
(int)lws_dsh_get_size(dsh, 1));
|
||||
|
||||
if (hss)
|
||||
_lws_ss_request_tx(hss);
|
||||
}
|
||||
|
||||
if (n) {
|
||||
cp += n;
|
||||
par->rem = (uint16_t)(par->rem -
|
||||
(uint16_t)(unsigned int)n);
|
||||
len = (len + 1) - (unsigned int)n;
|
||||
/*
|
||||
* if we didn't consume it all, we'll come
|
||||
* around again and produce more dsh entries up
|
||||
* to 1380 each until it is gone
|
||||
*/
|
||||
}
|
||||
if (!par->rem)
|
||||
par->ps = RPAR_TYPE;
|
||||
break;
|
||||
|
||||
case RPAR_RX_TXCR_UPDATE:
|
||||
goto hangup;
|
||||
|
||||
case RPAR_INIT_PROVERS:
|
||||
/* Protocol version byte for this connection */
|
||||
par->protocol_version = *cp++;
|
||||
|
||||
/*
|
||||
* So we have to know what versions of the serialization
|
||||
* protocol we can support at the proxy side, and
|
||||
* reject anythng we don't know how to deal with
|
||||
* noisily in the logs.
|
||||
*/
|
||||
|
||||
if (par->protocol_version != 1) {
|
||||
lwsl_err("%s: Rejecting client with "
|
||||
"unsupported SSv%d protocol\n",
|
||||
__func__, par->protocol_version);
|
||||
|
||||
goto hangup;
|
||||
}
|
||||
|
||||
if (!--par->rem)
|
||||
goto hangup;
|
||||
par->ctr = 0;
|
||||
par->ps = RPAR_INIT_PID;
|
||||
break;
|
||||
|
||||
|
||||
case RPAR_INIT_PID:
|
||||
if (!--par->rem)
|
||||
goto hangup;
|
||||
|
||||
par->temp32 = (par->temp32 << 8) | *cp++;
|
||||
if (++par->ctr < 4)
|
||||
break;
|
||||
|
||||
par->client_pid = (uint32_t)par->temp32;
|
||||
par->ctr = 0;
|
||||
par->ps = RPAR_INITTXC0;
|
||||
break;
|
||||
|
||||
case RPAR_INITTXC0:
|
||||
if (!--par->rem)
|
||||
goto hangup;
|
||||
|
||||
par->temp32 = (par->temp32 << 8) | *cp++;
|
||||
if (++par->ctr < 4)
|
||||
break;
|
||||
|
||||
par->txcr_out = par->temp32;
|
||||
par->ctr = 0;
|
||||
par->ps = RPAR_STREAMTYPE;
|
||||
break;
|
||||
|
||||
/*
|
||||
* These are the client adjusting our / the remote peer ability
|
||||
* to send back to him. He's sending a signed u32 BE
|
||||
*/
|
||||
|
||||
case RPAR_TXCR0:
|
||||
|
||||
par->temp32 = (par->temp32 << 8) | *cp++;
|
||||
if (++par->ctr < 4) {
|
||||
if (!--par->rem)
|
||||
goto hangup;
|
||||
break;
|
||||
}
|
||||
|
||||
if (--par->rem)
|
||||
goto hangup;
|
||||
|
||||
/*
|
||||
* We're the proxy, being told by the client
|
||||
* that it wants to allow more tx from the peer
|
||||
* on the onward connection towards it.
|
||||
*/
|
||||
#if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT)
|
||||
if (proxy_pss_to_ss_h(pss) &&
|
||||
proxy_pss_to_ss_h(pss)->wsi) {
|
||||
lws_wsi_tx_credit(
|
||||
proxy_pss_to_ss_h(pss)->wsi,
|
||||
LWSTXCR_PEER_TO_US,
|
||||
par->temp32);
|
||||
lwsl_notice("%s: proxy RX_PEER_TXCR: +%d (est %d)\n",
|
||||
__func__, par->temp32,
|
||||
proxy_pss_to_ss_h(pss)->wsi->
|
||||
txc.peer_tx_cr_est);
|
||||
_lws_ss_request_tx(proxy_pss_to_ss_h(pss));
|
||||
} else
|
||||
#endif
|
||||
lwsl_info("%s: dropping TXCR\n", __func__);
|
||||
|
||||
par->ps = RPAR_TYPE;
|
||||
break;
|
||||
|
||||
case RPAR_TIMEOUT0:
|
||||
|
||||
par->temp32 = (par->temp32 << 8) | *cp++;
|
||||
if (++par->ctr < 4) {
|
||||
if (!--par->rem)
|
||||
goto hangup;
|
||||
break;
|
||||
}
|
||||
|
||||
if (--par->rem)
|
||||
goto hangup;
|
||||
|
||||
/*
|
||||
* Proxy...
|
||||
*
|
||||
* *pss may have gone away asynchronously inbetweentimes
|
||||
*/
|
||||
|
||||
if (proxy_pss_to_ss_h(pss)) {
|
||||
|
||||
if ((unsigned int)par->temp32 == 0xffffffff) {
|
||||
lwsl_notice("%s: cancel ss timeout\n",
|
||||
__func__);
|
||||
lws_ss_cancel_timeout(
|
||||
proxy_pss_to_ss_h(pss));
|
||||
} else {
|
||||
|
||||
if (!par->temp32)
|
||||
par->temp32 = (int)
|
||||
proxy_pss_to_ss_h(pss)->
|
||||
policy->timeout_ms;
|
||||
|
||||
lwsl_notice("%s: set ss timeout for +%ums\n",
|
||||
__func__, par->temp32);
|
||||
|
||||
lws_ss_start_timeout(
|
||||
proxy_pss_to_ss_h(pss),
|
||||
(unsigned int)par->temp32);
|
||||
}
|
||||
}
|
||||
|
||||
par->ps = RPAR_TYPE;
|
||||
break;
|
||||
|
||||
case RPAR_PAYLEN0:
|
||||
/*
|
||||
* It's the length from lws_ss_request_tx_len() being
|
||||
* passed up to the proxy
|
||||
*/
|
||||
par->temp32 = (par->temp32 << 8) | *cp++;
|
||||
if (++par->ctr < 4) {
|
||||
if (!--par->rem)
|
||||
goto hangup;
|
||||
break;
|
||||
}
|
||||
|
||||
if (--par->rem)
|
||||
goto hangup;
|
||||
|
||||
lwsl_notice("%s: set payload len %u\n", __func__,
|
||||
par->temp32);
|
||||
|
||||
par->ps = RPAR_TYPE;
|
||||
|
||||
if (proxy_pss_to_ss_h(pss)) {
|
||||
r = lws_ss_request_tx_len(proxy_pss_to_ss_h(pss),
|
||||
(unsigned long)par->temp32);
|
||||
if (r == LWSSSSRET_DESTROY_ME)
|
||||
goto hangup;
|
||||
}
|
||||
break;
|
||||
|
||||
case RPAR_METADATA_NAMELEN:
|
||||
/* both client and proxy */
|
||||
if (!--par->rem)
|
||||
goto hangup;
|
||||
par->slen = *cp++;
|
||||
if (par->slen >= sizeof(par->metadata_name) - 1)
|
||||
goto hangup;
|
||||
par->ctr = 0;
|
||||
par->ps++;
|
||||
break;
|
||||
|
||||
case RPAR_METADATA_NAME:
|
||||
/* both client and proxy */
|
||||
if (!--par->rem)
|
||||
goto hangup;
|
||||
par->metadata_name[par->ctr++] = (char)*cp++;
|
||||
if (par->ctr != par->slen)
|
||||
break;
|
||||
par->metadata_name[par->ctr] = '\0';
|
||||
par->ps = RPAR_METADATA_VALUE;
|
||||
|
||||
/* proxy side is receiving it */
|
||||
|
||||
if (!proxy_pss_to_ss_h(pss))
|
||||
goto hangup;
|
||||
|
||||
if (!proxy_pss_to_ss_h(pss)->policy) {
|
||||
lwsl_err("%s: null policy\n", __func__);
|
||||
goto hangup;
|
||||
}
|
||||
|
||||
/*
|
||||
* This is the policy's metadata list for the given
|
||||
* name
|
||||
*/
|
||||
pm = lws_ss_policy_metadata(
|
||||
proxy_pss_to_ss_h(pss)->policy,
|
||||
par->metadata_name);
|
||||
if (!pm) {
|
||||
lwsl_err("%s: metadata %s not in proxy policy\n",
|
||||
__func__, par->metadata_name);
|
||||
|
||||
goto hangup;
|
||||
}
|
||||
|
||||
par->ssmd = lws_ss_get_handle_metadata(
|
||||
proxy_pss_to_ss_h(pss),
|
||||
par->metadata_name);
|
||||
|
||||
if (par->ssmd) {
|
||||
|
||||
if (par->ssmd->value_on_lws_heap)
|
||||
lws_free_set_NULL(par->ssmd->value__may_own_heap);
|
||||
par->ssmd->value_on_lws_heap = 0;
|
||||
|
||||
if (proxy_pss_to_ss_h(pss) &&
|
||||
lws_fi(&proxy_pss_to_ss_h(pss)->fic, "ssproxy_rx_metadata_oom"))
|
||||
par->ssmd->value__may_own_heap = NULL;
|
||||
else
|
||||
par->ssmd->value__may_own_heap =
|
||||
lws_malloc((unsigned int)par->rem + 1, "metadata");
|
||||
|
||||
if (!par->ssmd->value__may_own_heap) {
|
||||
lwsl_err("%s: OOM mdv\n", __func__);
|
||||
goto hangup;
|
||||
}
|
||||
par->ssmd->length = par->rem;
|
||||
((uint8_t *)par->ssmd->value__may_own_heap)[par->rem] = '\0';
|
||||
/* mark it as needing cleanup */
|
||||
par->ssmd->value_on_lws_heap = 1;
|
||||
}
|
||||
par->ctr = 0;
|
||||
break;
|
||||
|
||||
case RPAR_METADATA_VALUE:
|
||||
/* both client and proxy */
|
||||
|
||||
if (!par->ssmd) {
|
||||
/* we don't recognize the name */
|
||||
|
||||
cp++;
|
||||
|
||||
if (--par->rem)
|
||||
break;
|
||||
|
||||
par->ps = RPAR_TYPE;
|
||||
break;
|
||||
}
|
||||
|
||||
((uint8_t *)(par->ssmd->value__may_own_heap))[par->ctr++] = *cp++;
|
||||
|
||||
if (--par->rem)
|
||||
break;
|
||||
|
||||
/* we think we got all the value */
|
||||
|
||||
lwsl_ss_info(proxy_pss_to_ss_h(pss),
|
||||
"RPAR_METADATA_VALUE for %s (len %d)",
|
||||
par->ssmd->name,
|
||||
(int)par->ssmd->length);
|
||||
lwsl_hexdump_ss_info(proxy_pss_to_ss_h(pss),
|
||||
par->ssmd->value__may_own_heap,
|
||||
par->ssmd->length);
|
||||
|
||||
par->ps = RPAR_TYPE;
|
||||
break;
|
||||
|
||||
case RPAR_STREAMTYPE:
|
||||
|
||||
/* only the proxy can get these */
|
||||
|
||||
if (par->ctr == sizeof(par->streamtype) - 1)
|
||||
goto hangup;
|
||||
|
||||
/*
|
||||
* We can only expect to get this if we ourselves are
|
||||
* in the state that we're waiting for it. If it comes
|
||||
* later it's a protocol error.
|
||||
*/
|
||||
|
||||
if (*state != LPCSPROX_WAIT_INITIAL_TX)
|
||||
goto hangup;
|
||||
|
||||
/*
|
||||
* We're the proxy, creating an SS on behalf of a
|
||||
* client
|
||||
*/
|
||||
|
||||
par->streamtype[par->ctr++] = (char)*cp++;
|
||||
if (--par->rem)
|
||||
break;
|
||||
|
||||
par->ps = RPAR_TYPE;
|
||||
par->streamtype[par->ctr] = '\0';
|
||||
lwsl_info("%s: proxy ss '%s', sssv%d, txcr %d\n",
|
||||
__func__, par->streamtype,
|
||||
par->protocol_version, par->txcr_out);
|
||||
|
||||
ssi->streamtype = par->streamtype;
|
||||
if (par->txcr_out) // !!!
|
||||
ssi->manual_initial_tx_credit = par->txcr_out;
|
||||
|
||||
/*
|
||||
* Even for a synthetic SS proxing action like _lws_smd,
|
||||
* we create an actual SS in the proxy representing the
|
||||
* connection
|
||||
*/
|
||||
|
||||
ssi->flags |= LWSSSINFLAGS_PROXIED;
|
||||
ssi->sss_protocol_version = par->protocol_version;
|
||||
ssi->client_pid = par->client_pid;
|
||||
|
||||
if (lws_ss_create(context, 0, ssi, parconn, pss,
|
||||
NULL, NULL)) {
|
||||
/*
|
||||
* We're unable to create the onward secure
|
||||
* stream he asked for... schedule a chance to
|
||||
* inform him
|
||||
*/
|
||||
lwsl_err("%s: create '%s' fail\n", __func__,
|
||||
par->streamtype);
|
||||
*state = LPCSPROX_REPORTING_FAIL;
|
||||
break;
|
||||
} else {
|
||||
lwsl_debug("%s: create '%s' OK\n",
|
||||
__func__, par->streamtype);
|
||||
*state = LPCSPROX_REPORTING_OK;
|
||||
}
|
||||
|
||||
if (*pss) {
|
||||
(*pss)->being_serialized = 1;
|
||||
#if defined(LWS_WITH_SYS_SMD)
|
||||
if ((*pss)->policy != &pol_smd)
|
||||
/*
|
||||
* In SMD case we overloaded the
|
||||
* initial credit to be the class mask
|
||||
*/
|
||||
#endif
|
||||
{
|
||||
lwsl_info("%s: Created SS initial credit %d\n",
|
||||
__func__, par->txcr_out);
|
||||
|
||||
(*pss)->info.manual_initial_tx_credit = par->txcr_out;
|
||||
}
|
||||
}
|
||||
|
||||
/* parent needs to schedule write on client conn */
|
||||
break;
|
||||
|
||||
/* clientside states */
|
||||
|
||||
case RPAR_RESULT_CREATION:
|
||||
case RPAR_RESULT_CREATION_RIDESHARE:
|
||||
case RPAR_RESULT_CREATION_DSH:
|
||||
case RPAR_STATEINDEX:
|
||||
case RPAR_ORD3:
|
||||
case RPAR_ORD2:
|
||||
case RPAR_ORD1:
|
||||
case RPAR_ORD0:
|
||||
goto hangup;
|
||||
|
||||
default:
|
||||
goto hangup;
|
||||
}
|
||||
}
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
|
||||
hangup:
|
||||
|
||||
lwsl_cx_notice(context, "hangup");
|
||||
|
||||
return LWSSSSRET_DISCONNECT_ME;
|
||||
}
|
@ -0,0 +1,291 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2019 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
*
|
||||
* Proxy side of Client <-> Proxy wsi connection, usually on Unix Domain Socket
|
||||
*/
|
||||
|
||||
#include <private-lib-core.h>
|
||||
|
||||
struct raw_pss {
|
||||
struct lws_sss_proxy_conn *conn;
|
||||
};
|
||||
|
||||
static int
|
||||
lws_sss_proxy_transport_wsi_cb(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
void *user, void *in, size_t len)
|
||||
{
|
||||
struct raw_pss *pss = (struct raw_pss *)user;
|
||||
struct lws_sss_proxy_conn *conn = NULL;
|
||||
|
||||
if (pss)
|
||||
conn = pss->conn;
|
||||
|
||||
switch (reason) {
|
||||
|
||||
/* callbacks related to raw socket descriptor "accepted side" */
|
||||
|
||||
case LWS_CALLBACK_RAW_ADOPT:
|
||||
lwsl_user("LWS_CALLBACK_RAW_ADOPT %s\n", lws_txp_inside_proxy.name);
|
||||
|
||||
if (lws_txp_inside_proxy.event_new_conn(
|
||||
wsi->a.context,
|
||||
&lws_txp_inside_proxy,
|
||||
(lws_transport_priv_t)conn,
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
&wsi->fic,
|
||||
#endif
|
||||
&pss->conn,
|
||||
(lws_transport_priv_t)wsi)) {
|
||||
lwsl_err("%s: hangup from new_conn\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* dsh is allocated when the onward ss is done */
|
||||
|
||||
wsi->bound_ss_proxy_conn = 1; /* opaque is conn */
|
||||
lws_set_opaque_user_data(wsi, pss->conn);
|
||||
|
||||
pss->conn->state = LPCSPROX_WAIT_INITIAL_TX;
|
||||
|
||||
/*
|
||||
* Client is expected to follow the unix domain socket
|
||||
* acceptance up rapidly with an initial tx containing the
|
||||
* streamtype name. We can't create the stream until then.
|
||||
*/
|
||||
lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND, 3);
|
||||
lwsl_user("%s: ADOPT: accepted\n", __func__);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_RAW_CLOSE:
|
||||
lwsl_info("LWS_CALLBACK_RAW_CLOSE:\n");
|
||||
|
||||
if (!conn)
|
||||
break;
|
||||
|
||||
/*
|
||||
* the client unix domain socket connection (wsi / conn->wsi)
|
||||
* has closed... eg, client has exited or otherwise has
|
||||
* definitively finished with the proxying and onward connection
|
||||
*
|
||||
* But right now, the SS and possibly the SS onward wsi are
|
||||
* still live...
|
||||
*/
|
||||
|
||||
assert(conn->txp_path.priv_onw == wsi);
|
||||
|
||||
// if (conn->ss)
|
||||
// conn->ss = NULL;
|
||||
|
||||
/* sever relationship with conn */
|
||||
lws_set_opaque_user_data(wsi, NULL);
|
||||
|
||||
lws_txp_inside_proxy.event_close_conn(conn);
|
||||
|
||||
/* pss is about to be deleted */
|
||||
if (pss)
|
||||
pss->conn = NULL;
|
||||
lwsl_notice("%s: close finished ok\n", __func__);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_RAW_RX:
|
||||
/*
|
||||
* ie, the proxy is receiving something from a client
|
||||
*/
|
||||
lwsl_info("%s: RX: rx %d\n", __func__, (int)len);
|
||||
|
||||
if (!conn) {
|
||||
lwsl_err("%s: rx with conn %p / priv_in %p\n", __func__,
|
||||
conn, conn->txp_path.priv_in);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (conn->txp_path.ops_in->proxy_read(
|
||||
conn, in, len))
|
||||
return -1;
|
||||
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_RAW_WRITEABLE:
|
||||
|
||||
lwsl_debug("%s: %s: LWS_CALLBACK_RAW_WRITEABLE, state 0x%x\n",
|
||||
__func__, lws_wsi_tag(wsi), lwsi_state(wsi));
|
||||
|
||||
/*
|
||||
* We can transmit something back to the client from the dsh
|
||||
* of stuff we received on its behalf from the ss
|
||||
*/
|
||||
|
||||
if (!conn)
|
||||
break;
|
||||
|
||||
assert_is_conn(conn);
|
||||
|
||||
if (lws_txp_inside_proxy.event_proxy_can_write(conn
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
, &wsi->fic
|
||||
#endif
|
||||
))
|
||||
return -1;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return lws_callback_http_dummy(wsi, reason, user, in, len);
|
||||
}
|
||||
|
||||
static const struct lws_protocols protocols[] = {
|
||||
{
|
||||
"ssproxy-protocol",
|
||||
lws_sss_proxy_transport_wsi_cb,
|
||||
sizeof(struct raw_pss),
|
||||
2048, 2048, NULL, 0
|
||||
},
|
||||
{ NULL, NULL, 0, 0, 0, NULL, 0 }
|
||||
};
|
||||
|
||||
static void
|
||||
lws_sss_proxy_wsi_onward_bind(lws_transport_priv_t priv, lws_ss_handle_t *h)
|
||||
{
|
||||
struct lws *wsi = (struct lws *)priv;
|
||||
|
||||
__lws_lc_tag_append(&wsi->lc, lws_ss_tag(h));
|
||||
}
|
||||
|
||||
static void
|
||||
lws_sss_proxy_wsi_req_write(lws_transport_priv_t priv)
|
||||
{
|
||||
struct lws *wsi = (struct lws *)priv;
|
||||
|
||||
if (wsi)
|
||||
lws_callback_on_writable(wsi);
|
||||
}
|
||||
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
static const lws_fi_ctx_t *
|
||||
lws_sss_proxy_wsi_fault_context(lws_transport_priv_t priv)
|
||||
{
|
||||
struct lws *wsi = (struct lws *)priv;
|
||||
|
||||
if (!wsi)
|
||||
return NULL;
|
||||
|
||||
return &wsi->fic;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int
|
||||
lws_sss_proxy_wsi_write(lws_transport_priv_t priv, uint8_t *buf, size_t *len)
|
||||
{
|
||||
struct lws *wsi = (struct lws *)priv;
|
||||
|
||||
if (lws_write(wsi, buf, *len, LWS_WRITE_RAW) != (ssize_t)*len) {
|
||||
lwsl_wsi_notice(wsi, "failed");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* leave *len alone */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
lws_sss_proxy_wsi_init_proxy_server(struct lws_context *context,
|
||||
const struct lws_transport_proxy_ops *txp_ops_inward,
|
||||
lws_transport_priv_t txp_priv_inward,
|
||||
lws_txp_path_proxy_t *txp_ppath,
|
||||
const void *txp_info,
|
||||
const char *bind, int port)
|
||||
{
|
||||
struct lws_context_creation_info info;
|
||||
|
||||
memset(&info, 0, sizeof(info));
|
||||
|
||||
info.vhost_name = "ssproxy";
|
||||
info.options = LWS_SERVER_OPTION_ADOPT_APPLY_LISTEN_ACCEPT_CONFIG |
|
||||
LWS_SERVER_OPTION_SS_PROXY;
|
||||
info.port = port;
|
||||
if (!port) {
|
||||
if (!bind)
|
||||
#if defined(__linux__)
|
||||
bind = "@proxy.ss.lws";
|
||||
#else
|
||||
bind = "/tmp/proxy.ss.lws";
|
||||
#endif
|
||||
info.options |= LWS_SERVER_OPTION_UNIX_SOCK;
|
||||
}
|
||||
info.iface = bind;
|
||||
#if defined(__linux__)
|
||||
info.unix_socket_perms = "root:root";
|
||||
#else
|
||||
#endif
|
||||
info.listen_accept_role = "raw-skt";
|
||||
info.listen_accept_protocol = "ssproxy-protocol";
|
||||
info.protocols = protocols;
|
||||
|
||||
if (!lws_create_vhost(context, &info)) {
|
||||
lwsl_err("%s: Failed to create ss proxy vhost\n", __func__);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
lws_sss_proxy_wsi_client_up(lws_transport_priv_t priv)
|
||||
{
|
||||
struct lws *wsi = (struct lws *)priv;
|
||||
|
||||
lws_set_timeout(wsi, 0, 0);
|
||||
}
|
||||
|
||||
static int
|
||||
lws_sss_proxy_check_write_more(lws_transport_priv_t priv)
|
||||
{
|
||||
struct lws *wsi = (struct lws *)priv;
|
||||
|
||||
if (lws_send_pipe_choked(wsi))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const lws_transport_proxy_ops_t txp_ops_ssproxy_wsi = {
|
||||
.name = "txp_proxy_wsi",
|
||||
.init_proxy_server = lws_sss_proxy_wsi_init_proxy_server,
|
||||
.proxy_req_write = lws_sss_proxy_wsi_req_write,
|
||||
.proxy_write = lws_sss_proxy_wsi_write,
|
||||
|
||||
.event_onward_bind = lws_sss_proxy_wsi_onward_bind,
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
.fault_context = lws_sss_proxy_wsi_fault_context,
|
||||
#endif
|
||||
.event_client_up = lws_sss_proxy_wsi_client_up,
|
||||
.proxy_check_write_more = lws_sss_proxy_check_write_more,
|
||||
};
|
@ -0,0 +1,423 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2019 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <private-lib-core.h>
|
||||
|
||||
/*
|
||||
* Proxy has received a new connection from a client
|
||||
*/
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lws_ssproxy_txp_new_conn(struct lws_context *cx,
|
||||
const struct lws_transport_proxy_ops *txp_ops_inward,
|
||||
lws_transport_priv_t txp_priv_inward,
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
const lws_fi_ctx_t *fic,
|
||||
#endif
|
||||
struct lws_sss_proxy_conn **conn,
|
||||
lws_transport_priv_t txp_priv)
|
||||
{
|
||||
if (
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
fic &&
|
||||
#endif
|
||||
lws_fi(fic, "ssproxy_client_adopt_oom"))
|
||||
*conn = NULL;
|
||||
else
|
||||
*conn = lws_zalloc(sizeof(**conn), __func__);
|
||||
if (!*conn)
|
||||
return 1;
|
||||
|
||||
/* dsh is allocated when the onward ss is done */
|
||||
|
||||
#if defined(_DEBUG)
|
||||
(*conn)->magic = LWS_PROXY_CONN_MAGIC;
|
||||
#endif
|
||||
(*conn)->state = LPCSPROX_WAIT_INITIAL_TX;
|
||||
(*conn)->txp_path = cx->txp_ppath;
|
||||
(*conn)->txp_path.priv_onw = txp_priv;
|
||||
|
||||
(*conn)->txp_path.ops_in = txp_ops_inward;
|
||||
(*conn)->txp_path.priv_in = txp_priv_inward;
|
||||
(*conn)->cx = cx;
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Proxy has received a close indication from a client
|
||||
*/
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lws_ssproxy_txp_close_conn(struct lws_sss_proxy_conn *conn)
|
||||
{
|
||||
lws_transport_priv_t epriv;
|
||||
|
||||
conn->txp_path.priv_onw = NULL;
|
||||
epriv = conn->txp_path.priv_onw;
|
||||
|
||||
/*
|
||||
* If there's an outgoing, proxied SS conn on our behalf, we
|
||||
* have to destroy it
|
||||
*
|
||||
* Wsi related stuff in here is talking about the onward wsi / ss
|
||||
* connection, it doesn't introduce any dependency on the proxy -
|
||||
* client link transport
|
||||
*/
|
||||
|
||||
if (conn->ss) {
|
||||
struct lws *cw;
|
||||
|
||||
cw = conn->ss->wsi;
|
||||
|
||||
/*
|
||||
* conn->ss is the onward connection SS
|
||||
*/
|
||||
|
||||
lwsl_info("%s: destroying %s, wsi %s\n",
|
||||
__func__, lws_ss_tag(conn->ss),
|
||||
lws_wsi_tag(conn->ss->wsi));
|
||||
|
||||
/* sever conn relationship with onward ss about to be deleted */
|
||||
|
||||
conn->ss->wsi = NULL;
|
||||
|
||||
if (cw && epriv != (lws_transport_priv_t)cw) {
|
||||
|
||||
/* disconnect onward SS from its wsi */
|
||||
|
||||
lws_set_opaque_user_data(cw, NULL);
|
||||
|
||||
/*
|
||||
* The wsi doing the onward connection can no
|
||||
* longer relate to the conn... otherwise when
|
||||
* he gets callbacks he wants to bind to
|
||||
* the ss we are about to delete
|
||||
*/
|
||||
lws_wsi_close(cw, LWS_TO_KILL_ASYNC);
|
||||
}
|
||||
|
||||
/* destroy the onward ss (setting conn->ss NULL) */
|
||||
lws_ss_destroy(&conn->ss);
|
||||
|
||||
/*
|
||||
* Conn may have gone, at ss destroy handler in
|
||||
* ssi.state for proxied ss
|
||||
*/
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
if (conn->state == LPCSPROX_DESTROYED || !conn->ss) {
|
||||
/*
|
||||
* There's no onward secure stream and our client
|
||||
* connection is closing. Destroy the conn.
|
||||
*/
|
||||
lws_dsh_destroy(&conn->dsh);
|
||||
lws_free(conn);
|
||||
} else
|
||||
lwsl_debug("%s: CLOSE; %s\n", __func__, lws_ss_tag(conn->ss));
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lws_ssproxy_txp_rx(lws_transport_priv_t txp_priv, const uint8_t *in, size_t len)
|
||||
{
|
||||
struct lws_sss_proxy_conn *conn = (struct lws_sss_proxy_conn *)txp_priv;
|
||||
lws_ss_state_return_t r;
|
||||
lws_ss_info_t ssi;
|
||||
|
||||
assert_is_conn(conn);
|
||||
|
||||
// lwsl_hexdump_info(in, len);
|
||||
|
||||
if (conn->state == LPCSPROX_WAIT_INITIAL_TX) {
|
||||
memset(&ssi, 0, sizeof(ssi));
|
||||
ssi.user_alloc = sizeof(ss_proxy_t);
|
||||
ssi.handle_offset = offsetof(ss_proxy_t, ss);
|
||||
ssi.opaque_user_data_offset = offsetof(ss_proxy_t, conn);
|
||||
ssi.rx = lws_sss_proxy_onward_rx;
|
||||
ssi.tx = lws_sss_proxy_onward_tx;
|
||||
}
|
||||
ssi.state = lws_sss_proxy_onward_state;
|
||||
ssi.flags = 0;
|
||||
|
||||
// coverity[uninit_use_in_call]
|
||||
r = lws_ss_proxy_deserialize_parse(&conn->parser, conn->cx, conn->dsh,
|
||||
in, len, &conn->state, conn,
|
||||
&conn->ss, &ssi);
|
||||
switch (r) {
|
||||
default:
|
||||
break;
|
||||
case LWSSSSRET_DISCONNECT_ME:
|
||||
return r;
|
||||
case LWSSSSRET_DESTROY_ME:
|
||||
if (conn->ss)
|
||||
lws_ss_destroy(&conn->ss);
|
||||
return r;
|
||||
}
|
||||
|
||||
if ((conn->state == LPCSPROX_REPORTING_FAIL ||
|
||||
conn->state == LPCSPROX_REPORTING_OK) &&
|
||||
conn->txp_path.priv_onw)
|
||||
conn->txp_path.ops_onw->proxy_req_write(conn->txp_path.priv_onw);
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
static lws_ss_state_return_t
|
||||
lws_ssproxy_txp_proxy_can_write(lws_transport_priv_t priv
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
, const lws_fi_ctx_t *fic
|
||||
#endif
|
||||
)
|
||||
{
|
||||
struct lws_sss_proxy_conn *conn = (struct lws_sss_proxy_conn *)priv;
|
||||
const lws_ss_policy_t *rsp;
|
||||
lws_ss_metadata_t *md;
|
||||
const uint8_t *cp;
|
||||
char _s[1580 + LWS_PRE], *s = _s + LWS_PRE;
|
||||
size_t si, csi;
|
||||
uint8_t *p;
|
||||
char pay;
|
||||
int n;
|
||||
|
||||
assert_is_conn(conn);
|
||||
|
||||
n = 0;
|
||||
pay = 0;
|
||||
|
||||
*(s + 3) = 0;
|
||||
cp = (const uint8_t *)s;
|
||||
|
||||
switch (conn->state) {
|
||||
case LPCSPROX_REPORTING_FAIL:
|
||||
*(s + 3) = 1;
|
||||
/* fallthru */
|
||||
case LPCSPROX_REPORTING_OK:
|
||||
*s = LWSSS_SER_RXPRE_CREATE_RESULT;
|
||||
*(s + 1) = 0;
|
||||
*(s + 2) = 1;
|
||||
|
||||
n = 8;
|
||||
|
||||
lws_ser_wu32be((uint8_t *)s + 4, conn->ss &&
|
||||
conn->ss->policy ?
|
||||
conn->ss->policy->client_buflen : 0);
|
||||
|
||||
/*
|
||||
* If there's rideshare sequencing, it's added after the
|
||||
* first 4 bytes or the create result, comma-separated
|
||||
*/
|
||||
|
||||
if (conn->ss) {
|
||||
rsp = conn->ss->policy;
|
||||
|
||||
while (rsp) {
|
||||
if (n != 4 && n < (int)sizeof(_s) - LWS_PRE - 2)
|
||||
*(s + (n++)) = ',';
|
||||
n += lws_snprintf(s + n, sizeof(_s) - LWS_PRE - (unsigned int)n,
|
||||
"%s", rsp->streamtype);
|
||||
rsp = lws_ss_policy_lookup(conn->cx,
|
||||
rsp->rideshare_streamtype);
|
||||
}
|
||||
}
|
||||
*(s + 2) = (char)(n - 3);
|
||||
conn->state = LPCSPROX_OPERATIONAL;
|
||||
conn->txp_path.ops_onw->event_client_up(conn->txp_path.priv_onw);
|
||||
break;
|
||||
|
||||
case LPCSPROX_OPERATIONAL:
|
||||
|
||||
/*
|
||||
* returning [onward -> ] proxy]-> client
|
||||
* rx metadata has priority 1
|
||||
*/
|
||||
|
||||
md = conn->ss->metadata;
|
||||
while (md) {
|
||||
// lwsl_notice("%s: check %s: %d\n", __func__,
|
||||
// md->name, md->pending_onward);
|
||||
if (md->pending_onward) {
|
||||
size_t naml = strlen(md->name);
|
||||
|
||||
// lwsl_notice("%s: proxy issuing rxmd\n", __func__);
|
||||
|
||||
if (4 + naml + md->length > sizeof(_s) - LWS_PRE) {
|
||||
lwsl_err("%s: rxmdata too big\n",
|
||||
__func__);
|
||||
goto hangup;
|
||||
}
|
||||
md->pending_onward = 0;
|
||||
p = (uint8_t *)s;
|
||||
p[0] = LWSSS_SER_RXPRE_METADATA;
|
||||
lws_ser_wu16be(&p[1], (uint16_t)(1 + naml +
|
||||
md->length));
|
||||
p[3] = (uint8_t)naml;
|
||||
memcpy(&p[4], md->name, naml);
|
||||
p += 4 + naml;
|
||||
memcpy(p, md->value__may_own_heap,
|
||||
md->length);
|
||||
p += md->length;
|
||||
|
||||
n = lws_ptr_diff(p, cp);
|
||||
goto do_write_nz;
|
||||
}
|
||||
|
||||
md = md->next;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we have performance data, render it in JSON
|
||||
* and send that in LWSSS_SER_RXPRE_PERF has
|
||||
* priority 2
|
||||
*/
|
||||
|
||||
#if defined(LWS_WITH_CONMON)
|
||||
if (conn->ss->conmon_json) {
|
||||
unsigned int xlen = conn->ss->conmon_len;
|
||||
|
||||
if (xlen > sizeof(s) - 3)
|
||||
xlen = sizeof(s) - 3;
|
||||
cp = (uint8_t *)s;
|
||||
p = (uint8_t *)s;
|
||||
p[0] = LWSSS_SER_RXPRE_PERF;
|
||||
lws_ser_wu16be(&p[1], (uint16_t)xlen);
|
||||
memcpy(&p[3], conn->ss->conmon_json, xlen);
|
||||
|
||||
lws_free_set_NULL(conn->ss->conmon_json);
|
||||
n = (int)(xlen + 3);
|
||||
|
||||
pay = 0;
|
||||
goto do_write_nz;
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* if no fresh rx metadata, just pass through incoming
|
||||
* dsh
|
||||
*/
|
||||
|
||||
if (lws_dsh_get_head(conn->dsh, KIND_SS_TO_P, (void **)&p, &si))
|
||||
break;
|
||||
|
||||
cp = p;
|
||||
pay = 1;
|
||||
n = (int)si;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
do_write_nz:
|
||||
if (!n)
|
||||
return LWSSSSRET_OK;
|
||||
|
||||
if (
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
fic &&
|
||||
#endif
|
||||
lws_fi(fic, "ssproxy_client_write_fail"))
|
||||
n = -1;
|
||||
else {
|
||||
si = csi = (size_t)n;
|
||||
n = conn->txp_path.ops_onw->proxy_write(conn->txp_path.priv_onw,
|
||||
(uint8_t *)cp, &csi);
|
||||
}
|
||||
|
||||
if (n < 0) {
|
||||
lwsl_info("%s: WRITEABLE: %d\n", __func__, n);
|
||||
|
||||
goto hangup;
|
||||
}
|
||||
|
||||
switch (conn->state) {
|
||||
case LPCSPROX_REPORTING_FAIL:
|
||||
goto hangup;
|
||||
case LPCSPROX_OPERATIONAL:
|
||||
if (!conn)
|
||||
break;
|
||||
if (pay) {
|
||||
if (si == csi)
|
||||
lws_dsh_free((void **)&p);
|
||||
else
|
||||
lws_dsh_consume(conn->dsh, KIND_SS_TO_P, csi);
|
||||
|
||||
/*
|
||||
* Did we go below the rx flow threshold for
|
||||
* this dsh?
|
||||
*/
|
||||
|
||||
if (conn->onward_in_flow_control &&
|
||||
conn->ss->policy->proxy_buflen_rxflow_on_above &&
|
||||
conn->ss->wsi &&
|
||||
lws_dsh_get_size(conn->dsh, KIND_SS_TO_P) <
|
||||
conn->ss->policy->proxy_buflen_rxflow_off_below) {
|
||||
lwsl_user("%s: %s: rxflow enabling rx (%lu / %lu, lwm %lu)\n", __func__,
|
||||
lws_wsi_tag(conn->ss->wsi),
|
||||
(unsigned long)lws_dsh_get_size(conn->dsh, KIND_SS_TO_P),
|
||||
(unsigned long)conn->ss->policy->proxy_buflen,
|
||||
(unsigned long)conn->ss->policy->proxy_buflen_rxflow_off_below);
|
||||
/*
|
||||
* Resume receiving taking in rx once
|
||||
* below the low threshold
|
||||
*/
|
||||
lws_rx_flow_control(conn->ss->wsi,
|
||||
LWS_RXFLOW_ALLOW);
|
||||
conn->onward_in_flow_control = 0;
|
||||
}
|
||||
}
|
||||
if (!lws_dsh_get_head(conn->dsh, KIND_SS_TO_P,
|
||||
(void **)&p, &si)) {
|
||||
|
||||
if (conn && conn->txp_path.ops_onw->proxy_check_write_more &&
|
||||
conn->txp_path.ops_onw->proxy_check_write_more(
|
||||
conn->txp_path.priv_onw)) {
|
||||
cp = p;
|
||||
pay = 1;
|
||||
n = (int)si;
|
||||
goto do_write_nz;
|
||||
}
|
||||
|
||||
conn->txp_path.ops_onw->proxy_req_write(
|
||||
conn->txp_path.priv_onw);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
|
||||
hangup:
|
||||
return LWSSSSRET_DISCONNECT_ME;
|
||||
}
|
||||
|
||||
const lws_transport_proxy_ops_t lws_txp_inside_proxy = {
|
||||
.name = "txp_inside_proxy",
|
||||
.event_new_conn = lws_ssproxy_txp_new_conn,
|
||||
.proxy_read = lws_ssproxy_txp_rx,
|
||||
.event_close_conn = lws_ssproxy_txp_close_conn,
|
||||
.event_proxy_can_write = lws_ssproxy_txp_proxy_can_write,
|
||||
};
|
||||
|
492
Kinc/Sources/kinc/libs/secure-streams/serialized/proxy/proxy.c
Normal file
492
Kinc/Sources/kinc/libs/secure-streams/serialized/proxy/proxy.c
Normal file
@ -0,0 +1,492 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2019 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
*
|
||||
* When the user code is in a different process, a non-tls unix domain socket
|
||||
* proxy is used to asynchronusly transfer buffers in each direction via the
|
||||
* network stack, without explicit IPC
|
||||
*
|
||||
* user_process{ [user code] | shim | socket-}------ lws_process{ lws }
|
||||
*
|
||||
* Lws exposes a listening unix domain socket in this case, the user processes
|
||||
* connect to it and pass just info.streamtype in an initial tx packet. All
|
||||
* packets are prepended by a 1-byte type field when used in this mode. See
|
||||
* lws-secure-streams.h for documentation and definitions.
|
||||
*
|
||||
* Proxying in either direction can face the situation it cannot send the onward
|
||||
* packet immediately and is subject to separating the write request from the
|
||||
* write action. To make the best use of memory, a single preallocated buffer
|
||||
* stashes pending packets in all four directions (c->p, p->c, p->ss, ss->p).
|
||||
* This allows it to adapt to different traffic patterns without wasted areas
|
||||
* dedicated to traffic that isn't coming in a particular application.
|
||||
*
|
||||
* A shim is provided to monitor the process' unix domain socket and regenerate
|
||||
* the secure sockets api there with callbacks happening in the process thread
|
||||
* context.
|
||||
*
|
||||
* This file implements the listening unix domain socket proxy... this code is
|
||||
* only going to run on a Linux-class device with its implications about memory
|
||||
* availability.
|
||||
*/
|
||||
|
||||
#include <private-lib-core.h>
|
||||
/*
|
||||
* Proxy - onward secure-stream handler
|
||||
*/
|
||||
|
||||
void
|
||||
lws_proxy_clean_conn_ss(struct lws *wsi)
|
||||
{
|
||||
#if 0
|
||||
lws_ss_handle_t *h = (lws_ss_handle_t *)wsi->a.opaque_user_data;
|
||||
struct lws_sss_proxy_conn *conn = h->conn_if_sspc_onw;
|
||||
|
||||
if (!wsi)
|
||||
return;
|
||||
|
||||
if (conn && conn->ss)
|
||||
conn->ss->wsi = NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ss_proxy_onward_link_proxy_req_writeable(lws_ss_handle_t *h_onward)
|
||||
{
|
||||
ss_proxy_t *m = (ss_proxy_t *)&h_onward[1];
|
||||
|
||||
if (m->conn->txp_path.priv_onw)
|
||||
m->conn->txp_path.ops_onw->proxy_req_write(m->conn->txp_path.priv_onw);
|
||||
}
|
||||
|
||||
int
|
||||
__lws_ss_proxy_bind_ss_to_conn_wsi(void *parconn, size_t dsh_size)
|
||||
{
|
||||
struct lws_sss_proxy_conn *conn = (struct lws_sss_proxy_conn *)parconn;
|
||||
struct lws_context_per_thread *pt;
|
||||
|
||||
if (!conn || !conn->txp_path.priv_onw || !conn->ss)
|
||||
return -1;
|
||||
|
||||
pt = &conn->ss->context->pt[(int)conn->ss->tsi];
|
||||
|
||||
if (lws_fi(&conn->ss->fic, "ssproxy_dsh_create_oom"))
|
||||
return -1;
|
||||
conn->dsh = lws_dsh_create(&pt->ss_dsh_owner, dsh_size,
|
||||
(int)(conn->txp_path.ops_onw->flags | 2));
|
||||
if (!conn->dsh)
|
||||
return -1;
|
||||
|
||||
conn->dsh->splitat = 1300;
|
||||
|
||||
conn->txp_path.ops_onw->event_onward_bind(conn->txp_path.priv_onw,
|
||||
conn->ss);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* event loop received something and is queueing it for the foreign side of
|
||||
* the dsh to consume later as serialized rx
|
||||
*/
|
||||
|
||||
static int
|
||||
lws_ss_serialize_rx_payload(struct lws_dsh *dsh, const uint8_t *buf,
|
||||
size_t len, int flags, const char *rsp)
|
||||
{
|
||||
lws_usec_t us = lws_now_usecs();
|
||||
uint8_t pre[128];
|
||||
int est = 19, l = 0;
|
||||
|
||||
if (flags & LWSSS_FLAG_RIDESHARE) {
|
||||
/*
|
||||
* We should have the rideshare name if we have been told it's
|
||||
* on a non-default rideshare
|
||||
*/
|
||||
assert(rsp);
|
||||
if (!rsp)
|
||||
return 1;
|
||||
l = (int)strlen(rsp);
|
||||
est += 1 + l;
|
||||
} else
|
||||
assert(!rsp);
|
||||
|
||||
// lwsl_user("%s: len %d, flags: %d\n", __func__, (int)len, flags);
|
||||
// lwsl_hexdump_info(buf, len);
|
||||
|
||||
pre[0] = LWSSS_SER_RXPRE_RX_PAYLOAD;
|
||||
lws_ser_wu16be(&pre[1], (uint16_t)(len + (size_t)est - 3));
|
||||
lws_ser_wu32be(&pre[3], (uint32_t)flags);
|
||||
lws_ser_wu32be(&pre[7], 0); /* write will compute latency here... */
|
||||
lws_ser_wu64be(&pre[11], (uint64_t)us); /* ... and set this to the write time */
|
||||
|
||||
/*
|
||||
* If we are on a non-default rideshare, append the non-default name to
|
||||
* the headers of the payload part, 1-byte length first
|
||||
*/
|
||||
|
||||
if (flags & LWSSS_FLAG_RIDESHARE) {
|
||||
pre[19] = (uint8_t)l;
|
||||
memcpy(&pre[20], rsp, (unsigned int)l);
|
||||
}
|
||||
|
||||
if (lws_dsh_alloc_tail(dsh, KIND_SS_TO_P, pre, (unsigned int)est, buf, len)) {
|
||||
#if defined(_DEBUG)
|
||||
lws_dsh_describe(dsh, __func__);
|
||||
#endif
|
||||
lwsl_err("%s: unable to alloc in dsh 1\n", __func__);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
lwsl_notice("%s: dsh c2p %d, p2c %d\n", __func__,
|
||||
(int)lws_dsh_get_size(dsh, KIND_C_TO_P),
|
||||
(int)lws_dsh_get_size(dsh, KIND_SS_TO_P));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Onward secure streams payload interface */
|
||||
|
||||
lws_ss_state_return_t
|
||||
lws_sss_proxy_onward_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
|
||||
{
|
||||
ss_proxy_t *m = (ss_proxy_t *)userobj;
|
||||
const char *rsp = NULL;
|
||||
int n;
|
||||
|
||||
// lwsl_notice("%s: len %d\n", __func__, (int)len);
|
||||
|
||||
/*
|
||||
* The onward secure stream connection has received something.
|
||||
*/
|
||||
|
||||
if (m->ss->rideshare != m->ss->policy && m->ss->rideshare) {
|
||||
rsp = m->ss->rideshare->streamtype;
|
||||
flags |= LWSSS_FLAG_RIDESHARE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Apply SSS framing around this chunk of RX and stash it in the dsh
|
||||
* in ss -> proxy [ -> client] direction. This can fail...
|
||||
*/
|
||||
|
||||
n = 1;
|
||||
if (m->conn->dsh && !lws_fi(&m->ss->fic, "ssproxy_dsh_rx_queue_oom"))
|
||||
n = lws_ss_serialize_rx_payload(m->conn->dsh, buf, len,
|
||||
flags, rsp);
|
||||
if (n) {
|
||||
if (m->conn->dsh) {
|
||||
#if defined(_DEBUG)
|
||||
lws_dsh_describe(m->conn->dsh, __func__);
|
||||
#endif
|
||||
/*
|
||||
* We couldn't buffer this rx, eg due to OOM, let's
|
||||
* escalate it to be a "loss of connection", which it
|
||||
* basically is... as part of that, drop the dshes.
|
||||
*
|
||||
* This just affects the one stream that owns the
|
||||
* dsh, caller should enter stream close flow and not
|
||||
* send any further payload.
|
||||
*/
|
||||
|
||||
lwsl_warn("%s: dropping SS dsh due to OOM\n", __func__);
|
||||
lws_dsh_empty(m->conn->dsh);
|
||||
}
|
||||
|
||||
return LWSSSSRET_DISCONNECT_ME;
|
||||
}
|
||||
|
||||
/*
|
||||
* Manage rx flow on the SS (onward) side according to our situation
|
||||
* in the dsh holding proxy->client serialized forwarding rx
|
||||
*/
|
||||
|
||||
if (!m->conn->onward_in_flow_control && m->ss->wsi &&
|
||||
m->ss->policy->proxy_buflen_rxflow_on_above &&
|
||||
lws_dsh_get_size(m->conn->dsh, KIND_SS_TO_P) >=
|
||||
m->ss->policy->proxy_buflen_rxflow_on_above) {
|
||||
lwsl_ss_user(m->ss, "rxflow disabling rx (%lu / %lu, hwm %lu)",
|
||||
(unsigned long)lws_dsh_get_size(m->conn->dsh,
|
||||
KIND_SS_TO_P),
|
||||
(unsigned long)m->ss->policy->proxy_buflen,
|
||||
(unsigned long)m->ss->policy->proxy_buflen_rxflow_on_above);
|
||||
/*
|
||||
* stop taking in rx once the onward wsi rx is above the
|
||||
* high water mark
|
||||
*/
|
||||
lws_rx_flow_control(m->ss->wsi, 0);
|
||||
m->conn->onward_in_flow_control = 1;
|
||||
}
|
||||
|
||||
if (m->conn->txp_path.priv_onw) /* if possible, request client conn write */
|
||||
m->conn->txp_path.ops_onw->proxy_req_write(m->conn->txp_path.priv_onw);
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* we are transmitting buffered payload originally from the client on to the ss
|
||||
*/
|
||||
|
||||
lws_ss_state_return_t
|
||||
lws_sss_proxy_onward_tx(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf,
|
||||
size_t *len, int *flags)
|
||||
{
|
||||
ss_proxy_t *m = (ss_proxy_t *)userobj;
|
||||
void *p;
|
||||
size_t si;
|
||||
|
||||
if (!m->conn->ss || m->conn->state != LPCSPROX_OPERATIONAL) {
|
||||
lwsl_notice("%s: ss not ready\n", __func__);
|
||||
*len = 0;
|
||||
|
||||
return LWSSSSRET_TX_DONT_SEND;
|
||||
}
|
||||
|
||||
/*
|
||||
* The onward secure stream says that we could send something to it
|
||||
* (by putting it in buf, and setting *len and *flags)... dredge the
|
||||
* next thing out of the dsh
|
||||
*/
|
||||
|
||||
if (lws_ss_deserialize_tx_payload(m->conn->dsh, m->ss->wsi,
|
||||
ord, buf, len, flags))
|
||||
return LWSSSSRET_TX_DONT_SEND;
|
||||
|
||||
/* ... there's more we want to send? */
|
||||
if (!lws_dsh_get_head(m->conn->dsh, KIND_C_TO_P, (void **)&p, &si))
|
||||
_lws_ss_request_tx(m->conn->ss);
|
||||
|
||||
if (!*len && !*flags)
|
||||
/* we don't actually want to send anything */
|
||||
return LWSSSSRET_TX_DONT_SEND;
|
||||
|
||||
lwsl_info("%s: onward tx %d fl 0x%x\n", __func__, (int)*len, *flags);
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* event loop side is issuing state, serialize and put it in the dbuf for
|
||||
* the foreign side to consume later
|
||||
*/
|
||||
|
||||
static int
|
||||
lws_ss_serialize_state(struct lws_sss_proxy_conn *conn, lws_ss_constate_t state,
|
||||
lws_ss_tx_ordinal_t ack)
|
||||
{
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
const lws_fi_ctx_t *fic = conn->txp_path.ops_onw->fault_context(
|
||||
conn->txp_path.priv_onw);
|
||||
#endif
|
||||
struct lws_dsh *dsh = conn->dsh;
|
||||
uint8_t pre[12];
|
||||
int n = 4;
|
||||
|
||||
if (state == LWSSSCS_EVENT_WAIT_CANCELLED)
|
||||
return 0;
|
||||
|
||||
lwsl_info("%s: %s, ord 0x%x\n", __func__, lws_ss_state_name((int)state),
|
||||
(unsigned int)ack);
|
||||
|
||||
if (!dsh) {
|
||||
/* he can't store anything further on the link */
|
||||
lwsl_notice("%s: dsh for conn was destroyed\n", __func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pre[0] = LWSSS_SER_RXPRE_CONNSTATE;
|
||||
pre[1] = 0;
|
||||
|
||||
if (state > 255) {
|
||||
pre[2] = 8;
|
||||
lws_ser_wu32be(&pre[3], state);
|
||||
n = 7;
|
||||
} else {
|
||||
pre[2] = 5;
|
||||
pre[3] = (uint8_t)state;
|
||||
}
|
||||
|
||||
lws_ser_wu32be(&pre[n], ack);
|
||||
|
||||
if (lws_dsh_alloc_tail(dsh, KIND_SS_TO_P, pre, (unsigned int)n + 4, NULL, 0)
|
||||
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
||||
|| (fic && lws_fi(fic, "sspc_dsh_ss2p_oom"))
|
||||
#endif
|
||||
) {
|
||||
lwsl_err("%s: unable to alloc in dsh 2\n", __func__);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
lws_ss_state_return_t
|
||||
lws_sss_proxy_onward_state(void *userobj, void *sh, lws_ss_constate_t state,
|
||||
lws_ss_tx_ordinal_t ack)
|
||||
{
|
||||
ss_proxy_t *m = (ss_proxy_t *)userobj;
|
||||
size_t dsh_size;
|
||||
|
||||
switch (state) {
|
||||
case LWSSSCS_CREATING:
|
||||
|
||||
/*
|
||||
* conn is private to -process.c, call thru to a) adjust
|
||||
* the accepted incoming proxy link wsi tag name to be
|
||||
* appended with the onward ss tag information now we
|
||||
* have it, and b) allocate the dsh buffer now we
|
||||
* can find out the policy about it for the streamtype.
|
||||
*/
|
||||
|
||||
dsh_size = m->ss->policy->proxy_buflen ?
|
||||
m->ss->policy->proxy_buflen : 32768;
|
||||
|
||||
lwsl_notice("%s: %s: initializing dsh max len %lu\n",
|
||||
__func__, lws_ss_tag(m->ss),
|
||||
(unsigned long)dsh_size);
|
||||
|
||||
/* this includes ssproxy_dsh_create_oom fault generation */
|
||||
|
||||
if (__lws_ss_proxy_bind_ss_to_conn_wsi(m->conn, dsh_size)) {
|
||||
|
||||
/* failed to allocate the dsh */
|
||||
|
||||
lwsl_notice("%s: dsh init failed\n", __func__);
|
||||
|
||||
return LWSSSSRET_DESTROY_ME;
|
||||
}
|
||||
break;
|
||||
|
||||
case LWSSSCS_DESTROYING:
|
||||
if (!m->conn)
|
||||
break;
|
||||
if (!m->conn->txp_path.priv_onw) {
|
||||
/*
|
||||
* Our onward secure stream is closing and our client
|
||||
* connection has already gone away... destroy the conn.
|
||||
*/
|
||||
lwsl_notice("%s: Destroying conn\n", __func__);
|
||||
lws_dsh_empty(m->conn->dsh);
|
||||
if (!m->conn->ss) {
|
||||
lws_dsh_destroy(&m->conn->dsh);
|
||||
free(m->conn);
|
||||
m->conn = NULL;
|
||||
}
|
||||
return 0;
|
||||
} else
|
||||
lwsl_info("%s: ss DESTROYING, wsi up\n", __func__);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!m->conn) {
|
||||
lwsl_warn("%s: dropping state due to conn not up\n", __func__);
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
if (lws_ss_serialize_state(m->conn, state, ack))
|
||||
/*
|
||||
* Failed to alloc state packet that we want to send in dsh,
|
||||
* we will lose coherence and have to disconnect the link
|
||||
*/
|
||||
return LWSSSSRET_DISCONNECT_ME;
|
||||
|
||||
if (state != LWSSSCS_DESTROYING &&
|
||||
m->conn->txp_path.priv_onw) /* if possible, request client conn write */
|
||||
m->conn->txp_path.ops_onw->proxy_req_write(m->conn->txp_path.priv_onw);
|
||||
|
||||
return LWSSSSRET_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* event loop side was told about remote peer tx credit window update, serialize
|
||||
* and put it in the dbuf for the foreign side to consume later
|
||||
*/
|
||||
|
||||
static int
|
||||
lws_ss_serialize_txcr(struct lws_dsh *dsh, int txcr)
|
||||
{
|
||||
uint8_t pre[7];
|
||||
|
||||
lwsl_info("%s: %d\n", __func__, txcr);
|
||||
|
||||
pre[0] = LWSSS_SER_RXPRE_TXCR_UPDATE;
|
||||
pre[1] = 0;
|
||||
pre[2] = 4;
|
||||
lws_ser_wu32be(&pre[3], (uint32_t)txcr);
|
||||
|
||||
if (lws_dsh_alloc_tail(dsh, KIND_SS_TO_P, pre, 7, NULL, 0)) {
|
||||
lwsl_err("%s: unable to alloc in dsh 2\n", __func__);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
ss_proxy_onward_txcr(void *userobj, int bump)
|
||||
{
|
||||
ss_proxy_t *m = (ss_proxy_t *)userobj;
|
||||
|
||||
if (!m->conn)
|
||||
return;
|
||||
|
||||
lws_ss_serialize_txcr(m->conn->dsh, bump);
|
||||
|
||||
if (m->conn->txp_path.priv_onw) /* if possible, request client conn write */
|
||||
m->conn->txp_path.ops_onw->proxy_req_write(m->conn->txp_path.priv_onw);
|
||||
}
|
||||
|
||||
/*
|
||||
* called from create_context()
|
||||
*/
|
||||
|
||||
int
|
||||
lws_ss_proxy_create(struct lws_context *cx, const char *bind, int port)
|
||||
{
|
||||
assert(cx->txp_ppath.ops_onw);
|
||||
return cx->txp_ppath.ops_onw->init_proxy_server(cx,
|
||||
&lws_txp_inside_proxy,
|
||||
NULL,
|
||||
&cx->txp_ppath,
|
||||
cx->txp_ssproxy_info,
|
||||
bind, port);
|
||||
}
|
||||
|
||||
lws_ss_state_return_t
|
||||
lws_ss_proxy_destroy(struct lws_context *cx)
|
||||
{
|
||||
if (!cx->txp_ppath.ops_onw)
|
||||
return 0;
|
||||
|
||||
if (!cx->txp_ppath.ops_onw->destroy_proxy_server)
|
||||
return 0;
|
||||
return cx->txp_ppath.ops_onw->destroy_proxy_server(cx);
|
||||
}
|
Reference in New Issue
Block a user