Skip to content

Commit 7a8e42e

Browse files
rvdgrachtBastian-Krause
authored andcommitted
Share connection between curl requests
Using a curl_share between curl requests allows sharing an open connection and re-use cached DNS, PSL and TLS session id. This change allows performing multiple requests without having to do re-perform the (full) TLS handshake. For reference, on a stm32mp151c with OPTEE + pkcs11 TA a full TLS handshake takes ~8 seconds. Mostly due to small pager pool (internal sram) available for OPTEE. With this change a mTLS curl request take around 60ms after the initial connection has been established. Signed-off-by: Robin van der Gracht <[email protected]>
1 parent 8d94615 commit 7a8e42e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/hawkbit-client.c

+23
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ static Config *hawkbit_config = NULL;
6969
static GSourceFunc software_ready_cb;
7070
static struct HawkbitAction *active_action = NULL;
7171
static GThread *thread_download = NULL;
72+
static CURLSH *curl_share = NULL;
73+
static pthread_mutex_t curl_share_locks[CURL_LOCK_DATA_LAST + 1];
7274

7375
GQuark rhu_hawkbit_client_error_quark(void)
7476
{
@@ -85,6 +87,16 @@ GQuark rhu_hawkbit_client_http_error_quark(void)
8587
return g_quark_from_static_string("rhu_hawkbit_client_http_error_quark");
8688
}
8789

90+
void curl_share_lock(CURL *handle, curl_lock_data data, curl_lock_access access, void *clientp)
91+
{
92+
pthread_mutex_lock(&curl_share_locks[data]);
93+
}
94+
95+
96+
void curl_share_unlock(CURL *handle, curl_lock_data data, curl_lock_access access, void *clientp)
97+
{
98+
pthread_mutex_unlock(&curl_share_locks[data]);
99+
}
88100
/**
89101
* @brief Create and initialize an HawkbitAction.
90102
*
@@ -256,6 +268,7 @@ static void set_default_curl_opts(CURL *curl)
256268
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, hawkbit_config->connect_timeout);
257269
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, hawkbit_config->ssl_verify ? 1L : 0L);
258270
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, hawkbit_config->ssl_verify ? 1L : 0L);
271+
curl_easy_setopt(curl, CURLOPT_SHARE, curl_share);
259272
}
260273

261274
/**
@@ -1432,6 +1445,14 @@ int hawkbit_start_service_sync()
14321445
g_autoptr(sd_event) event = NULL;
14331446
#endif
14341447

1448+
curl_share = curl_share_init();
1449+
curl_share_setopt(curl_share, CURLSHOPT_LOCKFUNC, curl_share_lock);
1450+
curl_share_setopt(curl_share, CURLSHOPT_UNLOCKFUNC, curl_share_unlock);
1451+
curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
1452+
curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
1453+
curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
1454+
curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
1455+
14351456
active_action = action_new();
14361457

14371458
ctx = g_main_context_new();
@@ -1481,6 +1502,8 @@ int hawkbit_start_service_sync()
14811502
g_source_destroy(event_source);
14821503
sd_event_set_watchdog(event, FALSE);
14831504
#endif
1505+
curl_share_cleanup(curl_share);
1506+
14841507
g_main_loop_unref(cdata.loop);
14851508
if (res < 0)
14861509
g_warning("%s", strerror(-res));

0 commit comments

Comments
 (0)