diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-04-10 20:17:18 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-04-10 20:17:18 +0200 |
commit | 80f981c0521dc8d7202d27430f3a5280bbd8484d (patch) | |
tree | 6992b4b7e8d5422e836b790853fdd2186b8b19ca /src | |
parent | 2b3043e578f00a5071dfae40c237b86cc6d70c7e (diff) | |
download | fastd-80f981c0521dc8d7202d27430f3a5280bbd8484d.tar fastd-80f981c0521dc8d7202d27430f3a5280bbd8484d.zip |
Move handling of resolve returns requests to a new source file
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/async.c | 73 | ||||
-rw-r--r-- | src/async.h | 34 | ||||
-rw-r--r-- | src/fastd.c | 52 | ||||
-rw-r--r-- | src/fastd.h | 5 | ||||
-rw-r--r-- | src/resolve.c | 2 |
6 files changed, 120 insertions, 49 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e7fb940..750883f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,11 +18,12 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/fastd_config.h.in ${CMAKE_CURRENT_BIN BISON_TARGET(fastd_config_parse config.y ${CMAKE_CURRENT_BINARY_DIR}/config.yy.c) add_executable(fastd - fastd.c + async.c capabilities.c config.c handshake.c hkdf_sha256.c + fastd.c lex.c log.c options.c diff --git a/src/async.c b/src/async.c new file mode 100644 index 0000000..e0d4feb --- /dev/null +++ b/src/async.c @@ -0,0 +1,73 @@ +/* + Copyright (c) 2012-2014, Matthias Schiffer <mschiffer@universe-factory.net> + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "async.h" +#include "fastd.h" +#include "peer.h" + + +void fastd_async_init(fastd_context_t *ctx) { + fastd_open_pipe(ctx, &ctx->async_rfd, &ctx->async_wfd); +} + +static void handle_resolve_return(fastd_context_t *ctx) { + fastd_resolve_return_t resolve_return; + while (read(ctx->async_rfd, &resolve_return, sizeof(resolve_return)) < 0) { + if (errno != EINTR) + exit_errno(ctx, "handle_resolve_return: read"); + } + + fastd_peer_address_t addresses[resolve_return.n_addr]; + while (read(ctx->async_rfd, &addresses, sizeof(addresses)) < 0) { + if (errno != EINTR) + exit_errno(ctx, "handle_resolve_return: read"); + } + + fastd_peer_t *peer; + for (peer = ctx->peers; peer; peer = peer->next) { + if (!peer->config) + continue; + + fastd_remote_t *remote; + for (remote = peer->remotes; remote; remote = remote->next) { + if (remote == resolve_return.remote) + break; + } + + if (!remote) + continue; + + fastd_peer_handle_resolve(ctx, peer, remote, resolve_return.n_addr, addresses); + + break; + } + + fastd_remote_unref(resolve_return.remote); +} + +void fastd_async_handle(fastd_context_t *ctx) { + handle_resolve_return(ctx); +} diff --git a/src/async.h b/src/async.h new file mode 100644 index 0000000..8ee6314 --- /dev/null +++ b/src/async.h @@ -0,0 +1,34 @@ +/* + Copyright (c) 2012-2014, Matthias Schiffer <mschiffer@universe-factory.net> + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#pragma once + +#include "types.h" + + +void fastd_async_init(fastd_context_t *ctx); +void fastd_async_handle(fastd_context_t *ctx); + diff --git a/src/fastd.c b/src/fastd.c index bbc5c61..6807ca5 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -25,6 +25,7 @@ #include "fastd.h" +#include "async.h" #include "config.h" #include "crypto.h" #include "handshake.h" @@ -115,7 +116,7 @@ static void init_signals(fastd_context_t *ctx) { } -static void open_pipe(fastd_context_t *ctx, int *readfd, int *writefd) { +void fastd_open_pipe(fastd_context_t *ctx, int *readfd, int *writefd) { int pipefd[2]; if (pipe(pipefd)) @@ -128,10 +129,6 @@ static void open_pipe(fastd_context_t *ctx, int *readfd, int *writefd) { *writefd = pipefd[1]; } -static inline void init_pipes(fastd_context_t *ctx) { - open_pipe(ctx, &ctx->resolverfd, &ctx->resolvewfd); -} - static void init_log(fastd_context_t *ctx) { uid_t uid = geteuid(); gid_t gid = getegid(); @@ -522,41 +519,6 @@ static void handle_tun(fastd_context_t *ctx) { fastd_send_all(ctx, NULL, buffer); } -static void handle_resolve_returns(fastd_context_t *ctx) { - fastd_resolve_return_t resolve_return; - while (read(ctx->resolverfd, &resolve_return, sizeof(resolve_return)) < 0) { - if (errno != EINTR) - exit_errno(ctx, "handle_resolve_return: read"); - } - - fastd_peer_address_t addresses[resolve_return.n_addr]; - while (read(ctx->resolverfd, &addresses, sizeof(addresses)) < 0) { - if (errno != EINTR) - exit_errno(ctx, "handle_resolve_return: read"); - } - - fastd_peer_t *peer; - for (peer = ctx->peers; peer; peer = peer->next) { - if (!peer->config) - continue; - - fastd_remote_t *remote; - for (remote = peer->remotes; remote; remote = remote->next) { - if (remote == resolve_return.remote) - break; - } - - if (!remote) - continue; - - fastd_peer_handle_resolve(ctx, peer, remote, resolve_return.n_addr, addresses); - - break; - } - - fastd_remote_unref(resolve_return.remote); -} - static inline int handshake_timeout(fastd_context_t *ctx) { if (!ctx->handshake_queue.next) return -1; @@ -575,7 +537,7 @@ static void handle_input(fastd_context_t *ctx) { struct pollfd fds[n_fds]; fds[0].fd = ctx->tunfd; fds[0].events = POLLIN; - fds[1].fd = ctx->resolverfd; + fds[1].fd = ctx->async_rfd; fds[1].events = POLLIN; unsigned i; @@ -621,7 +583,7 @@ static void handle_input(fastd_context_t *ctx) { if (fds[0].revents & POLLIN) handle_tun(ctx); if (fds[1].revents & POLLIN) - handle_resolve_returns(ctx); + fastd_async_handle(ctx); for (i = 2; i < ctx->n_socks+2; i++) { if (fds[i].revents & (POLLERR|POLLHUP|POLLNVAL)) @@ -789,7 +751,7 @@ static int daemonize(fastd_context_t *ctx) { uint8_t status = 1; int parent_rpipe, parent_wpipe; - open_pipe(ctx, &parent_rpipe, &parent_wpipe); + fastd_open_pipe(ctx, &parent_rpipe, &parent_wpipe); pid_t fork1 = fork(); @@ -805,7 +767,7 @@ static int daemonize(fastd_context_t *ctx) { pr_error_errno(ctx, "setsid"); int child_rpipe, child_wpipe; - open_pipe(ctx, &child_rpipe, &child_wpipe); + fastd_open_pipe(ctx, &child_rpipe, &child_wpipe); pid_t fork2 = fork(); @@ -976,7 +938,7 @@ int main(int argc, char *argv[]) { /* change groups early as the can be relevant for file access (for PID file & log files) */ set_groups(&ctx); - init_pipes(&ctx); + fastd_async_init(&ctx); init_sockets(&ctx); if (!fastd_socket_handle_binds(&ctx)) diff --git a/src/fastd.h b/src/fastd.h index 0c3b7a4..3070a07 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -262,8 +262,8 @@ struct fastd_context { fastd_dlist_head_t handshake_queue; struct timespec next_keepalives; - int resolverfd; - int resolvewfd; + int async_rfd; + int async_wfd; int tunfd; @@ -309,6 +309,7 @@ fastd_socket_t* fastd_socket_open(fastd_context_t *ctx, fastd_peer_t *peer, int void fastd_socket_close(fastd_context_t *ctx, fastd_socket_t *sock); void fastd_socket_error(fastd_context_t *ctx, fastd_socket_t *sock); +void fastd_open_pipe(fastd_context_t *ctx, int *readfd, int *writefd); void fastd_setfd(const fastd_context_t *ctx, int fd, int set, int unset); void fastd_setfl(const fastd_context_t *ctx, int fd, int set, int unset); diff --git a/src/resolve.c b/src/resolve.c index 673c606..6267838 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -94,7 +94,7 @@ static void* resolve_peer(void *varg) { ret->n_addr = n_addr; - if (write(arg->ctx->resolvewfd, ret, sizeof(fastd_resolve_return_t) + n_addr*sizeof(fastd_peer_address_t)) < 0) + if (write(arg->ctx->async_wfd, ret, sizeof(fastd_resolve_return_t) + n_addr*sizeof(fastd_peer_address_t)) < 0) pr_error_errno(arg->ctx, "can't write resolve return"); freeaddrinfo(res); |