diff --git a/src/conf.c b/src/conf.c index 5d5b2801..7c7ad4bc 100644 --- a/src/conf.c +++ b/src/conf.c @@ -211,7 +211,8 @@ static int getserver(const char *server, ddns_name_t *name) if (-1 == name->port) name->port = HTTP_DEFAULT_PORT; } else { - name->port = HTTP_DEFAULT_PORT; + /* Let *ssl.c and tcp.c figure it out later */ + name->port = 0; } strlcpy(name->name, str, sizeof(name->name)); diff --git a/src/gnutls.c b/src/gnutls.c index 23a5f24c..140f87ef 100644 --- a/src/gnutls.c +++ b/src/gnutls.c @@ -195,6 +195,7 @@ int ssl_open(http_t *client, char *msg) const gnutls_datum_t *cert_list; unsigned int cert_list_size = 0; gnutls_x509_crt_t cert; + int port = 0; if (!client->ssl_enabled) return tcp_init(&client->tcp, msg); @@ -222,7 +223,9 @@ int ssl_open(http_t *client, char *msg) gnutls_credentials_set(client->ssl, GNUTLS_CRD_CERTIFICATE, xcred); /* connect to the peer */ - tcp_set_port(&client->tcp, HTTPS_DEFAULT_PORT); + http_get_port(client, &port); + if (!port) + http_set_port(client, HTTPS_DEFAULT_PORT); DO(tcp_init(&client->tcp, msg)); /* Forward TCP socket to GnuTLS, the set_int() API is perhaps too new still ... since 3.1.9 */ diff --git a/src/http.c b/src/http.c index 55a7b4d6..7cc51cee 100644 --- a/src/http.c +++ b/src/http.c @@ -51,16 +51,11 @@ int http_destruct(http_t *client, int num) static int local_set_params(http_t *client) { int timeout = 0; - int port = 0; http_get_remote_timeout(client, &timeout); if (timeout == 0) http_set_remote_timeout(client, HTTP_DEFAULT_TIMEOUT); - http_get_port(client, &port); - if (port == 0) - http_set_port(client, HTTP_DEFAULT_PORT); - return 0; } diff --git a/src/mbedtls.c b/src/mbedtls.c index 4f113ded..7f0a8b8e 100644 --- a/src/mbedtls.c +++ b/src/mbedtls.c @@ -11,12 +11,15 @@ void ssl_exit(void) {} int ssl_open(http_t *client, char *msg) { + int port = 0; int rc; if (!client->ssl_enabled) return tcp_init(&client->tcp, msg); - tcp_set_port(&client->tcp, HTTPS_DEFAULT_PORT); + http_get_port(client, &port); + if (!port) + http_set_port(client, HTTPS_DEFAULT_PORT); rc = tcp_init(&client->tcp, msg); if (rc) return rc; diff --git a/src/openssl.c b/src/openssl.c index f06f64d0..57a09f31 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -150,13 +150,16 @@ int ssl_open(http_t *client, char *msg) { const char *sn; char buf[512]; + int port = 0; X509 *cert; int rc; if (!client->ssl_enabled) return tcp_init(&client->tcp, msg); - tcp_set_port(&client->tcp, HTTPS_DEFAULT_PORT); + http_get_port(client, &port); + if (!port) + http_set_port(client, HTTPS_DEFAULT_PORT); DO(tcp_init(&client->tcp, msg)); logit(LOG_INFO, "%s, initiating HTTPS ...", msg); diff --git a/src/tcp.c b/src/tcp.c index 24797baa..2ccf557b 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -34,6 +34,7 @@ #include #include +#include "http.h" #include "log.h" #include "tcp.h" @@ -105,6 +106,16 @@ static void set_timeouts(int sd, int timeout) logit(LOG_INFO, "Failed setting send timeout socket option: %s", strerror(errno)); } +static void set_params(tcp_sock_t *tcp) +{ + int port = 0; + + tcp_get_port(tcp, &port); + if (port == 0) + tcp_set_port(tcp, HTTP_DEFAULT_PORT); + +} + int tcp_init(tcp_sock_t *tcp, char *msg) { int rc = 0; @@ -114,6 +125,7 @@ int tcp_init(tcp_sock_t *tcp, char *msg) if (tcp->initialized == 1) return 0; + set_params(tcp); do { int s, sd, tries = 0; char port[10];