From 00211ec7e7a0da7655547cb65c77bf900b6b8060 Mon Sep 17 00:00:00 2001 From: sepfy Date: Wed, 10 Jul 2024 10:49:14 +0800 Subject: [PATCH] Return error if tcp connection is failed --- src/peer_signaling.c | 7 ++++++- src/ssl_transport.c | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/peer_signaling.c b/src/peer_signaling.c index dabdac7..7aa81bd 100644 --- a/src/peer_signaling.c +++ b/src/peer_signaling.c @@ -307,6 +307,7 @@ static int peer_signaling_mqtt_connect(const char *hostname, int port) { bool session_present; if (ssl_transport_connect(&g_ps.net_ctx, hostname, port, NULL) < 0) { + LOGE("ssl transport connect failed"); return -1; } @@ -446,7 +447,11 @@ int peer_signaling_join_channel() { return -1; } - peer_signaling_mqtt_connect(g_ps.mqtt_host, g_ps.mqtt_port); + if (peer_signaling_mqtt_connect(g_ps.mqtt_host, g_ps.mqtt_port) < 0) { + LOGW("Connect MQTT server failed"); + return -1; + } + peer_signaling_mqtt_subscribe(1); return 0; } diff --git a/src/ssl_transport.c b/src/ssl_transport.c index e874dd1..fb58e7b 100644 --- a/src/ssl_transport.c +++ b/src/ssl_transport.c @@ -37,6 +37,7 @@ int ssl_transport_connect(NetworkContext_t *net_ctx, if ((ret = mbedtls_ctr_drbg_seed(&net_ctx->ctr_drbg, mbedtls_entropy_func, &net_ctx->entropy, (const unsigned char *) pers, strlen(pers))) != 0) { + return -1; } if ((ret = mbedtls_ssl_config_defaults(&net_ctx->conf, @@ -45,6 +46,7 @@ int ssl_transport_connect(NetworkContext_t *net_ctx, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { LOGE("ssl config error: -0x%x", (unsigned int) -ret); + return -1; } @@ -63,15 +65,20 @@ int ssl_transport_connect(NetworkContext_t *net_ctx, if ((ret = mbedtls_ssl_setup(&net_ctx->ssl, &net_ctx->conf)) != 0) { LOGE("ssl setup error: -0x%x", (unsigned int) -ret); + return -1; } if ((ret = mbedtls_ssl_set_hostname(&net_ctx->ssl, host)) != 0) { LOGE("ssl set hostname error: -0x%x", (unsigned int) -ret); + return -1; } tcp_socket_open(&net_ctx->tcp_socket); tcp_blocking_timeout(&net_ctx->tcp_socket, 1000); - ports_resolve_addr(host, &resolved_addr); + if (ports_resolve_addr(host, &resolved_addr) < 0) { + LOGE("Failed to connect tcp server"); + return -1; + } resolved_addr.port = port; tcp_socket_connect(&net_ctx->tcp_socket, &resolved_addr);