diff options
author | Andrew Zaborowski <andrew.zaborowski@intel.com> | 2022-11-07 12:30:12 +0100 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2022-11-08 09:49:07 -0600 |
commit | ef9313bba970064fc24721ab222425cd6c5d86fd (patch) | |
tree | 1d6f2e4d7c6b2ba5b4125b2f0940a5e0352dbe74 | |
parent | d2632b4665ac0f4f24a5c89e545ce64b86136b8a (diff) |
examples: Update https example code
Update the l_tls_set_session_cache call signature in https-client-test
and add similar session caching support in https-server-test.
-rw-r--r-- | examples/https-client-test.c | 2 | ||||
-rw-r--r-- | examples/https-server-test.c | 43 |
2 files changed, 44 insertions, 1 deletions
diff --git a/examples/https-client-test.c b/examples/https-client-test.c index 2c6939ab..6b12f77e 100644 --- a/examples/https-client-test.c +++ b/examples/https-client-test.c @@ -238,7 +238,7 @@ int main(int argc, char *argv[]) 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, + 24 * 3600 * L_USEC_PER_SEC, 0, https_tls_session_cache_update_cb, NULL); } diff --git a/examples/https-server-test.c b/examples/https-server-test.c index b626fd2a..5e861d5e 100644 --- a/examples/https-server-test.c +++ b/examples/https-server-test.c @@ -32,12 +32,17 @@ #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; bool served; +static struct l_settings *session_cache; +static char *session_cache_path; static void https_io_disconnect(struct l_io *io, void *user_data) { @@ -115,6 +120,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 sockaddr_in addr = {}; @@ -210,6 +236,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-server-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, "tls-session", + 24 * 3600 * L_USEC_PER_SEC, 10, + https_tls_session_cache_update_cb, + NULL); + } + auth_ok = l_tls_set_auth_data(tls, cert, priv_key) && (argc <= 4 || l_tls_set_cacert(tls, ca_cert)) && l_tls_start(tls); |