task-manager: Add helper function to calculate the total retransmit timeout
authorTobias Brunner <tobias@strongswan.org>
Mon, 13 Mar 2017 11:00:40 +0000 (12:00 +0100)
committerTobias Brunner <tobias@strongswan.org>
Tue, 23 May 2017 16:05:58 +0000 (18:05 +0200)
src/libcharon/sa/task_manager.c
src/libcharon/sa/task_manager.h

index c42008b..bd11914 100644 (file)
 
 #include "task_manager.h"
 
+#include <math.h>
 #include <sa/ikev1/task_manager_v1.h>
 #include <sa/ikev2/task_manager_v2.h>
 
-/**
+/*
+ * See header
+ */
+u_int task_manager_total_retransmit_timeout()
+{
+       double timeout, base, limit = 0, total = 0;
+       int tries, i;
+
+       tries = lib->settings->get_int(lib->settings, "%s.retransmit_tries",
+                                                                  RETRANSMIT_TRIES, lib->ns);
+       base = lib->settings->get_double(lib->settings, "%s.retransmit_base",
+                                                                        RETRANSMIT_BASE, lib->ns);
+       timeout = lib->settings->get_double(lib->settings, "%s.retransmit_timeout",
+                                                                               RETRANSMIT_TIMEOUT, lib->ns);
+       limit = lib->settings->get_double(lib->settings, "%s.retransmit_limit",
+                                                                         0, lib->ns);
+
+       for (i = 0; i <= tries; i++)
+       {
+               double interval = timeout * pow(base, i);
+               if (limit)
+               {
+                       interval = min(interval, limit);
+               }
+               total += interval;
+       }
+       return (u_int)total;
+}
+
+/*
  * See header
  */
 task_manager_t *task_manager_create(ike_sa_t *ike_sa)
index 4d3f9be..e3fddf3 100644 (file)
@@ -303,6 +303,17 @@ struct task_manager_t {
 };
 
 /**
+ * Calculate total timeout of the retransmission mechanism.
+ *
+ * This is affected by modifications of retransmit_base, retransmit_timeout,
+ * retransmit_limit or retransmit_tries. The resulting value can then be used
+ * e.g. in kernel plugins to set the system's acquire timeout properly.
+ *
+ * @return                                     calculated total retransmission timeout in seconds
+ */
+u_int task_manager_total_retransmit_timeout();
+
+/**
  * Create a task manager instance for the correct IKE version.
  *
  * @param ike_sa                       IKE_SA to create a task manager for