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