[Olsr-dev] [PATCH v1 44/44] jsoninfo: move json support functions into their own file

Ferry Huberts (spam-protected)
Fri Nov 27 10:43:37 CET 2015


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 lib/jsoninfo/src/olsrd_jsoninfo.c                  | 175 +---------------
 lib/jsoninfo/src/olsrd_jsoninfo.h                  |   2 -
 lib/jsoninfo/src/olsrd_jsoninfo_helpers.c          | 220 +++++++++++++++++++++
 .../{olsrd_jsoninfo.h => olsrd_jsoninfo_helpers.h} |  59 +++---
 lib/jsoninfo/src/olsrd_plugin.c                    |   1 +
 5 files changed, 252 insertions(+), 205 deletions(-)
 create mode 100644 lib/jsoninfo/src/olsrd_jsoninfo_helpers.c
 copy lib/jsoninfo/src/{olsrd_jsoninfo.h => olsrd_jsoninfo_helpers.h} (63%)

diff --git a/lib/jsoninfo/src/olsrd_jsoninfo.c b/lib/jsoninfo/src/olsrd_jsoninfo.c
index 8a50195..f6ade32 100644
--- a/lib/jsoninfo/src/olsrd_jsoninfo.c
+++ b/lib/jsoninfo/src/olsrd_jsoninfo.c
@@ -62,10 +62,6 @@
 #include <libgen.h>
 #include <assert.h>
 
-#ifdef __linux__
-#include <fcntl.h>
-#endif /* __linux__ */
-
 #include "ipcalc.h"
 #include "olsr.h"
 #include "builddata.h"
@@ -82,6 +78,7 @@
 #include "common/autobuf.h"
 #include "gateway.h"
 #include "egressTypes.h"
+#include "olsrd_jsoninfo_helpers.h"
 
 #include "olsrd_jsoninfo.h"
 #include "olsrd_plugin.h"
@@ -158,171 +155,6 @@ static int outbuffer_count = 0;
 static struct timer_entry *writetimer_entry;
 static struct timeval start_time;
 
-char uuid[UUIDLEN + 1];
-char uuidfile[FILENAME_MAX];
-
-/* JSON support functions */
-
-/* JSON does not allow commas dangling at the end of arrays, so we need to
- * count which entry number we're at in order to make sure we don't tack a
- * dangling comma on at the end */
-static int entrynumber[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-static int currentjsondepth = 0;
-
-static void abuf_json_new_indent(struct autobuf *abuf) {
-  int i = currentjsondepth;
-
-  if (!i) {
-    return;
-  }
-
-  abuf_puts(abuf, "\n");
-  while (i-- > 0) {
-    abuf_puts(abuf, "  ");
-  }
-}
-
-static void abuf_json_mark_output(bool open, struct autobuf *abuf) {
-  if (open) {
-    assert(!currentjsondepth);
-    abuf_json_new_indent(abuf);
-    abuf_puts(abuf, "{");
-    currentjsondepth++;
-    entrynumber[currentjsondepth] = 0;
-  } else {
-    entrynumber[currentjsondepth] = 0;
-    currentjsondepth--;
-    assert(!currentjsondepth);
-    abuf_json_new_indent(abuf);
-    abuf_puts(abuf, "\n}");
-  }
-}
-
-static void abuf_json_mark_object(bool open, bool array, struct autobuf *abuf, const char* header) {
-  if (open) {
-    if (entrynumber[currentjsondepth]) {
-      abuf_appendf(abuf, ",");
-    }
-    abuf_json_new_indent(abuf);
-    if (header) {
-      abuf_appendf(abuf, "\"%s\": %s", header, array ? "[" : "{");
-    } else {
-      abuf_appendf(abuf, "%s", array ? "[" : "{");
-    }
-    entrynumber[currentjsondepth]++;
-    currentjsondepth++;
-    entrynumber[currentjsondepth] = 0;
-  } else {
-    entrynumber[currentjsondepth] = 0;
-    currentjsondepth--;
-    abuf_json_new_indent(abuf);
-    abuf_appendf(abuf, "%s", array ? "]" : "}");
-  }
-}
-
-static void abuf_json_mark_array_entry(bool open, struct autobuf *abuf) {
-  abuf_json_mark_object(open, false, abuf, NULL);
-}
-
-static void abuf_json_insert_comma(struct autobuf *abuf) {
-  if (entrynumber[currentjsondepth])
-    abuf_appendf(abuf, ",");
-}
-
-static void abuf_json_boolean(struct autobuf *abuf, const char* key, int value) {
-  abuf_json_insert_comma(abuf);
-  abuf_json_new_indent(abuf);
-  abuf_appendf(abuf, "\"%s\": %s", key, value ? "true" : "false");
-  entrynumber[currentjsondepth]++;
-}
-
-static void abuf_json_string(struct autobuf *abuf, const char* key, const char* value) {
-  abuf_json_insert_comma(abuf);
-  abuf_json_new_indent(abuf);
-  abuf_appendf(abuf, "\"%s\": \"%s\"", key, value);
-  entrynumber[currentjsondepth]++;
-}
-
-static void abuf_json_int(struct autobuf *abuf, const char* key, long value) {
-  abuf_json_insert_comma(abuf);
-  abuf_json_new_indent(abuf);
-  abuf_appendf(abuf, "\"%s\": %li", key, value);
-  entrynumber[currentjsondepth]++;
-}
-
-static void abuf_json_float(struct autobuf *abuf, const char* key, float value) {
-  abuf_json_insert_comma(abuf);
-  abuf_json_new_indent(abuf);
-  abuf_appendf(abuf, "\"%s\": %.03f", key, (double) value);
-  entrynumber[currentjsondepth]++;
-}
-
-/* Linux specific functions for getting system info */
-
-#ifdef __linux__
-static int get_string_from_file(const char* filename, char* buf, int len) {
-  int bytes = -1;
-  int fd = open(filename, O_RDONLY);
-  if (fd > -1) {
-    bytes = read(fd, buf, len);
-    if (bytes < len)
-      buf[bytes - 1] = '\0'; // remove trailing \n
-    else
-      buf[len - 1] = '\0';
-    close(fd);
-  }
-  return bytes;
-}
-
-static int abuf_json_sysdata(struct autobuf *abuf, const char* key, const char* syspath) {
-  int ret = -1;
-  char buf[256];
-  *buf = 0;
-  ret = get_string_from_file(syspath, buf, 256);
-  if (*buf)
-    abuf_json_string(abuf, key, buf);
-  return ret;
-}
-
-static void abuf_json_sys_class_net(struct autobuf *abuf, const char* key, const char* ifname, const char* datapoint) {
-  char filename[256];
-  snprintf(filename, 255, "/sys/class/net/%s/%s", ifname, datapoint);
-  abuf_json_sysdata(abuf, key, filename);
-}
-
-#endif /* __linux__ */
-
-static int read_uuid_from_file(const char *file) {
-  FILE *f;
-  char* end;
-  int r = 0;
-  size_t chars;
-
-  memset(uuid, 0, sizeof(uuid));
-
-  f = fopen(file, "r");
-  olsr_printf(1, "("PLUGIN_NAME") Reading UUID from '%s'\n", file);
-  if (f == NULL) {
-    olsr_printf(1, "("PLUGIN_NAME") Could not open '%s': %s\n", file, strerror(errno));
-    return -1;
-  }
-  chars = fread(uuid, 1, UUIDLEN, f);
-  if (chars > 0) {
-    uuid[chars] = '\0'; /* null-terminate the string */
-
-    /* we only use the first line of the file */
-    end = strchr(uuid, '\n');
-    if (end)
-      *end = 0;
-    r = 0;
-  } else {
-    olsr_printf(1, "("PLUGIN_NAME") Could not read UUID from '%s': %s\n", file, strerror(errno));
-    r = -1;
-  }
-
-  fclose(f);
-  return r;
-}
 
 static size_t build_http_header(const char *status, const char *mime, uint32_t msgsize, char *buf, uint32_t bufsize) {
   time_t currtime;
@@ -383,7 +215,7 @@ int olsrd_plugin_init(void) {
 
   if (!strlen(uuidfile))
     strscpy(uuidfile, "uuid.txt", sizeof(uuidfile));
-  read_uuid_from_file(uuidfile);
+  read_uuid_from_file(PLUGIN_NAME, uuidfile);
 
   plugin_ipc_init();
   return 1;
@@ -1357,8 +1189,7 @@ static void send_info(unsigned int send_what, int the_socket) {
   const char *content_type = (send_what & SIW_ALL) ? "application/json" : "text/plain";
 
   /* global variables for tracking when to put a comma in for JSON */
-  entrynumber[0] = 0;
-  currentjsondepth = 0;
+  abuf_json_reset_entry_number_and_depth();
 
   abuf_init(&abuf, 2 * 4096);
 
diff --git a/lib/jsoninfo/src/olsrd_jsoninfo.h b/lib/jsoninfo/src/olsrd_jsoninfo.h
index 25ed487..9018686 100644
--- a/lib/jsoninfo/src/olsrd_jsoninfo.h
+++ b/lib/jsoninfo/src/olsrd_jsoninfo.h
@@ -53,8 +53,6 @@
 /* uncomment this to allow connections from 127.0.0.1 regardless of olsrd.conf (useful to allow externel ip/network + localhost) (ipv4 only)*/
 /* #define JSONINFO_ALLOW_LOCALHOST */
 
-#define UUIDLEN 256
-extern char uuid[UUIDLEN + 1];
 extern char uuidfile[FILENAME_MAX];
 
 extern union olsr_ip_addr jsoninfo_accept_ip;
diff --git a/lib/jsoninfo/src/olsrd_jsoninfo_helpers.c b/lib/jsoninfo/src/olsrd_jsoninfo_helpers.c
new file mode 100644
index 0000000..b87a0e3
--- /dev/null
+++ b/lib/jsoninfo/src/olsrd_jsoninfo_helpers.c
@@ -0,0 +1,220 @@
+/*
+ * The olsr.org Optimized Link-State Routing daemon(olsrd)
+ * Copyright (c) 2004
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in
+ *   the documentation and/or other materials provided with the
+ *   distribution.
+ * * Neither the name of olsr.org, olsrd nor the names of its
+ *   contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Visit http://www.olsr.org for more information.
+ *
+ * If you find this software useful feel free to make a donation
+ * to the project. For more information see the website or contact
+ * the copyright holders.
+ *
+ */
+
+#include "olsrd_jsoninfo_helpers.h"
+#include "olsr.h"
+
+#include <stdbool.h>
+#include <assert.h>
+#include <unistd.h>
+
+#ifdef __linux__
+#include <fcntl.h>
+#endif /* __linux__ */
+
+char uuid[UUIDLEN + 1];
+
+/* JSON support functions */
+
+/* JSON does not allow commas dangling at the end of arrays, so we need to
+ * count which entry number we're at in order to make sure we don't tack a
+ * dangling comma on at the end */
+static int entrynumber[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+static int currentjsondepth = 0;
+
+static void abuf_json_new_indent(struct autobuf *abuf) {
+  int i = currentjsondepth;
+
+  if (!i) {
+    return;
+  }
+
+  abuf_puts(abuf, "\n");
+  while (i-- > 0) {
+    abuf_puts(abuf, "  ");
+  }
+}
+
+void abuf_json_reset_entry_number_and_depth(void) {
+  entrynumber[0] = 0;
+  currentjsondepth = 0;
+}
+
+void abuf_json_mark_output(bool open, struct autobuf *abuf) {
+  if (open) {
+    assert(!currentjsondepth);
+    abuf_json_new_indent(abuf);
+    abuf_puts(abuf, "{");
+    currentjsondepth++;
+    entrynumber[currentjsondepth] = 0;
+  } else {
+    entrynumber[currentjsondepth] = 0;
+    currentjsondepth--;
+    assert(!currentjsondepth);
+    abuf_json_new_indent(abuf);
+    abuf_puts(abuf, "\n}");
+  }
+}
+
+void abuf_json_mark_object(bool open, bool array, struct autobuf *abuf, const char* header) {
+  if (open) {
+    if (entrynumber[currentjsondepth]) {
+      abuf_appendf(abuf, ",");
+    }
+    abuf_json_new_indent(abuf);
+    if (header) {
+      abuf_appendf(abuf, "\"%s\": %s", header, array ? "[" : "{");
+    } else {
+      abuf_appendf(abuf, "%s", array ? "[" : "{");
+    }
+    entrynumber[currentjsondepth]++;
+    currentjsondepth++;
+    entrynumber[currentjsondepth] = 0;
+  } else {
+    entrynumber[currentjsondepth] = 0;
+    currentjsondepth--;
+    abuf_json_new_indent(abuf);
+    abuf_appendf(abuf, "%s", array ? "]" : "}");
+  }
+}
+
+void abuf_json_mark_array_entry(bool open, struct autobuf *abuf) {
+  abuf_json_mark_object(open, false, abuf, NULL);
+}
+
+void abuf_json_insert_comma(struct autobuf *abuf) {
+  if (entrynumber[currentjsondepth])
+    abuf_appendf(abuf, ",");
+}
+
+void abuf_json_boolean(struct autobuf *abuf, const char* key, int value) {
+  abuf_json_insert_comma(abuf);
+  abuf_json_new_indent(abuf);
+  abuf_appendf(abuf, "\"%s\": %s", key, value ? "true" : "false");
+  entrynumber[currentjsondepth]++;
+}
+
+void abuf_json_string(struct autobuf *abuf, const char* key, const char* value) {
+  abuf_json_insert_comma(abuf);
+  abuf_json_new_indent(abuf);
+  abuf_appendf(abuf, "\"%s\": \"%s\"", key, value);
+  entrynumber[currentjsondepth]++;
+}
+
+void abuf_json_int(struct autobuf *abuf, const char* key, long value) {
+  abuf_json_insert_comma(abuf);
+  abuf_json_new_indent(abuf);
+  abuf_appendf(abuf, "\"%s\": %li", key, value);
+  entrynumber[currentjsondepth]++;
+}
+
+void abuf_json_float(struct autobuf *abuf, const char* key, float value) {
+  abuf_json_insert_comma(abuf);
+  abuf_json_new_indent(abuf);
+  abuf_appendf(abuf, "\"%s\": %.03f", key, (double) value);
+  entrynumber[currentjsondepth]++;
+}
+
+/* Linux specific functions for getting system info */
+
+#ifdef __linux__
+static int get_string_from_file(const char* filename, char* buf, int len) {
+  int bytes = -1;
+  int fd = open(filename, O_RDONLY);
+  if (fd > -1) {
+    bytes = read(fd, buf, len);
+    if (bytes < len)
+      buf[bytes - 1] = '\0'; // remove trailing \n
+    else
+      buf[len - 1] = '\0';
+    close(fd);
+  }
+  return bytes;
+}
+
+static int abuf_json_sysdata(struct autobuf *abuf, const char* key, const char* syspath) {
+  int ret = -1;
+  char buf[256];
+  *buf = 0;
+  ret = get_string_from_file(syspath, buf, 256);
+  if (*buf)
+    abuf_json_string(abuf, key, buf);
+  return ret;
+}
+
+void abuf_json_sys_class_net(struct autobuf *abuf, const char* key, const char* ifname, const char* datapoint) {
+  char filename[256];
+  snprintf(filename, 255, "/sys/class/net/%s/%s", ifname, datapoint);
+  abuf_json_sysdata(abuf, key, filename);
+}
+#endif /* __linux__ */
+
+int read_uuid_from_file(const char * name, const char *file) {
+  FILE *f;
+  char* end;
+  int r = 0;
+  size_t chars;
+
+  memset(uuid, 0, sizeof(uuid));
+
+  f = fopen(file, "r");
+  olsr_printf(1, "(%s) Reading UUID from '%s'\n", name, file);
+  if (f == NULL) {
+    olsr_printf(1, "(%s) Could not open '%s': %s\n", name, file, strerror(errno));
+    return -1;
+  }
+  chars = fread(uuid, 1, UUIDLEN, f);
+  if (chars > 0) {
+    uuid[chars] = '\0'; /* null-terminate the string */
+
+    /* we only use the first line of the file */
+    end = strchr(uuid, '\n');
+    if (end)
+      *end = 0;
+    r = 0;
+  } else {
+    olsr_printf(1, "(%s) Could not read UUID from '%s': %s\n", name, file, strerror(errno));
+    r = -1;
+  }
+
+  fclose(f);
+  return r;
+}
diff --git a/lib/jsoninfo/src/olsrd_jsoninfo.h b/lib/jsoninfo/src/olsrd_jsoninfo_helpers.h
similarity index 63%
copy from lib/jsoninfo/src/olsrd_jsoninfo.h
copy to lib/jsoninfo/src/olsrd_jsoninfo_helpers.h
index 25ed487..2cffd41 100644
--- a/lib/jsoninfo/src/olsrd_jsoninfo.h
+++ b/lib/jsoninfo/src/olsrd_jsoninfo_helpers.h
@@ -1,7 +1,7 @@
 /*
  * The olsr.org Optimized Link-State Routing daemon(olsrd)
- * Copyright (c) 2004, Andreas Tonnesen((spam-protected))
- *                     includes code by Bruno Randolf
+ * Copyright (c) 2004
+ *
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -39,41 +39,38 @@
  *
  */
 
-/*
- * Dynamic linked library for the olsr.org olsr daemon
- */
-
-#ifndef _OLSRD_JSONINFO
-#define _OLSRD_JSONINFO
+#ifndef LIB_TXTINFO_SRC_OLSRD_JSONINFO_HELPERS_H_
+#define LIB_TXTINFO_SRC_OLSRD_JSONINFO_HELPERS_H_
 
-#include "olsr_types.h"
-#include "olsrd_plugin.h"
-#include "plugin_util.h"
+#include <stdio.h>
 
-/* uncomment this to allow connections from 127.0.0.1 regardless of olsrd.conf (useful to allow externel ip/network + localhost) (ipv4 only)*/
-/* #define JSONINFO_ALLOW_LOCALHOST */
+#include "common/autobuf.h"
 
 #define UUIDLEN 256
 extern char uuid[UUIDLEN + 1];
-extern char uuidfile[FILENAME_MAX];
 
-extern union olsr_ip_addr jsoninfo_accept_ip;
-extern union olsr_ip_addr jsoninfo_listen_ip;
-extern int ipc_port;
-extern int nompr;
-extern bool http_headers;
-extern int jsoninfo_ipv6_only;
+void abuf_json_reset_entry_number_and_depth(void);
 
-int olsrd_plugin_interface_version(void);
-int olsrd_plugin_init(void);
-void olsr_plugin_exit(void);
-void olsrd_get_plugin_parameters(const struct olsrd_plugin_parameters **params, int *size);
+void abuf_json_mark_output(bool open, struct autobuf *abuf);
 
-#endif /* _OLSRD_JSONINFO */
+void abuf_json_mark_object(bool open, bool array, struct autobuf *abuf, const char* header);
 
-/*
- * Local Variables:
- * c-basic-offset: 2
- * indent-tabs-mode: nil
- * End:
- */
+void abuf_json_mark_array_entry(bool open, struct autobuf *abuf);
+
+void abuf_json_insert_comma(struct autobuf *abuf);
+
+void abuf_json_boolean(struct autobuf *abuf, const char* key, int value);
+
+void abuf_json_string(struct autobuf *abuf, const char* key, const char* value);
+
+void abuf_json_int(struct autobuf *abuf, const char* key, long value);
+
+void abuf_json_float(struct autobuf *abuf, const char* key, float value);
+
+#ifdef __linux__
+void abuf_json_sys_class_net(struct autobuf *abuf, const char* key, const char* ifname, const char* datapoint);
+#endif /* __linux__ */
+
+int read_uuid_from_file(const char * name, const char *file);
+
+#endif /* LIB_TXTINFO_SRC_OLSRD_JSONINFO_HELPERS_H_ */
diff --git a/lib/jsoninfo/src/olsrd_plugin.c b/lib/jsoninfo/src/olsrd_plugin.c
index 440f659..5ec21cb 100644
--- a/lib/jsoninfo/src/olsrd_plugin.c
+++ b/lib/jsoninfo/src/olsrd_plugin.c
@@ -63,6 +63,7 @@ union olsr_ip_addr jsoninfo_accept_ip;
 union olsr_ip_addr jsoninfo_listen_ip;
 int ipc_port;
 int nompr;
+char uuidfile[FILENAME_MAX];
 bool http_headers;
 int jsoninfo_ipv6_only;
 
-- 
2.5.0




More information about the Olsr-dev mailing list