diff --git a/src/libcharon/plugins/vici/libvici.c b/src/libcharon/plugins/vici/libvici.c index f184a3b27..61ff5b993 100644 --- a/src/libcharon/plugins/vici/libvici.c +++ b/src/libcharon/plugins/vici/libvici.c @@ -417,64 +417,12 @@ void vici_free_req(vici_req_t *req) free(req); } -int vici_dump(vici_res_t *res, FILE *out) +int vici_dump(vici_res_t *res, char *label, FILE *out) { - enumerator_t *enumerator; - int ident = 0, delta = 2; - vici_type_t type; - char *name; - chunk_t value; - - enumerator = res->message->create_enumerator(res->message); - while (enumerator->enumerate(enumerator, &type, &name, &value)) + if (res->message->dump(res->message, label, out)) { - switch (type) - { - case VICI_SECTION_START: - fprintf(out, "%*s%s {\n", ident, "", name); - ident += delta; - break; - case VICI_SECTION_END: - ident -= delta; - fprintf(out, "%*s}\n", ident, ""); - break; - case VICI_KEY_VALUE: - if (chunk_printable(value, NULL, ' ')) - { - fprintf(out, "%*s%s = %.*s\n", - ident, "", name, (int)value.len, value.ptr); - } - else - { - fprintf(out, "%*s%s = 0x%+#B\n", - ident, "", name, &value); - } - break; - case VICI_LIST_START: - fprintf(out, "%*s%s = [\n", ident, "", name); - ident += delta; - break; - case VICI_LIST_END: - ident -= delta; - fprintf(out, "%*s]\n", ident, ""); - break; - case VICI_LIST_ITEM: - if (chunk_printable(value, NULL, ' ')) - { - fprintf(out, "%*s%.*s\n", - ident, "", (int)value.len, value.ptr); - } - else - { - fprintf(out, "%*s 0x%+#B\n", ident, "", &value); - } - break; - case VICI_END: - enumerator->destroy(enumerator); - return 0; - } + return 0; } - enumerator->destroy(enumerator); errno = EBADMSG; return 1; } diff --git a/src/libcharon/plugins/vici/libvici.h b/src/libcharon/plugins/vici/libvici.h index fed356547..499e9a0b1 100644 --- a/src/libcharon/plugins/vici/libvici.h +++ b/src/libcharon/plugins/vici/libvici.h @@ -247,10 +247,11 @@ void vici_free_req(vici_req_t *req); * Dump a message text representation to a FILE stream. * * @param res response message to dump + * @param label a label to print for this message * @param out FILE to dump to * @return 0 if dumped complete message, 1 on error */ -int vici_dump(vici_res_t *res, FILE *out); +int vici_dump(vici_res_t *res, char *label, FILE *out); /** * Parse next element from a vici response message. diff --git a/src/libcharon/plugins/vici/vici_message.c b/src/libcharon/plugins/vici/vici_message.c index 1a1820acb..644cbce59 100644 --- a/src/libcharon/plugins/vici/vici_message.c +++ b/src/libcharon/plugins/vici/vici_message.c @@ -412,6 +412,72 @@ METHOD(vici_message_t, get_encoding, chunk_t, return this->encoding; } +METHOD(vici_message_t, dump, bool, + private_vici_message_t *this, char *label, FILE *out) +{ + enumerator_t *enumerator; + int ident = 0, delta = 2; + vici_type_t type; + char *name; + chunk_t value; + + fprintf(out, "%s {\n", label); + ident += delta; + + enumerator = create_enumerator(this); + while (enumerator->enumerate(enumerator, &type, &name, &value)) + { + switch (type) + { + case VICI_SECTION_START: + fprintf(out, "%*s%s {\n", ident, "", name); + ident += delta; + break; + case VICI_SECTION_END: + ident -= delta; + fprintf(out, "%*s}\n", ident, ""); + break; + case VICI_KEY_VALUE: + if (chunk_printable(value, NULL, ' ')) + { + fprintf(out, "%*s%s = %.*s\n", + ident, "", name, (int)value.len, value.ptr); + } + else + { + fprintf(out, "%*s%s = 0x%+#B\n", + ident, "", name, &value); + } + break; + case VICI_LIST_START: + fprintf(out, "%*s%s = [\n", ident, "", name); + ident += delta; + break; + case VICI_LIST_END: + ident -= delta; + fprintf(out, "%*s]\n", ident, ""); + break; + case VICI_LIST_ITEM: + if (chunk_printable(value, NULL, ' ')) + { + fprintf(out, "%*s%.*s\n", + ident, "", (int)value.len, value.ptr); + } + else + { + fprintf(out, "%*s 0x%+#B\n", ident, "", &value); + } + break; + case VICI_END: + fprintf(out, "}\n"); + enumerator->destroy(enumerator); + return TRUE; + } + } + enumerator->destroy(enumerator); + return FALSE; +} + METHOD(vici_message_t, destroy, void, private_vici_message_t *this) { @@ -440,6 +506,7 @@ vici_message_t *vici_message_create_from_data(chunk_t data, bool cleanup) .get_value = _get_value, .vget_value = _vget_value, .get_encoding = _get_encoding, + .dump = _dump, .destroy = _destroy, }, .strings = linked_list_create(), diff --git a/src/libcharon/plugins/vici/vici_message.h b/src/libcharon/plugins/vici/vici_message.h index 9e1a36659..4fc59d746 100644 --- a/src/libcharon/plugins/vici/vici_message.h +++ b/src/libcharon/plugins/vici/vici_message.h @@ -137,6 +137,15 @@ struct vici_message_t { */ chunk_t (*get_encoding)(vici_message_t *this); + /** + * Dump a message text representation to a FILE stream. + * + * @param label label to print for message + * @param out FILE stream to dump to + * @return TRUE if message valid + */ + bool (*dump)(vici_message_t *this, char *label, FILE *out); + /** * Destroy a vici_message_t. */