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