diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index 00764ac6..de18418e 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -267,6 +267,7 @@ void dc_local_peer_stop(DcLocalPeer *peer); void dc_local_peer_free(DcLocalPeer *peer); int dc_local_peer_send(DcLocalPeer *peer, const DcJson *json); int dc_local_peer_online(const DcLocalPeer *peer); +DcLocalConn *dc_local_peer_conn(const DcLocalPeer *peer); /* * Local file transfer. A sender offers name/size/sha256, sends 64 KiB diff --git a/lib/libDtChat/local.c b/lib/libDtChat/local.c index e0b4da76..906aa12b 100644 --- a/lib/libDtChat/local.c +++ b/lib/libDtChat/local.c @@ -416,6 +416,13 @@ dispatch_line(DcLocalConn *conn, const char *line) } ok = dc_json_bool(dc_json_get(json, "ok")); pin = dc_json_string(dc_json_get(json, "pin")); + { + const char *id = dc_json_string(dc_json_get(json, "id")); + + if (id != NULL) + snprintf(conn->peer_id, sizeof(conn->peer_id), + "%s", id); + } if (conn->timer > 0) { (void)dc_loop_del_timer(conn->loop, conn->timer); conn->timer = 0; @@ -1135,3 +1142,9 @@ dc_local_peer_online(const DcLocalPeer *peer) { return peer != NULL ? peer->online : 0; } + +DcLocalConn * +dc_local_peer_conn(const DcLocalPeer *peer) +{ + return peer != NULL ? peer->conn : NULL; +} diff --git a/programs/dtchat/Makefile.am b/programs/dtchat/Makefile.am index c449ba50..ee0a5d97 100644 --- a/programs/dtchat/Makefile.am +++ b/programs/dtchat/Makefile.am @@ -1,7 +1,7 @@ MAINTAINERCLEANFILES = Makefile.in bin_PROGRAMS = dtchat -noinst_PROGRAMS = dtchat-demo dtchat-irc +noinst_PROGRAMS = dtchat-demo dtchat-irc dtchat-headless dtchat_CPPFLAGS = -I$(top_srcdir)/include \ -I$(top_srcdir)/lib/libDtChat -I$(top_builddir)/lib/libDtChat \ @@ -28,6 +28,12 @@ dtchat_irc_CPPFLAGS = -I$(top_srcdir)/include \ dtchat_irc_LDADD = $(top_builddir)/lib/libDtChat/libDtChat.la \ $(top_builddir)/lib/libDtChat/libDtChatIpc.la $(LIBTLS) +dtchat_headless_SOURCES = headless.c +dtchat_headless_CPPFLAGS = -I$(top_srcdir)/include \ + -I$(top_srcdir)/lib/libDtChat -I$(top_builddir)/lib/libDtChat +dtchat_headless_LDADD = $(top_builddir)/lib/libDtChat/libDtChat.la \ + $(top_builddir)/lib/libDtChat/libDtChatIpc.la $(LIBTLS) $(CRYPTOLIB) + appdefaultsdir = $(CDE_INSTALLATION_TOP)/app-defaults/C dist_appdefaults_DATA = Dtchat diff --git a/programs/dtchat/headless.c b/programs/dtchat/headless.c new file mode 100644 index 00000000..5d9b567d --- /dev/null +++ b/programs/dtchat/headless.c @@ -0,0 +1,467 @@ +/* + * headless.c -- dtchat without a display + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * usage: dtchat-headless [--config DIR] [--listen [HOST:]PORT] + * [--peer HOST:PORT ...] + * + * Starts the local TLS listener and one reconnecting link per --peer. Every + * event is one JSON object on stdout; commands are JSON lines on stdin: + * + * {"cmd":"send","peer":"HOST:PORT"|"ID","text":"..."} + * {"cmd":"quit"} + * + * Events: ready, peer-online, peer-offline, message, typing, file-offer, + * error. The test harness is tests/p2p.sh. + */ +#include "dc_internal.h" + +#include +#include +#include +#include +#include +#include + +struct Link { + char target[256]; + DcLocalPeer *peer; + int online; + char id[128]; + struct Link *next; +}; + +struct Accepted { + DcLocalConn *conn; + char id[128]; + char pin[128]; + struct Accepted *next; +}; + +static DcLoop *loop; +static DcLocalServer *server; +static struct Link *links; +static struct Accepted *accepted; +static int quit_flag; +static int stdin_id; +static DcFrameBuf stdin_frame; + +static void +emit(DcJson *json) +{ + size_t len = 0; + char *text = dc_json_write(json, &len); + + if (text != NULL) { + (void)write(STDOUT_FILENO, text, len); + (void)write(STDOUT_FILENO, "\n", 1); + free(text); + } + dc_json_free(json); +} + +static void +emit_error(const char *text) +{ + DcJson *json = dc_json_new_object(); + + (void)dc_json_object_set(json, "event", dc_json_new_string("error")); + (void)dc_json_object_set(json, "text", dc_json_new_string(text)); + emit(json); +} + +static void +emit_peer(const char *event, const char *mode, const char *target, + const char *id, const char *pin) +{ + DcJson *json = dc_json_new_object(); + + (void)dc_json_object_set(json, "event", dc_json_new_string(event)); + (void)dc_json_object_set(json, "mode", dc_json_new_string(mode)); + if (target != NULL) + (void)dc_json_object_set(json, "target", + dc_json_new_string(target)); + if (id != NULL) + (void)dc_json_object_set(json, "id", dc_json_new_string(id)); + if (pin != NULL) + (void)dc_json_object_set(json, "pin", + dc_json_new_string(pin)); + emit(json); +} + +static struct Accepted * +accepted_find(DcLocalConn *conn) +{ + struct Accepted *a; + + for (a = accepted; a != NULL; a = a->next) + if (a->conn == conn) + return a; + return NULL; +} + +static struct Accepted * +accepted_by_id(const char *id) +{ + struct Accepted *a; + + for (a = accepted; a != NULL; a = a->next) + if (strcmp(a->id, id) == 0) + return a; + return NULL; +} + +static void +server_hello(DcLocalConn *conn, const DcJson *hello, void *arg) +{ + struct Accepted *a = calloc(1, sizeof(*a)); + const char *id = dc_json_string(dc_json_get(hello, "id")); + const char *pin = dc_json_string(dc_json_get(hello, "pin")); + + (void)arg; + if (a == NULL) + return; + a->conn = conn; + snprintf(a->id, sizeof(a->id), "%s", id != NULL ? id : ""); + snprintf(a->pin, sizeof(a->pin), "%s", pin != NULL ? pin : ""); + a->next = accepted; + accepted = a; + emit_peer("peer-online", "server", NULL, a->id, a->pin); +} + +static void +emit_message(const char *from, const DcJson *json) +{ + DcJson *out = dc_json_new_object(); + const char *text = dc_json_string(dc_json_get(json, "text")); + + (void)dc_json_object_set(out, "event", + dc_json_new_string("message")); + (void)dc_json_object_set(out, "from", + dc_json_new_string(from != NULL ? from : "")); + (void)dc_json_object_set(out, "text", + dc_json_new_string(text != NULL ? text : "")); + emit(out); +} + +static void +common_line(const char *from, DcLocalConn *conn, const DcJson *json) +{ + const char *type = dc_json_string(dc_json_get(json, "type")); + + if (type == NULL) + return; + if (strcmp(type, "msg") == 0) { + emit_message(from, json); + if (conn != NULL) + (void)dc_local_send_msg_ack(conn, dc_json_string( + dc_json_get(json, "client_id"))); + } else if (strcmp(type, "typing") == 0) { + DcJson *out = dc_json_new_object(); + + (void)dc_json_object_set(out, "event", + dc_json_new_string("typing")); + (void)dc_json_object_set(out, "from", + dc_json_new_string(from)); + (void)dc_json_object_set(out, "on", dc_json_new_bool( + dc_json_bool(dc_json_get(json, "on")))); + emit(out); + } else if (strcmp(type, "file-offer") == 0) { + DcJson *out = dc_json_new_object(); + + (void)dc_json_object_set(out, "event", + dc_json_new_string("file-offer")); + (void)dc_json_object_set(out, "from", + dc_json_new_string(from)); + (void)dc_json_object_set(out, "name", dc_json_new_string( + dc_json_string(dc_json_get(json, "name")))); + (void)dc_json_object_set(out, "size", + dc_json_new_int(dc_json_get(json, "size") != NULL ? + dc_json_int(dc_json_get(json, "size")) : 0)); + emit(out); + } +} + +static void +server_line(DcLocalConn *conn, const DcJson *json, void *arg) +{ + struct Accepted *a = accepted_find(conn); + + (void)arg; + common_line(a != NULL ? a->id : "?", conn, json); +} + +static void +server_closed(DcLocalConn *conn, void *arg) +{ + struct Accepted **slot = &accepted; + + (void)arg; + while (*slot != NULL) { + if ((*slot)->conn == conn) { + struct Accepted *dead = *slot; + + emit_peer("peer-offline", "server", NULL, dead->id, + NULL); + *slot = dead->next; + free(dead); + return; + } + slot = &(*slot)->next; + } +} + +static void +link_line(DcLocalPeer *peer, const DcJson *json, void *arg) +{ + struct Link *link = arg; + + (void)peer; + common_line(link->id, dc_local_peer_conn(peer), json); +} + +static void +link_state(DcLocalPeer *peer, int online, void *arg) +{ + struct Link *link = arg; + DcLocalConn *conn = dc_local_peer_conn(peer); + const char *id = conn != NULL ? dc_local_conn_peer_id(conn) : NULL; + const char *pin = conn != NULL ? dc_local_conn_peer_pin(conn) : NULL; + + link->online = online; + if (online && id != NULL) + snprintf(link->id, sizeof(link->id), "%s", id); + emit_peer(online ? "peer-online" : "peer-offline", "client", + link->target, link->id, online ? pin : NULL); +} + +static struct Link * +link_by_name(const char *name) +{ + struct Link *link; + + for (link = links; link != NULL; link = link->next) + if (strcmp(link->target, name) == 0 || + (link->id[0] != '\0' && strcmp(link->id, name) == 0)) + return link; + return NULL; +} + +static void +stdin_readable(int fd, short revents, void *arg) +{ + char buf[4096]; + ssize_t n; + + (void)revents; + (void)arg; + n = read(fd, buf, sizeof(buf)); + if (n <= 0) { + quit_flag = 1; + return; + } + if (dc_frame_feed(&stdin_frame, buf, (size_t)n) != 0) + return; + for (;;) { + size_t len = 0; + char *line = dc_frame_next(&stdin_frame, &len); + DcJson *cmd; + const char *what; + + if (line == NULL) + break; + cmd = dc_json_parse(line, len, NULL, 0); + free(line); + if (cmd == NULL) { + emit_error("invalid command"); + continue; + } + what = dc_json_string(dc_json_get(cmd, "cmd")); + if (what == NULL) { + emit_error("missing cmd"); + } else if (strcmp(what, "quit") == 0) { + quit_flag = 1; + } else if (strcmp(what, "send") == 0) { + const char *peer = dc_json_string(dc_json_get(cmd, + "peer")); + const char *text = dc_json_string(dc_json_get(cmd, + "text")); + struct Link *link = link_by_name(peer); + DcJson *msg; + + if (link != NULL) { + msg = dc_json_new_object(); + (void)dc_json_object_set(msg, "type", + dc_json_new_string("msg")); + (void)dc_json_object_set(msg, "text", + dc_json_new_string(text != NULL ? + text : "")); + if (dc_local_peer_send(link->peer, msg) != 0) + emit_error("peer is offline"); + dc_json_free(msg); + } else { + struct Accepted *a = accepted_by_id(peer); + + if (a != NULL) { + if (dc_local_send_msg(a->conn, text, + NULL) != 0) + emit_error("send failed"); + } else { + emit_error("unknown peer"); + } + } + } else { + emit_error("unknown cmd"); + } + dc_json_free(cmd); + } +} + +static int +split_host_port(const char *text, char *host, size_t hostlen, int *port) +{ + const char *colon = strrchr(text, ':'); + char *end = NULL; + long value; + + if (colon == NULL) { + snprintf(host, hostlen, "127.0.0.1"); + value = strtol(text, &end, 10); + } else { + size_t len = (size_t)(colon - text); + + if (len >= hostlen) + return -1; + memcpy(host, text, len); + host[len] = '\0'; + if (host[0] == '\0') + snprintf(host, hostlen, "127.0.0.1"); + value = strtol(colon + 1, &end, 10); + } + if (end == NULL || *end != '\0' || value < 0 || value > 65535) + return -1; + *port = (int)value; + return 0; +} + +int +main(int argc, char **argv) +{ + const char *config = NULL; + char default_config[512]; + const char *listen_text = NULL; + const char *peer_text[8]; + int npeers = 0; + DcIdentity *identity; + int i; + int port = 0; + char listen_host[128] = "127.0.0.1"; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "--config") == 0 && i + 1 < argc) { + config = argv[++i]; + } else if (strcmp(argv[i], "--listen") == 0 && + i + 1 < argc) { + listen_text = argv[++i]; + } else if (strcmp(argv[i], "--peer") == 0 && + i + 1 < argc && npeers < 8) { + peer_text[npeers++] = argv[++i]; + } else { + fprintf(stderr, "usage: %s [--config DIR] " + "[--listen [HOST:]PORT] [--peer HOST:PORT ...]\n", + argv[0]); + return 2; + } + } + if (config == NULL) { + const char *home = getenv("HOME"); + + if (home == NULL) + return 1; + snprintf(default_config, sizeof(default_config), + "%s/.dt/dtchat", home); + config = default_config; + } + signal(SIGPIPE, SIG_IGN); + identity = dc_identity_load_or_create(config); + if (identity == NULL) { + fprintf(stderr, "dtchat-headless: cannot load the identity\n"); + return 1; + } + loop = dc_loop_new(); + if (loop == NULL) + return 1; + if (listen_text != NULL) { + if (split_host_port(listen_text, listen_host, + sizeof(listen_host), &port) != 0) { + fprintf(stderr, "dtchat-headless: bad --listen\n"); + return 2; + } + server = dc_local_server_new(loop, identity, listen_host, port, + NULL, NULL, server_hello, server_line, server_closed, NULL); + if (server == NULL) { + fprintf(stderr, "dtchat-headless: cannot listen\n"); + return 1; + } + port = dc_local_server_port(server); + } + for (i = 0; i < npeers; i++) { + char host[128]; + int p; + struct Link *link; + + if (split_host_port(peer_text[i], host, sizeof(host), &p) != 0 || + p <= 0) { + fprintf(stderr, "dtchat-headless: bad --peer\n"); + return 2; + } + link = calloc(1, sizeof(*link)); + if (link == NULL) + return 1; + snprintf(link->target, sizeof(link->target), "%s", peer_text[i]); + link->next = links; + links = link; + link->peer = dc_local_peer_new(loop, identity, host, p, NULL, + link_line, link_state, link); + if (link->peer == NULL || dc_local_peer_start(link->peer) != 0) + emit_error("cannot start the peer link"); + } + { + DcJson *ready = dc_json_new_object(); + + (void)dc_json_object_set(ready, "event", + dc_json_new_string("ready")); + (void)dc_json_object_set(ready, "listen", dc_json_new_int(port)); + (void)dc_json_object_set(ready, "id", dc_json_new_string( + dc_identity_public_b64(identity))); + (void)dc_json_object_set(ready, "pin", dc_json_new_string( + dc_identity_pin(identity))); + emit(ready); + } + dc_frame_init(&stdin_frame); + stdin_id = dc_loop_add_fd(loop, STDIN_FILENO, POLLIN, stdin_readable, + NULL); + while (!quit_flag) + (void)dc_loop_run(loop, 200); + if (stdin_id > 0) + (void)dc_loop_del_fd(loop, stdin_id); + while (links != NULL) { + struct Link *next = links->next; + + dc_local_peer_free(links->peer); + free(links); + links = next; + } + dc_local_server_free(server); + while (accepted != NULL) { + struct Accepted *next = accepted->next; + + free(accepted); + accepted = next; + } + dc_frame_free(&stdin_frame); + dc_loop_free(loop); + dc_identity_free(identity); + return 0; +} diff --git a/tests/p2p.sh b/tests/p2p.sh new file mode 100755 index 00000000..d52b2b5c --- /dev/null +++ b/tests/p2p.sh @@ -0,0 +1,93 @@ +#!/bin/sh +# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. +# +# p2p.sh -- two headless dtchat instances talk over loopback (T5.7) +# +# usage: tests/p2p.sh [path-to-dtchat-headless] +# default: build/mcde-host/programs/dtchat/dtchat-headless +# +# A listens on an ephemeral port, B connects. B sends a message, A answers +# the peer by its id, both quit. Every step must show up in the JSON log. + +set -u + +TOP=$(cd "$(dirname "$0")/.." && pwd) +BIN=${1:-"$TOP/../../../build/mcde-host/programs/dtchat/dtchat-headless"} +case $BIN in +/*) ;; +*) BIN=$(cd "$(dirname "$BIN")" && pwd)/$(basename "$BIN") ;; +esac +[ -x "$BIN" ] || { echo "p2p: no $BIN" >&2; exit 2; } + +TMP=$(mktemp -d /tmp/dtchat-p2p-XXXXXX) || exit 2 +A_IN=$TMP/a.in +B_IN=$TMP/b.in +mkfifo "$A_IN" "$B_IN" || exit 2 +exec 3<>"$A_IN" +exec 4<>"$B_IN" + +cleanup() { + exec 3>&- 2>/dev/null + exec 4>&- 2>/dev/null + [ -n "${PA:-}" ] && kill "$PA" 2>/dev/null + [ -n "${PB:-}" ] && kill "$PB" 2>/dev/null + rm -rf "$TMP" +} + +wait_for() { # file, fixed string, tries + i=0 + while [ "$i" -lt "$3" ]; do + if grep -q -F "$2" "$1" 2>/dev/null; then + return 0 + fi + sleep 0.2 + i=$((i + 1)) + done + return 1 +} + +fail() { + echo "p2p: FAIL $*" >&2 + echo "--- a.log" >&2 + cat "$TMP/a.log" >&2 2>/dev/null + echo "--- b.log" >&2 + cat "$TMP/b.log" >&2 2>/dev/null + cleanup + exit 1 +} + +trap cleanup EXIT INT TERM + +"$BIN" --config "$TMP/a" --listen 127.0.0.1:0 <&3 >"$TMP/a.log" 2>"$TMP/a.err" & +PA=$! +wait_for "$TMP/a.log" '"event":"ready"' 50 || fail "A did not become ready" +PORT=$(sed -n 's/.*"listen":\([0-9][0-9]*\).*/\1/p' "$TMP/a.log" | head -1) +[ -n "$PORT" ] || fail "no listen port" +echo "p2p: A listens on $PORT" + +"$BIN" --config "$TMP/b" --peer "127.0.0.1:$PORT" <&4 >"$TMP/b.log" 2>"$TMP/b.err" & +PB=$! +wait_for "$TMP/a.log" '"event":"peer-online"' 50 || fail "A saw no peer" +wait_for "$TMP/b.log" '"event":"peer-online"' 50 || fail "B saw no peer" +echo "p2p: both peers are online" + +AID=$(sed -n 's/.*"event":"peer-online".*"id":"\([^"]*\)".*/\1/p' "$TMP/a.log" | tail -1) +[ -n "$AID" ] || fail "no peer id on A" + +echo "{\"cmd\":\"send\",\"peer\":\"127.0.0.1:$PORT\",\"text\":\"hello from b\"}" >&4 +wait_for "$TMP/a.log" '"text":"hello from b"' 50 || fail "A got no message from B" +echo "p2p: B -> A message arrived" + +# A answers the connecting peer by the id it learned +echo "{\"cmd\":\"send\",\"peer\":\"$AID\",\"text\":\"reply from a\"}" >&3 +wait_for "$TMP/b.log" '"text":"reply from a"' 50 || fail "B got no reply from A" +echo "p2p: A -> B reply arrived" + +echo "{\"cmd\":\"quit\"}" >&4 +echo "{\"cmd\":\"quit\"}" >&3 +wait "$PB" 2>/dev/null || fail "B did not exit cleanly" +PB= +wait "$PA" 2>/dev/null || fail "A did not exit cleanly" +PA= +echo "p2p: both exited" +echo "tests/p2p.sh: all ok"