[Olsr-dev] [PATCH v1 041/115] txtinfo: wrap generic info plugin configuration in a struct

Ferry Huberts (spam-protected)
Wed Dec 2 21:28:14 CET 2015


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 lib/info/info_types.h           | 10 ++++++++++
 lib/txtinfo/src/olsrd_plugin.c  | 42 +++++++++++++++++++----------------------
 lib/txtinfo/src/olsrd_txtinfo.c | 26 +++++++++++--------------
 lib/txtinfo/src/olsrd_txtinfo.h |  9 ++-------
 4 files changed, 42 insertions(+), 45 deletions(-)

diff --git a/lib/info/info_types.h b/lib/info/info_types.h
index 8d051bb..2378e81 100644
--- a/lib/info/info_types.h
+++ b/lib/info/info_types.h
@@ -110,4 +110,14 @@ typedef struct {
   int count;
 } outbuffer_t;
 
+typedef struct {
+    union olsr_ip_addr accept_ip;
+    union olsr_ip_addr listen_ip;
+    int ipc_port;
+    int nompr;
+    bool http_headers;
+    bool allow_localhost;
+    int ipv6_only;
+} info_plugin_config_t;
+
 #endif /* _OLSRD_INFO_TYPES_H */
diff --git a/lib/txtinfo/src/olsrd_plugin.c b/lib/txtinfo/src/olsrd_plugin.c
index def3368..0d7be62 100644
--- a/lib/txtinfo/src/olsrd_plugin.c
+++ b/lib/txtinfo/src/olsrd_plugin.c
@@ -52,6 +52,7 @@
 #include "olsrd_plugin.h"
 #include "olsrd_txtinfo.h"
 #include "defs.h"
+#include "../../info/info_types.h"
 
 #define PLUGIN_NAME    "OLSRD txtinfo plugin"
 #define PLUGIN_VERSION "0.1"
@@ -59,13 +60,7 @@
 #define MOD_DESC PLUGIN_NAME " " PLUGIN_VERSION " by " PLUGIN_AUTHOR
 #define PLUGIN_INTERFACE_VERSION 5
 
-union olsr_ip_addr txtinfo_accept_ip;
-union olsr_ip_addr txtinfo_listen_ip;
-int ipc_port;
-int nompr;
-bool http_headers;
-bool allow_localhost;
-int txtinfo_ipv6_only;
+info_plugin_config_t info_plugin_config;
 bool vtime;
 
 static void my_init(void) __attribute__ ((constructor));
@@ -79,22 +74,23 @@ static void my_init(void) {
   printf("%s\n", MOD_DESC);
 
   /* defaults for parameters */
-  ipc_port = 2006;
-  http_headers = true;
-  allow_localhost = false;
-  txtinfo_ipv6_only = false;
-  vtime = false;
+  info_plugin_config.ipc_port = 2006;
+  info_plugin_config.http_headers = true;
+  info_plugin_config.allow_localhost = false;
+  info_plugin_config.ipv6_only = false;
 
   if (olsr_cnf->ip_version == AF_INET) {
-    txtinfo_accept_ip.v4.s_addr = htonl(INADDR_LOOPBACK);
-    txtinfo_listen_ip.v4.s_addr = htonl(INADDR_ANY);
+    info_plugin_config.accept_ip.v4.s_addr = htonl(INADDR_LOOPBACK);
+    info_plugin_config.listen_ip.v4.s_addr = htonl(INADDR_ANY);
   } else {
-    txtinfo_accept_ip.v6 = in6addr_loopback;
-    txtinfo_listen_ip.v6 = in6addr_any;
+    info_plugin_config.accept_ip.v6 = in6addr_loopback;
+    info_plugin_config.listen_ip.v6 = in6addr_any;
   }
 
   /* highlite neighbours by default */
-  nompr = 0;
+  info_plugin_config.nompr = 0;
+
+  vtime = false;
 }
 
 /**
@@ -116,12 +112,12 @@ int olsrd_plugin_interface_version(void) {
 
 static const struct olsrd_plugin_parameters plugin_parameters[] = { //
     //
-        { .name = "port", .set_plugin_parameter = &set_plugin_port, .data = &ipc_port }, //
-        { .name = "accept", .set_plugin_parameter = &set_plugin_ipaddress, .data = &txtinfo_accept_ip }, //
-        { .name = "listen", .set_plugin_parameter = &set_plugin_ipaddress, .data = &txtinfo_listen_ip }, //
-        { .name = "httpheaders", .set_plugin_parameter = &set_plugin_boolean, .data = &http_headers }, //
-        { .name = "allowlocalhost", .set_plugin_parameter = &set_plugin_boolean, .data = &allow_localhost }, //
-        { .name = "ipv6only", .set_plugin_parameter = &set_plugin_boolean, .data = &txtinfo_ipv6_only }, //
+        { .name = "port", .set_plugin_parameter = &set_plugin_port, .data = &info_plugin_config.ipc_port }, //
+        { .name = "accept", .set_plugin_parameter = &set_plugin_ipaddress, .data = &info_plugin_config.accept_ip }, //
+        { .name = "listen", .set_plugin_parameter = &set_plugin_ipaddress, .data = &info_plugin_config.listen_ip }, //
+        { .name = "httpheaders", .set_plugin_parameter = &set_plugin_boolean, .data = &info_plugin_config.http_headers }, //
+        { .name = "allowlocalhost", .set_plugin_parameter = &set_plugin_boolean, .data = &info_plugin_config.allow_localhost }, //
+        { .name = "ipv6only", .set_plugin_parameter = &set_plugin_boolean, .data = &info_plugin_config.ipv6_only }, //
         { .name = "vtime", .set_plugin_parameter = &set_plugin_boolean, .data = &vtime } //
     };
 
diff --git a/lib/txtinfo/src/olsrd_txtinfo.c b/lib/txtinfo/src/olsrd_txtinfo.c
index a48b645..4430188 100644
--- a/lib/txtinfo/src/olsrd_txtinfo.c
+++ b/lib/txtinfo/src/olsrd_txtinfo.c
@@ -53,7 +53,6 @@
 #include "../../info/http_headers.h"
 #include "../../info/info_types.h"
 #include "txtinfo_printers.h"
-
 #include "olsrd_txtinfo.h"
 
 #ifdef _WIN32
@@ -62,9 +61,6 @@
 
 /* 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
 
 static int ipc_socket;
 
@@ -208,7 +204,7 @@ static int plugin_ipc_init(void) {
     }
 #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 (info_plugin_config.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;
@@ -225,16 +221,16 @@ static int plugin_ipc_init(void) {
 #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);
+      sst.in4.sin_addr.s_addr = info_plugin_config.listen_ip.v4.s_addr;
+      sst.in4.sin_port = htons(info_plugin_config.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);
+      sst.in6.sin6_addr = info_plugin_config.listen_ip.v6;
+      sst.in6.sin6_port = htons(info_plugin_config.ipc_port);
     }
 
     /* bind the socket to the port number */
@@ -257,7 +253,7 @@ static int plugin_ipc_init(void) {
     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);
+    olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", info_plugin_config.ipc_port);
 #endif /* NODEBUG */
   }
   return 1;
@@ -289,8 +285,8 @@ static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int
   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) {
-      if (!allow_localhost || ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
+    if (!ip4equal(&pin.in4.sin_addr, &info_plugin_config.accept_ip.v4) && info_plugin_config.accept_ip.v4.s_addr != INADDR_ANY) {
+      if (!info_plugin_config.allow_localhost || ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
         olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
         close(ipc_connection);
         return;
@@ -300,7 +296,7 @@ static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int
     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)) {
+    if (!ip6equal(&in6addr_any, &info_plugin_config.accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &info_plugin_config.accept_ip.v6)) {
       olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
       close(ipc_connection);
       return;
@@ -399,7 +395,7 @@ static void send_info(unsigned int send_what, int the_socket) {
 
   abuf_init(&abuf, 2 * 4096);
 
-  if (http_headers) {
+  if (info_plugin_config.http_headers) {
     build_http_header(PLUGIN_NAME, HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
     headerLength = abuf.len;
   }
@@ -443,7 +439,7 @@ static void send_info(unsigned int send_what, int the_socket) {
     (*printer_functions.olsrd_conf)(&abuf);
   }
 
-  if (http_headers) {
+  if (info_plugin_config.http_headers) {
     http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
   }
 
diff --git a/lib/txtinfo/src/olsrd_txtinfo.h b/lib/txtinfo/src/olsrd_txtinfo.h
index eac7126..ed1e1ae 100644
--- a/lib/txtinfo/src/olsrd_txtinfo.h
+++ b/lib/txtinfo/src/olsrd_txtinfo.h
@@ -49,14 +49,9 @@
 #include "olsr_types.h"
 #include "olsrd_plugin.h"
 #include "plugin_util.h"
+#include "../../info/info_types.h"
 
-extern union olsr_ip_addr txtinfo_accept_ip;
-extern union olsr_ip_addr txtinfo_listen_ip;
-extern int ipc_port;
-extern int nompr;
-extern bool http_headers;
-extern bool allow_localhost;
-extern int txtinfo_ipv6_only;
+extern info_plugin_config_t info_plugin_config;
 
 int olsrd_plugin_interface_version(void);
 int olsrd_plugin_init(void);
-- 
2.5.0




More information about the Olsr-dev mailing list