Make read/write function pointers

as per Ivan's feedback
This commit is contained in:
Kedar Sovani 2017-11-17 15:49:12 +05:30 committed by Jitin George
parent 7f35c4ff3e
commit 306d59d32c
2 changed files with 31 additions and 13 deletions

View file

@ -47,6 +47,16 @@ static struct addrinfo *resolve_host_name(const char *host, size_t hostlen, int
return res; 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) static int esp_tcp_connect(const char *host, int hostlen, int port)
{ {
struct addrinfo *res = resolve_host_name(host, hostlen, 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); 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) 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); 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; return NULL;
} }
tls->sockfd = sockfd; tls->sockfd = sockfd;
tls->read = tcp_read;
tls->write = tcp_write;
if (is_tls) { if (is_tls) {
if (create_ssl_handle(tls) != 0) { if (create_ssl_handle(tls) != 0) {
esp_tls_conn_delete(tls); esp_tls_conn_delete(tls);
return NULL; return NULL;
} }
tls->read = tls_read;
tls->write = tls_write;
} }
return tls; return tls;
} }

View file

@ -6,29 +6,23 @@
#include <openssl/ssl.h> #include <openssl/ssl.h>
struct esp_tls { struct esp_tls {
SSL_CTX *ctx; SSL_CTX *ctx;
SSL *ssl; SSL *ssl;
int sockfd; 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); 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) static inline ssize_t esp_tls_conn_write(struct esp_tls *tls, const char *data, size_t datalen)
{ {
if (tls->ssl) { return tls->write(tls, data, datalen);
return SSL_write(tls->ssl, data, datalen);
} else {
return send(tls->sockfd, data, datalen, 0);
}
} }
static inline ssize_t esp_tls_conn_read(struct esp_tls *tls, char *data, size_t datalen) static inline ssize_t esp_tls_conn_read(struct esp_tls *tls, char *data, size_t datalen)
{ {
if (tls->ssl) { return tls->read(tls, data, datalen);
return SSL_read(tls->ssl, data, datalen);
} else {
return recv(tls->sockfd, data, datalen, 0);
}
} }
void esp_tls_conn_delete(struct esp_tls *tls); void esp_tls_conn_delete(struct esp_tls *tls);