|
| 1 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 | +// Copyright (c) Status Research & Development GmbH |
| 3 | + |
| 4 | +#include <errno.h> |
| 5 | +#include <pthread.h> |
| 6 | +#include <stdbool.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | +#include <time.h> |
| 11 | + |
| 12 | +#include "libp2p.h" |
| 13 | + |
| 14 | +enum { |
| 15 | + LIBP2P_FFI_MUXER_YAMUX = 1, |
| 16 | + LIBP2P_FFI_TRANSPORT_TCP = 1, |
| 17 | + CALLBACK_TIMEOUT_SECONDS = 20, |
| 18 | +}; |
| 19 | + |
| 20 | +typedef struct { |
| 21 | + pthread_mutex_t mutex; |
| 22 | + pthread_cond_t cond; |
| 23 | + int done; |
| 24 | + int err_code; |
| 25 | + char err_msg[512]; |
| 26 | + LibP2PCtx *ctx; |
| 27 | + bool bool_reply; |
| 28 | +} CallbackWait; |
| 29 | + |
| 30 | +static void wait_init(CallbackWait *wait) { |
| 31 | + memset(wait, 0, sizeof(*wait)); |
| 32 | + wait->err_code = NIMFFI_RET_OK; |
| 33 | + if (pthread_mutex_init(&wait->mutex, NULL) != 0) { |
| 34 | + fprintf(stderr, "pthread_mutex_init failed\n"); |
| 35 | + exit(2); |
| 36 | + } |
| 37 | + if (pthread_cond_init(&wait->cond, NULL) != 0) { |
| 38 | + fprintf(stderr, "pthread_cond_init failed\n"); |
| 39 | + exit(2); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +static void wait_destroy(CallbackWait *wait) { |
| 44 | + pthread_cond_destroy(&wait->cond); |
| 45 | + pthread_mutex_destroy(&wait->mutex); |
| 46 | +} |
| 47 | + |
| 48 | +static void record_error(CallbackWait *wait, int err_code, |
| 49 | + const char *err_msg) { |
| 50 | + wait->err_code = err_code; |
| 51 | + if (err_msg != NULL && err_msg[0] != '\0') { |
| 52 | + snprintf(wait->err_msg, sizeof(wait->err_msg), "%s", err_msg); |
| 53 | + } else if (err_code != NIMFFI_RET_OK) { |
| 54 | + snprintf(wait->err_msg, sizeof(wait->err_msg), "FFI returned error code %d", |
| 55 | + err_code); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +static void signal_done(CallbackWait *wait) { |
| 60 | + wait->done = 1; |
| 61 | + pthread_cond_signal(&wait->cond); |
| 62 | +} |
| 63 | + |
| 64 | +static void on_created(int err_code, LibP2PCtx *ctx, const char *err_msg, |
| 65 | + void *user_data) { |
| 66 | + CallbackWait *wait = (CallbackWait *)user_data; |
| 67 | + pthread_mutex_lock(&wait->mutex); |
| 68 | + record_error(wait, err_code, err_msg); |
| 69 | + if (err_code == NIMFFI_RET_OK && ctx == NULL) { |
| 70 | + record_error(wait, -1, "create callback returned a null context"); |
| 71 | + } else { |
| 72 | + wait->ctx = ctx; |
| 73 | + } |
| 74 | + signal_done(wait); |
| 75 | + pthread_mutex_unlock(&wait->mutex); |
| 76 | +} |
| 77 | + |
| 78 | +static void on_bool_reply(int err_code, const bool *reply, const char *err_msg, |
| 79 | + void *user_data) { |
| 80 | + CallbackWait *wait = (CallbackWait *)user_data; |
| 81 | + pthread_mutex_lock(&wait->mutex); |
| 82 | + record_error(wait, err_code, err_msg); |
| 83 | + if (err_code == NIMFFI_RET_OK && reply == NULL) { |
| 84 | + record_error(wait, -1, "bool callback returned a null reply"); |
| 85 | + } else if (reply != NULL) { |
| 86 | + wait->bool_reply = *reply; |
| 87 | + } |
| 88 | + signal_done(wait); |
| 89 | + pthread_mutex_unlock(&wait->mutex); |
| 90 | +} |
| 91 | + |
| 92 | +static int wait_for_callback(CallbackWait *wait, const char *op) { |
| 93 | + struct timespec deadline; |
| 94 | + if (clock_gettime(CLOCK_REALTIME, &deadline) != 0) { |
| 95 | + fprintf(stderr, "%s: clock_gettime failed: %s\n", op, strerror(errno)); |
| 96 | + return 1; |
| 97 | + } |
| 98 | + deadline.tv_sec += CALLBACK_TIMEOUT_SECONDS; |
| 99 | + |
| 100 | + pthread_mutex_lock(&wait->mutex); |
| 101 | + int rc = 0; |
| 102 | + while (!wait->done && rc == 0) { |
| 103 | + rc = pthread_cond_timedwait(&wait->cond, &wait->mutex, &deadline); |
| 104 | + } |
| 105 | + |
| 106 | + int err_code = wait->err_code; |
| 107 | + char err_msg[sizeof(wait->err_msg)]; |
| 108 | + snprintf(err_msg, sizeof(err_msg), "%s", wait->err_msg); |
| 109 | + pthread_mutex_unlock(&wait->mutex); |
| 110 | + |
| 111 | + if (rc == ETIMEDOUT) { |
| 112 | + fprintf(stderr, "%s: timed out after %d seconds\n", op, |
| 113 | + CALLBACK_TIMEOUT_SECONDS); |
| 114 | + return 1; |
| 115 | + } |
| 116 | + if (rc != 0) { |
| 117 | + fprintf(stderr, "%s: pthread_cond_timedwait failed: %s\n", op, |
| 118 | + strerror(rc)); |
| 119 | + return 1; |
| 120 | + } |
| 121 | + if (err_code != NIMFFI_RET_OK) { |
| 122 | + fprintf(stderr, "%s: %s\n", op, |
| 123 | + err_msg[0] ? err_msg : "FFI callback failed"); |
| 124 | + return 1; |
| 125 | + } |
| 126 | + return 0; |
| 127 | +} |
| 128 | + |
| 129 | +static int create_node(LibP2PCtx **out_ctx) { |
| 130 | + Libp2pConfig config; |
| 131 | + memset(&config, 0, sizeof(config)); |
| 132 | + |
| 133 | + NimFfiStr listen_addrs[] = {nimffi_str("/ip4/127.0.0.1/tcp/0")}; |
| 134 | + config.addrs.data = listen_addrs; |
| 135 | + config.addrs.len = 1; |
| 136 | + config.dnsResolver = nimffi_str(""); |
| 137 | + config.transport = LIBP2P_FFI_TRANSPORT_TCP; |
| 138 | + config.muxer = LIBP2P_FFI_MUXER_YAMUX; |
| 139 | + config.maxConnections = 8; |
| 140 | + config.maxIn = 4; |
| 141 | + config.maxOut = 4; |
| 142 | + config.maxConnsPerPeer = 2; |
| 143 | + |
| 144 | + CallbackWait create_wait; |
| 145 | + wait_init(&create_wait); |
| 146 | + int submit = libp2p_ctx_create(&config, on_created, &create_wait); |
| 147 | + if (submit != 0) { |
| 148 | + fprintf(stderr, "create: submit failed\n"); |
| 149 | + wait_destroy(&create_wait); |
| 150 | + return 1; |
| 151 | + } |
| 152 | + if (wait_for_callback(&create_wait, "create") != 0) { |
| 153 | + wait_destroy(&create_wait); |
| 154 | + return 1; |
| 155 | + } |
| 156 | + |
| 157 | + *out_ctx = create_wait.ctx; |
| 158 | + wait_destroy(&create_wait); |
| 159 | + return 0; |
| 160 | +} |
| 161 | + |
| 162 | +static int call_bool_method( |
| 163 | + const char *op, LibP2PCtx *ctx, |
| 164 | + int (*fn)(const LibP2PCtx *, |
| 165 | + void (*)(int, const bool *, const char *, void *), void *)) { |
| 166 | + CallbackWait wait; |
| 167 | + wait_init(&wait); |
| 168 | + int submit = fn(ctx, on_bool_reply, &wait); |
| 169 | + if (submit != 0) { |
| 170 | + fprintf(stderr, "%s: submit failed\n", op); |
| 171 | + wait_destroy(&wait); |
| 172 | + return 1; |
| 173 | + } |
| 174 | + if (wait_for_callback(&wait, op) != 0) { |
| 175 | + wait_destroy(&wait); |
| 176 | + return 1; |
| 177 | + } |
| 178 | + if (!wait.bool_reply) { |
| 179 | + fprintf(stderr, "%s: expected true reply\n", op); |
| 180 | + wait_destroy(&wait); |
| 181 | + return 1; |
| 182 | + } |
| 183 | + wait_destroy(&wait); |
| 184 | + return 0; |
| 185 | +} |
| 186 | + |
| 187 | +int main(void) { |
| 188 | + setvbuf(stdout, NULL, _IONBF, 0); |
| 189 | + setvbuf(stderr, NULL, _IONBF, 0); |
| 190 | + |
| 191 | + LibP2PCtx *ctx = NULL; |
| 192 | + if (create_node(&ctx) != 0) { |
| 193 | + return 1; |
| 194 | + } |
| 195 | + |
| 196 | + int rc = 0; |
| 197 | + if (call_bool_method("start", ctx, libp2p_ctx_start) != 0) { |
| 198 | + rc = 1; |
| 199 | + goto cleanup; |
| 200 | + } |
| 201 | + if (call_bool_method("stop", ctx, libp2p_ctx_stop) != 0) { |
| 202 | + rc = 1; |
| 203 | + goto cleanup; |
| 204 | + } |
| 205 | + |
| 206 | +cleanup: |
| 207 | + libp2p_ctx_destroy(ctx); |
| 208 | + return rc; |
| 209 | +} |
0 commit comments