Browse code

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 authored on 16/02/2010 14:32:16
Showing 8 changed files
... ...
@@ -40,7 +40,7 @@
40 40
 #include "rerrno.h"
41 41
 #include "reg_mod.h"
42 42
 #include "common.h"
43
-
43
+#include "config.h"
44 44
 
45 45
 #define MAX_AOR_LEN 256
46 46
 
... ...
@@ -109,7 +109,7 @@ int extract_aor(str* _uri, str* _a)
109 109
 		}
110 110
 	}
111 111
 
112
-	if (case_sensitive && user_len) {
112
+	if (cfg_get(registrar, registrar_cfg, case_sensitive) && user_len) {
113 113
 		tmp.s = _a->s + user_len + 1;
114 114
 		tmp.len = _a->s + _a->len - tmp.s;
115 115
 		strlower(&tmp);
116 116
new file mode 100644
... ...
@@ -0,0 +1,61 @@
1
+/*
2
+ * $Id$
3
+ * This file is part of SIP-router, a free SIP server.
4
+ *
5
+ * SIP-router is free software; you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation; either version 2 of the License, or
8
+ * (at your option) any later version
9
+ *
10
+ * SIP-router is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program; if not, write to the Free Software
17
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
+ *
19
+ * History
20
+ * -------
21
+ *  2008-02-05	adapting tm module for the configuration framework (Miklos)
22
+ */
23
+
24
+/*!
25
+ * \file 
26
+ * \brief Registrar :: Configuration
27
+ * \ingroup Registrar
28
+ */
29
+
30
+
31
+#include "../../cfg/cfg.h"
32
+#include "../../parser/msg_parser.h" /* method types */
33
+
34
+#include "config.h"
35
+
36
+struct cfg_group_registrar	default_registrar_cfg = {
37
+		3600, 	/* default_expires */
38
+		60,	/* min_expires */
39
+		0,	/* max_expires */
40
+		0,	/* max_contacts */
41
+		0,	/* retry_after */
42
+		0	/* case_sensitive */
43
+	};
44
+
45
+void	*registrar_cfg = &default_registrar_cfg;
46
+
47
+cfg_def_t	registrar_cfg_def[] = {
48
+	{"default_expires",	CFG_VAR_INT | CFG_ATOMIC,	0, 0, 0, 0,
49
+		"Contains number of second to expire if no expire hf or contact expire present" },
50
+	{"min_expires",		CFG_VAR_INT | CFG_ATOMIC, 	0, 0, 0, 0,
51
+		"The minimum expires value of a Contact. Value 0 disables the checking. "},
52
+	{"max_expires",		CFG_VAR_INT | CFG_ATOMIC, 	0, 0, 0, 0,
53
+		"The maximum expires value of a Contact. Value 0 disables the checking. "},
54
+	{"max_contacts",	CFG_VAR_INT | CFG_ATOMIC, 	0, 0, 0, 0,
55
+		"The maximum number of Contacts for an AOR. Value 0 disables the checking. "},
56
+	{"retry_after",		CFG_VAR_INT | CFG_ATOMIC, 	0, 0, 0, 0,
57
+		"If you want to add the Retry-After header field in 5xx replies, set this parameter to a value grater than zero"},
58
+	{"case_sensitive",	CFG_VAR_INT | CFG_ATOMIC,	0, 0, 0, 0,
59
+		"If set to 1 then AOR comparison will be case sensitive. Recommended and default is 0, case insensitive"},
60
+	{0, 0, 0, 0, 0, 0}
61
+};
0 62
new file mode 100644
... ...
@@ -0,0 +1,42 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * SIP-router is distributed in the hope that it will be useful,
5
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7
+ * GNU General Public License for more details.
8
+ *
9
+ * You should have received a copy of the GNU General Public License 
10
+ * along with this program; if not, write to the Free Software 
11
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
12
+ */
13
+
14
+/*!
15
+ * \file 
16
+ * \brief Registrar :: Configuration
17
+ * \ingroup registrar
18
+ */
19
+
20
+
21
+#ifndef _REGISTRAR_CONFIG_H
22
+#define _REGISTRAR_CONFIG_H
23
+
24
+
25
+#include "../../cfg/cfg.h"
26
+#include "../../str.h"
27
+
28
+struct cfg_group_registrar {
29
+	unsigned int	default_expires;
30
+	unsigned int	min_expires;
31
+	unsigned int	max_expires;
32
+	unsigned int	max_contacts;
33
+	unsigned int	retry_after;
34
+	unsigned int	case_sensitive;
35
+};
36
+
37
+extern struct cfg_group_registrar	default_registrar_cfg;
38
+extern void	*registrar_cfg;
39
+extern cfg_def_t	registrar_cfg_def[];
40
+
41
+
42
+#endif
... ...
@@ -76,7 +76,7 @@
76 76
 #include "regpv.h"
77 77
 #include "reply.h"
78 78
 #include "reg_mod.h"
79
-
79
+#include "config.h"
80 80
 
81 81
 MODULE_VERSION
82 82
 
... ...
@@ -95,17 +95,10 @@ static int fetchc_fixup(void** param, int param_no);
95 95
 static int add_sock_hdr(struct sip_msg* msg, char *str, char *foo);
96 96
 
97 97
 
98
-int default_expires = 3600; 			/*!< Default expires value in seconds */
99 98
 qvalue_t default_q  = Q_UNSPECIFIED;		/*!< Default q value multiplied by 1000 */
100 99
 int append_branches = 1;			/*!< If set to 1, lookup will put all contacts found in msg structure */
101 100
 int case_sensitive  = 0;			/*!< If set to 1, username in aor will be case sensitive */
102 101
 int tcp_persistent_flag = -1;			/*!< if the TCP connection should be kept open */
103
-int min_expires     = 60;			/*!< Minimum expires the phones are allowed to use in seconds
104
- 						 * use 0 to switch expires checking off */
105
-int max_expires     = 0;			/*!< Maximum expires the phones are allowed to use in seconds,
106
- 						 * use 0 to switch expires checking off */
107
-int max_contacts = 0;				/*!< Maximum number of contacts per AOR (0=no checking) */
108
-int retry_after = 0;				/*!< The value of Retry-After HF in 5xx replies */
109 102
 int method_filtering = 0;			/*!< if the looked up contacts should be filtered based on supported methods */
110 103
 int path_enabled = 0;				/*!< if the Path HF should be handled */
111 104
 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[] = {
189 182
  * Exported parameters
190 183
  */
191 184
 static param_export_t params[] = {
192
-	{"default_expires",    INT_PARAM, &default_expires     },
193
-	{"default_q",          INT_PARAM, &default_q           },
194
-	{"append_branches",    INT_PARAM, &append_branches     },
195
-	{"case_sensitive",     INT_PARAM, &case_sensitive      },
185
+	{"default_expires",    INT_PARAM, &default_registrar_cfg.default_expires     	},
186
+	{"default_q",          INT_PARAM, &default_q           				},
187
+	{"append_branches",    INT_PARAM, &append_branches     				},
188
+	{"case_sensitive",     INT_PARAM, &default_registrar_cfg.case_sensitive		},
196 189
 	/*	{"tcp_persistent_flag",INT_PARAM, &tcp_persistent_flag }, */
197
-	{"realm_prefix",       STR_PARAM, &realm_pref          },
198
-	{"min_expires",        INT_PARAM, &min_expires         },
199
-	{"max_expires",        INT_PARAM, &max_expires         },
200
-	{"received_param",     STR_PARAM, &rcv_param           },
201
-	{"received_avp",       STR_PARAM, &rcv_avp_param       },
202
-	{"aor_avp",            STR_PARAM, &aor_avp_param       },
203
-	{"reg_callid_avp",     STR_PARAM, &reg_callid_avp_param},
204
-	{"max_contacts",       INT_PARAM, &max_contacts        },
205
-	{"retry_after",        INT_PARAM, &retry_after         },
206
-	{"sock_flag",          INT_PARAM, &sock_flag           },
207
-	{"sock_hdr_name",      STR_PARAM, &sock_hdr_name.s     },
208
-	{"method_filtering",   INT_PARAM, &method_filtering    },
209
-	{"use_path",           INT_PARAM, &path_enabled        },
210
-	{"path_mode",          INT_PARAM, &path_mode           },
211
-	{"path_use_received",  INT_PARAM, &path_use_params     },
190
+	{"realm_prefix",       STR_PARAM, &realm_pref          				},
191
+	{"min_expires",        INT_PARAM, &default_registrar_cfg.min_expires		},
192
+	{"max_expires",        INT_PARAM, &default_registrar_cfg.max_expires		},
193
+	{"received_param",     STR_PARAM, &rcv_param           				},
194
+	{"received_avp",       STR_PARAM, &rcv_avp_param       				},
195
+	{"aor_avp",            STR_PARAM, &aor_avp_param       				},
196
+	{"reg_callid_avp",     STR_PARAM, &reg_callid_avp_param				},
197
+	{"max_contacts",       INT_PARAM, &default_registrar_cfg.max_contacts		},
198
+	{"retry_after",        INT_PARAM, &default_registrar_cfg.retry_after		},
199
+	{"sock_flag",          INT_PARAM, &sock_flag           				},
200
+	{"sock_hdr_name",      STR_PARAM, &sock_hdr_name.s     				},
201
+	{"method_filtering",   INT_PARAM, &method_filtering    				},
202
+	{"use_path",           INT_PARAM, &path_enabled        				},
203
+	{"path_mode",          INT_PARAM, &path_mode           				},
204
+	{"path_use_received",  INT_PARAM, &path_use_params     				},
212 205
 	{0, 0, 0}
213 206
 };
214 207
 
... ...
@@ -224,6 +217,7 @@ stat_export_t mod_stats[] = {
224 217
 };
225 218
 
226 219
 
220
+
227 221
 /*! \brief
228 222
  * Module exports structure
229 223
  */
... ...
@@ -271,6 +265,13 @@ static int mod_init(void)
271 265
 	realm_prefix.len = strlen(realm_pref);
272 266
 
273 267
 	rcv_param.len = strlen(rcv_param.s);
268
+	
269
+	if(cfg_declare("registrar", registrar_cfg_def, &default_registrar_cfg, cfg_sizeof(registrar), &registrar_cfg)){
270
+		LM_ERR("Fail to declare the configuration\n");
271
+	        return -1;
272
+	}
273
+	                                                
274
+	                                                
274 275
 
275 276
 	if (rcv_avp_param && *rcv_avp_param) {
276 277
 		s.s = rcv_avp_param; s.len = strlen(s.s);
... ...
@@ -378,9 +379,10 @@ static int child_init(int rank)
378 379
 {
379 380
 	if (rank==1) {
380 381
 		/* init stats */
381
-		update_stat( max_expires_stat, max_expires );
382
-		update_stat( max_contacts_stat, max_contacts );
383
-		update_stat( default_expire_stat, default_expires );
382
+		//TODO if parameters are modified via cfg framework do i change them?
383
+		update_stat( max_expires_stat, default_registrar_cfg.max_expires );
384
+		update_stat( max_contacts_stat, default_registrar_cfg.max_contacts );
385
+		update_stat( default_expire_stat, default_registrar_cfg.default_expires );
384 386
 	}
385 387
 
386 388
 	return 0;
... ...
@@ -73,8 +73,6 @@ extern int append_branches;
73 73
 extern int case_sensitive;
74 74
 extern int nat_flag;
75 75
 extern int tcp_persistent_flag;
76
-extern int min_expires;
77
-extern int max_expires;
78 76
 extern int received_avp;
79 77
 extern int reg_use_domain;
80 78
 extern str realm_prefix;
... ...
@@ -88,8 +86,6 @@ extern unsigned short reg_callid_avp_type;
88 86
 extern int_str reg_callid_avp_name;
89 87
 
90 88
 extern str rcv_param;
91
-extern int max_contacts;
92
-extern int retry_after;
93 89
 extern int method_filtering;
94 90
 extern int path_enabled;
95 91
 extern int path_mode;
... ...
@@ -45,7 +45,7 @@
45 45
 #include "reg_mod.h"
46 46
 #include "regtime.h"
47 47
 #include "reply.h"
48
-
48
+#include "config.h"
49 49
 
50 50
 #define MAX_CONTACT_BUFFER 1024
51 51
 
... ...
@@ -308,7 +308,7 @@ static int add_retry_after(struct sip_msg* _m)
308 308
 	char* buf, *ra_s;
309 309
  	int ra_len;
310 310
  	
311
- 	ra_s = int2str(retry_after, &ra_len);
311
+ 	ra_s = int2str(cfg_get(registrar, registrar_cfg, retry_after), &ra_len);
312 312
  	buf = (char*)pkg_malloc(RETRY_AFTER_LEN + ra_len + CRLF_LEN);
313 313
  	if (!buf) {
314 314
  		LM_ERR("no pkg memory left\n");
... ...
@@ -420,7 +420,7 @@ int reg_send_reply(struct sip_msg* _m)
420 420
 		add_lump_rpl( _m, buf, E_INFO_LEN + error_info[rerrno].len + CRLF_LEN,
421 421
 			LUMP_RPL_HDR|LUMP_RPL_NODUP);
422 422
 
423
-		if (code >= 500 && code < 600 && retry_after) {
423
+		if (code >= 500 && code < 600 && cfg_get(registrar, registrar_cfg, retry_after)) {
424 424
 			if (add_retry_after(_m) < 0) {
425 425
 				return -1;
426 426
 			}
... ...
@@ -71,6 +71,7 @@
71 71
 #include "regtime.h"
72 72
 #include "path.h"
73 73
 #include "save.h"
74
+#include "config.h"
74 75
 
75 76
 static int mem_only = 0;
76 77
 
... ...
@@ -396,7 +397,7 @@ static inline int insert_contacts(struct sip_msg* _m, contact_t* _c,
396 397
 		if (expires == 0)
397 398
 			continue;
398 399
 
399
-		if (max_contacts && (num >= max_contacts)) {
400
+		if (cfg_get(registrar, registrar_cfg, max_contacts) && (num >= cfg_get(registrar, registrar_cfg, max_contacts))) {
400 401
 			LM_INFO("too many contacts (%d) for AOR <%.*s>\n", 
401 402
 					num, _a->len, _a->s);
402 403
 			rerrno = R_TOO_MANY;
... ...
@@ -511,7 +512,7 @@ static int test_max_contacts(struct sip_msg* _m, urecord_t* _r, contact_t* _c,
511 512
 	}
512 513
 	
513 514
 	LM_DBG("%d contacts after commit\n", num);
514
-	if (num > max_contacts) {
515
+	if (num > cfg_get(registrar, registrar_cfg, max_contacts)) {
515 516
 		LM_INFO("too many contacts for AOR <%.*s>\n", _r->aor.len, _r->aor.s);
516 517
 		rerrno = R_TOO_MANY;
517 518
 		return -1;
... ...
@@ -555,7 +556,7 @@ static inline int update_contacts(struct sip_msg* _m, urecord_t* _r,
555 556
 		goto error;
556 557
 	}
557 558
 
558
-	if (max_contacts && test_max_contacts(_m, _r, _c, ci) != 0 )
559
+	if (cfg_get(registrar, registrar_cfg, max_contacts) && test_max_contacts(_m, _r, _c, ci) != 0 )
559 560
 		goto error;
560 561
 
561 562
 #ifdef USE_TCP
... ...
@@ -779,16 +780,18 @@ int save(struct sip_msg* _m, char* _d, char* _cflags)
779 780
 			goto error;
780 781
 		ret = (ret==0)?1:ret;
781 782
 	}
782
-
783
+#ifdef STATISTICS
783 784
 	update_stat(accepted_registrations, 1);
785
+#endif
784 786
 	/* Only send reply upon request, not upon reply */
785 787
 	if ((route_type == REQUEST_ROUTE) && !is_cflag_set(REG_SAVE_NORPL_FL) && (reg_send_reply(_m) < 0))
786 788
 		return -1;
787 789
 
788 790
 	return ret;
789 791
 error:
792
+#ifdef STATISTICS
790 793
 	update_stat(rejected_registrations, 1);
791
-
794
+#endif
792 795
 	if ((route_type == REQUEST_ROUTE) && !is_cflag_set(REG_SAVE_NORPL_FL) )
793 796
 		reg_send_reply(_m);
794 797
 
... ...
@@ -39,7 +39,7 @@
39 39
 #include "regtime.h"                     /* act_time */
40 40
 #include "rerrno.h"
41 41
 #include "sip_msg.h"
42
-
42
+#include "config.h"
43 43
 
44 44
 static struct hdr_field* act_contact;
45 45
 
... ...
@@ -60,9 +60,9 @@ static inline int get_expires_hf(struct sip_msg* _m)
60 60
 			if (p->val != 0) {
61 61
 				return p->val + act_time;
62 62
 			} else return 0;
63
-		} else return act_time + default_expires;
63
+		} else return act_time + cfg_get(registrar, registrar_cfg, default_expires);
64 64
 	} else {
65
-		return act_time + default_expires;
65
+		return act_time + cfg_get(registrar, registrar_cfg, default_expires);
66 66
 	}
67 67
 }
68 68
 
... ...
@@ -237,18 +237,18 @@ void calc_contact_expires(struct sip_msg* _m, param_t* _ep, int* _e)
237 237
 		*_e = get_expires_hf(_m);
238 238
 	} else {
239 239
 		if (str2int(&_ep->body, (unsigned int*)_e) < 0) {
240
-			*_e = default_expires;
240
+			*_e = cfg_get(registrar, registrar_cfg, default_expires);
241 241
 		}
242 242
 		/* Convert to absolute value */
243 243
 		if (*_e != 0) *_e += act_time;
244 244
 	}
245 245
 
246
-	if ((*_e != 0) && ((*_e - act_time) < min_expires)) {
247
-		*_e = min_expires + act_time;
246
+	if ((*_e != 0) && ((*_e - act_time) < cfg_get(registrar, registrar_cfg, min_expires))) {
247
+		*_e = cfg_get(registrar, registrar_cfg, min_expires) + act_time;
248 248
 	}
249 249
 
250
-	if ((*_e != 0) && max_expires && ((*_e - act_time) > max_expires)) {
251
-		*_e = max_expires + act_time;
250
+	if ((*_e != 0) && cfg_get(registrar, registrar_cfg, max_expires) && ((*_e - act_time) > cfg_get(registrar, registrar_cfg, max_expires))) {
251
+		*_e = cfg_get(registrar, registrar_cfg, max_expires) + act_time;
252 252
 	}
253 253
 }
254 254