diff --git a/lib/libDtChat/Makefile.am b/lib/libDtChat/Makefile.am index 7002d087..9584cd0f 100644 --- a/lib/libDtChat/Makefile.am +++ b/lib/libDtChat/Makefile.am @@ -11,14 +11,14 @@ libDtChatIpc_la_LDFLAGS = -version-info 1:0:0 libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c adapter.c instance.c \ - irc.c identity.c peers.c dc_internal.h -libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) + irc.c identity.c peers.c local.c dc_internal.h +libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS) libDtChat_la_LDFLAGS = -version-info 1:0:0 # Built by make mcde-host as well, so tools/mcde-tests.sh finds them in # build/mcde-host; make check still builds and runs them. noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text test_irc \ - test_identity test_peers test_adapter test_instance fake_adapter + test_identity test_peers test_local test_adapter test_instance fake_adapter test_json_SOURCES = test_json.c test_json_CPPFLAGS = -I$(top_srcdir)/include test_json_LDADD = libDtChatIpc.la @@ -56,5 +56,8 @@ test_identity_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) test_peers_SOURCES = test_peers.c test_peers_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) test_peers_LDADD = libDtChat.la libDtChatIpc.la +test_local_SOURCES = test_local.c +test_local_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) +test_local_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS) -lpthread TESTS = test_json test_frame test_loop test_store test_conv test_text test_irc \ - test_identity test_peers test_adapter test_instance + test_identity test_peers test_local test_adapter test_instance diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index 41ed9f06..3845ceec 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -176,6 +176,7 @@ void dc_identity_free(DcIdentity *id); const char *dc_identity_pin(const DcIdentity *id); const char *dc_identity_public_b64(const DcIdentity *id); const char *dc_identity_cert_pem(const DcIdentity *id); +const char *dc_identity_key_pem(const DcIdentity *id); /* * Local peers. One peer per identity id; dc_peers_announce() takes the @@ -208,6 +209,29 @@ int dc_peer_online(const DcPeer *peer); int dc_peer_blocked(const DcPeer *peer); int dc_peer_manual(const DcPeer *peer); +/* + * The local peer listener. dc_local_server_new() binds HOST (NULL or + * "0.0.0.0" = all interfaces) and PORT (0 = ephemeral); the identity is the + * TLS server certificate. A peer sends {"type":"hello","id":...,"pin":...}; + * ID and PIN are checked against EXPECT_ID/EXPECT_PIN (either may be NULL) + * and a hello-ack answers ok true/false. A wrong PIN closes the connection. + * FN is called once with the accepted hello (owned by the connection and + * valid only during the call). Everything runs from DcLoop. + */ +typedef struct DcLocalServer DcLocalServer; +typedef struct DcLocalConn DcLocalConn; +typedef void (*DcLocalHelloFn)(DcLocalConn *conn, const DcJson *hello, + void *arg); + +DcLocalServer *dc_local_server_new(DcLoop *loop, const DcIdentity *identity, + const char *host, int port, const char *expect_id, const char *expect_pin, + DcLocalHelloFn fn, void *arg); +int dc_local_server_port(const DcLocalServer *server); +void dc_local_server_free(DcLocalServer *server); +void dc_local_conn_close(DcLocalConn *conn); +const char *dc_local_conn_peer_id(const DcLocalConn *conn); +const char *dc_local_conn_peer_pin(const DcLocalConn *conn); + /* * IRC lines (RFC 1459/2812 + IRCv3 tags). dc_irc_parse() copies the tags, * the prefix, the command, up to 15 parameters and the trailing text; diff --git a/lib/libDtChat/identity.c b/lib/libDtChat/identity.c index 64958ece..f92d33cf 100644 --- a/lib/libDtChat/identity.c +++ b/lib/libDtChat/identity.c @@ -28,6 +28,7 @@ struct DcIdentity { EVP_PKEY *key; X509 *cert; char *cert_pem; + char *key_pem; char pin[80]; char pub_b64[64]; }; @@ -85,6 +86,36 @@ sha256_hex(const unsigned char *data, size_t len, char *out, size_t outlen) return 0; } +static char * +key_to_pem(EVP_PKEY *key) +{ + BIO *bio = BIO_new(BIO_s_mem()); + char *data = NULL; + long len; + + if (bio == NULL) + return NULL; + if (PEM_write_bio_PrivateKey(bio, key, NULL, NULL, 0, NULL, NULL) != 1) { + BIO_free(bio); + return NULL; + } + len = BIO_get_mem_data(bio, &data); + if (len <= 0) { + BIO_free(bio); + return NULL; + } + { + char *pem = malloc((size_t)len + 1); + + if (pem != NULL) { + memcpy(pem, data, (size_t)len); + pem[len] = '\0'; + } + BIO_free(bio); + return pem; + } +} + static char * cert_to_pem(X509 *cert) { @@ -189,7 +220,8 @@ identity_generate(void) return NULL; } id->cert_pem = cert_to_pem(id->cert); - if (id->cert_pem == NULL || + id->key_pem = key_to_pem(id->key); + if (id->cert_pem == NULL || id->key_pem == NULL || cert_pin(id->cert, id->pin, sizeof(id->pin)) != 0 || public_b64(id->key, id->pub_b64, sizeof(id->pub_b64)) != 0) { dc_identity_free(id); @@ -248,7 +280,8 @@ identity_from_json(const DcJson *json) if (X509_check_private_key(id->cert, id->key) != 1) goto fail; id->cert_pem = strdup(cert_pem); - if (id->cert_pem == NULL || + id->key_pem = key_to_pem(id->key); + if (id->cert_pem == NULL || id->key_pem == NULL || cert_pin(id->cert, id->pin, sizeof(id->pin)) != 0 || public_b64(id->key, id->pub_b64, sizeof(id->pub_b64)) != 0) goto fail; @@ -346,6 +379,7 @@ dc_identity_free(DcIdentity *id) EVP_PKEY_free(id->key); X509_free(id->cert); free(id->cert_pem); + free(id->key_pem); free(id); } @@ -366,3 +400,9 @@ dc_identity_cert_pem(const DcIdentity *id) { return id != NULL ? id->cert_pem : NULL; } + +const char * +dc_identity_key_pem(const DcIdentity *id) +{ + return id != NULL ? id->key_pem : NULL; +} diff --git a/lib/libDtChat/local.c b/lib/libDtChat/local.c new file mode 100644 index 00000000..4201a67a --- /dev/null +++ b/lib/libDtChat/local.c @@ -0,0 +1,495 @@ +/* + * local.c -- dtchat's local TLS listener and the peer hello + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * One listener per dtchat, bound to 127.0.0.1:HOST or all interfaces (NULL). + * The identity's Ed25519 certificate is the TLS server certificate; the + * handshake is non-blocking and driven by DcLoop, like the IRC adapter. A + * peer sends one JSON line {"type":"hello","id":...,"pin":...}; the server + * compares ID and PIN with what it expects (the TOFU store of T5.5) and + * answers {"type":"hello-ack","ok":true|false,...}. A wrong PIN closes the + * connection after the negative ack. The client side arrives with T5.5. + */ +#include "dc_internal.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define LOCAL_HANDSHAKE 0 +#define LOCAL_HELLO 1 +#define LOCAL_ACK 2 +#define LOCAL_READY 3 + +struct DcLocalConn { + DcLocalServer *server; + int fd; + int fd_id; + struct tls *tls; + int state; + int accepted; + char in[8192]; + size_t inlen; + char out[1024]; + size_t outlen; + size_t outoff; + char peer_id[256]; + char peer_pin[128]; + DcJson *hello; + DcLocalConn *next; +}; + +struct DcLocalServer { + DcLoop *loop; + int fd; + int fd_id; + int port; + struct tls_config *cfg; + struct tls *ctx; + char *own_pin; + char *expect_id; + char *expect_pin; + DcLocalHelloFn fn; + void *arg; + DcLocalConn *conns; +}; + +/* "sha256:aa:bb", "SHA256:AABB", "aa:bb" -> 64 lowercase hex, or -1. */ +static int +normalize_pin(const char *text, char out[65]) +{ + size_t i = 0, o = 0; + + if (text == NULL) + return -1; + if (strncasecmp(text, "sha256:", 7) == 0) + text += 7; + for (; text[i] != '\0'; i++) { + char c = text[i]; + + if (c == ':') + continue; + if (c >= 'A' && c <= 'F') + c = (char)(c - 'A' + 'a'); + if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) + return -1; + if (o >= 64) + return -1; + out[o++] = c; + } + out[o] = '\0'; + return o == 64 ? 0 : -1; +} + +static void conn_cb(int fd, short revents, void *arg); + +static void +set_fd_events(DcLocalConn *conn, short events) +{ + if (conn->fd_id > 0) + (void)dc_loop_del_fd(conn->server->loop, conn->fd_id); + conn->fd_id = dc_loop_add_fd(conn->server->loop, conn->fd, events, + conn_cb, conn); +} + +static void +conn_unlink(DcLocalConn *conn) +{ + DcLocalConn **slot = &conn->server->conns; + + while (*slot != NULL) { + if (*slot == conn) { + *slot = conn->next; + return; + } + slot = &(*slot)->next; + } +} + +void +dc_local_conn_close(DcLocalConn *conn) +{ + if (conn == NULL) + return; + if (conn->fd_id > 0) + (void)dc_loop_del_fd(conn->server->loop, conn->fd_id); + if (conn->tls != NULL) { + (void)tls_close(conn->tls); + tls_free(conn->tls); + conn->tls = NULL; + } + if (conn->fd >= 0) + close(conn->fd); + conn->fd = -1; + conn_unlink(conn); + free(conn); +} + +static void +queue_ack(DcLocalConn *conn, int ok, const char *reason) +{ + DcJson *json = dc_json_new_object(); + size_t len = 0; + char *text; + + (void)dc_json_object_set(json, "type", + dc_json_new_string("hello-ack")); + (void)dc_json_object_set(json, "ok", dc_json_new_bool(ok)); + if (conn->server->own_pin != NULL) + (void)dc_json_object_set(json, "pin", + dc_json_new_string(conn->server->own_pin)); + if (reason != NULL) + (void)dc_json_object_set(json, "reason", + dc_json_new_string(reason)); + text = dc_json_write(json, &len); + dc_json_free(json); + if (text == NULL) { + dc_local_conn_close(conn); + return; + } + if (len >= sizeof(conn->out)) { + free(text); + dc_local_conn_close(conn); + return; + } + memcpy(conn->out, text, len); + if (len + 1 < sizeof(conn->out)) + conn->out[len++] = '\n'; + conn->outlen = len; + conn->outoff = 0; + free(text); + conn->accepted = ok; + conn->state = LOCAL_ACK; +} + +static void +handle_hello(DcLocalConn *conn, const char *line) +{ + DcJson *hello; + const char *type; + const char *id; + const char *pin; + char normalized[65]; + char expected[65]; + + hello = dc_json_parse(line, strlen(line), NULL, 0); + if (hello == NULL) { + queue_ack(conn, 0, "protocol"); + return; + } + type = dc_json_string(dc_json_get(hello, "type")); + id = dc_json_string(dc_json_get(hello, "id")); + pin = dc_json_string(dc_json_get(hello, "pin")); + if (type == NULL || strcmp(type, "hello") != 0 || + id == NULL || *id == '\0' || pin == NULL) { + dc_json_free(hello); + queue_ack(conn, 0, "protocol"); + return; + } + snprintf(conn->peer_id, sizeof(conn->peer_id), "%s", id); + snprintf(conn->peer_pin, sizeof(conn->peer_pin), "%s", pin); + if (conn->server->expect_id != NULL && + strcmp(conn->server->expect_id, id) != 0) { + dc_json_free(hello); + queue_ack(conn, 0, "id"); + return; + } + if (conn->server->expect_pin != NULL) { + if (normalize_pin(pin, normalized) != 0 || + normalize_pin(conn->server->expect_pin, expected) != 0 || + strcmp(normalized, expected) != 0) { + dc_json_free(hello); + queue_ack(conn, 0, "pin"); + return; + } + } + conn->hello = hello; + queue_ack(conn, 1, NULL); +} + +static void +conn_pump(DcLocalConn *conn) +{ + for (;;) { + if (conn->state == LOCAL_HANDSHAKE) { + int rc = tls_handshake(conn->tls); + + if (rc == 0) { + conn->state = LOCAL_HELLO; + set_fd_events(conn, POLLIN); + continue; + } + if (rc == TLS_WANT_POLLIN) { + set_fd_events(conn, POLLIN); + return; + } + if (rc == TLS_WANT_POLLOUT) { + set_fd_events(conn, POLLOUT); + return; + } + dc_local_conn_close(conn); + return; + } + if (conn->state == LOCAL_HELLO) { + ssize_t n = tls_read(conn->tls, conn->in + conn->inlen, + sizeof(conn->in) - 1 - conn->inlen); + char *nl; + + if (n == TLS_WANT_POLLIN) { + set_fd_events(conn, POLLIN); + return; + } + if (n == TLS_WANT_POLLOUT) { + set_fd_events(conn, POLLOUT); + return; + } + if (n <= 0) { + dc_local_conn_close(conn); + return; + } + conn->inlen += (size_t)n; + conn->in[conn->inlen] = '\0'; + nl = strchr(conn->in, '\n'); + if (nl == NULL) { + if (conn->inlen >= sizeof(conn->in) - 1) + dc_local_conn_close(conn); + return; + } + *nl = '\0'; + if (nl > conn->in && nl[-1] == '\r') + nl[-1] = '\0'; + handle_hello(conn, conn->in); + if (conn->state == LOCAL_ACK) + set_fd_events(conn, POLLOUT); + continue; + } + if (conn->state == LOCAL_ACK) { + while (conn->outoff < conn->outlen) { + ssize_t n = tls_write(conn->tls, + conn->out + conn->outoff, + conn->outlen - conn->outoff); + + if (n == TLS_WANT_POLLIN) { + set_fd_events(conn, POLLIN); + return; + } + if (n == TLS_WANT_POLLOUT) { + set_fd_events(conn, POLLOUT); + return; + } + if (n <= 0) { + dc_local_conn_close(conn); + return; + } + conn->outoff += (size_t)n; + } + if (!conn->accepted) { + dc_local_conn_close(conn); + return; + } + conn->state = LOCAL_READY; + if (conn->server->fn != NULL) { + DcLocalHelloFn fn = conn->server->fn; + + fn(conn, conn->hello, conn->server->arg); + } + dc_json_free(conn->hello); + conn->hello = NULL; + set_fd_events(conn, POLLIN); + return; + } + return; + } +} + +static void +conn_cb(int fd, short revents, void *arg) +{ + DcLocalConn *conn = arg; + + (void)fd; + (void)revents; + conn_pump(conn); +} + +static void +server_accept(DcLocalServer *server) +{ + for (;;) { + int fd = accept(server->fd, NULL, NULL); + DcLocalConn *conn; + + if (fd < 0) + return; + conn = calloc(1, sizeof(*conn)); + if (conn == NULL) { + close(fd); + return; + } + conn->server = server; + conn->fd = fd; + conn->fd_id = -1; + conn->state = LOCAL_HANDSHAKE; + (void)fcntl(fd, F_SETFD, FD_CLOEXEC); + if (tls_accept_socket(server->ctx, &conn->tls, fd) != 0) { + close(fd); + free(conn); + return; + } + { + int fl = fcntl(fd, F_GETFL, 0); + + if (fl >= 0) + (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK); + } + conn->next = server->conns; + server->conns = conn; + conn->fd_id = dc_loop_add_fd(server->loop, fd, POLLIN, + conn_cb, conn); + if (conn->fd_id <= 0) { + dc_local_conn_close(conn); + return; + } + } +} + +static void +listen_cb(int fd, short revents, void *arg) +{ + (void)fd; + (void)revents; + server_accept(arg); +} + +DcLocalServer * +dc_local_server_new(DcLoop *loop, const DcIdentity *identity, const char *host, + int port, const char *expect_id, const char *expect_pin, + DcLocalHelloFn fn, void *arg) +{ + DcLocalServer *server; + struct sockaddr_in addr; + int fd; + int yes = 1; + + if (loop == NULL || identity == NULL) + return NULL; + server = calloc(1, sizeof(*server)); + if (server == NULL) + return NULL; + server->loop = loop; + server->fn = fn; + server->arg = arg; + server->own_pin = strdup(dc_identity_pin(identity)); + server->expect_id = expect_id != NULL ? strdup(expect_id) : NULL; + server->expect_pin = expect_pin != NULL ? strdup(expect_pin) : NULL; + if (server->own_pin == NULL || + (expect_id != NULL && server->expect_id == NULL) || + (expect_pin != NULL && server->expect_pin == NULL)) + goto fail; + if (tls_init() != 0) + goto fail; + server->cfg = tls_config_new(); + if (server->cfg == NULL || + tls_config_set_cert_mem(server->cfg, + (const uint8_t *)dc_identity_cert_pem(identity), + strlen(dc_identity_cert_pem(identity))) != 0 || + tls_config_set_key_mem(server->cfg, + (const uint8_t *)dc_identity_key_pem(identity), + strlen(dc_identity_key_pem(identity))) != 0) + goto fail; + server->ctx = tls_server(); + if (server->ctx == NULL || tls_configure(server->ctx, server->cfg) != 0) + goto fail; + fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) + goto fail; + (void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons((uint16_t)port); + if (host == NULL || strcmp(host, "0.0.0.0") == 0) + addr.sin_addr.s_addr = htonl(INADDR_ANY); + else if (inet_pton(AF_INET, host, &addr.sin_addr) != 1) { + close(fd); + goto fail; + } + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0 || + listen(fd, 8) != 0) { + close(fd); + goto fail; + } + { + socklen_t len = sizeof(addr); + + if (getsockname(fd, (struct sockaddr *)&addr, &len) == 0) + server->port = ntohs(addr.sin_port); + } + { + int fl = fcntl(fd, F_GETFL, 0); + + if (fl >= 0) + (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK); + (void)fcntl(fd, F_SETFD, FD_CLOEXEC); + } + server->fd = fd; + server->fd_id = dc_loop_add_fd(loop, fd, POLLIN, listen_cb, server); + if (server->fd_id <= 0) + goto fail; + return server; +fail: + dc_local_server_free(server); + return NULL; +} + +int +dc_local_server_port(const DcLocalServer *server) +{ + return server != NULL ? server->port : 0; +} + +void +dc_local_server_free(DcLocalServer *server) +{ + if (server == NULL) + return; + if (server->fd_id > 0) + (void)dc_loop_del_fd(server->loop, server->fd_id); + while (server->conns != NULL) + dc_local_conn_close(server->conns); + if (server->fd >= 0) + close(server->fd); + if (server->ctx != NULL) { + (void)tls_close(server->ctx); + tls_free(server->ctx); + } + if (server->cfg != NULL) + tls_config_free(server->cfg); + free(server->own_pin); + free(server->expect_id); + free(server->expect_pin); + free(server); +} + +const char * +dc_local_conn_peer_id(const DcLocalConn *conn) +{ + return conn != NULL ? conn->peer_id : NULL; +} + +const char * +dc_local_conn_peer_pin(const DcLocalConn *conn) +{ + return conn != NULL ? conn->peer_pin : NULL; +} diff --git a/lib/libDtChat/test_identity.c b/lib/libDtChat/test_identity.c index 0fbce3c8..294e0f75 100644 --- a/lib/libDtChat/test_identity.c +++ b/lib/libDtChat/test_identity.c @@ -61,6 +61,8 @@ main(void) CHECK(strlen(pub) == 44); CHECK(dc_identity_cert_pem(id) != NULL); CHECK(strstr(dc_identity_cert_pem(id), "BEGIN CERTIFICATE") != NULL); + CHECK(dc_identity_key_pem(id) != NULL); + CHECK(strstr(dc_identity_key_pem(id), "BEGIN PRIVATE KEY") != NULL); CHECK(access(path, F_OK) == 0); mode_is(dir, 0700); mode_is(path, 0600); diff --git a/lib/libDtChat/test_local.c b/lib/libDtChat/test_local.c new file mode 100644 index 00000000..0fc5451c --- /dev/null +++ b/lib/libDtChat/test_local.c @@ -0,0 +1,255 @@ +/* + * test_local.c -- tests for the local TLS listener (T5.4) + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + */ +#include "dc_internal.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static int fails; + +#define CHECK(cond) do {\ + if (!(cond)) {\ + fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,\ + #cond);\ + fails++;\ + }\ +} while (0) + +#define PIN_A \ + "sha256:1111111111111111111111111111111111111111111111111111111111111111" +#define PIN_B \ + "sha256:2222222222222222222222222222222222222222222222222222222222222222" + +struct ClientArg { + const char *id; + const char *pin; + int port; + int done; + int ack_ok; + int ack_seen; +}; + +static void * +client_thread(void *arg) +{ + struct ClientArg *c = arg; + struct tls_config *cfg; + struct tls *ctx; + char port[16]; + char hello[512]; + size_t off = 0; + int fd = -1; + + snprintf(port, sizeof(port), "%d", c->port); + snprintf(hello, sizeof(hello), + "{\"type\":\"hello\",\"id\":\"%s\",\"pin\":\"%s\"}\n", + c->id, c->pin); + if (tls_init() != 0) + goto out; + cfg = tls_config_new(); + if (cfg == NULL) + goto out; + tls_config_insecure_noverifycert(cfg); + tls_config_insecure_noverifyname(cfg); + ctx = tls_client(); + if (ctx == NULL || tls_configure(ctx, cfg) != 0) { + tls_config_free(cfg); + goto out; + } + tls_config_free(cfg); + { + struct sockaddr_in addr; + + fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) + goto out; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons((uint16_t)c->port); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) { + close(fd); + fd = -1; + goto out; + } + if (tls_connect_socket(ctx, fd, "127.0.0.1") != 0 || + tls_handshake(ctx) != 0) { + close(fd); + fd = -1; + tls_free(ctx); + goto out; + } + } + while (off < strlen(hello)) { + ssize_t n = tls_write(ctx, hello + off, strlen(hello) - off); + + if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) { + struct pollfd p; + + p.fd = fd; + p.events = n == TLS_WANT_POLLIN ? POLLIN : POLLOUT; + p.revents = 0; + if (poll(&p, 1, 5000) <= 0) + break; + continue; + } + if (n <= 0) { + tls_free(ctx); + goto out; + } + off += (size_t)n; + } + { + char line[1024]; + size_t used = 0; + + while (used < sizeof(line) - 1) { + ssize_t n = tls_read(ctx, line + used, + sizeof(line) - 1 - used); + DcJson *ack; + char *nl; + + if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) { + struct pollfd p; + + p.fd = fd; + p.events = n == TLS_WANT_POLLIN ? + POLLIN : POLLOUT; + p.revents = 0; + if (poll(&p, 1, 5000) <= 0) + break; + continue; + } + if (n <= 0) { + break; + } + used += (size_t)n; + line[used] = '\0'; + nl = strchr(line, '\n'); + if (nl == NULL) + continue; + *nl = '\0'; + ack = dc_json_parse(line, strlen(line), NULL, 0); + if (ack != NULL) { + c->ack_seen = 1; + c->ack_ok = dc_json_bool(dc_json_get(ack, + "ok")); + dc_json_free(ack); + } + break; + } + } + (void)tls_close(ctx); + tls_free(ctx); + if (fd >= 0) + close(fd); +out: + c->done = 1; + return NULL; +} + +struct ServerCtx { + int called; + char id[128]; + char pin[128]; +}; + +static void +hello_cb(DcLocalConn *conn, const DcJson *hello, void *arg) +{ + struct ServerCtx *ctx = arg; + + ctx->called++; + if (hello != NULL) { + const char *id = dc_json_string(dc_json_get(hello, "id")); + const char *pin = dc_json_string(dc_json_get(hello, "pin")); + + if (id != NULL) + snprintf(ctx->id, sizeof(ctx->id), "%s", id); + if (pin != NULL) + snprintf(ctx->pin, sizeof(ctx->pin), "%s", pin); + } + (void)conn; +} + +static void +run_case(const char *want_id, const char *want_pin, const char *send_id, + const char *send_pin, int expect_ok, int expect_cb) +{ + char base[] = "/tmp/opencode/dtchat-local-XXXXXX"; + char dir[512]; + DcIdentity *identity; + DcLoop *loop; + DcLocalServer *server; + struct ServerCtx ctx; + struct ClientArg client; + pthread_t thread; + long long deadline; + + memset(&ctx, 0, sizeof(ctx)); + memset(&client, 0, sizeof(client)); + CHECK(mkdtemp(base) != NULL); + snprintf(dir, sizeof(dir), "%s/state", base); + identity = dc_identity_load_or_create(dir); + CHECK(identity != NULL); + if (identity == NULL) + return; + loop = dc_loop_new(); + CHECK(loop != NULL); + server = dc_local_server_new(loop, identity, "127.0.0.1", 0, want_id, + want_pin, hello_cb, &ctx); + CHECK(server != NULL); + if (server == NULL) + return; + CHECK(dc_local_server_port(server) > 0); + + client.id = send_id; + client.pin = send_pin; + client.port = dc_local_server_port(server); + CHECK(pthread_create(&thread, NULL, client_thread, &client) == 0); + deadline = dc_loop_now_ms() + 5000; + while (!client.done && dc_loop_now_ms() < deadline) + (void)dc_loop_run(loop, 50); + pthread_join(thread, NULL); + + CHECK(client.ack_seen); + CHECK(client.ack_ok == expect_ok); + if (expect_cb) { + CHECK(ctx.called == 1); + CHECK(strcmp(ctx.id, send_id) == 0); + CHECK(strcmp(ctx.pin, send_pin) == 0); + } else { + CHECK(ctx.called == 0); + } + dc_local_server_free(server); + dc_loop_free(loop); + dc_identity_free(identity); +} + +int +main(void) +{ + signal(SIGPIPE, SIG_IGN); + run_case("peer1", PIN_A, "peer1", PIN_A, 1, 1); + run_case("peer1", PIN_B, "peer1", PIN_A, 0, 0); + run_case(NULL, NULL, "peer2", PIN_A, 1, 1); + if (fails != 0) { + fprintf(stderr, "%d test(s) failed\n", fails); + return 1; + } + printf("test_local: all ok\n"); + return 0; +}