From d1b4837082a800978142a31d6b6d40e2e4814069 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 24 Jan 2013 13:21:48 +0100 Subject: resolver: write whole hostname instead of a pointer to the resolver pipe Shouldn't make a difference, but feels cleaner now, and silences a clang-analyzer warning. --- src/fastd.c | 18 +++++++++++------- src/fastd.h | 6 +++--- src/resolve.c | 40 ++++++++++++++++++++++++++-------------- 3 files changed, 40 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/fastd.c b/src/fastd.c index 18f9d96..e48bb8a 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -851,19 +851,25 @@ static void handle_socket(fastd_context_t *ctx, fastd_socket_t *sock) { static void handle_resolv_returns(fastd_context_t *ctx) { fastd_resolve_return_t resolve_return; - if (read(ctx->resolverfd, &resolve_return, sizeof(resolve_return)) < 0) { + while (read(ctx->resolverfd, &resolve_return, sizeof(resolve_return)) < 0) { if (errno != EINTR) - pr_warn(ctx, "read: %s", strerror(errno)); + exit_errno(ctx, "handle_resolv_return: read"); + } - return; + char hostname[resolve_return.hostname_len+1]; + while (read(ctx->resolverfd, hostname, resolve_return.hostname_len) < 0) { + if (errno != EINTR) + exit_errno(ctx, "handle_resolv_return: read"); } + hostname[resolve_return.hostname_len] = 0; + fastd_peer_t *peer; for (peer = ctx->peers; peer; peer = peer->next) { if (!peer->config) continue; - if (!strequal(peer->config->hostname, resolve_return.hostname)) + if (!strequal(peer->config->hostname, hostname)) continue; if (!fastd_peer_config_matches_dynamic(peer->config, &resolve_return.constraints)) @@ -875,13 +881,11 @@ static void handle_resolv_returns(fastd_context_t *ctx) { send_handshake(ctx, peer); } else { - pr_warn(ctx, "hostname `%s' resolved to address %I which is used by a fixed peer", resolve_return.hostname, &resolve_return.addr); + pr_warn(ctx, "hostname `%s' resolved to address %I which is used by a fixed peer", hostname, &resolve_return.addr); fastd_task_schedule_handshake(ctx, peer, fastd_rand(ctx, 17500, 22500)); } break; } - - free(resolve_return.hostname); } static inline void handle_socket_error(fastd_context_t *ctx, fastd_socket_t *sock) { diff --git a/src/fastd.h b/src/fastd.h index 9223ffe..b2b7de7 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -109,10 +109,10 @@ union fastd_peer_address { }; struct fastd_resolve_return { - char *hostname; - fastd_peer_address_t constraints; - fastd_peer_address_t addr; + + fastd_peer_address_t constraints; + size_t hostname_len; }; struct fastd_log_file { diff --git a/src/resolve.c b/src/resolve.c index b1ef696..02a986d 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -69,25 +69,32 @@ static void* resolve_peer(void *varg) { error = true; } - fastd_resolve_return_t ret; - memset(&ret, 0, sizeof(ret)); + size_t hostname_len = strlen(arg->hostname); + char buf[sizeof(fastd_resolve_return_t) + hostname_len]; - ret.hostname = arg->hostname; - ret.constraints = arg->constraints; + fastd_resolve_return_t *ret = (void*)buf; + char *hostname = buf + sizeof(fastd_resolve_return_t); + + memset(ret, 0, sizeof(fastd_resolve_return_t)); + + ret->constraints = arg->constraints; + ret->hostname_len = hostname_len; + memcpy(hostname, arg->hostname, hostname_len); if (!error) { pr_verbose(arg->ctx, "resolved host `%s' successfully", arg->hostname); - memcpy(&ret.addr, res->ai_addr, res->ai_addrlen); - fastd_peer_address_simplify(&ret.addr); + memcpy(&ret->addr, res->ai_addr, res->ai_addrlen); + fastd_peer_address_simplify(&ret->addr); } else { - ret.addr.sa.sa_family = AF_UNSPEC; + ret->addr.sa.sa_family = AF_UNSPEC; } - if (write(arg->ctx->resolvewfd, &ret, sizeof(ret)) < 0) + if (write(arg->ctx->resolvewfd, buf, sizeof(buf)) < 0) pr_error_errno(arg->ctx, "can't write resolve return"); freeaddrinfo(res); + free(arg->hostname); free(arg); return NULL; @@ -102,14 +109,19 @@ void fastd_resolve_peer(fastd_context_t *ctx, fastd_peer_t *peer) { if (timespec_diff(&ctx->now, &peer->last_resolve) < ctx->conf->min_resolve_interval*1000) { pr_debug(ctx, "not resolving %P as it has been resolved a short time ago", peer); - fastd_resolve_return_t ret; - memset(&ret, 0, sizeof(ret)); + size_t hostname_len = strlen(peer->config->hostname); + char buf[sizeof(fastd_resolve_return_t) + hostname_len]; + + fastd_resolve_return_t *ret = (void*)buf; + char *hostname = buf + sizeof(fastd_resolve_return_t); + + memset(ret, 0, sizeof(fastd_resolve_return_t)); - ret.hostname = strdup(peer->config->hostname); - ret.constraints = peer->config->address; - ret.addr = peer->address; + ret->constraints = peer->config->address; + ret->hostname_len = hostname_len; + memcpy(hostname, peer->config->hostname, hostname_len); - if (write(ctx->resolvewfd, &ret, sizeof(ret)) < 0) + if (write(ctx->resolvewfd, buf, sizeof(buf)) < 0) pr_error_errno(ctx, "can't write resolve return"); return; -- cgit v1.2.3