1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

client: support socket source port setting

This commit is contained in:
Sylvian Saunier 2022-01-14 10:29:53 +00:00 committed by Andy Green
parent b3ec5a0a4e
commit 38a39a3710
5 changed files with 14 additions and 3 deletions

View file

@ -165,6 +165,9 @@ struct lws_client_connect_info {
const char *iface;
/**< NULL to allow routing on any interface, or interface name or IP
* to bind the socket to */
int local_port;
/**< 0 to pick an ephemeral port, or a specific local port
* to bind the socket to */
const char *local_protocol_name;
/**< NULL: .protocol is used both to select the local protocol handler
* to bind to and as the list of remote ws protocols we could

View file

@ -350,6 +350,7 @@ enum lws_token_indexes {
_WSI_TOKEN_CLIENT_ORIGIN,
_WSI_TOKEN_CLIENT_METHOD,
_WSI_TOKEN_CLIENT_IFACE,
_WSI_TOKEN_CLIENT_LOCALPORT,
_WSI_TOKEN_CLIENT_ALPN,
/* always last real token index*/

View file

@ -131,6 +131,7 @@ lws_client_connect_via_info(const struct lws_client_connect_info *i)
struct lws *wsi, *safe = NULL;
const struct lws_protocols *p;
const char *cisin[CIS_COUNT];
char buf_localport[8];
struct lws_vhost *vh;
int tsi;
@ -357,6 +358,8 @@ lws_client_connect_via_info(const struct lws_client_connect_info *i)
cisin[CIS_PROTOCOL] = i->protocol;
cisin[CIS_METHOD] = i->method;
cisin[CIS_IFACE] = i->iface;
lws_snprintf(buf_localport, sizeof(buf_localport), "%u", i->local_port);
cisin[CIS_LOCALPORT] = buf_localport;
cisin[CIS_ALPN] = i->alpn;
if (lws_client_stash_create(wsi, cisin))

View file

@ -153,7 +153,7 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads,
struct sockaddr_un sau;
#endif
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
const char *cce = "Unable to connect", *iface;
const char *cce = "Unable to connect", *iface, *local_port;
const struct sockaddr *psa = NULL;
uint16_t port = wsi->conn_port;
char dcce[48], t16[16];
@ -435,9 +435,12 @@ ads_known:
iface = lws_wsi_client_stash_item(wsi, CIS_IFACE,
_WSI_TOKEN_CLIENT_IFACE);
if (iface && *iface) {
local_port = lws_wsi_client_stash_item(wsi, CIS_LOCALPORT,
_WSI_TOKEN_CLIENT_LOCALPORT);
if ((iface && *iface) || (local_port && atoi(local_port))) {
m = lws_socket_bind(wsi->a.vhost, wsi, wsi->desc.sockfd,
0, iface, af);
(local_port ? atoi(local_port) : 0), iface, af);
if (m < 0) {
lws_snprintf(dcce, sizeof(dcce),
"conn fail: socket bind");

View file

@ -221,6 +221,7 @@ enum {
CIS_METHOD,
CIS_IFACE,
CIS_ALPN,
CIS_LOCALPORT,
CIS_COUNT