diff --git a/flock/ctl.c b/flock/ctl.c index ea34a7fe..1552993b 100644 --- a/flock/ctl.c +++ b/flock/ctl.c @@ -1,4 +1,5 @@ #include "lib/birdlib.h" +#include "lib/cbor.h" #include "lib/string.h" #include "lib/io-loop.h" @@ -19,6 +20,7 @@ struct cbor_parser_context { linpool *lp; + sock *sock; PACKED enum { CPE_TYPE = 0, @@ -58,12 +60,13 @@ struct cbor_parser_context { } while (0) struct cbor_parser_context * -hcs_parser_init(pool *p) +hcs_parser_init(sock *s) { - linpool *lp = lp_new(p); + linpool *lp = lp_new(s->pool); struct cbor_parser_context *ctx = lp_allocz(lp, sizeof *ctx); ctx->lp = lp; + ctx->sock = s; ctx->type = 0xff; ctx->stack_countdown[0] = 1; @@ -154,7 +157,7 @@ hcs_parse(struct cbor_parser_context *ctx, const byte *buf, s64 size) case 1: /* inside toplevel mapping */ if (ctx->type != 0) CBOR_PARSER_ERROR("Expected integer, got %u", ctx->type); - + if (ctx->value >= 4) CBOR_PARSER_ERROR("Command key too high, got %lu", ctx->value); @@ -167,6 +170,14 @@ hcs_parse(struct cbor_parser_context *ctx, const byte *buf, s64 size) log(L_INFO "Requested shutdown via CLI"); ev_send_loop(&main_birdloop, &poweroff_event); + { + struct cbor_writer *cw = cbor_init(ctx->sock->tbuf, ctx->sock->tbsize, ctx->lp); + cbor_open_block_with_length(cw, 1); + cbor_add_int(cw, -1); + cbor_add_string(cw, "OK"); + sk_send(ctx->sock, cw->pt); + } + ctx->major_state = 1; break; @@ -175,6 +186,7 @@ hcs_parse(struct cbor_parser_context *ctx, const byte *buf, s64 size) CBOR_PARSER_ERROR("Expected null, got %u-%u", ctx->type, ctx->value); log(L_INFO "Requested telnet open"); + ctx->major_state = 1; break; diff --git a/flock/flock.h b/flock/flock.h index 2f83c558..8cc33480 100644 --- a/flock/flock.h +++ b/flock/flock.h @@ -6,6 +6,7 @@ #include "lib/event.h" #include "lib/obstacle.h" #include "lib/resource.h" +#include "lib/socket.h" void hypervisor_exposed_fork(void); void hypervisor_control_socket(void); @@ -18,7 +19,7 @@ struct flock_config { extern struct flock_config flock_config; -struct cbor_parser_context *hcs_parser_init(pool *p); +struct cbor_parser_context *hcs_parser_init(sock *s); s64 hcs_parse(struct cbor_parser_context *ctx, const byte *buf, s64 size); void hcs_parser_cleanup(struct cbor_parser_context *ctx); const char *hcs_error(struct cbor_parser_context *ctx); diff --git a/flock/hypervisor.c b/flock/hypervisor.c index 26d5e455..217eb5a7 100644 --- a/flock/hypervisor.c +++ b/flock/hypervisor.c @@ -39,7 +39,7 @@ hcs_rx(sock *s, uint size) /* TODO do something more */ hcs_parser_cleanup(s->data); - s->data = hcs_parser_init(s->pool); + s->data = hcs_parser_init(s); if (sz == size) return 1; @@ -63,7 +63,7 @@ hcs_connect(sock *s, uint size UNUSED) s->rx_hook = hcs_rx; s->err_hook = hcs_err; - s->data = hcs_parser_init(s->pool); + s->data = hcs_parser_init(s); return 1; } @@ -103,6 +103,7 @@ hypervisor_control_socket(void) s->rx_hook = hcs_connect; s->err_hook = hcs_connect_err; s->rbsize = 1024; + s->tbsize = 1024; unlink(flock_config.control_socket_path); if (sk_open_unix(s, loop, flock_config.control_socket_path) < 0)