summaryrefslogtreecommitdiffstats
path: root/src/fastd.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-04-17 21:42:22 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-04-17 21:44:02 +0200
commitdaf3d6e8db9ef39b50dce040f864fb409bd7044a (patch)
tree49abaa8d953a515f4b8b445d46f7480ee1f8c3fd /src/fastd.c
parent41ea8e454937444da4d2bb33fe768adb5b02776b (diff)
downloadfastd-daf3d6e8db9ef39b50dce040f864fb409bd7044a.tar
fastd-daf3d6e8db9ef39b50dce040f864fb409bd7044a.zip
Fix handling of the local address in shell commands
Without this fix, using on-establish/disestablish/verify would cause a strange zero port when a bind with a random port was used, and a segmentation fault with dynamic binds.
Diffstat (limited to 'src/fastd.c')
-rw-r--r--src/fastd.c40
1 files changed, 36 insertions, 4 deletions
diff --git a/src/fastd.c b/src/fastd.c
index 3c3d130..d82589e 100644
--- a/src/fastd.c
+++ b/src/fastd.c
@@ -261,13 +261,33 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b
if (warn) {
if (addr->bindtodev)
pr_warn(ctx, "unable to bind to %B on `%s'", &addr->addr, addr->bindtodev);
- else
+ else
pr_warn(ctx, "unable to bind to %B", &addr->addr);
}
return -1;
}
+static bool set_bound_address(fastd_context_t *ctx, fastd_socket_t *sock) {
+ fastd_peer_address_t addr = {};
+ socklen_t len = sizeof(addr);
+
+ if (getsockname(sock->fd, &addr.sa, &len) < 0) {
+ pr_error_errno(ctx, "getsockname");
+ return false;
+ }
+
+ if (len > sizeof(addr)) {
+ pr_error(ctx, "getsockname: got strange long address");
+ return false;
+ }
+
+ sock->bound_addr = calloc(1, sizeof(addr));
+ *sock->bound_addr = addr;
+
+ return true;
+}
+
static bool bind_sockets(fastd_context_t *ctx) {
unsigned i;
@@ -278,10 +298,15 @@ static bool bind_sockets(fastd_context_t *ctx) {
ctx->socks[i].fd = bind_socket(ctx, ctx->socks[i].addr, ctx->socks[i].fd < -1);
if (ctx->socks[i].fd >= 0) {
+ if (!set_bound_address(ctx, &ctx->socks[i])) {
+ fastd_socket_close(ctx, &ctx->socks[i]);
+ continue;
+ }
+
if (ctx->socks[i].addr->bindtodev)
- pr_info(ctx, "successfully bound to %B on `%s'", &ctx->socks[i].addr->addr, ctx->socks[i].addr->bindtodev);
+ pr_info(ctx, "successfully bound to %B on `%s'", ctx->socks[i].bound_addr, ctx->socks[i].addr->bindtodev);
else
- pr_info(ctx, "successfully bound to %B", &ctx->socks[i].addr->addr);
+ pr_info(ctx, "successfully bound to %B", ctx->socks[i].bound_addr);
}
}
@@ -302,8 +327,15 @@ fastd_socket_t* fastd_socket_open(fastd_context_t *ctx, fastd_peer_t *peer, int
sock->fd = fd;
sock->addr = NULL;
+ sock->bound_addr = NULL;
sock->peer = peer;
+ if (!set_bound_address(ctx, sock)) {
+ fastd_socket_close(ctx, sock);
+ free(sock);
+ return NULL;
+ }
+
return sock;
}
@@ -799,7 +831,7 @@ static void handle_socket(fastd_context_t *ctx, fastd_socket_t *sock) {
fastd_peer_address_t recvaddr;
socklen_t recvaddrlen = sizeof(recvaddr);
-
+
ssize_t len = recvfrom(sock->fd, buffer.data, buffer.len, 0, (struct sockaddr*)&recvaddr, &recvaddrlen);
if (len <= 0) {
if (len < 0 && errno != EINTR)