Jelajahi Sumber

modules_k:registrar Implementing cfg framework for registrar

Started to implement cfg framework for registrar, having the possibility to change module paraments without restarting k/s
Marius Zbihlei 15 tahun lalu
induk
melakukan
0d3a8dc2a4

+ 2 - 2
modules_k/registrar/common.c

@@ -40,7 +40,7 @@
 #include "rerrno.h"
 #include "reg_mod.h"
 #include "common.h"
-
+#include "config.h"
 
 #define MAX_AOR_LEN 256
 
@@ -109,7 +109,7 @@ int extract_aor(str* _uri, str* _a)
 		}
 	}
 
-	if (case_sensitive && user_len) {
+	if (cfg_get(registrar, registrar_cfg, case_sensitive) && user_len) {
 		tmp.s = _a->s + user_len + 1;
 		tmp.len = _a->s + _a->len - tmp.s;
 		strlower(&tmp);

+ 61 - 0
modules_k/registrar/config.c

@@ -0,0 +1,61 @@
+/*
+ * $Id$
+ * This file is part of SIP-router, a free SIP server.
+ *
+ * SIP-router is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * SIP-router is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * History
+ * -------
+ *  2008-02-05	adapting tm module for the configuration framework (Miklos)
+ */
+
+/*!
+ * \file 
+ * \brief Registrar :: Configuration
+ * \ingroup Registrar
+ */
+
+
+#include "../../cfg/cfg.h"
+#include "../../parser/msg_parser.h" /* method types */
+
+#include "config.h"
+
+struct cfg_group_registrar	default_registrar_cfg = {
+		3600, 	/* default_expires */
+		60,	/* min_expires */
+		0,	/* max_expires */
+		0,	/* max_contacts */
+		0,	/* retry_after */
+		0	/* case_sensitive */
+	};
+
+void	*registrar_cfg = &default_registrar_cfg;
+
+cfg_def_t	registrar_cfg_def[] = {
+	{"default_expires",	CFG_VAR_INT | CFG_ATOMIC,	0, 0, 0, 0,
+		"Contains number of second to expire if no expire hf or contact expire present" },
+	{"min_expires",		CFG_VAR_INT | CFG_ATOMIC, 	0, 0, 0, 0,
+		"The minimum expires value of a Contact. Value 0 disables the checking. "},
+	{"max_expires",		CFG_VAR_INT | CFG_ATOMIC, 	0, 0, 0, 0,
+		"The maximum expires value of a Contact. Value 0 disables the checking. "},
+	{"max_contacts",	CFG_VAR_INT | CFG_ATOMIC, 	0, 0, 0, 0,
+		"The maximum number of Contacts for an AOR. Value 0 disables the checking. "},
+	{"retry_after",		CFG_VAR_INT | CFG_ATOMIC, 	0, 0, 0, 0,
+		"If you want to add the Retry-After header field in 5xx replies, set this parameter to a value grater than zero"},
+	{"case_sensitive",	CFG_VAR_INT | CFG_ATOMIC,	0, 0, 0, 0,
+		"If set to 1 then AOR comparison will be case sensitive. Recommended and default is 0, case insensitive"},
+	{0, 0, 0, 0, 0, 0}
+};

+ 42 - 0
modules_k/registrar/config.h

@@ -0,0 +1,42 @@
+/*
+ * $Id$
+ *
+ * SIP-router is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*!
+ * \file 
+ * \brief Registrar :: Configuration
+ * \ingroup registrar
+ */
+
+
+#ifndef _REGISTRAR_CONFIG_H
+#define _REGISTRAR_CONFIG_H
+
+
+#include "../../cfg/cfg.h"
+#include "../../str.h"
+
+struct cfg_group_registrar {
+	unsigned int	default_expires;
+	unsigned int	min_expires;
+	unsigned int	max_expires;
+	unsigned int	max_contacts;
+	unsigned int	retry_after;
+	unsigned int	case_sensitive;
+};
+
+extern struct cfg_group_registrar	default_registrar_cfg;
+extern void	*registrar_cfg;
+extern cfg_def_t	registrar_cfg_def[];
+
+
+#endif

+ 32 - 30
modules_k/registrar/reg_mod.c

@@ -76,7 +76,7 @@
 #include "regpv.h"
 #include "reply.h"
 #include "reg_mod.h"
-
+#include "config.h"
 
 MODULE_VERSION
 
@@ -95,17 +95,10 @@ static int fetchc_fixup(void** param, int param_no);
 static int add_sock_hdr(struct sip_msg* msg, char *str, char *foo);
 
 
-int default_expires = 3600; 			/*!< Default expires value in seconds */
 qvalue_t default_q  = Q_UNSPECIFIED;		/*!< Default q value multiplied by 1000 */
 int append_branches = 1;			/*!< If set to 1, lookup will put all contacts found in msg structure */
 int case_sensitive  = 0;			/*!< If set to 1, username in aor will be case sensitive */
 int tcp_persistent_flag = -1;			/*!< if the TCP connection should be kept open */
-int min_expires     = 60;			/*!< Minimum expires the phones are allowed to use in seconds
- 						 * use 0 to switch expires checking off */
-int max_expires     = 0;			/*!< Maximum expires the phones are allowed to use in seconds,
- 						 * use 0 to switch expires checking off */
-int max_contacts = 0;				/*!< Maximum number of contacts per AOR (0=no checking) */
-int retry_after = 0;				/*!< The value of Retry-After HF in 5xx replies */
 int method_filtering = 0;			/*!< if the looked up contacts should be filtered based on supported methods */
 int path_enabled = 0;				/*!< if the Path HF should be handled */
 int path_mode = PATH_MODE_STRICT;		/*!< if the Path HF should be inserted in the reply.
@@ -189,26 +182,26 @@ static cmd_export_t cmds[] = {
  * Exported parameters
  */
 static param_export_t params[] = {
-	{"default_expires",    INT_PARAM, &default_expires     },
-	{"default_q",          INT_PARAM, &default_q           },
-	{"append_branches",    INT_PARAM, &append_branches     },
-	{"case_sensitive",     INT_PARAM, &case_sensitive      },
+	{"default_expires",    INT_PARAM, &default_registrar_cfg.default_expires     	},
+	{"default_q",          INT_PARAM, &default_q           				},
+	{"append_branches",    INT_PARAM, &append_branches     				},
+	{"case_sensitive",     INT_PARAM, &default_registrar_cfg.case_sensitive		},
 	/*	{"tcp_persistent_flag",INT_PARAM, &tcp_persistent_flag }, */
-	{"realm_prefix",       STR_PARAM, &realm_pref          },
-	{"min_expires",        INT_PARAM, &min_expires         },
-	{"max_expires",        INT_PARAM, &max_expires         },
-	{"received_param",     STR_PARAM, &rcv_param           },
-	{"received_avp",       STR_PARAM, &rcv_avp_param       },
-	{"aor_avp",            STR_PARAM, &aor_avp_param       },
-	{"reg_callid_avp",     STR_PARAM, &reg_callid_avp_param},
-	{"max_contacts",       INT_PARAM, &max_contacts        },
-	{"retry_after",        INT_PARAM, &retry_after         },
-	{"sock_flag",          INT_PARAM, &sock_flag           },
-	{"sock_hdr_name",      STR_PARAM, &sock_hdr_name.s     },
-	{"method_filtering",   INT_PARAM, &method_filtering    },
-	{"use_path",           INT_PARAM, &path_enabled        },
-	{"path_mode",          INT_PARAM, &path_mode           },
-	{"path_use_received",  INT_PARAM, &path_use_params     },
+	{"realm_prefix",       STR_PARAM, &realm_pref          				},
+	{"min_expires",        INT_PARAM, &default_registrar_cfg.min_expires		},
+	{"max_expires",        INT_PARAM, &default_registrar_cfg.max_expires		},
+	{"received_param",     STR_PARAM, &rcv_param           				},
+	{"received_avp",       STR_PARAM, &rcv_avp_param       				},
+	{"aor_avp",            STR_PARAM, &aor_avp_param       				},
+	{"reg_callid_avp",     STR_PARAM, &reg_callid_avp_param				},
+	{"max_contacts",       INT_PARAM, &default_registrar_cfg.max_contacts		},
+	{"retry_after",        INT_PARAM, &default_registrar_cfg.retry_after		},
+	{"sock_flag",          INT_PARAM, &sock_flag           				},
+	{"sock_hdr_name",      STR_PARAM, &sock_hdr_name.s     				},
+	{"method_filtering",   INT_PARAM, &method_filtering    				},
+	{"use_path",           INT_PARAM, &path_enabled        				},
+	{"path_mode",          INT_PARAM, &path_mode           				},
+	{"path_use_received",  INT_PARAM, &path_use_params     				},
 	{0, 0, 0}
 };
 
@@ -224,6 +217,7 @@ stat_export_t mod_stats[] = {
 };
 
 
+
 /*! \brief
  * Module exports structure
  */
@@ -271,6 +265,13 @@ static int mod_init(void)
 	realm_prefix.len = strlen(realm_pref);
 
 	rcv_param.len = strlen(rcv_param.s);
+	
+	if(cfg_declare("registrar", registrar_cfg_def, &default_registrar_cfg, cfg_sizeof(registrar), &registrar_cfg)){
+		LM_ERR("Fail to declare the configuration\n");
+	        return -1;
+	}
+	                                                
+	                                                
 
 	if (rcv_avp_param && *rcv_avp_param) {
 		s.s = rcv_avp_param; s.len = strlen(s.s);
@@ -378,9 +379,10 @@ static int child_init(int rank)
 {
 	if (rank==1) {
 		/* init stats */
-		update_stat( max_expires_stat, max_expires );
-		update_stat( max_contacts_stat, max_contacts );
-		update_stat( default_expire_stat, default_expires );
+		//TODO if parameters are modified via cfg framework do i change them?
+		update_stat( max_expires_stat, default_registrar_cfg.max_expires );
+		update_stat( max_contacts_stat, default_registrar_cfg.max_contacts );
+		update_stat( default_expire_stat, default_registrar_cfg.default_expires );
 	}
 
 	return 0;

+ 0 - 4
modules_k/registrar/reg_mod.h

@@ -73,8 +73,6 @@ extern int append_branches;
 extern int case_sensitive;
 extern int nat_flag;
 extern int tcp_persistent_flag;
-extern int min_expires;
-extern int max_expires;
 extern int received_avp;
 extern int reg_use_domain;
 extern str realm_prefix;
@@ -88,8 +86,6 @@ extern unsigned short reg_callid_avp_type;
 extern int_str reg_callid_avp_name;
 
 extern str rcv_param;
-extern int max_contacts;
-extern int retry_after;
 extern int method_filtering;
 extern int path_enabled;
 extern int path_mode;

+ 3 - 3
modules_k/registrar/reply.c

@@ -45,7 +45,7 @@
 #include "reg_mod.h"
 #include "regtime.h"
 #include "reply.h"
-
+#include "config.h"
 
 #define MAX_CONTACT_BUFFER 1024
 
@@ -308,7 +308,7 @@ static int add_retry_after(struct sip_msg* _m)
 	char* buf, *ra_s;
  	int ra_len;
  	
- 	ra_s = int2str(retry_after, &ra_len);
+ 	ra_s = int2str(cfg_get(registrar, registrar_cfg, retry_after), &ra_len);
  	buf = (char*)pkg_malloc(RETRY_AFTER_LEN + ra_len + CRLF_LEN);
  	if (!buf) {
  		LM_ERR("no pkg memory left\n");
@@ -420,7 +420,7 @@ int reg_send_reply(struct sip_msg* _m)
 		add_lump_rpl( _m, buf, E_INFO_LEN + error_info[rerrno].len + CRLF_LEN,
 			LUMP_RPL_HDR|LUMP_RPL_NODUP);
 
-		if (code >= 500 && code < 600 && retry_after) {
+		if (code >= 500 && code < 600 && cfg_get(registrar, registrar_cfg, retry_after)) {
 			if (add_retry_after(_m) < 0) {
 				return -1;
 			}

+ 8 - 5
modules_k/registrar/save.c

@@ -71,6 +71,7 @@
 #include "regtime.h"
 #include "path.h"
 #include "save.h"
+#include "config.h"
 
 static int mem_only = 0;
 
@@ -396,7 +397,7 @@ static inline int insert_contacts(struct sip_msg* _m, contact_t* _c,
 		if (expires == 0)
 			continue;
 
-		if (max_contacts && (num >= max_contacts)) {
+		if (cfg_get(registrar, registrar_cfg, max_contacts) && (num >= cfg_get(registrar, registrar_cfg, max_contacts))) {
 			LM_INFO("too many contacts (%d) for AOR <%.*s>\n", 
 					num, _a->len, _a->s);
 			rerrno = R_TOO_MANY;
@@ -511,7 +512,7 @@ static int test_max_contacts(struct sip_msg* _m, urecord_t* _r, contact_t* _c,
 	}
 	
 	LM_DBG("%d contacts after commit\n", num);
-	if (num > max_contacts) {
+	if (num > cfg_get(registrar, registrar_cfg, max_contacts)) {
 		LM_INFO("too many contacts for AOR <%.*s>\n", _r->aor.len, _r->aor.s);
 		rerrno = R_TOO_MANY;
 		return -1;
@@ -555,7 +556,7 @@ static inline int update_contacts(struct sip_msg* _m, urecord_t* _r,
 		goto error;
 	}
 
-	if (max_contacts && test_max_contacts(_m, _r, _c, ci) != 0 )
+	if (cfg_get(registrar, registrar_cfg, max_contacts) && test_max_contacts(_m, _r, _c, ci) != 0 )
 		goto error;
 
 #ifdef USE_TCP
@@ -779,16 +780,18 @@ int save(struct sip_msg* _m, char* _d, char* _cflags)
 			goto error;
 		ret = (ret==0)?1:ret;
 	}
-
+#ifdef STATISTICS
 	update_stat(accepted_registrations, 1);
+#endif
 	/* Only send reply upon request, not upon reply */
 	if ((route_type == REQUEST_ROUTE) && !is_cflag_set(REG_SAVE_NORPL_FL) && (reg_send_reply(_m) < 0))
 		return -1;
 
 	return ret;
 error:
+#ifdef STATISTICS
 	update_stat(rejected_registrations, 1);
-
+#endif
 	if ((route_type == REQUEST_ROUTE) && !is_cflag_set(REG_SAVE_NORPL_FL) )
 		reg_send_reply(_m);
 

+ 8 - 8
modules_k/registrar/sip_msg.c

@@ -39,7 +39,7 @@
 #include "regtime.h"                     /* act_time */
 #include "rerrno.h"
 #include "sip_msg.h"
-
+#include "config.h"
 
 static struct hdr_field* act_contact;
 
@@ -60,9 +60,9 @@ static inline int get_expires_hf(struct sip_msg* _m)
 			if (p->val != 0) {
 				return p->val + act_time;
 			} else return 0;
-		} else return act_time + default_expires;
+		} else return act_time + cfg_get(registrar, registrar_cfg, default_expires);
 	} else {
-		return act_time + default_expires;
+		return act_time + cfg_get(registrar, registrar_cfg, default_expires);
 	}
 }
 
@@ -237,18 +237,18 @@ void calc_contact_expires(struct sip_msg* _m, param_t* _ep, int* _e)
 		*_e = get_expires_hf(_m);
 	} else {
 		if (str2int(&_ep->body, (unsigned int*)_e) < 0) {
-			*_e = default_expires;
+			*_e = cfg_get(registrar, registrar_cfg, default_expires);
 		}
 		/* Convert to absolute value */
 		if (*_e != 0) *_e += act_time;
 	}
 
-	if ((*_e != 0) && ((*_e - act_time) < min_expires)) {
-		*_e = min_expires + act_time;
+	if ((*_e != 0) && ((*_e - act_time) < cfg_get(registrar, registrar_cfg, min_expires))) {
+		*_e = cfg_get(registrar, registrar_cfg, min_expires) + act_time;
 	}
 
-	if ((*_e != 0) && max_expires && ((*_e - act_time) > max_expires)) {
-		*_e = max_expires + act_time;
+	if ((*_e != 0) && cfg_get(registrar, registrar_cfg, max_expires) && ((*_e - act_time) > cfg_get(registrar, registrar_cfg, max_expires))) {
+		*_e = cfg_get(registrar, registrar_cfg, max_expires) + act_time;
 	}
 }