diff options
author | Andrew Zaborowski <andrew.zaborowski@intel.com> | 2022-10-26 15:15:58 +0200 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2022-10-28 13:26:51 -0500 |
commit | b2898c90e077ed36aba518aaacee65568c44f174 (patch) | |
tree | 85ad1377844846f0eb1b591533fbf500b967f207 | |
parent | cfab297d3bd3d8a59f3e4021f1da8e507fca4a95 (diff) |
examples: Cache sessions in https-client-test
If the environment variable TLS_CACHE is set, use
l_tls_set_session_cache() to enable session resumption.
-rw-r--r-- | examples/https-client-test.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/https-client-test.c b/examples/https-client-test.c index b0c24b40..2c6939ab 100644 --- a/examples/https-client-test.c +++ b/examples/https-client-test.c @@ -32,13 +32,18 @@ #include <unistd.h> #include <errno.h> #include <arpa/inet.h> +#include <sys/stat.h> +#include <fcntl.h> #include <ell/ell.h> +#include <ell/useful.h> static struct l_io *io; static struct l_tls *tls; static const char *hostname; static bool ready; +static struct l_settings *session_cache; +static char *session_cache_path; static void https_io_disconnect(struct l_io *io, void *user_data) { @@ -127,6 +132,27 @@ static void https_tls_debug_cb(const char *str, void *user_data) printf("%s\n", str); } +static void https_tls_session_cache_update_cb(void *user_data) +{ + size_t len; + char *data = l_settings_to_data(session_cache, &len); + _auto_(close) int fd = L_TFR(creat(session_cache_path, 0600)); + + if (!data) { + fprintf(stderr, "l_settings_to_data() failed\n"); + return; + } + + if (fd < 0) { + fprintf(stderr, "can't open %s: %s\n", + session_cache_path, strerror(errno)); + return; + } + + if (L_TFR(write(fd, data, len)) < (ssize_t) len) + fprintf(stderr, "short write to %s\n", session_cache_path); +} + int main(int argc, char *argv[]) { struct hostent *he; @@ -200,6 +226,23 @@ int main(int argc, char *argv[]) l_free(str); } + if (getenv("TLS_CACHE")) { + const char *homedir = getenv("HOME"); + + if (!homedir) + homedir = "/tmp"; + + session_cache_path = + l_strdup_printf("%s/.ell-https-client-test", homedir); + session_cache = l_settings_new(); + l_settings_load_from_file(session_cache, session_cache_path); + + l_tls_set_session_cache(tls, session_cache, hostname, + 24 * 3600 * L_USEC_PER_SEC, + https_tls_session_cache_update_cb, + NULL); + } + if (argc >= 3) { ca_cert = l_pem_load_certificate_list(argv[2]); if (!ca_cert) { @@ -244,6 +287,11 @@ int main(int argc, char *argv[]) l_io_destroy(io); l_tls_free(tls); + if (session_cache) { + l_settings_free(session_cache); + l_free(session_cache_path); + } + l_main_exit(); return 0; |