vici: Move dumping to core message class, use it in libvici
authorMartin Willi <martin@revosec.ch>
Tue, 11 Feb 2014 13:36:42 +0000 (14:36 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 7 May 2014 12:13:35 +0000 (14:13 +0200)
src/libcharon/plugins/vici/libvici.c
src/libcharon/plugins/vici/libvici.h
src/libcharon/plugins/vici/vici_message.c
src/libcharon/plugins/vici/vici_message.h

index f184a3b..61ff5b9 100644 (file)
@@ -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;
 }
index fed3565..499e9a0 100644 (file)
@@ -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.
index 1a1820a..644cbce 100644 (file)
@@ -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(),
index 9e1a366..4fc59d7 100644 (file)
@@ -138,6 +138,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.
         */
        void (*destroy)(vici_message_t *this);