2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
42 #include "olsrd_jsoninfo_helpers.h"
54 #endif /* __linux__ */
56 static const char * empty = "";
60 /* JSON support functions */
62 /* JSON does not allow commas dangling at the end of arrays, so we need to
63 * count which entry number we're at in order to make sure we don't tack a
64 * dangling comma on at the end */
65 #define ENTRY_NUMBER_MAX_DEPTH 16
66 static int entrynumber[ENTRY_NUMBER_MAX_DEPTH] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
67 static int currentjsondepth = 0;
69 void abuf_json_reset_entry_number_and_depth(void) {
74 static void abuf_json_new_indent(struct autobuf *abuf) {
77 if (currentjsondepth) {
78 int i = currentjsondepth;
80 abuf_puts(abuf, "\n");
87 void abuf_json_insert_comma(struct autobuf *abuf) {
90 if (entrynumber[currentjsondepth])
91 abuf_appendf(abuf, ",");
94 void abuf_json_mark_output(bool open, struct autobuf *abuf) {
98 assert(!currentjsondepth);
99 abuf_json_new_indent(abuf);
100 abuf_puts(abuf, "{");
102 entrynumber[currentjsondepth] = 0;
104 assert(currentjsondepth == 1);
105 entrynumber[currentjsondepth] = 0;
107 abuf_json_new_indent(abuf);
108 abuf_puts(abuf, "\n}");
112 void abuf_json_mark_object(bool open, bool array, struct autobuf *abuf, const char* header) {
116 abuf_json_insert_comma(abuf);
117 abuf_json_new_indent(abuf);
119 abuf_appendf(abuf, "\"%s\": %s", header, array ? "[" : "{");
121 abuf_appendf(abuf, "%s", array ? "[" : "{");
123 entrynumber[currentjsondepth]++;
125 assert(currentjsondepth < ENTRY_NUMBER_MAX_DEPTH);
126 entrynumber[currentjsondepth] = 0;
128 entrynumber[currentjsondepth] = 0;
130 assert(currentjsondepth >= 0);
131 abuf_json_new_indent(abuf);
132 abuf_appendf(abuf, "%s", array ? "]" : "}");
136 void abuf_json_mark_array_entry(bool open, struct autobuf *abuf) {
139 abuf_json_mark_object(open, false, abuf, NULL);
142 void abuf_json_boolean(struct autobuf *abuf, const char* key, bool value) {
146 abuf_json_insert_comma(abuf);
147 abuf_json_new_indent(abuf);
148 abuf_appendf(abuf, "\"%s\": %s", key, value ? "true" : "false");
149 entrynumber[currentjsondepth]++;
152 void abuf_json_string(struct autobuf *abuf, const char* key, const char* value) {
156 assert(key || value);
164 abuf_json_insert_comma(abuf);
165 abuf_json_new_indent(abuf);
167 abuf_appendf(abuf, "\"%s\"", value);
169 abuf_appendf(abuf, "\"%s\": \"%s\"", key, val);
171 entrynumber[currentjsondepth]++;
174 void abuf_json_int(struct autobuf *abuf, const char* key, long long value) {
181 fmt = "\"%s\": %lld";
186 abuf_json_insert_comma(abuf);
187 abuf_json_new_indent(abuf);
188 abuf_appendf(abuf, fmt, key, value);
189 entrynumber[currentjsondepth]++;
192 void abuf_json_float(struct autobuf *abuf, const char* key, double value) {
194 int isInf = isinf(v);
201 } else if (isInf < 0) {
203 } else if (isInf > 0) {
207 abuf_json_insert_comma(abuf);
208 abuf_json_new_indent(abuf);
209 abuf_appendf(abuf, "\"%s\": %f", key, v);
210 entrynumber[currentjsondepth]++;
213 void abuf_json_ip_address(struct autobuf *abuf, const char* key, union olsr_ip_addr *ip) {
214 struct ipaddr_str ipStr;
223 value = olsr_ip_to_string(&ipStr, ip);
226 abuf_json_insert_comma(abuf);
227 abuf_json_new_indent(abuf);
229 abuf_appendf(abuf, "\"%s\"", value);
231 abuf_appendf(abuf, "\"%s\": \"%s\"", key, value);
233 entrynumber[currentjsondepth]++;
236 void abuf_json_ip_address46(struct autobuf *abuf, const char* key, void *ip, int af) {
237 struct ipaddr_str ipStr;
245 } else if (af == AF_INET) {
246 value = ip4_to_string(&ipStr, *((const struct in_addr*) ip));
248 value = ip6_to_string(&ipStr, (const struct in6_addr * const ) ip);
251 abuf_json_insert_comma(abuf);
252 abuf_json_new_indent(abuf);
254 abuf_appendf(abuf, "\"%s\"", value);
256 abuf_appendf(abuf, "\"%s\": \"%s\"", key, value);
258 entrynumber[currentjsondepth]++;
261 int read_uuid_from_file(const char * name, const char *file) {
270 memset(uuid, 0, sizeof(uuid));
272 f = fopen(file, "r");
273 olsr_printf(1, "(%s) Reading UUID from '%s'\n", name, file);
275 olsr_printf(1, "(%s) Could not open '%s': %s\n", name, file, strerror(errno));
278 chars = fread(uuid, 1, sizeof(uuid) - 1, f);
280 uuid[chars] = '\0'; /* null-terminate the string */
282 /* we only use the first line of the file */
283 end = strchr(uuid, '\n');
288 olsr_printf(1, "(%s) Could not read UUID from '%s': %s\n", name, file, strerror(errno));