Skip to content

Commit 7c1ca5d

Browse files
committed
[websockets] Add server name parameter to allow the use of an IP address for the connection URL (DNS resolution done beforehand) when using a TLS link (server name is used for certificate hostname check)
1 parent 8d3eff3 commit 7c1ca5d

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/websockets/IWebsocketClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ class IWebsocketClient
149149
/** @brief Skip server name check in certificates for TLS connections
150150
* (Warning : enabling this feature is not recommended in production) */
151151
bool skip_server_name_check;
152+
/** @brief Server name (used for server certificate check) */
153+
std::string server_name;
152154
};
153155
};
154156

src/websockets/libwebsockets/LibWebsocketClient.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,15 @@ void LibWebsocketClient::connectCallback(struct lws_sorted_usec_list* sul) noexc
358358
i.context = client->m_context;
359359
i.address = client->m_url.address().c_str();
360360
i.path = client->m_url.path().c_str();
361-
i.host = i.address;
362-
i.origin = i.address;
361+
if (client->m_credentials.server_name.empty())
362+
{
363+
i.host = i.address;
364+
}
365+
else
366+
{
367+
i.host = client->m_credentials.server_name.c_str();
368+
}
369+
i.origin = i.address;
363370
if (client->m_url.protocol() == "wss")
364371
{
365372
i.ssl_connection = LCCSCF_USE_SSL;

src/websockets/libwebsockets/LibWebsocketClientPool.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void LibWebsocketClientPool::process()
147147

148148
// Dummy vhost to handle context related events
149149
struct lws_protocols protocols[] = {{"LibWebsocketClientPool", &LibWebsocketClientPool::eventCallback, 0, 0, 0, this, 0},
150-
LWS_PROTOCOL_LIST_TERM};
150+
LWS_PROTOCOL_LIST_TERM};
151151
struct lws_context_creation_info vhost_info;
152152
memset(&vhost_info, 0, sizeof(vhost_info));
153153
vhost_info.protocols = protocols;
@@ -537,8 +537,15 @@ void LibWebsocketClientPool::Client::connectCallback(struct lws_sorted_usec_list
537537
connect_info.vhost = client->m_vhost;
538538
connect_info.address = client->m_url.address().c_str();
539539
connect_info.path = client->m_url.path().c_str();
540-
connect_info.host = connect_info.address;
541-
connect_info.origin = connect_info.address;
540+
if (client->m_credentials.server_name.empty())
541+
{
542+
connect_info.host = connect_info.address;
543+
}
544+
else
545+
{
546+
connect_info.host = client->m_credentials.server_name.c_str();
547+
}
548+
connect_info.origin = connect_info.address;
542549
if (client->m_url.protocol() == "wss")
543550
{
544551
connect_info.ssl_connection = LCCSCF_USE_SSL;

0 commit comments

Comments
 (0)