Browse Source

maxfwd(k): max_limit module param can be changed at runtime

- changed to use cfg param reload framework
- added aliases to existing functions to make them compatible with ser
  flavour
- config functions can take variables as parameters
Daniel-Constantin Mierla 12 years ago
parent
commit
9a9cbdc825
4 changed files with 143 additions and 64 deletions
  1. 44 16
      modules_k/maxfwd/README
  2. 46 7
      modules_k/maxfwd/doc/maxfwd_admin.xml
  3. 52 40
      modules_k/maxfwd/maxfwd.c
  4. 1 1
      modules_k/maxfwd/mf_funcs.c

+ 44 - 16
modules_k/maxfwd/README

@@ -27,13 +27,17 @@ Bogdan-Andrei Iancu
 
         4. Functions
 
-              4.1. mf_process_maxfwd_header(max_value)
-              4.2. is_maxfwd_lt(max_value)
+              4.1. maxfwd_process(max_value)
+              4.2. mf_process_maxfwd_header(max_value)
+              4.3. process_maxfwd(max_value)
+              4.4. is_maxfwd_lt(max_value)
+              4.5. maxfwd_at_least(max_value)
+              4.6. mf_lowlimit(max_value)
 
    List of Examples
 
    1.1. Set max_limit parameter
-   1.2. mx_process_maxfwd_header usage
+   1.2. maxfwd_process usage
    1.3. is_maxfwd_lt usage
 
 Chapter 1. Admin Guide
@@ -52,8 +56,12 @@ Chapter 1. Admin Guide
 
    4. Functions
 
-        4.1. mf_process_maxfwd_header(max_value)
-        4.2. is_maxfwd_lt(max_value)
+        4.1. maxfwd_process(max_value)
+        4.2. mf_process_maxfwd_header(max_value)
+        4.3. process_maxfwd(max_value)
+        4.4. is_maxfwd_lt(max_value)
+        4.5. maxfwd_at_least(max_value)
+        4.6. mf_lowlimit(max_value)
 
 1. Overview
 
@@ -88,11 +96,11 @@ Chapter 1. Admin Guide
    exceed this max_limits - if it does, the header value will by decreased
    to "max_limit".
 
-   Note: This check is done when calling the mf_process_maxfwd_header()
-   header.
+   Note: This check is done when calling the maxfwd_process() function.
 
    The range of values stretches from 1 to 256, which is the maximum
-   MAX-FORWARDS value allowed by RFC 3261.
+   MAX-FORWARDS value allowed by RFC 3261. Its value can be changed at
+   runtime.
 
    Default value is "256".
 
@@ -103,15 +111,19 @@ modparam("maxfwd", "max_limit", 32)
 
 4. Functions
 
-   4.1. mf_process_maxfwd_header(max_value)
-   4.2. is_maxfwd_lt(max_value)
+   4.1. maxfwd_process(max_value)
+   4.2. mf_process_maxfwd_header(max_value)
+   4.3. process_maxfwd(max_value)
+   4.4. is_maxfwd_lt(max_value)
+   4.5. maxfwd_at_least(max_value)
+   4.6. mf_lowlimit(max_value)
 
-4.1. mf_process_maxfwd_header(max_value)
+4.1. maxfwd_process(max_value)
 
    If no Max-Forward header is present in the received request, a header
    will be added having the original value equal with "max_value". If a
    Max-Forward header is already present, its value will be decremented
-   (if not 0).
+   (if not 0). The parameter can be a variable.
 
    Retuning codes:
      * 2 (true) - header was not found and a new header was succesfully
@@ -131,21 +143,29 @@ modparam("maxfwd", "max_limit", 32)
 
    This function can be used from REQUEST_ROUTE.
 
-   Example 1.2. mx_process_maxfwd_header usage
+   Example 1.2. maxfwd_process usage
 ...
 # initial sanity checks -- messages with
 # max_forwards==0, or excessively long requests
-if (!mf_process_maxfwd_header("10") && $retcode==-1) {
+if (!maxfwd_process("10") && $retcode==-1) {
         sl_send_reply("483","Too Many Hops");
         exit;
 };
 ...
 
-4.2. is_maxfwd_lt(max_value)
+4.2. mf_process_maxfwd_header(max_value)
+
+   Same as maxfwd_process(max_value).
+
+4.3. process_maxfwd(max_value)
+
+   Same as maxfwd_process(max_value).
+
+4.4. is_maxfwd_lt(max_value)
 
    Checks if the Max-Forward header value is less then the "max_value"
    parameter value. It considers also the value of the new inserted header
-   (if locally added).
+   (if locally added). The parameter can be a variable.
 
    Retuning codes:
      * 1 (true) - header was found or set and its value is stricly less
@@ -171,3 +191,11 @@ if ( is_maxfwd_lt("1") ) {
         exit;
 };
 ...
+
+4.5. maxfwd_at_least(max_value)
+
+   Same as is_maxfwd_lt(max_value).
+
+4.6. mf_lowlimit(max_value)
+
+   Same as is_maxfwd_lt(max_value).

+ 46 - 7
modules_k/maxfwd/doc/maxfwd_admin.xml

@@ -63,11 +63,12 @@
 		decreased to <quote>max_limit</quote>.
 		</para>
 		<para>Note: This check is done when calling the 
-		mf_process_maxfwd_header() header.
+		maxfwd_process() function.
 		</para>
 		<para>
 		The range of values stretches from 1 to 256, which is the maximum 
-		MAX-FORWARDS value allowed by RFC 3261.
+		MAX-FORWARDS value allowed by RFC 3261. Its value can be changed at
+		runtime.
 		</para>
 		<para>
 		<emphasis>
@@ -90,13 +91,14 @@ modparam("maxfwd", "max_limit", 32)
 	<title>Functions</title>
 	<section>
 		<title>
-		<function moreinfo="none">mf_process_maxfwd_header(max_value)</function>
+		<function moreinfo="none">maxfwd_process(max_value)</function>
 		</title>
 		<para>
 		If no Max-Forward header is present in the received request, a header 
 		will be added having the original value equal with 
 		<quote>max_value</quote>. If a Max-Forward header is already present,
-		its value will be decremented (if not 0).
+		its value will be decremented (if not 0). The parameter can be a
+		variable.
 		</para>
 		<para>Retuning codes:</para>
 		<itemizedlist>
@@ -136,12 +138,12 @@ modparam("maxfwd", "max_limit", 32)
 		This function can be used from REQUEST_ROUTE.
 		</para>
 		<example>
-		<title><function>mx_process_maxfwd_header</function> usage</title>
+		<title><function>maxfwd_process</function> usage</title>
 		<programlisting format="linespecific">
 ...
 # initial sanity checks -- messages with
 # max_forwards==0, or excessively long requests
-if (!mf_process_maxfwd_header("10") &amp;&amp; $retcode==-1) {
+if (!maxfwd_process("10") &amp;&amp; $retcode==-1) {
 	sl_send_reply("483","Too Many Hops");
 	exit;
 };
@@ -150,6 +152,24 @@ if (!mf_process_maxfwd_header("10") &amp;&amp; $retcode==-1) {
 		</example>
 	</section>
 
+	<section>
+		<title>
+		<function moreinfo="none">mf_process_maxfwd_header(max_value)</function>
+		</title>
+		<para>
+			Same as maxfwd_process(max_value).
+		</para>
+	</section>
+
+	<section>
+		<title>
+		<function moreinfo="none">process_maxfwd(max_value)</function>
+		</title>
+		<para>
+			Same as maxfwd_process(max_value).
+		</para>
+	</section>
+
 	<section>
 		<title>
 		<function moreinfo="none">is_maxfwd_lt(max_value)</function>
@@ -157,7 +177,8 @@ if (!mf_process_maxfwd_header("10") &amp;&amp; $retcode==-1) {
 		<para>
 		Checks if the Max-Forward header value is less then the 
 		<quote>max_value</quote> parameter value. It considers also the value
-		of the new inserted header (if locally added).
+		of the new inserted header (if locally added). The parameter can be
+		a variable.
 		</para>
 		<para>Retuning codes:</para>
 		<itemizedlist>
@@ -208,6 +229,24 @@ if ( is_maxfwd_lt("1") ) {
 		</example>
 	</section>
 
+	<section>
+		<title>
+		<function moreinfo="none">maxfwd_at_least(max_value)</function>
+		</title>
+		<para>
+			Same as is_maxfwd_lt(max_value).
+		</para>
+	</section>
+
+	<section>
+		<title>
+		<function moreinfo="none">mf_lowlimit(max_value)</function>
+		</title>
+		<para>
+			Same as is_maxfwd_lt(max_value).
+		</para>
+	</section>
+
 	</section>
 </chapter>
 

+ 52 - 40
modules_k/maxfwd/maxfwd.c

@@ -44,16 +44,27 @@
 #include "../../error.h"
 #include "../../ut.h"
 #include "../../mem/mem.h"
+#include "../../cfg/cfg.h"
 #include "mf_funcs.h"
 #include "api.h"
 
 MODULE_VERSION
 
-#define MAXFWD_UPPER_LIMIT 256
+struct cfg_group_maxfwd {
+	int max_limit;
+};
+
+static struct cfg_group_maxfwd default_maxfwd_cfg = {
+	max_limit:16
+};
 
-static int max_limit = MAXFWD_UPPER_LIMIT;
+static void *maxfwd_cfg = &default_maxfwd_cfg;
+
+static cfg_def_t maxfwd_cfg_def[] = {
+        {"max_limit", CFG_VAR_INT, 0, 255, 0, 0, "Max. maxfwd limit"},
+        {0, 0, 0, 0, 0, 0}
+};
 
-static int fixup_maxfwd_header(void** param, int param_no);
 static int w_process_maxfwd_header(struct sip_msg* msg,char* str,char* str2);
 static int is_maxfwd_lt(struct sip_msg *msg, char *slimit, char *foo);
 static int mod_init(void);
@@ -61,17 +72,27 @@ static int mod_init(void);
 int bind_maxfwd(maxfwd_api_t *api);
 
 static cmd_export_t cmds[]={
+	{"maxfwd_process", (cmd_function)w_process_maxfwd_header, 1,
+		fixup_var_int_1, 0, REQUEST_ROUTE},
 	{"mf_process_maxfwd_header", (cmd_function)w_process_maxfwd_header, 1,
-		fixup_maxfwd_header, 0, REQUEST_ROUTE},
+		fixup_var_int_1, 0, REQUEST_ROUTE},
+	{"process_maxfwd", (cmd_function)w_process_maxfwd_header, 1,
+		fixup_var_int_1, 0, REQUEST_ROUTE},
+
 	{"is_maxfwd_lt", (cmd_function)is_maxfwd_lt, 1,
-		fixup_maxfwd_header, 0, REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
+		fixup_var_int_1, 0, REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
+	{"maxfwd_at_least", (cmd_function)is_maxfwd_lt, 1,
+		fixup_var_int_1, 0, REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
+	{"mf_lowlimit", (cmd_function)is_maxfwd_lt, 1,
+		fixup_var_int_1, 0, REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
+
 	{"bind_maxfwd",  (cmd_function)bind_maxfwd,  0,
 		0, 0, 0},
 	{0,0,0,0,0,0}
 };
 
 static param_export_t params[]={
-	{"max_limit",    INT_PARAM,  &max_limit},
+	{"max_limit",    INT_PARAM,  &default_maxfwd_cfg.max_limit},
 	{0,0,0}
 };
 
@@ -96,9 +117,9 @@ struct module_exports exports= {
 
 static int mod_init(void)
 {
-	if ( max_limit<1 || max_limit>MAXFWD_UPPER_LIMIT ) {
-		LM_ERR("invalid max limit (%d) [1,%d]\n",
-			max_limit,MAXFWD_UPPER_LIMIT);
+	if (cfg_declare("maxfwd", maxfwd_cfg_def, &default_maxfwd_cfg,
+				cfg_sizeof(maxfwd), &maxfwd_cfg)) {
+		LM_ERR("failed to declare the configuration\n");
 		return E_CFG;
 	}
 	return 0;
@@ -106,35 +127,6 @@ static int mod_init(void)
 
 
 
-static int fixup_maxfwd_header(void** param, int param_no)
-{
-	unsigned long code;
-	int err;
-
-	if (param_no==1){
-		code=str2s(*param, strlen(*param), &err);
-		if (err==0){
-			if (code<1 || code>MAXFWD_UPPER_LIMIT){
-				LM_ERR("invalid MAXFWD number <%ld> [1,%d]\n",
-					code,MAXFWD_UPPER_LIMIT);
-				return E_UNSPEC;
-			}
-			if (code>max_limit) {
-				LM_ERR("default value <%ld> bigger than max limit(%d)\n",
-					code, max_limit);
-				return E_UNSPEC;
-			}
-			pkg_free(*param);
-			*param=(void*)code;
-			return 0;
-		}else{
-			LM_ERR("bad  number <%s>\n",(char*)(*param));
-			return E_UNSPEC;
-		}
-	}
-	return 0;
-}
-
 
 /**
  * process max forward header
@@ -142,7 +134,14 @@ static int fixup_maxfwd_header(void** param, int param_no)
 int process_maxfwd_header(struct sip_msg *msg, int limit)
 {
 	int val;
-	str mf_value;
+	str mf_value = {0};
+	int max_limit;
+
+	if(limit<0 || limit>255) {
+		LM_ERR("invalid param value: %d\n", limit);
+		return -1;
+	}
+	max_limit = cfg_get(maxfwd, maxfwd_cfg, max_limit);
 
 	val=is_maxfwd_present(msg, &mf_value);
 	switch (val) {
@@ -178,7 +177,12 @@ error:
  */
 static int w_process_maxfwd_header(struct sip_msg* msg, char* str1, char* str2)
 {
-	return process_maxfwd_header(msg, (int)(unsigned long)str1);
+	int mfval;
+	if (get_int_fparam(&mfval, msg, (fparam_t*) str1) < 0) {
+		LM_ERR("could not get param value\n");
+		return -1;
+	}
+	return process_maxfwd_header(msg, mfval);
 }
 
 
@@ -192,6 +196,14 @@ static int is_maxfwd_lt(struct sip_msg *msg, char *slimit, char *foo)
 	int val;
 
 	limit = (int)(long)slimit;
+	if (get_int_fparam(&limit, msg, (fparam_t*) slimit) < 0) {
+		LM_ERR("could not get param value\n");
+		return -1;
+	}
+	if(limit<0 || limit>255) {
+		LM_ERR("invalid param value: %d\n", limit);
+		return -1;
+	}
 	val = is_maxfwd_present( msg, &mf_value);
 	LM_DBG("value = %d \n",val);
 

+ 1 - 1
modules_k/maxfwd/mf_funcs.c

@@ -51,7 +51,7 @@
 	(((int)(long)(_msg_)->maxforwards->parsed)-1)
 
 /* looks for the MAX FORWARDS header
-   returns the its value, -1 if is not present or -2 for error */
+   returns its value, -1 if is not present or -2 for error */
 int is_maxfwd_present( struct sip_msg* msg , str *foo)
 {
 	int x, err;