Skip to content

Commit

Permalink
Fix #385: use configured server:port, don't force 443 for HTTPS
Browse files Browse the repository at this point in the history
Move port defaulting as late as possible in the connection phase.  If a
user has configured a port value we use that, otherwise we leave it up
to the selected backend to figure out a good default port (80/443).

Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed May 11, 2022
1 parent cbfa3bd commit 0b5ac98
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
5 changes: 4 additions & 1 deletion src/gnutls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down
5 changes: 0 additions & 5 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion src/mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <netinet/in.h>
#include <resolv.h>

#include "http.h"
#include "log.h"
#include "tcp.h"

Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand Down

0 comments on commit 0b5ac98

Please sign in to comment.