summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-04-10 21:04:27 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-04-10 21:04:27 +0200
commite5a8f900352b242860ac3dd4e3b39c7297169480 (patch)
tree6044dfb9aea951575e63552f518c52399638d7eb /src
parent80f981c0521dc8d7202d27430f3a5280bbd8484d (diff)
downloadfastd-e5a8f900352b242860ac3dd4e3b39c7297169480.tar
fastd-e5a8f900352b242860ac3dd4e3b39c7297169480.zip
Add support for multiple async message types
Diffstat (limited to 'src')
-rw-r--r--src/async.c29
-rw-r--r--src/async.h10
-rw-r--r--src/fastd.h6
-rw-r--r--src/resolve.c8
-rw-r--r--src/types.h6
5 files changed, 44 insertions, 15 deletions
diff --git a/src/async.c b/src/async.c
index e0d4feb..28895b3 100644
--- a/src/async.c
+++ b/src/async.c
@@ -26,7 +26,6 @@
#include "async.h"
#include "fastd.h"
-#include "peer.h"
void fastd_async_init(fastd_context_t *ctx) {
@@ -34,7 +33,7 @@ void fastd_async_init(fastd_context_t *ctx) {
}
static void handle_resolve_return(fastd_context_t *ctx) {
- fastd_resolve_return_t resolve_return;
+ fastd_async_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");
@@ -69,5 +68,29 @@ static void handle_resolve_return(fastd_context_t *ctx) {
}
void fastd_async_handle(fastd_context_t *ctx) {
- handle_resolve_return(ctx);
+ fastd_async_type_t type;
+
+ while (read(ctx->async_rfd, &type, sizeof(type)) < 0) {
+ if (errno != EINTR)
+ exit_errno(ctx, "fastd_async_handle: read");
+ }
+
+ switch (type) {
+ case ASYNC_TYPE_RESOLVE_RETURN:
+ handle_resolve_return(ctx);
+ break;
+
+ default:
+ exit_bug(ctx, "fastd_async_handle: unknown type");
+ }
+}
+
+void fastd_async_enqueue(fastd_context_t *ctx, fastd_async_type_t type, const void *data, size_t len) {
+ struct iovec vec[2] = {
+ { .iov_base = &type, .iov_len = sizeof(type) },
+ { .iov_base = (void *)data, .iov_len = len },
+ };
+
+ if (writev(ctx->async_wfd, vec, 2) < 0)
+ pr_error_errno(ctx, "fastd_async_enqueue");
}
diff --git a/src/async.h b/src/async.h
index 8ee6314..a983ee9 100644
--- a/src/async.h
+++ b/src/async.h
@@ -27,8 +27,16 @@
#pragma once
#include "types.h"
+#include "peer.h"
+
+
+struct fastd_async_resolve_return {
+ fastd_remote_t *remote;
+ size_t n_addr;
+ fastd_peer_address_t addr[];
+};
void fastd_async_init(fastd_context_t *ctx);
void fastd_async_handle(fastd_context_t *ctx);
-
+void fastd_async_enqueue(fastd_context_t *ctx, fastd_async_type_t type, const void *data, size_t len);
diff --git a/src/fastd.h b/src/fastd.h
index 3070a07..9412116 100644
--- a/src/fastd.h
+++ b/src/fastd.h
@@ -78,12 +78,6 @@ union fastd_peer_address {
struct sockaddr_in6 in6;
};
-struct fastd_resolve_return {
- fastd_remote_t *remote;
- size_t n_addr;
- fastd_peer_address_t addr[];
-};
-
struct fastd_log_file {
fastd_log_file_t *next;
diff --git a/src/resolve.c b/src/resolve.c
index 6267838..ead1985 100644
--- a/src/resolve.c
+++ b/src/resolve.c
@@ -26,6 +26,7 @@
#include "fastd.h"
#include "peer.h"
+#include "async.h"
#include <netdb.h>
#include <pthread.h>
@@ -69,8 +70,8 @@ static void* resolve_peer(void *varg) {
n_addr++;
}
- uint8_t retbuf[sizeof(fastd_resolve_return_t) + n_addr*sizeof(fastd_peer_address_t)] __attribute__((aligned(8)));
- fastd_resolve_return_t *ret = (fastd_resolve_return_t*)retbuf;
+ uint8_t retbuf[sizeof(fastd_async_resolve_return_t) + n_addr*sizeof(fastd_peer_address_t)] __attribute__((aligned(8)));
+ fastd_async_resolve_return_t *ret = (fastd_async_resolve_return_t*)retbuf;
ret->remote = arg->remote;
if (n_addr) {
@@ -94,8 +95,7 @@ static void* resolve_peer(void *varg) {
ret->n_addr = n_addr;
- 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");
+ fastd_async_enqueue(arg->ctx, ASYNC_TYPE_RESOLVE_RETURN, ret, sizeof(fastd_async_resolve_return_t) + n_addr*sizeof(fastd_peer_address_t));
freeaddrinfo(res);
free(arg->hostname);
diff --git a/src/types.h b/src/types.h
index cb9f952..a474b78 100644
--- a/src/types.h
+++ b/src/types.h
@@ -90,6 +90,10 @@ typedef enum fastd_loglevel {
LL_DEBUG2,
} fastd_loglevel_t;
+typedef enum fastd_async_type {
+ ASYNC_TYPE_RESOLVE_RETURN,
+} fastd_async_type_t;
+
typedef struct fastd_buffer fastd_buffer_t;
@@ -128,7 +132,7 @@ typedef struct fastd_handshake fastd_handshake_t;
typedef struct fastd_lex fastd_lex_t;
typedef struct fastd_string_stack fastd_string_stack_t;
-typedef struct fastd_resolve_return fastd_resolve_return_t;
+typedef struct fastd_async_resolve_return fastd_async_resolve_return_t;
typedef union fastd_block128 {