Skip to content

Commit

Permalink
add per server source for tls and dtls
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Mauchle committed Oct 17, 2019
1 parent 34cb139 commit 0abb5f4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ int dtlsconnect(struct server *server, int timeout, char *text) {
struct hostportres *hp;
unsigned long error;
BIO *bio;
struct addrinfo *source = NULL;

debug(DBG_DBG, "dtlsconnect: called from %s", text);
pthread_mutex_lock(&server->lock);
Expand All @@ -533,6 +534,13 @@ int dtlsconnect(struct server *server, int timeout, char *text) {
pthread_mutex_unlock(&server->lock);

hp = (struct hostportres *)list_first(server->conf->hostports)->data;

if(server->conf->source) {
source = resolvepassiveaddrinfo(server->conf->source, AF_UNSPEC, NULL, protodefs.socktype);
if(!source)
debug(DBG_WARN, "dtlsconnect: could not resolve source address to bind for server %s, using default", server->conf->name);
}

gettimeofday(&start, NULL);

for (;;) {
Expand All @@ -553,12 +561,13 @@ int dtlsconnect(struct server *server, int timeout, char *text) {
gettimeofday(&now, NULL);
if (timeout && (now.tv_sec - start.tv_sec) > timeout) {
debug(DBG_DBG, "tlsconnect: timeout");
if (source) freeaddrinfo(source);
return 0;
}

debug(DBG_INFO, "dtlsconnect: connecting to %s port %s", hp->host, hp->port);

if ((server->sock = bindtoaddr(srcres, hp->addrinfo->ai_family, 0)) < 0)
if ((server->sock = bindtoaddr(source ? source : srcres, hp->addrinfo->ai_family, 0)) < 0)
continue;
if (connect(server->sock, hp->addrinfo->ai_addr, hp->addrinfo->ai_addrlen))
continue;
Expand Down Expand Up @@ -607,6 +616,7 @@ int dtlsconnect(struct server *server, int timeout, char *text) {
server->conreset = 1;
pthread_cond_signal(&server->newrq_cond);
pthread_mutex_unlock(&server->newrq_mutex);
if (source) freeaddrinfo(source);
return 1;
}

Expand Down
12 changes: 11 additions & 1 deletion tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,20 @@ int tlsconnect(struct server *server, int timeout, char *text) {
SSL_CTX *ctx = NULL;
unsigned long error;
int origflags;
struct addrinfo *source = NULL;

debug(DBG_DBG, "tlsconnect: called from %s", text);
pthread_mutex_lock(&server->lock);
if (server->state == RSP_SERVER_STATE_CONNECTED)
server->state = RSP_SERVER_STATE_RECONNECTING;
pthread_mutex_unlock(&server->lock);

if(server->conf->source) {
source = resolvepassiveaddrinfo(server->conf->source, AF_UNSPEC, NULL, protodefs.socktype);
if(!source)
debug(DBG_WARN, "tlsconnect: could not resolve source address to bind for server %s, using default", server->conf->name);
}

gettimeofday(&start, NULL);

for (;;) {
Expand All @@ -116,11 +124,12 @@ int tlsconnect(struct server *server, int timeout, char *text) {
gettimeofday(&now, NULL);
if (timeout && (now.tv_sec - start.tv_sec) > timeout) {
debug(DBG_DBG, "tlsconnect: timeout");
if (source) freeaddrinfo(source);
return 0;
}

debug(DBG_INFO, "tlsconnect: connecting to %s", server->conf->name);
if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) < 0)
if ((server->sock = connecttcphostlist(server->conf->hostports, source ? source : srcres)) < 0)
continue;
if (server->conf->keepalive)
enable_keepalive(server->sock);
Expand Down Expand Up @@ -171,6 +180,7 @@ int tlsconnect(struct server *server, int timeout, char *text) {
server->conreset = 1;
pthread_cond_signal(&server->newrq_cond);
pthread_mutex_unlock(&server->newrq_mutex);
if (source) freeaddrinfo(source);
return 1;
}

Expand Down

0 comments on commit 0abb5f4

Please sign in to comment.