From 306d59d32c0a768b7d8089068ab5972324ddebb6 Mon Sep 17 00:00:00 2001 From: Kedar Sovani Date: Fri, 17 Nov 2017 15:49:12 +0530 Subject: [PATCH] Make read/write function pointers as per Ivan's feedback --- components/esp-tls/esp-tls.c | 24 ++++++++++++++++++++++++ components/esp-tls/esp-tls.h | 20 +++++++------------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/components/esp-tls/esp-tls.c b/components/esp-tls/esp-tls.c index 6867272f40..032a91f7f9 100644 --- a/components/esp-tls/esp-tls.c +++ b/components/esp-tls/esp-tls.c @@ -47,6 +47,16 @@ static struct addrinfo *resolve_host_name(const char *host, size_t hostlen, int return res; } +static ssize_t tcp_read(struct esp_tls *tls, char *data, size_t datalen) +{ + return recv(tls->sockfd, data, datalen, 0); +} + +static ssize_t tls_read(struct esp_tls *tls, char *data, size_t datalen) +{ + return SSL_read(tls->ssl, data, datalen); +} + static int esp_tcp_connect(const char *host, int hostlen, int port) { struct addrinfo *res = resolve_host_name(host, hostlen, port); @@ -125,6 +135,16 @@ void esp_tls_conn_delete(struct esp_tls *tls) free(tls); }; +static ssize_t tcp_write(struct esp_tls *tls, const char *data, size_t datalen) +{ + return send(tls->sockfd, data, datalen, 0); +} + +static ssize_t tls_write(struct esp_tls *tls, const char *data, size_t datalen) +{ + return SSL_write(tls->ssl, data, datalen); +} + struct esp_tls *esp_tls_conn_new(const char *hostname, int hostlen, int port, bool is_tls) { int sockfd = esp_tcp_connect(hostname, hostlen, port); @@ -138,12 +158,16 @@ struct esp_tls *esp_tls_conn_new(const char *hostname, int hostlen, int port, bo return NULL; } tls->sockfd = sockfd; + tls->read = tcp_read; + tls->write = tcp_write; if (is_tls) { if (create_ssl_handle(tls) != 0) { esp_tls_conn_delete(tls); return NULL; } + tls->read = tls_read; + tls->write = tls_write; } return tls; } diff --git a/components/esp-tls/esp-tls.h b/components/esp-tls/esp-tls.h index 655edc08b8..bff2eda4d4 100644 --- a/components/esp-tls/esp-tls.h +++ b/components/esp-tls/esp-tls.h @@ -6,29 +6,23 @@ #include struct esp_tls { - SSL_CTX *ctx; - SSL *ssl; - int sockfd; + SSL_CTX *ctx; + SSL *ssl; + int sockfd; + ssize_t (*read)(struct esp_tls *tls, char *data, size_t datalen); + ssize_t (*write)(struct esp_tls *tls, const char *data, size_t datalen); }; struct esp_tls *esp_tls_conn_new(const char *hostname, int hostlen, int port, bool is_tls); static inline ssize_t esp_tls_conn_write(struct esp_tls *tls, const char *data, size_t datalen) { - if (tls->ssl) { - return SSL_write(tls->ssl, data, datalen); - } else { - return send(tls->sockfd, data, datalen, 0); - } + return tls->write(tls, data, datalen); } static inline ssize_t esp_tls_conn_read(struct esp_tls *tls, char *data, size_t datalen) { - if (tls->ssl) { - return SSL_read(tls->ssl, data, datalen); - } else { - return recv(tls->sockfd, data, datalen, 0); - } + return tls->read(tls, data, datalen); } void esp_tls_conn_delete(struct esp_tls *tls); -- 2.40.0