ctdb-tests: Add a utility to parse ctdb packets
authorAmitay Isaacs <amitay@gmail.com>
Thu, 10 Mar 2016 04:44:24 +0000 (15:44 +1100)
committerMartin Schwenke <martins@samba.org>
Thu, 17 Mar 2016 12:56:41 +0000 (13:56 +0100)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
Autobuild-User(master): Martin Schwenke <martins@samba.org>
Autobuild-Date(master): Thu Mar 17 13:56:41 CET 2016 on sn-devel-144

ctdb/tests/src/ctdb_packet_parse.c [new file with mode: 0644]
ctdb/wscript

diff --git a/ctdb/tests/src/ctdb_packet_parse.c b/ctdb/tests/src/ctdb_packet_parse.c
new file mode 100644 (file)
index 0000000..c337a2e
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+   CTDB protocol parser
+
+   Copyright (C) Amitay Isaacs  2016
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "system/network.h"
+#include "system/locale.h"
+
+#include <talloc.h>
+#include <tdb.h>
+
+#include "protocol/protocol.h"
+#include "protocol/protocol_api.h"
+
+static TDB_DATA strace_parser(char *buf, TALLOC_CTX *mem_ctx)
+{
+       TDB_DATA data;
+       int i = 0, j = 0;
+
+       data.dptr = talloc_size(mem_ctx, strlen(buf));
+       if (data.dptr == NULL) {
+               return tdb_null;
+       }
+
+       while (i < strlen(buf)) {
+               if (buf[i] == '\\') {
+                       char tmp[4] = { '\0', '\0', '\0', '\0' };
+
+                       /* first char after '\' has to be digit */
+                       tmp[0] = buf[i+1];
+                       if (isdigit(buf[i+2])) {
+                               tmp[1] = buf[i+2];
+                               if (isdigit(buf[i+3])) {
+                                       tmp[2] = buf[i+3];
+                                       i += 4;
+                               } else {
+                                       i += 3;
+                               }
+                       } else {
+                               i += 2;
+                       }
+
+                       data.dptr[j] = strtol(tmp, NULL, 8);
+                       j += 1;
+               } else if (buf[i] == '\n') {
+                       i += 1;
+               } else if (buf[i] == '\0') {
+                       break;
+               } else {
+                       data.dptr[j] = buf[i];
+                       i += 1;
+                       j += 1;
+               }
+       }
+
+       data.dsize = j;
+
+       return data;
+}
+
+int main(int argc, char *argv[])
+{
+       TALLOC_CTX *mem_ctx = talloc_new(NULL);
+       char line[1024];
+       char *ptr;
+       TDB_DATA (*parser)(char *, TALLOC_CTX *);
+
+       if (argc != 2) {
+               fprintf(stderr, "Usage: %s strace\n", argv[0]);
+               exit(1);
+       }
+
+       if (strcmp(argv[1], "strace") == 0) {
+               parser = strace_parser;
+       } else {
+               fprintf(stderr, "Unknown input format - %s\n", argv[1]);
+               exit(1);
+       }
+
+       while ((ptr = fgets(line, sizeof(line), stdin)) != NULL) {
+               TDB_DATA data;
+
+               data = parser(ptr, mem_ctx);
+               if (data.dptr == NULL) {
+                       continue;
+               }
+
+               ctdb_packet_print(data.dptr, data.dsize, stdout);
+               TALLOC_FREE(data.dptr);
+       }
+
+       return 0;
+}
index cb94fbe11c53b3ed581d554752e36533c041157f..95e5b45f8797f6358be43fac9ce349ab00ac4d04 100755 (executable)
@@ -642,6 +642,11 @@ def build(bld):
                      deps='samba-util',
                      install_path='${CTDB_TEST_LIBDIR}')
 
+    bld.SAMBA_BINARY('ctdb_packet_parse',
+                     source='tests/src/ctdb_packet_parse.c',
+                     deps='talloc tevent tdb ctdb-protocol',
+                     install_path='${CTDB_TEST_LIBDIR}')
+
     # Test binaries
     ctdb_tests = [
         'rb_test',