[Olsr-dev] [PATCH v1 021/115] txtinfo: move ipc_print_* functions into a 'printers' unit
Ferry Huberts
(spam-protected)
Wed Dec 2 21:27:54 CET 2015
From: Ferry Huberts <(spam-protected)>
Signed-off-by: Ferry Huberts <(spam-protected)>
---
lib/txtinfo/src/olsrd_txtinfo.c | 415 +-----------------
.../src/{olsrd_txtinfo.c => txtinfo_printers.c} | 483 +--------------------
.../src/txtinfo_printers.h} | 25 +-
3 files changed, 32 insertions(+), 891 deletions(-)
copy lib/txtinfo/src/{olsrd_txtinfo.c => txtinfo_printers.c} (54%)
copy lib/{info/http_headers.h => txtinfo/src/txtinfo_printers.h} (75%)
diff --git a/lib/txtinfo/src/olsrd_txtinfo.c b/lib/txtinfo/src/olsrd_txtinfo.c
index 68ff289..1c4b76a 100644
--- a/lib/txtinfo/src/olsrd_txtinfo.c
+++ b/lib/txtinfo/src/olsrd_txtinfo.c
@@ -43,42 +43,17 @@
* Dynamic linked library for the olsr.org olsr daemon
*/
-#include <sys/types.h>
-#include <sys/socket.h>
-#ifndef _WIN32
-#include <sys/select.h>
-#endif /* _WIN32 */
-#include <netinet/in.h>
#include <arpa/inet.h>
-#include <sys/time.h>
-#include <time.h>
-#include <math.h>
-#include <stdio.h>
#include <string.h>
-#include <stdlib.h>
#include <unistd.h>
-#include <errno.h>
-#include <assert.h>
#include "ipcalc.h"
#include "olsr.h"
-#include "builddata.h"
-#include "olsr_types.h"
-#include "neighbor_table.h"
-#include "two_hop_neighbor_table.h"
-#include "mpr_selector_set.h"
-#include "tc_set.h"
-#include "hna_set.h"
-#include "mid_set.h"
-#include "link_set.h"
-#include "net_olsr.h"
-#include "lq_plugin.h"
-#include "common/autobuf.h"
-#include "gateway.h"
+#include "scheduler.h"
#include "../../info/http_headers.h"
+#include "txtinfo_printers.h"
#include "olsrd_txtinfo.h"
-#include "olsrd_plugin.h"
#ifdef _WIN32
#define close(x) closesocket(x)
@@ -377,392 +352,6 @@ static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int
send_info(send_what, ipc_connection);
}
-static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
- struct ipaddr_str buf1;
- struct neighbor_entry *neigh;
- struct neighbor_2_list_entry *list_2;
- int thop_cnt;
-
- abuf_puts(abuf, "Table: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWill.");
- if (list_2hop)
- abuf_puts(abuf, "\n\t2hop interface adrress\n");
- else
- abuf_puts(abuf, "\t2 Hop Neighbors\n");
-
- /* Neighbors */
- OLSR_FOR_ALL_NBR_ENTRIES(neigh)
- {
- abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d\t", olsr_ip_to_string(&buf1, &neigh->neighbor_main_addr), (neigh->status == SYM) ? "YES" : "NO",
- neigh->is_mpr ? "YES" : "NO", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO", neigh->willingness);
- thop_cnt = 0;
-
- for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
- if (list_2hop)
- abuf_appendf(abuf, "\t%s\n", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
- else
- thop_cnt++;
- }
- if (!list_2hop) {
- abuf_appendf(abuf, "%d\n", thop_cnt);
- }
- }OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
- abuf_puts(abuf, "\n");
-}
-
-static void ipc_print_links(struct autobuf *abuf) {
- struct ipaddr_str buf1, buf2;
- struct lqtextbuffer lqbuffer1, lqbuffer2;
-
- struct link_entry *my_link = NULL;
-
-#ifdef ACTIVATE_VTIME_TXTINFO
- abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tVTime\tLQ\tNLQ\tCost\n");
-#else /* ACTIVATE_VTIME_TXTINFO */
- abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tHyst.\tLQ\tNLQ\tCost\n");
-#endif /* ACTIVATE_VTIME_TXTINFO */
-
- /* Link set */
- OLSR_FOR_ALL_LINK_ENTRIES(my_link)
- {
-#ifdef ACTIVATE_VTIME_TXTINFO
- int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
-
- abuf_appendf(abuf, "%s\t%s\t%d.%03d\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &my_link->local_iface_addr),
- olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr),
- diff/1000, abs(diff%1000),
- get_link_entry_text(my_link, '\t', &lqbuffer1),
- get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
-#else /* ACTIVATE_VTIME_TXTINFO */
- abuf_appendf(abuf, "%s\t%s\t0.00\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &my_link->local_iface_addr),
- olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr), get_link_entry_text(my_link, '\t', &lqbuffer1),
- get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
-#endif /* ACTIVATE_VTIME_TXTINFO */
- }OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
-
- abuf_puts(abuf, "\n");
-}
-
-static void ipc_print_routes(struct autobuf *abuf) {
- struct ipaddr_str buf1, buf2;
- struct rt_entry *rt;
- struct lqtextbuffer lqbuffer;
-
- abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
-
- /* Walk the route table */
- OLSR_FOR_ALL_RT_ENTRIES(rt)
- {
- abuf_appendf(abuf, "%s/%d\t%s\t%d\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &rt->rt_dst.prefix), rt->rt_dst.prefix_len,
- olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway), rt->rt_best->rtp_metric.hops,
- get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer), if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
- }OLSR_FOR_ALL_RT_ENTRIES_END(rt);
-
- abuf_puts(abuf, "\n");
-
-}
-
-static void ipc_print_topology(struct autobuf *abuf) {
- struct tc_entry *tc;
-
-#ifdef ACTIVATE_VTIME_TXTINFO
- abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
-#else /* ACTIVATE_VTIME_TXTINFO */
- abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\n");
-#endif /* ACTIVATE_VTIME_TXTINFO */
-
- /* Topology */
- OLSR_FOR_ALL_TC_ENTRIES(tc)
- {
- struct tc_edge_entry *tc_edge;
- OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge)
- {
- if (tc_edge->edge_inv) {
- struct ipaddr_str dstbuf, addrbuf;
- struct lqtextbuffer lqbuffer1, lqbuffer2;
-#ifdef ACTIVATE_VTIME_TXTINFO
- uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
- int diff = (int)(vt);
- abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d.%03d\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr),
- olsr_ip_to_string(&addrbuf, &tc->addr),
- get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1),
- get_linkcost_text(tc_edge->cost, false, &lqbuffer2),
- diff/1000, diff%1000);
-#else /* ACTIVATE_VTIME_TXTINFO */
- abuf_appendf(abuf, "%s\t%s\t%s\t%s\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr), olsr_ip_to_string(&addrbuf, &tc->addr),
- get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1), get_linkcost_text(tc_edge->cost, false, &lqbuffer2));
-#endif /* ACTIVATE_VTIME_TXTINFO */
- }
- }OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
- }OLSR_FOR_ALL_TC_ENTRIES_END(tc);
-
- abuf_puts(abuf, "\n");
-}
-
-static void ipc_print_hna(struct autobuf *abuf) {
- struct ip_prefix_list *hna;
- struct hna_entry *tmp_hna;
- struct hna_net *tmp_net;
- struct ipaddr_str buf, mainaddrbuf;
-
-#ifdef ACTIVATE_VTIME_TXTINFO
- abuf_puts(abuf, "Table: HNA\nDestination\tGateway\tVTime\n");
-#else /* ACTIVATE_VTIME_TXTINFO */
- abuf_puts(abuf, "Table: HNA\nDestination\tGateway\n");
-#endif /* ACTIVATE_VTIME_TXTINFO */
-
- /* Announced HNA entries */
- for (hna = olsr_cnf->hna_entries; hna != NULL ; hna = hna->next) {
- abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &hna->net.prefix), hna->net.prefix_len, olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
- }
-
- /* HNA entries */
- OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna)
- {
-
- /* Check all networks */
- for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
-#ifdef ACTIVATE_VTIME_TXTINFO
- uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
- int diff = (int)(vt);
- abuf_appendf(abuf, "%s/%d\t%s\t\%d.%03d\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix),
- tmp_net->hna_prefix.prefix_len, olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr),
- diff/1000, abs(diff%1000));
-#else /* ACTIVATE_VTIME_TXTINFO */
- abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix), tmp_net->hna_prefix.prefix_len,
- olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
-#endif /* ACTIVATE_VTIME_TXTINFO */
- }
- }OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
-
- abuf_puts(abuf, "\n");
-}
-
-static void ipc_print_mid(struct autobuf *abuf) {
- int idx;
- unsigned short is_first;
- struct mid_entry *entry;
- struct mid_address *alias;
-#ifdef ACTIVATE_VTIME_TXTINFO
- abuf_puts(abuf, "Table: MID\nIP address\tAlias\tVTime\n");
-#else /* ACTIVATE_VTIME_TXTINFO */
- abuf_puts(abuf, "Table: MID\nIP address\tAliases\n");
-#endif /* ACTIVATE_VTIME_TXTINFO */
-
- /* MID */
- for (idx = 0; idx < HASHSIZE; idx++) {
- entry = mid_set[idx].next;
-
- while (entry != &mid_set[idx]) {
-#ifdef ACTIVATE_VTIME_TXTINFO
- struct ipaddr_str buf, buf2;
-#else /* ACTIVATE_VTIME_TXTINFO */
- struct ipaddr_str buf;
- abuf_puts(abuf, olsr_ip_to_string(&buf, &entry->main_addr));
-#endif /* ACTIVATE_VTIME_TXTINFO */
- alias = entry->aliases;
- is_first = 1;
-
- while (alias) {
-#ifdef ACTIVATE_VTIME_TXTINFO
- uint32_t vt = alias->vtime - now_times;
- int diff = (int)(vt);
-
- abuf_appendf(abuf, "%s\t%s\t%d.%03d\n",
- olsr_ip_to_string(&buf, &entry->main_addr),
- olsr_ip_to_string(&buf2, &alias->alias),
- diff/1000, abs(diff%1000));
-#else /* ACTIVATE_VTIME_TXTINFO */
- abuf_appendf(abuf, "%s%s", (is_first ? "\t" : ";"), olsr_ip_to_string(&buf, &alias->alias));
-#endif /* ACTIVATE_VTIME_TXTINFO */
- alias = alias->next_alias;
- is_first = 0;
- }
- entry = entry->next;
-#ifndef ACTIVATE_VTIME_TXTINFO
- abuf_puts(abuf, "\n");
-#endif /* ACTIVATE_VTIME_TXTINFO */
- }
- }
- abuf_puts(abuf, "\n");
-}
-
-static void ipc_print_gateways(struct autobuf *abuf) {
-#ifndef __linux__
- abuf_puts(abuf, "Gateway mode is only supported in linux\n");
-#else /* __linux__ */
- static const char IPV4[] = "ipv4";
- static const char IPV4_NAT[] = "ipv4(n)";
- static const char IPV6[] = "ipv6";
- static const char NONE[] = "-";
-
- struct ipaddr_str buf;
- struct gateway_entry *gw;
- struct lqtextbuffer lqbuf;
-
- // Status IP ETX Hopcount Uplink-Speed Downlink-Speed ipv4/ipv4-nat/- ipv6/- ipv6-prefix/-
- abuf_puts(abuf, "Table: Gateways\nStatus\tGateway IP\tETX\tHopcnt\tUplink\tDownlnk\tIPv4\tIPv6\tPrefix\n");
- OLSR_FOR_ALL_GATEWAY_ENTRIES(gw)
- {
- char v4 = '-', v6 = '-';
- const char *v4type = NONE, *v6type = NONE;
- struct tc_entry *tc;
-
- if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
- continue;
- }
-
- if (gw == olsr_get_inet_gateway(false)) {
- v4 = 's';
- } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit) && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
- v4 = 'u';
- }
-
- if (gw == olsr_get_inet_gateway(true)) {
- v6 = 's';
- } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
- v6 = 'u';
- }
-
- if (gw->ipv4) {
- v4type = gw->ipv4nat ? IPV4_NAT : IPV4;
- }
- if (gw->ipv6) {
- v6type = IPV6;
- }
-
- abuf_appendf(abuf, "%c%c\t%s\t%s\t%d\t%u\t%u\t%s\t%s\t%s\n", v4, v6, olsr_ip_to_string(&buf, &gw->originator),
- get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops, gw->uplink, gw->downlink, v4type, v6type,
- gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
- }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
-#endif /* __linux__ */
-}
-
-#ifdef __linux__
-
-/** interface names for smart gateway tunnel interfaces, IPv4 */
-extern struct interfaceName * sgwTunnel4InterfaceNames;
-
-/** interface names for smart gateway tunnel interfaces, IPv6 */
-extern struct interfaceName * sgwTunnel6InterfaceNames;
-
-/**
- * Construct the sgw table for a given ip version
- *
- * @param abuf the string buffer
- * @param ipv6 true for IPv6, false for IPv4
- * @param fmtv the format for printing
- */
-static void sgw_ipvx(struct autobuf *abuf, bool ipv6, const char * fmth, const char * fmtv) {
- struct interfaceName * sgwTunnelInterfaceNames;
-
- abuf_appendf(abuf, "# Table: Smart Gateway IPv%s\n", ipv6 ? "6" : "4");
- abuf_appendf(abuf, fmth, "#", "Originator", "Prefix", "Uplink", "Downlink", "PathCost", "IPv4", "IPv4-NAT", "IPv6", "Tunnel-Name", "Destination", "Cost");
-
- sgwTunnelInterfaceNames = !ipv6 ? sgwTunnel4InterfaceNames : sgwTunnel6InterfaceNames;
- if (olsr_cnf->smart_gw_active && sgwTunnelInterfaceNames) {
- struct gateway_entry * current_gw = olsr_get_inet_gateway(ipv6);
- int i;
- for (i = 0; i < olsr_cnf->smart_gw_use_count; i++) {
- struct interfaceName * node = &sgwTunnelInterfaceNames[i];
- struct gateway_entry * gw = node->gw;
-
- if (!gw) {
- continue;
- }
-
- {
- struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
-
- struct ipaddr_str originatorStr;
- const char * originator = olsr_ip_to_string(&originatorStr, &gw->originator);
- struct ipaddr_str prefixIpStr;
- const char * prefix = olsr_ip_to_string(&prefixIpStr, &gw->external_prefix.prefix);
- union olsr_ip_addr netmask = { { 0 } };
- struct ipaddr_str prefixMaskStr;
- const char * prefixMASKStr;
- char prefixAndMask[strlen(prefix) + 1 + INET_ADDRSTRLEN + 1];
-
- if (!ipv6) {
- prefix_to_netmask((uint8_t *) &netmask, sizeof(netmask.v4), gw->external_prefix.prefix_len);
- prefixMASKStr = olsr_ip_to_string(&prefixMaskStr, &netmask);
- snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%s", prefix, prefixMASKStr);
- } else {
- snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%d", prefix, gw->external_prefix.prefix_len);
- }
-
- abuf_appendf(abuf, fmtv, //
- (current_gw && (current_gw == gw)) ? "*" : " ", // selected
- originator, // Originator
- prefixAndMask, // 4: Prefix IP / Prefix Mask, 6: Prefix IP / Prefix Length
- gw->uplink, // Uplink
- gw->downlink, // Downlink
- !tc ? ROUTE_COST_BROKEN : tc->path_cost, // PathCost
- gw->ipv4 ? "Y" : "N", // IPv4
- gw->ipv4nat ? "Y" : "N", // IPv4-NAT
- gw->ipv6 ? "Y" : "N", // IPv6
- node->name, // Tunnel-Name
- originator, // Destination
- gw->path_cost // Cost
- );
- }
- }
- }
-}
-#endif /* __linux__ */
-
-static void ipc_print_sgw(struct autobuf *abuf) {
-#ifndef __linux__
- abuf_puts(abuf, "Gateway mode is only supported in linux\n");
-#else
-
- static const char * fmth4 = "%s%-15s %-31s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-15s %s\n";
- static const char * fmtv4 = "%s%-15s %-31s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-15s %lld\n";
-#if 0
- static const char * fmth6 = "%s%-45s %-49s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-45s %s\n";
- static const char * fmtv6 = "%s%-45s %-49s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-45s %lld\n";
-#endif
-
- sgw_ipvx(abuf, false, fmth4, fmtv4);
- abuf_puts(abuf, "\n");
-#if 0
- sgw_ipvx(abuf, true, fmth6, fmtv6);
- abuf_puts(abuf, "\n");
-#endif
-#endif /* __linux__ */
-}
-
-static void ipc_print_version(struct autobuf *abuf) {
- abuf_appendf(abuf, "Version: %s (built on %s on %s)\n", olsrd_version, build_date, build_host);
-}
-
-static void ipc_print_olsrd_conf(struct autobuf *abuf) {
- olsrd_write_cnf_autobuf(abuf, olsr_cnf);
-}
-
-static void ipc_print_interfaces(struct autobuf *abuf) {
- const struct olsr_if *ifs;
- abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
- for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
- const struct interface_olsr * const rifs = ifs->interf;
- abuf_appendf(abuf, "%s\t", ifs->name);
- if (!rifs) {
- abuf_puts(abuf, "DOWN\n");
- continue;
- }
- abuf_appendf(abuf, "UP\t%d\t%s\t", rifs->int_mtu, rifs->is_wireless ? "Yes" : "No");
-
- if (olsr_cnf->ip_version == AF_INET) {
- struct ipaddr_str addrbuf, maskbuf, bcastbuf;
- abuf_appendf(abuf, "%s\t%s\t%s\n", ip4_to_string(&addrbuf, rifs->int_addr.sin_addr), ip4_to_string(&maskbuf, rifs->int_netmask.sin_addr),
- ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
- } else {
- struct ipaddr_str addrbuf, maskbuf;
- abuf_appendf(abuf, "%s\t\t%s\n", ip6_to_string(&addrbuf, &rifs->int6_addr.sin6_addr), ip6_to_string(&maskbuf, &rifs->int6_multaddr.sin6_addr));
- }
- }
- abuf_puts(abuf, "\n");
-}
-
static void info_write_data(void *foo __attribute__ ((unused))) {
fd_set set;
int result, i, j, max;
diff --git a/lib/txtinfo/src/olsrd_txtinfo.c b/lib/txtinfo/src/txtinfo_printers.c
similarity index 54%
copy from lib/txtinfo/src/olsrd_txtinfo.c
copy to lib/txtinfo/src/txtinfo_printers.c
index 68ff289..503f8a7 100644
--- a/lib/txtinfo/src/olsrd_txtinfo.c
+++ b/lib/txtinfo/src/txtinfo_printers.c
@@ -39,345 +39,20 @@
*
*/
-/*
- * Dynamic linked library for the olsr.org olsr daemon
- */
+#include "txtinfo_printers.h"
-#include <sys/types.h>
-#include <sys/socket.h>
-#ifndef _WIN32
-#include <sys/select.h>
-#endif /* _WIN32 */
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <sys/time.h>
-#include <time.h>
-#include <math.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
#include <unistd.h>
-#include <errno.h>
-#include <assert.h>
#include "ipcalc.h"
-#include "olsr.h"
#include "builddata.h"
-#include "olsr_types.h"
#include "neighbor_table.h"
-#include "two_hop_neighbor_table.h"
#include "mpr_selector_set.h"
-#include "tc_set.h"
-#include "hna_set.h"
#include "mid_set.h"
-#include "link_set.h"
-#include "net_olsr.h"
+#include "routing_table.h"
#include "lq_plugin.h"
-#include "common/autobuf.h"
#include "gateway.h"
-#include "../../info/http_headers.h"
-
-#include "olsrd_txtinfo.h"
-#include "olsrd_plugin.h"
-
-#ifdef _WIN32
-#define close(x) closesocket(x)
-#endif /* _WIN32 */
-
-/* defines to make txtinfo and jsoninfo look alike */
-#define PLUGIN_NAME "TXTINFO"
-#define info_accept_ip txtinfo_accept_ip
-#define info_listen_ip txtinfo_listen_ip
-#define info_ipv6_only txtinfo_ipv6_only
-#ifdef TXTINFO_ALLOW_LOCALHOST
-#define INFO_ALLOW_LOCALHOST TXTINFO_ALLOW_LOCALHOST
-#endif
-
-static int ipc_socket;
-
-/* IPC initialization function */
-static int plugin_ipc_init(void);
-
-static void send_info(unsigned int /*send_what*/, int /*socket*/);
-
-static void ipc_action(int, void *, unsigned int);
-
-#define TXT_IPC_BUFSIZE 256
-
-/* these provide all of the runtime status info */
-#define SIW_NEIGHBORS 0x0001
-#define SIW_LINKS 0x0002
-#define SIW_ROUTES 0x0004
-#define SIW_HNA 0x0008
-#define SIW_MID 0x0010
-#define SIW_TOPOLOGY 0x0020
-#define SIW_GATEWAYS 0x0040
-#define SIW_INTERFACES 0x0080
-#define SIW_2HOP 0x0100
-#define SIW_SGW 0x0200
-#define SIW_RUNTIME_ALL (SIW_NEIGHBORS | SIW_LINKS | SIW_ROUTES | SIW_HNA | SIW_MID | SIW_TOPOLOGY | SIW_GATEWAYS | SIW_INTERFACES | SIW_2HOP | SIW_SGW)
-
-/* these only change at olsrd startup */
-#define SIW_VERSION 0x0400
-#define SIW_CONFIG 0x0800
-#define SIW_PLUGINS 0x1000
-#define SIW_STARTUP_ALL (SIW_VERSION | SIW_CONFIG | SIW_PLUGINS)
-
-/* this is everything in normal format */
-#define SIW_ALL (SIW_RUNTIME_ALL | SIW_STARTUP_ALL)
-
-/* this data is not normal format but olsrd.conf format */
-#define SIW_OLSRD_CONF 0x2000
-
-#define MAX_CLIENTS 3
-
-static char *outbuffer[MAX_CLIENTS];
-static size_t outbuffer_size[MAX_CLIENTS];
-static size_t outbuffer_written[MAX_CLIENTS];
-static int outbuffer_socket[MAX_CLIENTS];
-static int outbuffer_count = 0;
-
-static struct timer_entry *writetimer_entry;
-
-static void determine_action(unsigned int *send_what, char *requ) {
- if (strstr(requ, "/con"))
- *send_what |= SIW_OLSRD_CONF;
- else if (strstr(requ, "/all"))
- *send_what = SIW_ALL;
- else {
- // these are the two overarching categories
- if (strstr(requ, "/runtime"))
- *send_what |= SIW_RUNTIME_ALL;
- if (strstr(requ, "/startup"))
- *send_what |= SIW_STARTUP_ALL;
-
- // these are the individual sections
- if (strstr(requ, "/nei"))
- *send_what |= SIW_NEIGHBORS;
- if (strstr(requ, "/lin"))
- *send_what |= SIW_LINKS;
- if (strstr(requ, "/rou"))
- *send_what |= SIW_ROUTES;
- if (strstr(requ, "/hna"))
- *send_what |= SIW_HNA;
- if (strstr(requ, "/mid"))
- *send_what |= SIW_MID;
- if (strstr(requ, "/top"))
- *send_what |= SIW_TOPOLOGY;
- if (strstr(requ, "/gat"))
- *send_what |= SIW_GATEWAYS;
- if (strstr(requ, "/int"))
- *send_what |= SIW_INTERFACES;
- if (strstr(requ, "/2ho"))
- *send_what |= SIW_2HOP;
- if (strstr(requ, "/sgw"))
- *send_what |= SIW_SGW;
-
- // specials
- if (strstr(requ, "/ver"))
- *send_what |= SIW_VERSION;
- if (strstr(requ, "/config"))
- *send_what |= SIW_CONFIG;
- if (strstr(requ, "/plugins"))
- *send_what |= SIW_PLUGINS;
-
- /* To print out neighbours only on the Freifunk Status
- * page the normal output is somewhat lengthy. The
- * header parsing is sufficient for standard wget.
- */
- if (strstr(requ, "/neighbours"))
- *send_what = SIW_NEIGHBORS | SIW_LINKS;
- }
-}
-
-static void plugin_init(void) {
- /* nothing to do */
-}
-
-/**
- *Do initialization here
- *
- *This function is called by the my_init
- *function in uolsrd_plugin.c
- */
-int olsrd_plugin_init(void) {
- /* Initial IPC value */
- ipc_socket = -1;
-
- plugin_init();
-
- plugin_ipc_init();
- return 1;
-}
-
-/**
- * destructor - called at unload
- */
-void olsr_plugin_exit(void) {
- if (ipc_socket != -1)
- close(ipc_socket);
-}
-
-static int plugin_ipc_init(void) {
- union olsr_sockaddr sst;
- uint32_t yes = 1;
- socklen_t addrlen;
-
- /* Init ipc socket */
- if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
-#ifndef NODEBUG
- olsr_printf(1, "("PLUGIN_NAME") socket()=%s\n", strerror(errno));
-#endif /* NODEBUG */
- return 0;
- } else {
- if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
-#ifndef NODEBUG
- olsr_printf(1, "("PLUGIN_NAME") setsockopt()=%s\n", strerror(errno));
-#endif /* NODEBUG */
- return 0;
- }
-#if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
- if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *) &yes, sizeof(yes)) < 0) {
- perror("SO_REUSEADDR failed");
- return 0;
- }
-#endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
-#if defined linux && defined IPV6_V6ONLY
- if (info_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
- if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &yes, sizeof(yes)) < 0) {
- perror("IPV6_V6ONLY failed");
- return 0;
- }
- }
-#endif /* defined linux && defined IPV6_V6ONLY */
- /* Bind the socket */
-
- /* complete the socket structure */
- memset(&sst, 0, sizeof(sst));
- if (olsr_cnf->ip_version == AF_INET) {
- sst.in4.sin_family = AF_INET;
- addrlen = sizeof(struct sockaddr_in);
-#ifdef SIN6_LEN
- sst.in4.sin_len = addrlen;
-#endif /* SIN6_LEN */
- sst.in4.sin_addr.s_addr = info_listen_ip.v4.s_addr;
- sst.in4.sin_port = htons(ipc_port);
- } else {
- sst.in6.sin6_family = AF_INET6;
- addrlen = sizeof(struct sockaddr_in6);
-#ifdef SIN6_LEN
- sst.in6.sin6_len = addrlen;
-#endif /* SIN6_LEN */
- sst.in6.sin6_addr = info_listen_ip.v6;
- sst.in6.sin6_port = htons(ipc_port);
- }
-
- /* bind the socket to the port number */
- if (bind(ipc_socket, &sst.in, addrlen) == -1) {
-#ifndef NODEBUG
- olsr_printf(1, "("PLUGIN_NAME") bind()=%s\n", strerror(errno));
-#endif /* NODEBUG */
- return 0;
- }
-
- /* show that we are willing to listen */
- if (listen(ipc_socket, 1) == -1) {
-#ifndef NODEBUG
- olsr_printf(1, "("PLUGIN_NAME") listen()=%s\n", strerror(errno));
-#endif /* NODEBUG */
- return 0;
- }
-
- /* Register with olsrd */
- add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
-
-#ifndef NODEBUG
- olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", ipc_port);
-#endif /* NODEBUG */
- }
- return 1;
-}
-
-static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
- union olsr_sockaddr pin;
-
- char addr[INET6_ADDRSTRLEN];
- fd_set rfds;
- struct timeval tv;
- unsigned int send_what = 0;
- int ipc_connection;
-
- socklen_t addrlen = sizeof(pin);
-
- if (outbuffer_count >= MAX_CLIENTS) {
- return;
- }
-
- if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
-#ifndef NODEBUG
- olsr_printf(1, "("PLUGIN_NAME") accept()=%s\n", strerror(errno));
-#endif /* NODEBUG */
- return;
- }
-
- tv.tv_sec = tv.tv_usec = 0;
- if (olsr_cnf->ip_version == AF_INET) {
- if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
- addr[0] = '\0';
- if (!ip4equal(&pin.in4.sin_addr, &info_accept_ip.v4) && info_accept_ip.v4.s_addr != INADDR_ANY) {
-#ifdef INFO_ALLOW_LOCALHOST
- if (ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
-#endif /* INFO_ALLOW_LOCALHOST */
- olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
- close(ipc_connection);
- return;
-#ifdef INFO_ALLOW_LOCALHOST
- }
-#endif /* INFO_ALLOW_LOCALHOST */
- }
- } else {
- if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
- addr[0] = '\0';
- /* Use in6addr_any (::) in olsr.conf to allow anybody. */
- if (!ip6equal(&in6addr_any, &info_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &info_accept_ip.v6)) {
- olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
- close(ipc_connection);
- return;
- }
- }
-#ifndef NODEBUG
- olsr_printf(2, "("PLUGIN_NAME") Connect from %s\n", addr);
-#endif /* NODEBUG */
-
- /* purge read buffer to prevent blocking on linux */
- FD_ZERO(&rfds);
- FD_SET((unsigned int )ipc_connection, &rfds); /* Win32 needs the cast here */
- if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
- char requ[1024];
- ssize_t s = recv(ipc_connection, (void *) &requ, sizeof(requ) - 1, 0); /* Win32 needs the cast here */
-
- if (s == sizeof(requ) - 1) {
- /* input was much too long, just skip the rest */
- char dummy[1024];
-
- while (recv(ipc_connection, (void *) &dummy, sizeof(dummy), 0) == sizeof(dummy))
- ;
- }
-
- if (0 < s) {
- requ[s] = 0;
- determine_action(&send_what, requ);
- }
-
- if (!send_what)
- send_what = SIW_ALL;
- }
-
- send_info(send_what, ipc_connection);
-}
-
-static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
+void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
struct ipaddr_str buf1;
struct neighbor_entry *neigh;
struct neighbor_2_list_entry *list_2;
@@ -409,7 +84,7 @@ static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
abuf_puts(abuf, "\n");
}
-static void ipc_print_links(struct autobuf *abuf) {
+void ipc_print_links(struct autobuf *abuf) {
struct ipaddr_str buf1, buf2;
struct lqtextbuffer lqbuffer1, lqbuffer2;
@@ -442,7 +117,7 @@ static void ipc_print_links(struct autobuf *abuf) {
abuf_puts(abuf, "\n");
}
-static void ipc_print_routes(struct autobuf *abuf) {
+void ipc_print_routes(struct autobuf *abuf) {
struct ipaddr_str buf1, buf2;
struct rt_entry *rt;
struct lqtextbuffer lqbuffer;
@@ -461,7 +136,7 @@ static void ipc_print_routes(struct autobuf *abuf) {
}
-static void ipc_print_topology(struct autobuf *abuf) {
+void ipc_print_topology(struct autobuf *abuf) {
struct tc_entry *tc;
#ifdef ACTIVATE_VTIME_TXTINFO
@@ -498,7 +173,7 @@ static void ipc_print_topology(struct autobuf *abuf) {
abuf_puts(abuf, "\n");
}
-static void ipc_print_hna(struct autobuf *abuf) {
+void ipc_print_hna(struct autobuf *abuf) {
struct ip_prefix_list *hna;
struct hna_entry *tmp_hna;
struct hna_net *tmp_net;
@@ -537,7 +212,7 @@ static void ipc_print_hna(struct autobuf *abuf) {
abuf_puts(abuf, "\n");
}
-static void ipc_print_mid(struct autobuf *abuf) {
+void ipc_print_mid(struct autobuf *abuf) {
int idx;
unsigned short is_first;
struct mid_entry *entry;
@@ -586,7 +261,7 @@ static void ipc_print_mid(struct autobuf *abuf) {
abuf_puts(abuf, "\n");
}
-static void ipc_print_gateways(struct autobuf *abuf) {
+void ipc_print_gateways(struct autobuf *abuf) {
#ifndef __linux__
abuf_puts(abuf, "Gateway mode is only supported in linux\n");
#else /* __linux__ */
@@ -633,7 +308,7 @@ static void ipc_print_gateways(struct autobuf *abuf) {
abuf_appendf(abuf, "%c%c\t%s\t%s\t%d\t%u\t%u\t%s\t%s\t%s\n", v4, v6, olsr_ip_to_string(&buf, &gw->originator),
get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops, gw->uplink, gw->downlink, v4type, v6type,
gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
- }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
+ }OLSR_FOR_ALL_GATEWAY_ENTRIES_END (gw)
#endif /* __linux__ */
}
@@ -710,7 +385,7 @@ static void sgw_ipvx(struct autobuf *abuf, bool ipv6, const char * fmth, const c
}
#endif /* __linux__ */
-static void ipc_print_sgw(struct autobuf *abuf) {
+void ipc_print_sgw(struct autobuf *abuf) {
#ifndef __linux__
abuf_puts(abuf, "Gateway mode is only supported in linux\n");
#else
@@ -731,15 +406,15 @@ static void ipc_print_sgw(struct autobuf *abuf) {
#endif /* __linux__ */
}
-static void ipc_print_version(struct autobuf *abuf) {
+void ipc_print_version(struct autobuf *abuf) {
abuf_appendf(abuf, "Version: %s (built on %s on %s)\n", olsrd_version, build_date, build_host);
}
-static void ipc_print_olsrd_conf(struct autobuf *abuf) {
+void ipc_print_olsrd_conf(struct autobuf *abuf) {
olsrd_write_cnf_autobuf(abuf, olsr_cnf);
}
-static void ipc_print_interfaces(struct autobuf *abuf) {
+void ipc_print_interfaces(struct autobuf *abuf) {
const struct olsr_if *ifs;
abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
@@ -763,133 +438,3 @@ static void ipc_print_interfaces(struct autobuf *abuf) {
abuf_puts(abuf, "\n");
}
-static void info_write_data(void *foo __attribute__ ((unused))) {
- fd_set set;
- int result, i, j, max;
- struct timeval tv;
-
- FD_ZERO(&set);
- max = 0;
- for (i = 0; i < outbuffer_count; i++) {
- /* And we cast here since we get a warning on Win32 */
- FD_SET((unsigned int )(outbuffer_socket[i]), &set);
-
- if (outbuffer_socket[i] > max) {
- max = outbuffer_socket[i];
- }
- }
-
- tv.tv_sec = 0;
- tv.tv_usec = 0;
-
- result = select(max + 1, NULL, &set, NULL, &tv);
- if (result <= 0) {
- return;
- }
-
- for (i = 0; i < outbuffer_count; i++) {
- if (FD_ISSET(outbuffer_socket[i], &set)) {
- result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
- if (result > 0) {
- outbuffer_written[i] += result;
- }
-
- if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
- /* close this socket and cleanup*/
- close(outbuffer_socket[i]);
- free(outbuffer[i]);
-
- for (j = i + 1; j < outbuffer_count; j++) {
- outbuffer[j - 1] = outbuffer[j];
- outbuffer_size[j - 1] = outbuffer_size[j];
- outbuffer_socket[j - 1] = outbuffer_socket[j];
- outbuffer_written[j - 1] = outbuffer_written[j];
- }
- outbuffer_count--;
- }
- }
- }
- if (!outbuffer_count) {
- olsr_stop_timer(writetimer_entry);
- }
-}
-
-static void send_info(unsigned int send_what, int the_socket) {
- struct autobuf abuf;
-
- const char *content_type = "text/plain; charset=utf-8";
- int contentLengthPlaceholderStart = 0;
- int headerLength = 0;
-
- abuf_init(&abuf, 2 * 4096);
-
- if (http_headers) {
- build_http_header(PLUGIN_NAME, HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
- headerLength = abuf.len;
- }
-
- // only add if normal format
- if (send_what & SIW_ALL) {
- if (send_what & SIW_LINKS)
- ipc_print_links(&abuf);
- if (send_what & SIW_NEIGHBORS)
- ipc_print_neighbors(&abuf, false);
- if (send_what & SIW_TOPOLOGY)
- ipc_print_topology(&abuf);
- if (send_what & SIW_HNA)
- ipc_print_hna(&abuf);
- if (send_what & SIW_SGW)
- ipc_print_sgw(&abuf);
- if (send_what & SIW_MID)
- ipc_print_mid(&abuf);
- if (send_what & SIW_ROUTES)
- ipc_print_routes(&abuf);
- if (send_what & SIW_GATEWAYS)
- ipc_print_gateways(&abuf);
- if (send_what & SIW_CONFIG) {
- /* not supported */
- }
- if (send_what & SIW_INTERFACES)
- ipc_print_interfaces(&abuf);
- if (send_what & SIW_2HOP)
- ipc_print_neighbors(&abuf, true);
- if (send_what & SIW_VERSION)
- ipc_print_version(&abuf);
- if (send_what & SIW_PLUGINS) {
- /* not supported */
- }
- } else if (send_what & SIW_OLSRD_CONF) {
- /* this outputs the olsrd.conf text directly, not normal format */
- ipc_print_olsrd_conf(&abuf);
- }
-
- if (http_headers) {
- http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
- }
-
- /* avoid a memcpy: just move the abuf.buf pointer and clear abuf */
- outbuffer[outbuffer_count] = abuf.buf;
- outbuffer_size[outbuffer_count] = abuf.len;
- outbuffer_written[outbuffer_count] = 0;
- outbuffer_socket[outbuffer_count] = the_socket;
- abuf.buf = NULL;
- abuf.len = 0;
- abuf.size = 0;
-
- outbuffer_count++;
-
- if (outbuffer_count == 1) {
- writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &info_write_data, NULL, 0);
- }
-
- abuf_free(&abuf);
-}
-
-/*
- * Local Variables:
- * mode: c
- * style: linux
- * c-basic-offset: 2
- * indent-tabs-mode: nil
- * End:
- */
diff --git a/lib/info/http_headers.h b/lib/txtinfo/src/txtinfo_printers.h
similarity index 75%
copy from lib/info/http_headers.h
copy to lib/txtinfo/src/txtinfo_printers.h
index a5c96ab..3fcd86c 100644
--- a/lib/info/http_headers.h
+++ b/lib/txtinfo/src/txtinfo_printers.h
@@ -39,16 +39,23 @@
*
*/
-#ifndef _OLSRD_INFO_COMMON_HTTP_HEADERS
-#define _OLSRD_INFO_COMMON_HTTP_HEADERS
+#ifndef _OLSRD_TXTINFO_PRINTERS_H
+#define _OLSRD_TXTINFO_PRINTERS_H
-#include "common/autobuf.h"
-
-/* Response types */
-#define HTTP_200 "HTTP/1.1 200 OK"
+#include <stdbool.h>
-void build_http_header(const char * plugin_name, const char *status, const char *mime, struct autobuf *abuf, int *contentLengthPlaceholderStart);
+#include "common/autobuf.h"
-void http_header_adjust_content_length(struct autobuf *abuf, int contentLengthPlaceholderStart, int contentLength);
+void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop);
+void ipc_print_links(struct autobuf *abuf);
+void ipc_print_routes(struct autobuf *abuf);
+void ipc_print_topology(struct autobuf *abuf);
+void ipc_print_hna(struct autobuf *abuf);
+void ipc_print_mid(struct autobuf *abuf);
+void ipc_print_gateways(struct autobuf *abuf);
+void ipc_print_sgw(struct autobuf *abuf);
+void ipc_print_version(struct autobuf *abuf);
+void ipc_print_olsrd_conf(struct autobuf *abuf);
+void ipc_print_interfaces(struct autobuf *abuf);
-#endif /* _OLSRD_INFO_COMMON_HTTP_HEADERS */
+#endif /* _OLSRD_TXTINFO_PRINTERS_H */
--
2.5.0
More information about the Olsr-dev
mailing list