From a044c807f88cb4f67032b1dbd9338849a161f301 Mon Sep 17 00:00:00 2001 From: Simon Goldschmidt Date: Mon, 24 Sep 2018 22:29:54 +0200 Subject: [PATCH] altcp_tls: rename altcp_tls_new -> altcp_tls_wrap, add altcp_tls_new The new altcp_tls_new() is a type safe version of altcp_tls_alloc() Signed-off-by: Simon Goldschmidt --- src/apps/altcp_tls/altcp_tls_mbedtls.c | 2 +- src/apps/http/altcp_proxyconnect.c | 2 +- src/apps/http/httpd.c | 5 +---- src/apps/mqtt/mqtt.c | 19 ++++++++----------- src/apps/smtp/smtp.c | 17 +++++++---------- src/core/altcp_alloc.c | 12 +++++++++--- src/include/lwip/altcp_tls.h | 16 +++++++++++----- 7 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/apps/altcp_tls/altcp_tls_mbedtls.c b/src/apps/altcp_tls/altcp_tls_mbedtls.c index a40c777e..d241a061 100644 --- a/src/apps/altcp_tls/altcp_tls_mbedtls.c +++ b/src/apps/altcp_tls/altcp_tls_mbedtls.c @@ -605,7 +605,7 @@ altcp_mbedtls_setup(void *conf, struct altcp_pcb *conn, struct altcp_pcb *inner_ } struct altcp_pcb * -altcp_tls_new(struct altcp_tls_config *config, struct altcp_pcb *inner_pcb) +altcp_tls_wrap(struct altcp_tls_config *config, struct altcp_pcb *inner_pcb) { struct altcp_pcb *ret; if (inner_pcb == NULL) { diff --git a/src/apps/http/altcp_proxyconnect.c b/src/apps/http/altcp_proxyconnect.c index f50dace9..e3135ba9 100644 --- a/src/apps/http/altcp_proxyconnect.c +++ b/src/apps/http/altcp_proxyconnect.c @@ -418,7 +418,7 @@ altcp_proxyconnect_tls_alloc(void *arg, u8_t ip_type) struct altcp_pcb *tls_pcb; proxy_pcb = altcp_proxyconnect_new_tcp(&cfg->proxy, ip_type); - tls_pcb = altcp_tls_new(cfg->tls_config, proxy_pcb); + tls_pcb = altcp_tls_wrap(cfg->tls_config, proxy_pcb); if (tls_pcb == NULL) { altcp_close(proxy_pcb); diff --git a/src/apps/http/httpd.c b/src/apps/http/httpd.c index 35e28aa4..ccc9ba72 100644 --- a/src/apps/http/httpd.c +++ b/src/apps/http/httpd.c @@ -2685,10 +2685,7 @@ void httpd_inits(struct altcp_tls_config *conf) { #if LWIP_ALTCP_TLS - struct altcp_pcb *pcb_tls; - struct altcp_pcb *pcb_tcp = altcp_tcp_new_ip_type(IPADDR_TYPE_ANY); - LWIP_ASSERT("httpd_init: tcp_new failed", pcb_tcp != NULL); - pcb_tls = altcp_tls_new(conf, pcb_tcp); + struct altcp_pcb *pcb_tls = altcp_tls_new(conf, IPADDR_TYPE_ANY); LWIP_ASSERT("httpd_init: altcp_tls_new failed", pcb_tls != NULL); httpd_init_pcb(pcb_tls, HTTPD_SERVER_PORT_HTTPS); #else /* LWIP_ALTCP_TLS */ diff --git a/src/apps/mqtt/mqtt.c b/src/apps/mqtt/mqtt.c index 6c10bc05..269f4a49 100644 --- a/src/apps/mqtt/mqtt.c +++ b/src/apps/mqtt/mqtt.c @@ -1362,20 +1362,17 @@ mqtt_client_connect(mqtt_client_t *client, const ip_addr_t *ip_addr, u16_t port, return ERR_MEM; } - client->conn = altcp_tcp_new(); +#if LWIP_ALTCP && LWIP_ALTCP_TLS + if (client_info->tls_config) { + client->conn = altcp_tls_new(client_info->tls_config, IP_GET_TYPE(ip_addr)); + } else +#endif + { + client->conn = altcp_tcp_new_ip_type(IP_GET_TYPE(ip_addr)); + } if (client->conn == NULL) { return ERR_MEM; } -#if LWIP_ALTCP && LWIP_ALTCP_TLS - if (client_info->tls_config) { - struct altcp_pcb *pcb_tls = altcp_tls_new(client_info->tls_config, client->conn); - if (pcb_tls == NULL) { - altcp_close(client->conn); - return ERR_MEM; - } - client->conn = pcb_tls; - } -#endif /* Set arg pointer for callbacks */ altcp_arg(client->conn, client); diff --git a/src/apps/smtp/smtp.c b/src/apps/smtp/smtp.c index 5ecf4627..8a87533a 100644 --- a/src/apps/smtp/smtp.c +++ b/src/apps/smtp/smtp.c @@ -460,18 +460,15 @@ smtp_setup_pcb(struct smtp_session *s, const ip_addr_t* remote_ip) struct altcp_pcb* pcb; LWIP_UNUSED_ARG(remote_ip); - pcb = altcp_tcp_new_ip_type(IP_GET_TYPE(remote_ip)); - if (pcb != NULL) { #if LWIP_ALTCP && LWIP_ALTCP_TLS - if (smtp_server_tls_config) { - struct altcp_pcb *pcb_tls = altcp_tls_new(smtp_server_tls_config, pcb); - if (pcb_tls == NULL) { - altcp_close(pcb); - return NULL; - } - pcb = pcb_tls; - } + if (smtp_server_tls_config) { + pcb = altcp_tls_new(smtp_server_tls_config, IP_GET_TYPE(remote_ip)); + } else #endif + { + pcb = altcp_tcp_new_ip_type(IP_GET_TYPE(remote_ip)); + } + if (pcb != NULL) { altcp_arg(pcb, s); altcp_recv(pcb, smtp_tcp_recv); altcp_err(pcb, smtp_tcp_err); diff --git a/src/core/altcp_alloc.c b/src/core/altcp_alloc.c index a85241c6..cd619bc1 100644 --- a/src/core/altcp_alloc.c +++ b/src/core/altcp_alloc.c @@ -58,23 +58,29 @@ /** This standard allocator function creates an altcp pcb for * TLS over TCP */ struct altcp_pcb * -altcp_tls_alloc(void *arg, u8_t ip_type) +altcp_tls_new(struct altcp_tls_config *config, u8_t ip_type) { struct altcp_pcb *inner_conn, *ret; - struct altcp_tls_config *config = (struct altcp_tls_config *)arg; LWIP_UNUSED_ARG(ip_type); inner_conn = altcp_tcp_new_ip_type(ip_type); if (inner_conn == NULL) { return NULL; } - ret = altcp_tls_new(config, inner_conn); + ret = altcp_tls_wrap(config, inner_conn); if (ret == NULL) { altcp_close(inner_conn); } return ret; } +/** This standard allocator function creates an altcp pcb for + * TLS over TCP */ +struct altcp_pcb * +altcp_tls_alloc(void *arg, u8_t ip_type) +{ + return altcp_tls_new((struct altcp_tls_config *)arg, ip_type); +} #endif /* LWIP_ALTCP_TLS */ diff --git a/src/include/lwip/altcp_tls.h b/src/include/lwip/altcp_tls.h index 6782de49..7b17c608 100644 --- a/src/include/lwip/altcp_tls.h +++ b/src/include/lwip/altcp_tls.h @@ -85,14 +85,20 @@ struct altcp_tls_config *altcp_tls_create_config_client_2wayauth(const u8_t *ca, void altcp_tls_free_config(struct altcp_tls_config *conf); /** @ingroup altcp_tls - * Create new ALTCP_TLS layer + * Create new ALTCP_TLS layer wrapping an existing pcb as inner connection (e.g. TLS over TCP) */ -struct altcp_pcb *altcp_tls_new(struct altcp_tls_config *config, struct altcp_pcb *inner_pcb); +struct altcp_pcb *altcp_tls_wrap(struct altcp_tls_config *config, struct altcp_pcb *inner_pcb); /** @ingroup altcp_tls - * Create new ALTCP_TLS layer - * This allocator function fits to @ref altcp_allocator_t / @ref altcp_new. - * 'arg' must contain a struct altcp_tls_config *. + * Create new ALTCP_TLS pcb and its inner tcp pcb + */ +struct altcp_pcb *altcp_tls_new(struct altcp_tls_config *config, u8_t ip_type); + +/** @ingroup altcp_tls + * Create new ALTCP_TLS layer pcb and its inner tcp pcb. + * Same as @ref altcp_tls_new but this allocator function fits to + * @ref altcp_allocator_t / @ref altcp_new.\n + 'arg' must contain a struct altcp_tls_config *. */ struct altcp_pcb *altcp_tls_alloc(void *arg, u8_t ip_type);