Tabla de Contenidos

Routeo dinámico

Parches

Ejecuta script desde plugin nameservice en OLSRd

Este patch a nameservice.c llama a un script especificado en la configuración de OLSR, que puede ser utilizado para diversos propósitos, como mandar señales a procesos (aunque el patch debajo es mucho más eficiente para mandar SIGHUP), actualizar bases de datos o sitios web que utilizen datos del plugin, mandar mails, etc, cada vez que se actualizan los nombres de los nodos, o los servicios que anuncia el plugin.

nameservice.script.patch.gz

--- nameservice.c.orig	2008-03-19 06:48:24.000000000 -0300
+++ nameservice.c	2008-03-19 07:32:42.000000000 -0300
@@ -71,7 +71,8 @@
 static double my_timeout = NAME_VALID_TIME;
 static char my_resolv_file[MAX_FILE +1];
 static char my_services_file[MAX_FILE + 1];
-static char my_change_script[MAX_FILE + 1];
+static char my_name_change_script[MAX_FILE + 1];
+static char my_services_change_script[MAX_FILE + 1];
 static char latlon_in_file[MAX_FILE + 1];
 static char my_latlon_file[MAX_FILE + 1];
 float my_lat = 0.0, my_lon = 0.0;
@@ -152,7 +153,8 @@
 	my_add_hosts[0] = '\0';
 	my_latlon_file[0] = '\0';
 	latlon_in_file[0] = '\0';
-        my_change_script[0] = '\0';
+        my_name_change_script[0] = '\0';
+	my_services_change_script[0] = '\0';
 
 	/* init the lists heads */
 	for(i = 0; i < HASHSIZE; i++) {
@@ -243,6 +245,8 @@
     { .name = "interval",      .set_plugin_parameter = &set_plugin_int,         .data = &my_interval },
     { .name = "timeout",       .set_plugin_parameter = &set_nameservice_float,  .data = &my_timeout },
     { .name = "hosts-file",    .set_plugin_parameter = &set_plugin_string,      .data = &my_hosts_file,    .addon = {sizeof(my_hosts_file)} },
+    { .name = "name-change-script", .set_plugin_parameter = &set_plugin_string, .data = &my_name_change_script, .addon = {sizeof(my_name_change_script)} },
+    { .name = "services-change-script", .set_plugin_parameter = &set_plugin_string, .data = &my_services_change_script, .addon = {sizeof(my_services_change_script)} },
     { .name = "resolv-file",   .set_plugin_parameter = &set_plugin_string,      .data = &my_resolv_file,   .addon = {sizeof(my_resolv_file)} },
     { .name = "suffix",        .set_plugin_parameter = &set_plugin_string,      .data = &my_suffix,        .addon = {sizeof(my_suffix)} },
     { .name = "add-hosts",     .set_plugin_parameter = &set_plugin_string,      .data = &my_add_hosts,     .addon = {sizeof(my_add_hosts)} },
@@ -1139,6 +1143,16 @@
 
 	fclose(hosts);
 	name_table_changed = OLSR_FALSE;
+	
+	/* Executes my_name_change_script after writing the hosts file */
+	if (my_name_change_script[0] != '\0') {
+		if(system(my_name_change_script) != -1) {
+			OLSR_PRINTF(2, "NAME PLUGIN: Name changed, %s executed\n", my_name_change_script);
+		}
+		else {
+			OLSR_PRINTF(2, "NAME PLUGIN: WARNING! Failed to execute %s on hosts change\n", my_name_change_script);
+		}
+	}
 }
 
 
@@ -1208,6 +1222,16 @@
 
 	fclose(services_file);
 	service_table_changed = OLSR_FALSE;
+	
+	/* Executes my_services_change_script after writing the services file */
+	if (my_services_change_script[0] != '\0') {
+		if(system(my_services_change_script) != -1) {
+			OLSR_PRINTF(2, "NAME PLUGIN: Services changed, %s executed\n", my_services_change_script);
+		}
+		else {
+			OLSR_PRINTF(2, "NAME PLUGIN: WARNING! Failed to execute %s on services change\n", my_services_change_script);
+		}
+	}
 }
 
 /**

Una configuración típica sería:

LoadPlugin "olsrd_nameservice.so.0.3"
{
      PlParam "name" "matrix.sirius"
      PlParam "suffix" ".mvdl"
      PlParam "name-change-script" "/somewhere/newname.sh"
      PlParam "services-change-script" "/somewhere/newservice.sh"
}

SIGHUP a proceso desde plugin nameservice en OLSRd

Este patch manda la señal SIGHUP a un proceso indicado en la configuración de olsrd al actualizarse los nombres del dns. Tiene la ventaja de ser mucho más eficiente que el patch anterior.

Es especialmente útil cuando se especifica el pidfile de dnsmasq como parámetro, ya que dnsmasq relee /etc/hosts y /etc/ethers (y si es ejecutado con –no-poll, también relee /etc/resolv.conf) al recibir SIGHUP. Esto le permite convertirse en servidor DNS para nodos que no corren OLSR.

nameservice.sighup.patch.gz

--- nameservice.c.orig	2008-03-12 16:15:00.000000000 -0300
+++ nameservice.c	2008-03-19 06:47:29.000000000 -0300
@@ -43,6 +43,9 @@
 #include <ctype.h>
 #include <sys/types.h>
 #include <regex.h>
+#include <sys/stat.h>
+#include <signal.h>
+#include <fcntl.h>
 
 #include "olsr.h"
 #include "ipcalc.h"
@@ -65,6 +68,8 @@
 
 /* config parameters */
 static char my_hosts_file[MAX_FILE + 1];
+static char my_sighup_pid_file[MAX_FILE + 1];
+
 static char my_add_hosts[MAX_FILE + 1];
 static char my_suffix[MAX_SUFFIX];
 static int my_interval = EMISSION_INTERVAL;
@@ -145,6 +150,7 @@
 	strcpy(my_hosts_file, "/var/run/hosts_olsr");
 	strcpy(my_services_file, "/var/run/services_olsr");
 	strcpy(my_resolv_file, "/var/run/resolvconf_olsr");
+	*my_sighup_pid_file = 0;
 #endif
 
 	my_suffix[0] = '\0';
@@ -240,6 +246,7 @@
 static const struct olsrd_plugin_parameters plugin_parameters[] = {
     { .name = "interval",      .set_plugin_parameter = &set_plugin_int,         .data = &my_interval },
     { .name = "timeout",       .set_plugin_parameter = &set_nameservice_float,  .data = &my_timeout },
+    { .name = "sighup-pid-file",.set_plugin_parameter = &set_plugin_string,      .data = &my_sighup_pid_file, .addon = {sizeof(my_sighup_pid_file)} },
     { .name = "hosts-file",    .set_plugin_parameter = &set_plugin_string,      .data = &my_hosts_file,    .addon = {sizeof(my_hosts_file)} },
     { .name = "resolv-file",   .set_plugin_parameter = &set_plugin_string,      .data = &my_resolv_file,   .addon = {sizeof(my_resolv_file)} },
     { .name = "suffix",        .set_plugin_parameter = &set_plugin_string,      .data = &my_suffix,        .addon = {sizeof(my_suffix)} },
@@ -1013,6 +1020,51 @@
 	}
 }
 
+#ifndef WIN32
+static void
+send_sighup_to_pidfile(char * pid_file){
+	int fd;
+	int i=0;
+	int result;
+	pid_t ipid;
+	char line[20];
+	char * endptr;
+
+	fd = open(pid_file, O_RDONLY);
+	if (fd<0) {
+		OLSR_PRINTF(2, "NAME PLUGIN: can't open file %s\n", pid_file);
+		return;
+	}
+
+	while (i<19) {
+		result = read(fd, line+i, 19-i);
+		if (!result) { /* EOF */
+			break;
+		} else if (result>0) {
+			i += result;
+		} else if(errno!=EINTR && errno!=EAGAIN) {
+			OLSR_PRINTF(2, "NAME PLUGIN: can't read file %s\n", pid_file);
+			return;
+		}
+	}
+	line[i]=0;
+	close(fd);
+	ipid = strtol(line, &endptr, 0);
+	if (endptr==line) {
+		OLSR_PRINTF(2, "NAME PLUGIN: invalid pid at file %s\n", pid_file);
+		return;	
+	}
+
+	result=kill(ipid, SIGHUP);
+	if (result==0){
+		OLSR_PRINTF(2, "NAME PLUGIN: SIGHUP sent to pid %i\n", ipid);	
+	} else {
+		OLSR_PRINTF(2, "NAME PLUGIN: failed to send SIGHUP to pid %i\n", ipid);
+	}
+
+}
+#endif
+
 /**
  * write names to a file in /etc/hosts compatible format
  */
@@ -1136,6 +1188,11 @@
 	}
 
 	fclose(hosts);
+
+#ifndef WIN32
+	if (*my_sighup_pid_file)
+		send_sighup_to_pidfile(my_sighup_pid_file);
+#endif
 	name_table_changed = OLSR_FALSE;
 }

Un ejemplo de configuración mínima:

LoadPlugin "olsrd_nameservice.so.0.3"
{
      PlParam "name" "marix.sirius"
      PlParam "suffix" ".mvdl"
      PlParam "sighup-pid-file" "/var/run/dnsmasq.pid"
}