2be95c637979905837304e402f2aca4cd2e5b646
[olsrd.git] / lib / jsoninfo / src / olsrd_jsoninfo.c
1 /*
2  * The olsr.org Optimized Link-State Routing daemon(olsrd)
3  * Copyright (c) 2004
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *   distribution.
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.
20  *
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.
33  *
34  * Visit http://www.olsr.org for more information.
35  *
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.
39  *
40  */
41
42 #include "olsrd_jsoninfo.h"
43
44 #include <unistd.h>
45 #include <ctype.h>
46 #include <libgen.h>
47
48 #include "ipcalc.h"
49 #include "builddata.h"
50 #include "neighbor_table.h"
51 #include "mpr_selector_set.h"
52 #include "routing_table.h"
53 #include "mid_set.h"
54 #include "gateway.h"
55 #include "egressTypes.h"
56 #include "olsrd_jsoninfo_helpers.h"
57 #include "olsrd_plugin.h"
58 #include "../../info/info_types.h"
59
60 extern char uuidfile[FILENAME_MAX];
61
62 struct timeval start_time;
63
64 void plugin_init(const char *plugin_name) {
65   /* Get start time */
66   gettimeofday(&start_time, NULL);
67
68   if (!strlen(uuidfile))
69     strscpy(uuidfile, "uuid.txt", sizeof(uuidfile));
70   read_uuid_from_file(plugin_name, uuidfile);
71 }
72
73 bool isCommand(const char *str, unsigned int siw) {
74   switch (siw) {
75     case SIW_OLSRD_CONF:
76       return strstr(str, "/olsrd.conf");
77
78     case SIW_ALL:
79       return strstr(str, "/all");
80
81     case SIW_RUNTIME_ALL:
82       return strstr(str, "/runtime");
83
84     case SIW_STARTUP_ALL:
85       return strstr(str, "/startup");
86
87     case SIW_NEIGHBORS:
88       return strstr(str, "/neighbors");
89
90     case SIW_LINKS:
91       return strstr(str, "/links");
92
93     case SIW_ROUTES:
94       return strstr(str, "/routes");
95
96     case SIW_HNA:
97       return strstr(str, "/hna");
98
99     case SIW_MID:
100       return strstr(str, "/mid");
101
102     case SIW_TOPOLOGY:
103       return strstr(str, "/topology");
104
105     case SIW_GATEWAYS:
106       return strstr(str, "/gateways");
107
108     case SIW_INTERFACES:
109       return strstr(str, "/interfaces");
110
111     case SIW_2HOP:
112       return strstr(str, "/2hop");
113
114     case SIW_SGW:
115       return strstr(str, "/sgw");
116
117     case SIW_VERSION:
118       return strstr(str, "/version");
119
120     case SIW_CONFIG:
121       return strstr(str, "/config");
122
123     case SIW_PLUGINS:
124       return strstr(str, "/plugins");
125
126     case SIW_NEIGHBORS_FREIFUNK:
127       return strstr(str, "/neighbours");
128
129     default:
130       return false;
131   }
132 }
133
134 const char * determine_mime_type(unsigned int send_what) {
135   return (send_what & SIW_ALL) ? "application/json; charset=utf-8" : "text/plain; charset=utf-8";
136 }
137
138 void output_start(struct autobuf *abuf) {
139   /* global variables for tracking when to put a comma in for JSON */
140   abuf_json_reset_entry_number_and_depth();
141   abuf_json_mark_output(true, abuf);
142
143   abuf_json_int(abuf, "systemTime", time(NULL));
144   abuf_json_int(abuf, "timeSinceStartup", now_times);
145   if (*uuid)
146     abuf_json_string(abuf, "uuid", uuid);
147 }
148
149 void output_end(struct autobuf *abuf) {
150   abuf_json_mark_output(false, abuf);
151   abuf_puts(abuf, "\n");
152 }
153
154 static void ipc_print_neighbors_internal(struct autobuf *abuf, bool list_2hop) {
155   struct ipaddr_str buf1;
156   struct neighbor_entry *neigh;
157   struct neighbor_2_list_entry *list_2;
158   int thop_cnt;
159
160   if (!list_2hop)
161     abuf_json_mark_object(true, true, abuf, "neighbors");
162   else
163     abuf_json_mark_object(true, true, abuf, "2hop");
164
165   /* Neighbors */
166   OLSR_FOR_ALL_NBR_ENTRIES(neigh)
167       {
168         abuf_json_mark_array_entry(true, abuf);
169
170         abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf1, &neigh->neighbor_main_addr));
171         abuf_json_boolean(abuf, "symmetric", (neigh->status == SYM));
172         abuf_json_boolean(abuf, "multiPointRelay", neigh->is_mpr);
173         abuf_json_boolean(abuf, "multiPointRelaySelector", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) != NULL);
174         abuf_json_int(abuf, "willingness", neigh->willingness);
175         thop_cnt = 0;
176
177         if (!list_2hop) {
178           for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
179             thop_cnt++;
180           }
181           abuf_json_int(abuf, "twoHopNeighborCount", thop_cnt);
182         } else {
183           abuf_json_mark_object(true, true, abuf, "twoHopNeighbors");
184           for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
185             abuf_json_mark_array_entry(true, abuf);
186             abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
187             abuf_json_mark_array_entry(false, abuf);
188           }
189           abuf_json_mark_object(false, true, abuf, false);
190         }
191
192         abuf_json_mark_array_entry(false, abuf);
193       }OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
194   abuf_json_mark_object(false, true, abuf, NULL);
195 }
196
197 void ipc_print_neighbors(struct autobuf *abuf) {
198   ipc_print_neighbors_internal(abuf, false);
199 }
200
201 void ipc_print_links(struct autobuf *abuf) {
202   struct ipaddr_str buf1, buf2;
203   struct lqtextbuffer lqbuffer1;
204
205   struct link_entry *my_link = NULL;
206
207   abuf_json_mark_object(true, true, abuf, "links");
208
209   OLSR_FOR_ALL_LINK_ENTRIES(my_link)
210       {
211         const char* lqs;
212         int diff = (unsigned int) (my_link->link_timer->timer_clock - now_times);
213
214         abuf_json_mark_array_entry(true, abuf);
215         abuf_json_string(abuf, "localIP", olsr_ip_to_string(&buf1, &my_link->local_iface_addr));
216         abuf_json_string(abuf, "remoteIP", olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr));
217         abuf_json_int(abuf, "validityTime", diff);
218         lqs = get_link_entry_text(my_link, '\t', &lqbuffer1);
219         abuf_json_float(abuf, "linkQuality", atof(lqs));
220         abuf_json_float(abuf, "neighborLinkQuality", atof(strrchr(lqs, '\t')));
221         if (my_link->linkcost >= LINK_COST_BROKEN)
222           abuf_json_int(abuf, "linkCost", LINK_COST_BROKEN);
223         else
224           abuf_json_int(abuf, "linkCost", my_link->linkcost);
225         abuf_json_mark_array_entry(false, abuf);
226       }OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
227   abuf_json_mark_object(false, true, abuf, NULL);
228 }
229
230 void ipc_print_routes(struct autobuf *abuf) {
231   struct ipaddr_str buf1, buf2;
232   struct rt_entry *rt;
233
234   abuf_json_mark_object(true, true, abuf, "routes");
235
236   /* Walk the route table */
237   OLSR_FOR_ALL_RT_ENTRIES(rt)
238       {
239         abuf_json_mark_array_entry(true, abuf);
240         abuf_json_string(abuf, "destination", olsr_ip_to_string(&buf1, &rt->rt_dst.prefix));
241         abuf_json_int(abuf, "genmask", rt->rt_dst.prefix_len);
242         abuf_json_string(abuf, "gateway", olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway));
243         abuf_json_int(abuf, "metric", rt->rt_best->rtp_metric.hops);
244         if (rt->rt_best->rtp_metric.cost >= ROUTE_COST_BROKEN)
245           abuf_json_int(abuf, "rtpMetricCost", ROUTE_COST_BROKEN);
246         else
247           abuf_json_int(abuf, "rtpMetricCost", rt->rt_best->rtp_metric.cost);
248         abuf_json_string(abuf, "networkInterface", if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
249         abuf_json_mark_array_entry(false, abuf);
250       }OLSR_FOR_ALL_RT_ENTRIES_END(rt);
251
252   abuf_json_mark_object(false, true, abuf, NULL);
253 }
254
255 void ipc_print_topology(struct autobuf *abuf) {
256   struct tc_entry *tc;
257
258   abuf_json_mark_object(true, true, abuf, "topology");
259
260   /* Topology */
261   OLSR_FOR_ALL_TC_ENTRIES(tc)
262       {
263         struct tc_edge_entry *tc_edge;
264         OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge)
265             {
266               if (tc_edge->edge_inv) {
267                 struct ipaddr_str dstbuf, addrbuf;
268                 struct lqtextbuffer lqbuffer1;
269                 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
270                 int diff = (int) (vt);
271                 const char* lqs;
272                 abuf_json_mark_array_entry(true, abuf);
273                 abuf_json_string(abuf, "destinationIP", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr));
274                 abuf_json_string(abuf, "lastHopIP", olsr_ip_to_string(&addrbuf, &tc->addr));
275                 lqs = get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1);
276                 abuf_json_float(abuf, "linkQuality", atof(lqs));
277                 abuf_json_float(abuf, "neighborLinkQuality", atof(strrchr(lqs, '\t')));
278                 if (tc_edge->cost >= LINK_COST_BROKEN)
279                   abuf_json_int(abuf, "tcEdgeCost", LINK_COST_BROKEN);
280                 else
281                   abuf_json_int(abuf, "tcEdgeCost", tc_edge->cost);
282                 abuf_json_int(abuf, "validityTime", diff);
283                 abuf_json_mark_array_entry(false, abuf);
284               }
285             }OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
286       }OLSR_FOR_ALL_TC_ENTRIES_END(tc);
287
288   abuf_json_mark_object(false, true, abuf, NULL);
289 }
290
291 void ipc_print_hna(struct autobuf *abuf) {
292   struct hna_entry *tmp_hna;
293   struct hna_net *tmp_net;
294   struct ipaddr_str buf, mainaddrbuf;
295
296   abuf_json_mark_object(true, true, abuf, "hna");
297
298   OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna)
299         {
300
301           /* Check all networks */
302           for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
303             uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
304             int diff = (int) (vt);
305             abuf_json_mark_array_entry(true, abuf);
306             abuf_json_string(abuf, "destination", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix)), abuf_json_int(abuf, "genmask",
307                 tmp_net->hna_prefix.prefix_len);
308             abuf_json_string(abuf, "gateway", olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
309             abuf_json_int(abuf, "validityTime", diff);
310             abuf_json_mark_array_entry(false, abuf);
311           }
312         }OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
313
314   abuf_json_mark_object(false, true, abuf, NULL);
315 }
316
317 void ipc_print_mid(struct autobuf *abuf) {
318   int idx;
319   struct mid_entry *entry;
320   struct mid_address *alias;
321
322   abuf_json_mark_object(true, true, abuf, "mid");
323
324   /* MID */
325   for (idx = 0; idx < HASHSIZE; idx++) {
326     entry = mid_set[idx].next;
327
328     while (entry != &mid_set[idx]) {
329       struct ipaddr_str buf, buf2;
330       abuf_json_mark_array_entry(true, abuf);
331       abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf, &entry->main_addr));
332
333       abuf_json_mark_object(true, true, abuf, "aliases");
334       alias = entry->aliases;
335       while (alias) {
336         uint32_t vt = alias->vtime - now_times;
337         int diff = (int) (vt);
338
339         abuf_json_mark_array_entry(true, abuf);
340         abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf2, &alias->alias));
341         abuf_json_int(abuf, "validityTime", diff);
342         abuf_json_mark_array_entry(false, abuf);
343
344         alias = alias->next_alias;
345       }
346       abuf_json_mark_object(false, true, abuf, NULL); // aliases
347       abuf_json_mark_array_entry(false, abuf);
348       entry = entry->next;
349     }
350   }
351   abuf_json_mark_object(false, true, abuf, NULL); // mid
352 }
353
354 void ipc_print_gateways(struct autobuf *abuf) {
355 #ifndef __linux__
356   abuf_json_string(abuf, "error", "Gateway mode is only supported in Linux");
357 #else /* __linux__ */
358
359   struct ipaddr_str buf;
360   struct gateway_entry *gw;
361
362   abuf_json_mark_object(true, true, abuf, "gateways");
363   OLSR_FOR_ALL_GATEWAY_ENTRIES(gw)
364       {
365         const char *v4 = "", *v6 = "";
366         bool autoV4 = false, autoV6 = false;
367         const char *ipType = "";
368         struct tc_entry *tc;
369
370         if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
371           continue;
372         }
373
374         if (gw == olsr_get_inet_gateway(false)) {
375           v4 = "s";
376         } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit) && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
377           v4 = "u";
378         }
379
380         if (gw == olsr_get_inet_gateway(true)) {
381           v6 = "s";
382         } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
383           v6 = "u";
384         }
385
386         abuf_json_mark_array_entry(true, abuf);
387         if (gw->ipv4) {
388           ipType = "ipv4";
389           abuf_json_string(abuf, "ipv4Status", v4);
390         } else if (gw->ipv6) {
391           ipType = "ipv6";
392           abuf_json_string(abuf, "ipv6Status", v6);
393         }
394         abuf_json_string(abuf, "ipType", ipType);
395         abuf_json_boolean(abuf, "ipv4", gw->ipv4);
396         abuf_json_boolean(abuf, "ipv4Nat", gw->ipv4nat);
397         abuf_json_boolean(abuf, "ipv6", gw->ipv6);
398         abuf_json_boolean(abuf, "autoIpv4", autoV4);
399         abuf_json_boolean(abuf, "autoIpv6", autoV6);
400         abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf, &gw->originator));
401         if (tc->path_cost >= ROUTE_COST_BROKEN)
402           abuf_json_int(abuf, "tcPathCost", ROUTE_COST_BROKEN);
403         else
404           abuf_json_int(abuf, "tcPathCost", tc->path_cost);
405         abuf_json_int(abuf, "hopCount", tc->hops);
406         abuf_json_int(abuf, "uplinkSpeed", gw->uplink);
407         abuf_json_int(abuf, "downlinkSpeed", gw->downlink);
408         if (!gw->external_prefix.prefix_len)
409           abuf_json_string(abuf, "externalPrefix", olsr_ip_prefix_to_string(&gw->external_prefix));
410         abuf_json_mark_array_entry(false, abuf);
411       }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
412   abuf_json_mark_object(false, true, abuf, NULL);
413 #endif /* __linux__ */
414 }
415
416 #ifdef __linux__
417
418 /** interface names for smart gateway tunnel interfaces, IPv4 */
419 extern struct interfaceName * sgwTunnel4InterfaceNames;
420
421 /** interface names for smart gateway tunnel interfaces, IPv6 */
422 extern struct interfaceName * sgwTunnel6InterfaceNames;
423
424 /**
425  * Construct the sgw table for a given ip version
426  *
427  * @param abuf the string buffer
428  * @param ipv6 true for IPv6, false for IPv4
429  * @param fmtv the format for printing
430  */
431 static void sgw_ipvx(struct autobuf *abuf, bool ipv6) {
432   struct interfaceName * sgwTunnelInterfaceNames;
433
434   abuf_json_mark_object(true, true, abuf, ipv6 ? "ipv6" : "ipv4");
435
436   sgwTunnelInterfaceNames = !ipv6 ? sgwTunnel4InterfaceNames : sgwTunnel6InterfaceNames;
437   if (olsr_cnf->smart_gw_active && sgwTunnelInterfaceNames) {
438     struct gateway_entry * current_gw = olsr_get_inet_gateway(ipv6);
439     int i;
440     for (i = 0; i < olsr_cnf->smart_gw_use_count; i++) {
441       struct interfaceName * node = &sgwTunnelInterfaceNames[i];
442       struct gateway_entry * gw = node->gw;
443
444       if (!gw) {
445         continue;
446       }
447
448       abuf_json_mark_array_entry(true, abuf);
449       {
450         struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
451
452         struct ipaddr_str originatorStr;
453         const char * originator = olsr_ip_to_string(&originatorStr, &gw->originator);
454         struct ipaddr_str prefixIpStr;
455         const char * prefix = olsr_ip_to_string(&prefixIpStr, &gw->external_prefix.prefix);
456
457         abuf_json_boolean(abuf, "selected", current_gw && (current_gw == gw));
458         abuf_json_string(abuf, "originator", originator);
459         abuf_json_string(abuf, "prefix", prefix);
460         abuf_json_int(abuf, "prefixLen", gw->external_prefix.prefix_len);
461         abuf_json_int(abuf, "uplink", gw->uplink);
462         abuf_json_int(abuf, "downlink", gw->downlink);
463         abuf_json_int(abuf, "pathcost", !tc ? ROUTE_COST_BROKEN : tc->path_cost);
464         abuf_json_boolean(abuf, "IPv4", gw->ipv4);
465         abuf_json_boolean(abuf, "IPv4-NAT", gw->ipv4nat);
466         abuf_json_boolean(abuf, "IPv6", gw->ipv6);
467         abuf_json_string(abuf, "tunnel", node->name);
468         abuf_json_string(abuf, "destination", originator);
469         abuf_json_int(abuf, "cost", gw->path_cost);
470         abuf_json_int(abuf, "tableNr", node->tableNr);
471         abuf_json_int(abuf, "ruleNr", node->ruleNr);
472         abuf_json_int(abuf, "bypassRuleNr", node->bypassRuleNr);
473       }
474       abuf_json_mark_array_entry(false, abuf);
475     }
476   }
477
478   abuf_json_mark_object(false, true, abuf, NULL);
479 }
480 #endif /* __linux__ */
481
482 void ipc_print_sgw(struct autobuf *abuf) {
483 #ifndef __linux__
484   abuf_json_string(abuf, "error", "Gateway mode is only supported in Linux");
485 #else
486   abuf_json_mark_object(true, false, abuf, "sgw");
487
488   sgw_ipvx(abuf, false);
489   sgw_ipvx(abuf, true);
490
491   abuf_json_mark_object(false, false, abuf, NULL);
492 #endif /* __linux__ */
493 }
494
495 void ipc_print_version(struct autobuf *abuf) {
496   abuf_json_mark_object(true, false, abuf, "version");
497
498   abuf_json_string(abuf, "version", olsrd_version);
499   abuf_json_string(abuf, "date", build_date);
500   abuf_json_string(abuf, "host", build_host);
501
502   abuf_json_mark_object(false, false, abuf, NULL);
503 }
504
505 void ipc_print_olsrd_conf(struct autobuf *abuf) {
506   olsrd_write_cnf_autobuf(abuf, olsr_cnf);
507 }
508
509 void ipc_print_interfaces(struct autobuf *abuf) {
510 #ifdef __linux__
511   int linklen;
512   char path[PATH_MAX], linkpath[PATH_MAX];
513 #endif /* __linux__ */
514   char ipv6_buf[INET6_ADDRSTRLEN]; /* buffer for IPv6 inet_htop */
515   struct olsr_lq_mult *mult;
516   const struct olsr_if *ifs;
517   abuf_json_mark_object(true, true, abuf, "interfaces");
518   for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
519     const struct interface_olsr * const rifs = ifs->interf;
520     abuf_json_mark_array_entry(true, abuf);
521     abuf_json_string(abuf, "name", ifs->name);
522
523     abuf_json_mark_object(true, true, abuf, "linkQualityMultipliers");
524     for (mult = ifs->cnf->lq_mult; mult != NULL ; mult = mult->next) {
525       abuf_json_mark_array_entry(true, abuf);
526       abuf_json_string(abuf, "route", inet_ntop(olsr_cnf->ip_version, &mult->addr, ipv6_buf, sizeof(ipv6_buf)));
527       abuf_json_float(abuf, "multiplier", mult->value / 65535.0);
528       abuf_json_mark_array_entry(false, abuf);
529     }
530     abuf_json_mark_object(false, true, abuf, NULL);
531
532     if (!rifs) {
533       abuf_json_string(abuf, "state", "down");
534     } else {
535       abuf_json_string(abuf, "state", "up");
536       abuf_json_string(abuf, "nameFromKernel", rifs->int_name);
537       abuf_json_int(abuf, "interfaceMode", rifs->mode);
538       abuf_json_boolean(abuf, "emulatedHostClientInterface", rifs->is_hcif);
539       abuf_json_boolean(abuf, "sendTcImmediately", rifs->immediate_send_tc);
540       abuf_json_int(abuf, "fishEyeTtlIndex", rifs->ttl_index);
541       abuf_json_int(abuf, "olsrForwardingTimeout", rifs->fwdtimer);
542       abuf_json_int(abuf, "olsrMessageSequenceNumber", rifs->olsr_seqnum);
543       abuf_json_int(abuf, "olsrInterfaceMetric", rifs->int_metric);
544       abuf_json_int(abuf, "olsrMTU", rifs->int_mtu);
545       abuf_json_int(abuf, "helloEmissionInterval", rifs->hello_etime);
546       abuf_json_int(abuf, "helloValidityTime", me_to_reltime(rifs->valtimes.hello));
547       abuf_json_int(abuf, "tcValidityTime", me_to_reltime(rifs->valtimes.tc));
548       abuf_json_int(abuf, "midValidityTime", me_to_reltime(rifs->valtimes.mid));
549       abuf_json_int(abuf, "hnaValidityTime", me_to_reltime(rifs->valtimes.hna));
550       abuf_json_boolean(abuf, "wireless", rifs->is_wireless);
551
552 #ifdef __linux__
553       abuf_json_boolean(abuf, "icmpRedirect", rifs->nic_state.redirect);
554       abuf_json_boolean(abuf, "spoofFilter", rifs->nic_state.spoof);
555 #endif /* __linux__ */
556
557       if (olsr_cnf->ip_version == AF_INET) {
558         struct ipaddr_str addrbuf, maskbuf, bcastbuf;
559         abuf_json_string(abuf, "ipv4Address", ip4_to_string(&addrbuf, rifs->int_addr.sin_addr));
560         abuf_json_string(abuf, "netmask", ip4_to_string(&maskbuf, rifs->int_netmask.sin_addr));
561         abuf_json_string(abuf, "broadcast", ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
562       } else {
563         struct ipaddr_str addrbuf, maskbuf;
564         abuf_json_string(abuf, "ipv6Address", ip6_to_string(&addrbuf, &rifs->int6_addr.sin6_addr));
565         abuf_json_string(abuf, "multicast", ip6_to_string(&maskbuf, &rifs->int6_multaddr.sin6_addr));
566       }
567     }
568 #ifdef __linux__
569     snprintf(path, PATH_MAX, "/sys/class/net/%s/device/driver/module", ifs->name);
570     linklen = readlink(path, linkpath, PATH_MAX - 1);
571     if (linklen > 1) {
572       linkpath[linklen] = '\0';
573       abuf_json_string(abuf, "kernelModule", basename(linkpath));
574     }
575
576     abuf_json_sys_class_net(abuf, "addressLength", ifs->name, "addr_len");
577     abuf_json_sys_class_net(abuf, "carrier", ifs->name, "carrier");
578     abuf_json_sys_class_net(abuf, "dormant", ifs->name, "dormant");
579     abuf_json_sys_class_net(abuf, "features", ifs->name, "features");
580     abuf_json_sys_class_net(abuf, "flags", ifs->name, "flags");
581     abuf_json_sys_class_net(abuf, "linkMode", ifs->name, "link_mode");
582     abuf_json_sys_class_net(abuf, "macAddress", ifs->name, "address");
583     abuf_json_sys_class_net(abuf, "ethernetMTU", ifs->name, "mtu");
584     abuf_json_sys_class_net(abuf, "operationalState", ifs->name, "operstate");
585     abuf_json_sys_class_net(abuf, "txQueueLength", ifs->name, "tx_queue_len");
586     abuf_json_sys_class_net(abuf, "collisions", ifs->name, "statistics/collisions");
587     abuf_json_sys_class_net(abuf, "multicastPackets", ifs->name, "statistics/multicast");
588     abuf_json_sys_class_net(abuf, "rxBytes", ifs->name, "statistics/rx_bytes");
589     abuf_json_sys_class_net(abuf, "rxCompressed", ifs->name, "statistics/rx_compressed");
590     abuf_json_sys_class_net(abuf, "rxCrcErrors", ifs->name, "statistics/rx_crc_errors");
591     abuf_json_sys_class_net(abuf, "rxDropped", ifs->name, "statistics/rx_dropped");
592     abuf_json_sys_class_net(abuf, "rxErrors", ifs->name, "statistics/rx_errors");
593     abuf_json_sys_class_net(abuf, "rxFifoErrors", ifs->name, "statistics/rx_fifo_errors");
594     abuf_json_sys_class_net(abuf, "rxFrameErrors", ifs->name, "statistics/rx_frame_errors");
595     abuf_json_sys_class_net(abuf, "rxLengthErrors", ifs->name, "statistics/rx_length_errors");
596     abuf_json_sys_class_net(abuf, "rxMissedErrors", ifs->name, "statistics/rx_missed_errors");
597     abuf_json_sys_class_net(abuf, "rxOverErrors", ifs->name, "statistics/rx_over_errors");
598     abuf_json_sys_class_net(abuf, "rxPackets", ifs->name, "statistics/rx_packets");
599     abuf_json_sys_class_net(abuf, "txAbortedErrors", ifs->name, "statistics/tx_aborted_errors");
600     abuf_json_sys_class_net(abuf, "txBytes", ifs->name, "statistics/tx_bytes");
601     abuf_json_sys_class_net(abuf, "txCarrierErrors", ifs->name, "statistics/tx_carrier_errors");
602     abuf_json_sys_class_net(abuf, "txCompressed", ifs->name, "statistics/tx_compressed");
603     abuf_json_sys_class_net(abuf, "txDropped", ifs->name, "statistics/tx_dropped");
604     abuf_json_sys_class_net(abuf, "txErrors", ifs->name, "statistics/tx_errors");
605     abuf_json_sys_class_net(abuf, "txFifoErrors", ifs->name, "statistics/tx_fifo_errors");
606     abuf_json_sys_class_net(abuf, "txHeartbeatErrors", ifs->name, "statistics/tx_heartbeat_errors");
607     abuf_json_sys_class_net(abuf, "txPackets", ifs->name, "statistics/tx_packets");
608     abuf_json_sys_class_net(abuf, "txWindowErrors", ifs->name, "statistics/tx_window_errors");
609     abuf_json_sys_class_net(abuf, "beaconing", ifs->name, "wireless/beacon");
610     abuf_json_sys_class_net(abuf, "encryptionKey", ifs->name, "wireless/crypt");
611     abuf_json_sys_class_net(abuf, "fragmentationThreshold", ifs->name, "wireless/fragment");
612     abuf_json_sys_class_net(abuf, "signalLevel", ifs->name, "wireless/level");
613     abuf_json_sys_class_net(abuf, "linkQuality", ifs->name, "wireless/link");
614     abuf_json_sys_class_net(abuf, "misc", ifs->name, "wireless/misc");
615     abuf_json_sys_class_net(abuf, "noiseLevel", ifs->name, "wireless/noise");
616     abuf_json_sys_class_net(abuf, "nwid", ifs->name, "wireless/nwid");
617     abuf_json_sys_class_net(abuf, "wirelessRetries", ifs->name, "wireless/retries");
618     abuf_json_sys_class_net(abuf, "wirelessStatus", ifs->name, "wireless/status");
619 #endif /* __linux__ */
620     abuf_json_mark_array_entry(false, abuf);
621   }
622   abuf_json_mark_object(false, true, abuf, NULL);
623 }
624
625 void ipc_print_twohop(struct autobuf *abuf) {
626   ipc_print_neighbors_internal(abuf, true);
627 }
628
629 void ipc_print_config(struct autobuf *abuf) {
630   struct ip_prefix_list *hna;
631   struct ipaddr_str buf, mainaddrbuf;
632   struct ip_prefix_list *ipcn;
633   struct olsr_lq_mult *mult;
634   char ipv6_buf[INET6_ADDRSTRLEN]; /* buffer for IPv6 inet_htop */
635
636   abuf_json_mark_object(true, false, abuf, "config");
637
638   abuf_json_int(abuf, "olsrPort", olsr_cnf->olsrport);
639   abuf_json_int(abuf, "debugLevel", olsr_cnf->debug_level);
640   abuf_json_boolean(abuf, "noFork", olsr_cnf->no_fork);
641   abuf_json_boolean(abuf, "hostEmulation", olsr_cnf->host_emul);
642   abuf_json_int(abuf, "ipVersion", olsr_cnf->ip_version);
643   abuf_json_boolean(abuf, "allowNoInterfaces", olsr_cnf->allow_no_interfaces);
644   abuf_json_int(abuf, "typeOfService", olsr_cnf->tos);
645   abuf_json_int(abuf, "rtProto", olsr_cnf->rt_proto);
646   abuf_json_int(abuf, "rtTable", olsr_cnf->rt_table);
647   abuf_json_int(abuf, "rtTableDefault", olsr_cnf->rt_table_default);
648   abuf_json_int(abuf, "rtTableTunnel", olsr_cnf->rt_table_tunnel);
649   abuf_json_int(abuf, "rtTablePriority", olsr_cnf->rt_table_pri);
650   abuf_json_int(abuf, "rtTableTunnelPriority", olsr_cnf->rt_table_tunnel_pri);
651   abuf_json_int(abuf, "rtTableDefauiltOlsrPriority", olsr_cnf->rt_table_defaultolsr_pri);
652   abuf_json_int(abuf, "rtTableDefaultPriority", olsr_cnf->rt_table_default_pri);
653   abuf_json_int(abuf, "willingness", olsr_cnf->willingness);
654   abuf_json_boolean(abuf, "willingnessAuto", olsr_cnf->willingness_auto);
655
656   abuf_json_int(abuf, "brokenLinkCost", LINK_COST_BROKEN);
657   abuf_json_int(abuf, "brokenRouteCost", ROUTE_COST_BROKEN);
658
659   abuf_json_string(abuf, "fibMetrics", FIB_METRIC_TXT[olsr_cnf->fib_metric]);
660
661   abuf_json_string(abuf, "defaultIpv6Multicast", inet_ntop(AF_INET6, &olsr_cnf->interface_defaults->ipv6_multicast.v6, ipv6_buf, sizeof(ipv6_buf)));
662   if (olsr_cnf->interface_defaults->ipv4_multicast.v4.s_addr)
663     abuf_json_string(abuf, "defaultIpv4Broadcast", inet_ntoa(olsr_cnf->interface_defaults->ipv4_multicast.v4));
664   else
665     abuf_json_string(abuf, "defaultIpv4Broadcast", "auto");
666
667   if (olsr_cnf->interface_defaults->mode == IF_MODE_ETHER)
668     abuf_json_string(abuf, "defaultInterfaceMode", "ether");
669   else
670     abuf_json_string(abuf, "defaultInterfaceMode", "mesh");
671
672   abuf_json_float(abuf, "defaultHelloEmissionInterval", olsr_cnf->interface_defaults->hello_params.emission_interval);
673   abuf_json_float(abuf, "defaultHelloValidityTime", olsr_cnf->interface_defaults->hello_params.validity_time);
674   abuf_json_float(abuf, "defaultTcEmissionInterval", olsr_cnf->interface_defaults->tc_params.emission_interval);
675   abuf_json_float(abuf, "defaultTcValidityTime", olsr_cnf->interface_defaults->tc_params.validity_time);
676   abuf_json_float(abuf, "defaultMidEmissionInterval", olsr_cnf->interface_defaults->mid_params.emission_interval);
677   abuf_json_float(abuf, "defaultMidValidityTime", olsr_cnf->interface_defaults->mid_params.validity_time);
678   abuf_json_float(abuf, "defaultHnaEmissionInterval", olsr_cnf->interface_defaults->hna_params.emission_interval);
679   abuf_json_float(abuf, "defaultHnaValidityTime", olsr_cnf->interface_defaults->hna_params.validity_time);
680   abuf_json_boolean(abuf, "defaultAutoDetectChanges", olsr_cnf->interface_defaults->autodetect_chg);
681
682   abuf_json_mark_object(true, true, abuf, "defaultLinkQualityMultipliers");
683   for (mult = olsr_cnf->interface_defaults->lq_mult; mult != NULL ; mult = mult->next) {
684     abuf_json_mark_array_entry(true, abuf);
685     abuf_json_string(abuf, "route", inet_ntop(olsr_cnf->ip_version, &mult->addr, ipv6_buf, sizeof(ipv6_buf)));
686     abuf_json_float(abuf, "multiplier", mult->value / 65535.0);
687     abuf_json_mark_array_entry(false, abuf);
688   }
689   abuf_json_mark_object(false, true, abuf, NULL);
690
691   abuf_json_mark_object(true, true, abuf, "hna");
692   for (hna = olsr_cnf->hna_entries; hna != NULL ; hna = hna->next) {
693     abuf_json_mark_array_entry(true, abuf);
694     abuf_json_string(abuf, "destination", olsr_ip_to_string(&buf, &hna->net.prefix));
695     abuf_json_int(abuf, "genmask", hna->net.prefix_len);
696     abuf_json_string(abuf, "gateway", olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
697     abuf_json_mark_array_entry(false, abuf);
698   }
699   abuf_json_mark_object(false, true, abuf, NULL);
700
701   abuf_json_int(abuf, "totalIpcConnectionsAllowed", olsr_cnf->ipc_connections);
702   abuf_json_mark_object(true, true, abuf, "ipcAllowedAddresses");
703   if (olsr_cnf->ipc_connections) {
704     for (ipcn = olsr_cnf->ipc_nets; ipcn != NULL ; ipcn = ipcn->next) {
705       abuf_json_mark_array_entry(true, abuf);
706       abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&mainaddrbuf, &ipcn->net.prefix));
707       abuf_json_int(abuf, "netmask", ipcn->net.prefix_len);
708       abuf_json_mark_array_entry(false, abuf);
709     }
710   }
711   abuf_json_mark_object(false, true, abuf, NULL);
712
713   // keep all time in ms, so convert these two, which are in seconds
714   abuf_json_int(abuf, "pollRate", olsr_cnf->pollrate * 1000);
715   abuf_json_int(abuf, "nicChangePollInterval", olsr_cnf->nic_chgs_pollrate * 1000);
716   abuf_json_boolean(abuf, "clearScreen", olsr_cnf->clear_screen);
717   abuf_json_int(abuf, "tcRedundancy", olsr_cnf->tc_redundancy);
718   abuf_json_int(abuf, "mprCoverage", olsr_cnf->mpr_coverage);
719
720   if (!olsr_cnf->lq_level) {
721     abuf_json_boolean(abuf, "useHysteresis", olsr_cnf->use_hysteresis);
722     if (olsr_cnf->use_hysteresis) {
723       abuf_json_float(abuf, "hysteresisScaling", olsr_cnf->hysteresis_param.scaling);
724       abuf_json_float(abuf, "hysteresisLowThreshold", olsr_cnf->hysteresis_param.thr_low);
725       abuf_json_float(abuf, "hysteresisHighThreshold", olsr_cnf->hysteresis_param.thr_high);
726     }
727   }
728   abuf_json_int(abuf, "linkQualityLevel", olsr_cnf->lq_level);
729   abuf_json_float(abuf, "linkQualityAging", olsr_cnf->lq_aging);
730   abuf_json_boolean(abuf, "linkQualityFisheye", olsr_cnf->lq_fish);
731   abuf_json_string(abuf, "linkQualityAlgorithm", olsr_cnf->lq_algorithm);
732   // keep all time in ms, so convert this from seconds
733   abuf_json_int(abuf, "minTcValidTime", olsr_cnf->min_tc_vtime * 1000);
734   abuf_json_boolean(abuf, "setIpForward", olsr_cnf->set_ip_forward);
735   abuf_json_string(abuf, "lockFile", olsr_cnf->lock_file);
736   abuf_json_boolean(abuf, "useNiit", olsr_cnf->use_niit);
737
738 #ifdef __linux__
739   abuf_json_boolean(abuf, "smartGateway", olsr_cnf->smart_gw_active);
740   if (olsr_cnf->smart_gw_active) {
741     abuf_json_boolean(abuf, "smartGatewayAlwaysRemoveServerTunnel", olsr_cnf->smart_gw_always_remove_server_tunnel);
742     abuf_json_int(abuf, "smartGatewayUseCount", olsr_cnf->smart_gw_use_count);
743     abuf_json_string(abuf, "smartGatewayInstanceId", olsr_cnf->smart_gw_instance_id);
744     abuf_json_string(abuf, "smartGatewayPolicyRoutingScript", olsr_cnf->smart_gw_policyrouting_script);
745     {
746       struct autobuf egressbuf;
747       struct sgw_egress_if * egressif = olsr_cnf->smart_gw_egress_interfaces;
748
749       abuf_init(&egressbuf, (olsr_cnf->smart_gw_egress_interfaces_count * IFNAMSIZ) /* interface names */
750       + (olsr_cnf->smart_gw_egress_interfaces_count - 1) /* commas */);
751       while (egressif) {
752         if (egressbuf.len) {
753           abuf_puts(&egressbuf, ",");
754         }
755         abuf_appendf(&egressbuf, "%s", egressif->name);
756         egressif = egressif->next;
757       }
758       abuf_json_string(abuf, "smartGatewayEgressInterfaces", egressbuf.buf);
759       abuf_free(&egressbuf);
760     }
761     abuf_json_int(abuf, "smartGatewayTablesOffset", olsr_cnf->smart_gw_offset_tables);
762     abuf_json_int(abuf, "smartGatewayRulesOffset", olsr_cnf->smart_gw_offset_rules);
763     abuf_json_boolean(abuf, "smartGatewayAllowNat", olsr_cnf->smart_gw_allow_nat);
764     abuf_json_boolean(abuf, "smartGatewayUplinkNat", olsr_cnf->smart_gw_uplink_nat);
765     abuf_json_int(abuf, "smartGatewayPeriod", olsr_cnf->smart_gw_period);
766     abuf_json_int(abuf, "smartGatewayStableCount", olsr_cnf->smart_gw_stablecount);
767     abuf_json_int(abuf, "smartGatewayThreshold", olsr_cnf->smart_gw_thresh);
768     abuf_json_int(abuf, "smartGatewayUplink", olsr_cnf->smart_gw_uplink);
769     abuf_json_int(abuf, "smartGatewayDownlink", olsr_cnf->smart_gw_downlink);
770     abuf_json_int(abuf, "smartGatewayType", olsr_cnf->smart_gw_type);
771     abuf_json_string(abuf, "smartGatewayPrefix", olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->smart_gw_prefix.prefix));
772     abuf_json_int(abuf, "smartGatewayPrefixLength", olsr_cnf->smart_gw_prefix.prefix_len);
773   }
774 #endif /* __linux__ */
775
776   abuf_json_string(abuf, "mainIpAddress", olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
777   abuf_json_string(abuf, "unicastSourceIpAddress", olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->unicast_src_ip));
778
779   abuf_json_boolean(abuf, "useSourceIpRoutes", olsr_cnf->use_src_ip_routes);
780
781   abuf_json_int(abuf, "maxPrefixLength", olsr_cnf->maxplen);
782   abuf_json_int(abuf, "ipSize", olsr_cnf->ipsize);
783   abuf_json_boolean(abuf, "deleteInternetGatewaysAtStartup", olsr_cnf->del_gws);
784   // keep all time in ms, so convert this from seconds
785   abuf_json_int(abuf, "willingnessUpdateInterval", olsr_cnf->will_int * 1000);
786   abuf_json_float(abuf, "maxSendMessageJitter", olsr_cnf->max_jitter);
787   abuf_json_int(abuf, "exitValue", olsr_cnf->exit_value);
788   // keep all time in ms, so convert this from seconds
789   abuf_json_int(abuf, "maxTcValidTime", olsr_cnf->max_tc_vtime * 1000);
790
791   abuf_json_int(abuf, "niit4to6InterfaceIndex", olsr_cnf->niit4to6_if_index);
792   abuf_json_int(abuf, "niit6to4InterfaceIndex", olsr_cnf->niit6to4_if_index);
793
794   abuf_json_boolean(abuf, "hasIpv4Gateway", olsr_cnf->has_ipv4_gateway);
795   abuf_json_boolean(abuf, "hasIpv6Gateway", olsr_cnf->has_ipv6_gateway);
796
797   abuf_json_int(abuf, "ioctlSocket", olsr_cnf->ioctl_s);
798 #ifdef __linux__
799   abuf_json_int(abuf, "routeNetlinkSocket", olsr_cnf->rtnl_s);
800   abuf_json_int(abuf, "routeMonitorSocket", olsr_cnf->rt_monitor_socket);
801 #endif /* __linux__ */
802
803 #if defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__
804   abuf_json_int(abuf, "routeChangeSocket", olsr_cnf->rts);
805 #endif /* defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__ */
806   abuf_json_float(abuf, "linkQualityNatThreshold", olsr_cnf->lq_nat_thresh);
807
808   abuf_json_string(abuf, "olsrdVersion", olsrd_version);
809   abuf_json_string(abuf, "olsrdBuildDate", build_date);
810   abuf_json_string(abuf, "olsrdBuildHost", build_host);
811
812 #if defined _WIN32 || defined _WIN64
813   abuf_json_string(abuf, "os", "Windows");
814 #elif defined __gnu_linux__
815   abuf_json_string(abuf, "os", "GNU/Linux");
816 #elif defined __ANDROID__
817   abuf_json_string(abuf, "os", "Android");
818 #elif defined __APPLE__
819   abuf_json_string(abuf, "os", "Mac OS X");
820 #elif defined __NetBSD__
821   abuf_json_string(abuf, "os", "NetBSD");
822 #elif defined __OpenBSD__
823   abuf_json_string(abuf, "os", "OpenBSD");
824 #elif defined __FreeBSD__ || defined __FreeBSD_kernel__
825   abuf_json_string(abuf, "os", "FreeBSD");
826 #else /* OS detection */
827   abuf_json_string(abuf, "os", "Undefined");
828 #endif /* OS detection */
829
830   abuf_json_int(abuf, "startTime", start_time.tv_sec);
831
832   abuf_json_mark_object(false, false, abuf, NULL);
833 }
834
835 void ipc_print_plugins(struct autobuf *abuf) {
836   struct plugin_entry *pentry;
837   struct plugin_param *pparam;
838   abuf_json_mark_object(true, true, abuf, "plugins");
839   if (olsr_cnf->plugins)
840     for (pentry = olsr_cnf->plugins; pentry; pentry = pentry->next) {
841       abuf_json_mark_array_entry(true, abuf);
842       abuf_json_string(abuf, "plugin", pentry->name);
843       for (pparam = pentry->params; pparam; pparam = pparam->next) {
844         int i, keylen = strlen(pparam->key);
845         char key[keylen + 1];
846         long value;
847         char valueTest[256];
848         strcpy(key, pparam->key);
849         for (i = 0; i < keylen; i++)
850           key[i] = tolower(key[i]);
851
852         // test if a int/long and set as such in JSON
853         value = atol(pparam->value);
854         snprintf(valueTest, 255, "%li", value);
855         if (!strcmp(valueTest, pparam->value))
856           abuf_json_int(abuf, key, value);
857         else
858           abuf_json_string(abuf, key, pparam->value);
859       }
860       abuf_json_mark_array_entry(false, abuf);
861     }
862   abuf_json_mark_object(false, true, abuf, NULL);
863 }