Browse code

Merge remote branch 'origin/sr_3.0'

latest sr_3.0 and kamailio_3.0 backports

* origin/sr_3.0: (90 commits)
usrloc(k): rpc version for ul.dump
core: init the len of sock_str attribute
ctl: added parameters to control buffer size
htable(k): rpc commad to dump htable
pv(k): fixed $shv engine
sanity: print From hdr in warning message
nathelper(k): print bad contact uri
core: more verbose when error parsing hdr
kamailio.cfg: updated cfg with sample PSTN gw routing
nathelper(k): fix for swap scenario
presence(k): aliased MLA to SLA
core: event parsing enahced for dialog;ma
tm: documented t_relay_to() function
tm: added t_relay_to(proxy, flags)
tm: documented t_replicate() functions
tm: t_replicate() can take avps & select as params
tm: added t_replicate(uri)
db_oracle: use PARAM_TYPE_MASK when checking param type
snmpstats: fix param type checking
core: define module_loaded to use find_module_by_name
...

Conflicts:
lib/srdb1/schema/dr_gateways.xml
lib/srdb1/schema/dr_groups.xml
lib/srdb1/schema/dr_gw_lists.xml
lib/srdb1/schema/dr_rules.xml
lib/srdb1/schema/kamailio-drouting.xml
modules/ctl/README
modules/topoh/README
modules_k/uac/auth.c
modules_k/uac/uac_send.c
parser/parse_param.h
utils/kamctl/mysql/drouting-create.sql
utils/kamctl/oracle/drouting-create.sql
utils/kamctl/postgres/drouting-create.sql

Andrei Pelinescu-Onciul authored on 27/01/2010 10:31:25
Showing 96 changed files
... ...
@@ -11,6 +11,7 @@ modules.lst
11 11
 *.d
12 12
 # ignore binary files and objects
13 13
 ser
14
+kamailio
14 15
 *.so
15 16
 *.so.*
16 17
 *.o
... ...
@@ -194,6 +194,71 @@ module_group_stable=cpl-c dbtext jabber osp sms pdb
194 194
 # not have dependencies
195 195
 module_group_experimental=tls oracle iptrtpproxy
196 196
 
197
+# Kamailio specific groups
198
+# Standard modules in K Debian distro
199
+module_group_kstandard=acc alias_db auth auth_db benchmark call_control \
200
+				cfgutils db_text dialog dispatcher diversion domain drouting \
201
+				exec group htable imc kex maxfwd mi_datagram mi_fifo msilo \
202
+				nat_traversal nathelper path pdt permissions pike pv qos \
203
+				ratelimit regex registrar rr rtimer siptrace siputils sl sms \
204
+				speeddial sqlops sst statistics textops tmx uac uac_redirect \
205
+				uri_db userblacklist usrloc xlog \
206
+				avpops cfg_db cfg_rpc ctl db_flatstore dialplan enum \
207
+				iptrtpproxy lcr mediaproxy mi_rpc pdb sanity tm topoh
208
+
209
+# K mysql module
210
+module_group_kmysql=db_mysql
211
+
212
+# K postgress module
213
+module_group_kpostgres=db_postgres
214
+
215
+# K cpl module
216
+module_group_kcpl=cpl-c
217
+
218
+# K radius modules
219
+module_group_kradius=auth_radius misc_radius peering
220
+
221
+# K unixodbc module
222
+module_group_kunixodbc=db_unixodbc
223
+
224
+# K xmlrpc modules
225
+module_group_kxmlrpc=xmlrpc mi_xmlrpc
226
+
227
+# K perl module
228
+module_group_kperl=perl perlvdb
229
+
230
+# K snmpstats module
231
+module_group_ksnmpstats=snmpstats
232
+
233
+# K xmpp module
234
+module_group_kxmpp=xmpp
235
+
236
+# K carrierroute module
237
+module_group_kcarrierroute=carrierroute
238
+
239
+# K berkeley module
240
+module_group_kberkeley=db_berkeley
241
+
242
+# K ldap modules
243
+module_group_kldap=ldap h350
244
+
245
+# K utils module
246
+module_group_kutils=utils
247
+
248
+# K purple module
249
+module_group_kpurple=purple
250
+
251
+# K memcached module
252
+module_group_kmemcached=memcached
253
+
254
+# K tls module
255
+module_group_ktls=tls
256
+
257
+# K presence modules
258
+module_group_kpresence=presence presence_dialoginfo presence_mwi presence_xml \
259
+						pua pua_bla pua_dialoginfo pua_mi pua_usrloc pua_xmpp \
260
+						rls xcap_client
261
+
197 262
 # if not set on the cmd. line, env or in the modules.lst (cfg_group_include)
198 263
 # exclude the below modules.
199 264
 ifneq ($(group_include)$(cfg_group_include),)
... ...
@@ -953,7 +1018,8 @@ install-sr-man: $(man_prefix)/$(man_dir)/man8 $(man_prefix)/$(man_dir)/man5
953 953
 			$(foreach m,$(modules_dirs),\
954 954
 				-e "s#/usr/lib/$(CFG_NAME)/$(m)\([^_]\)#$($(m)_target)\1#g") \
955 955
 			-e "s#/usr/share/doc/$(CFG_NAME)/#$(doc_target)#g" \
956
-			< $(CFG_NAME).8 >  \
956
+			-e "s#$(SRC_NAME)#$(MAIN_NAME)#g" \
957
+			< $(SRC_NAME).8 >  \
957 958
 							$(man_prefix)/$(man_dir)/man8/$(MAIN_NAME).8
958 959
 		@chmod 644  $(man_prefix)/$(man_dir)/man8/$(MAIN_NAME).8
959 960
 		@sed -e "s#/etc/$(CFG_NAME)/$(CFG_NAME)\.cfg#$(cfg_target)$(MAIN_NAME).cfg#g" \
... ...
@@ -961,7 +1027,8 @@ install-sr-man: $(man_prefix)/$(man_dir)/man8 $(man_prefix)/$(man_dir)/man5
961 961
 			$(foreach m,$(modules_dirs),\
962 962
 				-e "s#/usr/lib/$(CFG_NAME)/$(m)\([^_]\)#$($(m)_target)\1#g") \
963 963
 			-e "s#/usr/share/doc/$(CFG_NAME)/#$(doc_target)#g" \
964
-			< $(CFG_NAME).cfg.5 >  \
964
+			-e "s#$(SRC_NAME)#$(MAIN_NAME)#g" \
965
+			< $(SRC_NAME).cfg.5 >  \
965 966
 			$(man_prefix)/$(man_dir)/man5/$(MAIN_NAME).cfg.5
966 967
 		@chmod 644  $(man_prefix)/$(man_dir)/man5/$(MAIN_NAME).cfg.5
967 968
 
... ...
@@ -134,6 +134,8 @@ endif
134 134
 CFG_NAME=sip-router
135 135
 #config name/name-prefix for distributed scripts
136 136
 SCR_NAME=sip-router
137
+#name in source tree
138
+SRC_NAME=sip-router
137 139
 
138 140
 # what to install
139 141
 INSTALL_FLAVOUR=$(FLAVOUR)
... ...
@@ -210,30 +212,36 @@ lib_dir = lib/$(MAIN_NAME)/
210 210
 ifeq ($(OS), linux)
211 211
 	doc_dir = share/doc/$(MAIN_NAME)/
212 212
 	man_dir = share/man/
213
+	data_dir = share/$(MAIN_NAME)/
213 214
 	LOCALBASE ?= /usr/local
214 215
 else
215 216
 ifeq ($(OS), freebsd)
216 217
 	doc_dir = share/doc/$(MAIN_NAME)/
217 218
 	man_dir = man/
219
+	data_dir = share/$(MAIN_NAME)/
218 220
 	LOCALBASE ?= /usr/local
219 221
 else
220 222
 ifeq ($(OS), openbsd)
221 223
 	doc_dir = share/doc/$(MAIN_NAME)/
222 224
 	man_dir = man/
225
+	data_dir = share/$(MAIN_NAME)/
223 226
 	LOCALBASE ?= /usr/local
224 227
 else
225 228
 ifeq ($(OS), netbsd)
226 229
 	doc_dir = share/doc/$(MAIN_NAME)/
227 230
 	man_dir = man/
231
+	data_dir = share/$(MAIN_NAME)/
228 232
 	LOCALBASE ?= /usr/pkg
229 233
 else
230 234
 ifeq ($(OS), darwin)
231 235
 	doc_dir = share/doc/$(MAIN_NAME)/
232 236
 	man_dir = man/
237
+	data_dir = share/$(MAIN_NAME)/
233 238
 	LOCALBASE ?= /usr/local
234 239
 else
235 240
 	doc_dir = doc/$(MAIN_NAME)/
236 241
 	man_dir = man/
242
+	data_dir = $(MAIN_NAME)/
237 243
 	LOCALBASE ?= /usr/local
238 244
 endif
239 245
 endif
... ...
@@ -265,6 +273,7 @@ doc_prefix = $(basedir)$(prefix)
265 265
 man_prefix = $(basedir)$(prefix)
266 266
 ut_prefix = $(basedir)$(prefix)
267 267
 share_prefix = $(basedir)$(prefix)
268
+data_prefix = $(basedir)$(prefix)
268 269
 
269 270
 
270 271
 # target dirs for various stuff
... ...
@@ -273,6 +282,7 @@ bin_target = $(prefix)/$(bin_dir)
273 273
 #modules_target = $(prefix)/$(modules_dir)
274 274
 lib_target = $(prefix)/$(lib_dir)
275 275
 doc_target = $(prefix)/$(doc_dir)
276
+data_target = $(prefix)/$(data_dir)
276 277
 
277 278
 
278 279
 
... ...
@@ -1919,7 +1929,7 @@ export exported_vars
1919 1919
 #  cannot be overwritten from environment or command line, unless make cfg
1920 1920
 #  is run)
1921 1921
 saved_fixed_vars:=	MAIN_NAME  CFG_NAME SCR_NAME FLAVOUR INSTALL_FLAVOUR \
1922
-		RELEASE OS ARCH \
1922
+		SRC_NAME RELEASE OS ARCH \
1923 1923
 		C_DEFS DEFS_RM PROFILE CC LD MKDEP MKTAGS LDFLAGS C_INCLUDES \
1924 1924
 		MOD_LDFLAGS LIB_LDFLAGS UTILS_LDFLAGS LIB_SONAME LD_RPATH \
1925 1925
 		LIB_SUFFIX LIB_PREFIX \
... ...
@@ -1939,8 +1949,8 @@ saved_chg_vars:=\
1939 1939
 		PREFIX prefix\
1940 1940
 		cfg_prefix cfg_dir bin_prefix bin_dir modules_prefix modules_dir \
1941 1941
 		doc_prefix doc_dir man_prefix man_dir ut_prefix ut_dir \
1942
-		share_prefix share_dir lib_prefix lib_dir \
1943
-		cfg_target lib_target
1942
+		share_prefix share_dir lib_prefix lib_dir data_prefix data_dir \
1943
+		cfg_target lib_target data_target
1944 1944
 
1945 1945
 
1946 1946
 #export relevant variables to the sub-makes
... ...
@@ -64,6 +64,8 @@ config script changes:
64 64
   - while()
65 65
   - include file support: include_file "somefile"
66 66
   - event route support: event_route[module_name:eventid]
67
+  - user and shm_force_alloc must now appear prior to any modparam() or route
68
+     block.
67 69
 
68 70
 build system:
69 71
   - multiple modules directories are now supported (defined in Makefile.dirs)
... ...
@@ -79,6 +81,9 @@ new config variables:
79 79
            compiled with DBG_QM_MALLOC or DBG_F_MALLOC).
80 80
        Default: 1.
81 81
        Can be changed at runtime.
82
+  - shm = number  or shm_mem = number  - size of shared memory in MB. It's
83
+       overwritten if a value is specified on the command line (-m val).
84
+       Default: 32 Mb. Must appear prior to any modparam() or route block.
82 85
 
83 86
 new script commands:
84 87
   add_local_rport() - adds the rport parameter to the added via header
... ...
@@ -80,6 +80,7 @@
80 80
  *  2009-03-10  added SET_USERPHONE action (Miklos)
81 81
  *  2009-04-24  add strlen, strempty and defined operators (andrei)
82 82
  *  2009-03-07  RETCODE, it's now  a core pvar (andrei)
83
+ *  2010-01-10  added SHM_MEM_SZ (andrei)
83 84
 */
84 85
 
85 86
 
... ...
@@ -106,6 +107,12 @@
106 106
 	#define PVAR_P_S                7  /* pvar: $(...)  or $foo(...)*/
107 107
 	#define PVARID_S                8  /* $foo.bar...*/
108 108
 	#define STR_BETWEEN_S		9
109
+	#define LINECOMMENT_S            10
110
+	#define DEFINE_S                11
111
+	#define DEFINE_EOL_S            12
112
+	#define IFDEF_S                    13
113
+	#define IFDEF_EOL_S                14
114
+	#define IFDEF_SKIP_S            15
109 115
 
110 116
 	#define STR_BUF_ALLOC_UNIT	128
111 117
 	struct str_buf{
... ...
@@ -153,11 +160,19 @@
153 153
 		struct sr_yy_fname *next;
154 154
 	} *sr_yy_fname_list = 0;
155 155
 
156
+	static int  pp_define(int len, const char * text);
157
+	static int  pp_ifdef_type(int pos);
158
+	static void pp_ifdef_var(int len, const char * text);
159
+	static void pp_ifdef();
160
+	static void pp_else();
161
+	static void pp_endif();
162
+
156 163
 %}
157 164
 
158 165
 /* start conditions */
159 166
 %x STRING1 STRING2 STR_BETWEEN COMMENT COMMENT_LN ATTR SELECT AVP_PVAR PVAR_P 
160 167
 %x PVARID INCLF
168
+%x LINECOMMENT DEFINE_ID DEFINE_EOL IFDEF_ID IFDEF_EOL IFDEF_SKIP
161 169
 
162 170
 /* config script types : #!SER  or #!KAMAILIO or #!MAX_COMPAT */
163 171
 SER_CFG			SER
... ...
@@ -430,6 +445,7 @@ ADVERTISED_ADDRESS	"advertised_address"
430 430
 ADVERTISED_PORT		"advertised_port"
431 431
 DISABLE_CORE		"disable_core_dump"
432 432
 OPEN_FD_LIMIT		"open_files_limit"
433
+SHM_MEM_SZ		"shm"|"shm_mem"|"shm_mem_size"
433 434
 SHM_FORCE_ALLOC		"shm_force_alloc"
434 435
 MLOCK_PAGES			"mlock_pages"
435 436
 REAL_TIME			"real_time"
... ...
@@ -475,8 +491,9 @@ TLSv1			"tlsv1"|"TLSv1"|"TLSV1"
475 475
 
476 476
 LETTER		[a-zA-Z]
477 477
 DIGIT		[0-9]
478
-ALPHANUM	{LETTER}|{DIGIT}|[_]
479
-ID			{LETTER}{ALPHANUM}*
478
+LETTER_     {LETTER}|[_]
479
+ALPHANUM    {LETTER_}|{DIGIT}
480
+ID          {LETTER_}{ALPHANUM}*
480 481
 NUM_ID		{ALPHANUM}+
481 482
 HEX			[0-9a-fA-F]
482 483
 HEXNUMBER	0x{HEX}+
... ...
@@ -507,6 +524,12 @@ COM_LINE	#
507 507
 COM_START	"/\*"
508 508
 COM_END		"\*/"
509 509
 
510
+DEFINE       define
511
+IFDEF        ifdef
512
+IFNDEF       ifndef
513
+ENDIF        endif
514
+/* else is already defined */
515
+
510 516
 EAT_ABLE	[\ \t\b\r]
511 517
 
512 518
 %%
... ...
@@ -818,6 +841,8 @@ EAT_ABLE	[\ \t\b\r]
818 818
 									return DISABLE_CORE; }
819 819
 <INITIAL>{OPEN_FD_LIMIT}		{	count(); yylval.strval=yytext;
820 820
 									return OPEN_FD_LIMIT; }
821
+<INITIAL>{SHM_MEM_SZ}		{	count(); yylval.strval=yytext;
822
+									return SHM_MEM_SZ; }
821 823
 <INITIAL>{SHM_FORCE_ALLOC}		{	count(); yylval.strval=yytext;
822 824
 									return SHM_FORCE_ALLOC; }
823 825
 <INITIAL>{MLOCK_PAGES}		{	count(); yylval.strval=yytext;
... ...
@@ -1129,7 +1154,38 @@ EAT_ABLE	[\ \t\b\r]
1129 1129
 											sr_cfg_compat=SR_COMPAT_KAMAILIO;}
1130 1130
 <INITIAL>{COM_LINE}!{MAXCOMPAT_CFG}{CR}	{ count(); 
1131 1131
 												sr_cfg_compat=SR_COMPAT_MAX;}
1132
-<INITIAL>{COM_LINE}.*{CR}	{ count(); }
1132
+
1133
+<INITIAL>{COM_LINE}!{DEFINE}{EAT_ABLE}+        { count();
1134
+										state = DEFINE_S; BEGIN(DEFINE_ID); }
1135
+<DEFINE_ID>{ID}                                { count();
1136
+								if (pp_define(yyleng, yytext)) return 1;
1137
+								state = DEFINE_EOL_S; BEGIN(DEFINE_EOL); }
1138
+<DEFINE_EOL>{EAT_ABLE}*{CR}                    { count();
1139
+									state = INITIAL; BEGIN(INITIAL); }
1140
+
1141
+<INITIAL,IFDEF_SKIP>{COM_LINE}!{IFDEF}{EAT_ABLE}+    { count();
1142
+								if (pp_ifdef_type(1)) return 1;
1143
+								state = IFDEF_S; BEGIN(IFDEF_ID); }
1144
+<INITIAL,IFDEF_SKIP>{COM_LINE}!{IFNDEF}{EAT_ABLE}+    { count();
1145
+								if (pp_ifdef_type(0)) return 1;
1146
+								state = IFDEF_S; BEGIN(IFDEF_ID); }
1147
+<IFDEF_ID>{ID}                { count();
1148
+                                pp_ifdef_var(yyleng, yytext);
1149
+                                state = IFDEF_EOL_S; BEGIN(IFDEF_EOL); }
1150
+<IFDEF_EOL>{EAT_ABLE}*{CR}    { count(); pp_ifdef(); }
1151
+
1152
+<INITIAL,IFDEF_SKIP>{COM_LINE}!{ELSE}{EAT_ABLE}*{CR}    { count(); pp_else(); }
1153
+
1154
+<INITIAL,IFDEF_SKIP>{COM_LINE}!{ENDIF}{EAT_ABLE}*{CR}    { count();
1155
+															pp_endif(); }
1156
+
1157
+ /* we're in an ifdef that evaluated to false -- throw it away */
1158
+<IFDEF_SKIP>.|{CR}    { count(); }
1159
+
1160
+ /* this is split so the shebangs match more, giving them priority */
1161
+<INITIAL>{COM_LINE}        { count(); state = LINECOMMENT_S;
1162
+								BEGIN(LINECOMMENT); }
1163
+<LINECOMMENT>.*{CR}        { count(); state = INITIAL_S; BEGIN(INITIAL); }
1133 1164
 
1134 1165
 <INITIAL>{ID}			{ count(); addstr(&s_buf, yytext, yyleng);
1135 1166
 									yylval.strval=s_buf.s;
... ...
@@ -1480,3 +1536,102 @@ static int sr_pop_yy_state()
1480 1480
 	return 0;
1481 1481
 }
1482 1482
 
1483
+/* define/ifdef support */
1484
+
1485
+#define MAX_DEFINES    1024
1486
+static str pp_defines[MAX_DEFINES];
1487
+static int pp_num_defines = 0;
1488
+
1489
+/* pp_ifdef_stack[i] is 1 if the ifdef test at depth i is either
1490
+ * ifdef(defined), ifndef(undefined), or the opposite of these
1491
+ * two, but in an else branch
1492
+ */
1493
+#define MAX_IFDEFS    128
1494
+static int pp_ifdef_stack[MAX_IFDEFS];
1495
+static int pp_sptr = 0; /* stack pointer */
1496
+
1497
+static int pp_lookup(int len, const char * text)
1498
+{
1499
+	str var = {(char *)text, len};
1500
+	int i;
1501
+
1502
+	for (i=0; i<pp_num_defines; i++)
1503
+		if (STR_EQ(pp_defines[i], var))
1504
+			return i;
1505
+
1506
+	return -1;
1507
+}
1508
+
1509
+static int pp_define(int len, const char * text)
1510
+{
1511
+	if (pp_num_defines == MAX_DEFINES) {
1512
+		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
1513
+		return -1;
1514
+	}
1515
+
1516
+	if (pp_lookup(len, text) >= 0) {
1517
+		LOG(L_CRIT, "ERROR: already defined: %.*s\n", len, text);
1518
+		return -1;
1519
+	}
1520
+
1521
+	pp_defines[pp_num_defines].len = len;
1522
+	pp_defines[pp_num_defines].s = (char*)pkg_malloc(len+1);
1523
+	memcpy(pp_defines[pp_num_defines].s, text, len);
1524
+	pp_num_defines++;
1525
+
1526
+	return 0;
1527
+}
1528
+
1529
+static int pp_ifdef_type(int type)
1530
+{
1531
+	if (pp_sptr == MAX_IFDEFS) {
1532
+		LOG(L_CRIT, "ERROR: too many nested ifdefs -- adjust MAX_IFDEFS\n");
1533
+		return -1;
1534
+	}
1535
+
1536
+	pp_ifdef_stack[pp_sptr] = type;
1537
+	return 0;
1538
+}
1539
+
1540
+/* this sets the result of the if[n]def expr:
1541
+ * ifdef  defined   -> 1
1542
+ * ifdef  undefined -> 0
1543
+ * ifndef defined   -> 0
1544
+ * ifndef undefined -> 1
1545
+ */
1546
+static void pp_ifdef_var(int len, const char * text)
1547
+{
1548
+	pp_ifdef_stack[pp_sptr] ^= (pp_lookup(len, text) < 0);
1549
+}
1550
+
1551
+static void pp_update_state()
1552
+{
1553
+	int i;
1554
+
1555
+	for (i=0; i<pp_sptr; i++)
1556
+		if (! pp_ifdef_stack[i]) {
1557
+			state = IFDEF_SKIP_S; BEGIN(IFDEF_SKIP);
1558
+			return;
1559
+		}
1560
+
1561
+	state = INITIAL; BEGIN(INITIAL);
1562
+}
1563
+
1564
+static void pp_ifdef()
1565
+{
1566
+	pp_sptr++;
1567
+	pp_update_state();
1568
+}
1569
+
1570
+static void pp_else()
1571
+{
1572
+	pp_ifdef_stack[pp_sptr-1] ^= 1;
1573
+	pp_update_state();
1574
+}
1575
+
1576
+static void pp_endif()
1577
+{
1578
+	pp_sptr--;
1579
+	pp_update_state();
1580
+}
1581
+
... ...
@@ -96,6 +96,8 @@
96 96
  * 2009-01-26  case/switch() support (andrei)
97 97
  * 2009-03-10  added SET_USERPHONE action (Miklos)
98 98
  * 2009-05-04  switched if to rval_expr (andrei)
99
+ * 2010-01-10  init shm on first mod_param or route block;
100
+ *             added SHM_MEM_SZ (andrei)
99 101
 */
100 102
 
101 103
 %{
... ...
@@ -111,6 +113,9 @@
111 111
 #include <errno.h>
112 112
 #include "route_struct.h"
113 113
 #include "globals.h"
114
+#ifdef SHM_MEM
115
+#include "shm_init.h"
116
+#endif /* SHM_MEM */
114 117
 #include "route.h"
115 118
 #include "switch.h"
116 119
 #include "dprint.h"
... ...
@@ -490,6 +495,7 @@ extern char *finame;
490 490
 %token ADVERTISED_PORT
491 491
 %token DISABLE_CORE
492 492
 %token OPEN_FD_LIMIT
493
+%token SHM_MEM_SZ
493 494
 %token SHM_FORCE_ALLOC
494 495
 %token MLOCK_PAGES
495 496
 %token REAL_TIME
... ...
@@ -867,8 +873,20 @@ assign_stm:
867 867
 	| MEMSUM EQUAL error { yyerror("int value expected"); }
868 868
 	| SIP_WARNING EQUAL NUMBER { sip_warning=$3; }
869 869
 	| SIP_WARNING EQUAL error { yyerror("boolean value expected"); }
870
-	| USER EQUAL STRING     { user=$3; }
871
-	| USER EQUAL ID         { user=$3; }
870
+	| USER EQUAL STRING     {
871
+		if (shm_initialized())
872
+			yyerror("user must be before any modparam or the"
873
+					" route blocks");
874
+		else if (user==0)
875
+			user=$3; 
876
+	}
877
+	| USER EQUAL ID         {
878
+		if (shm_initialized())
879
+			yyerror("user must be before any modparam or the"
880
+					" route blocks");
881
+		else if (user==0)
882
+			user=$3;
883
+	}
872 884
 	| USER EQUAL error      { yyerror("string value expected"); }
873 885
 	| GROUP EQUAL STRING     { group=$3; }
874 886
 	| GROUP EQUAL ID         { group=$3; }
... ...
@@ -1447,7 +1465,21 @@ assign_stm:
1447 1447
 	| DISABLE_CORE EQUAL error { yyerror("boolean value expected"); }
1448 1448
 	| OPEN_FD_LIMIT EQUAL NUMBER { open_files_limit=$3; }
1449 1449
 	| OPEN_FD_LIMIT EQUAL error { yyerror("number expected"); }
1450
-	| SHM_FORCE_ALLOC EQUAL NUMBER { shm_force_alloc=$3; }
1450
+	| SHM_MEM_SZ EQUAL NUMBER {
1451
+		if (shm_initialized())
1452
+			yyerror("shm/shm_mem_size must be before any modparam or the"
1453
+					" route blocks");
1454
+		else if (shm_mem_size == 0)
1455
+			shm_mem_size=$3 * 1024 * 1024;
1456
+	}
1457
+	| SHM_MEM_SZ EQUAL error { yyerror("number expected"); }
1458
+	| SHM_FORCE_ALLOC EQUAL NUMBER {
1459
+		if (shm_initialized())
1460
+			yyerror("shm_force_alloc must be before any modparam or the"
1461
+					" route blocks");
1462
+		else
1463
+			shm_force_alloc=$3;
1464
+	}
1451 1465
 	| SHM_FORCE_ALLOC EQUAL error { yyerror("boolean value expected"); }
1452 1466
 	| MLOCK_PAGES EQUAL NUMBER { mlock_pages=$3; }
1453 1467
 	| MLOCK_PAGES EQUAL error { yyerror("boolean value expected"); }
... ...
@@ -1560,11 +1592,23 @@ module_stm:
1560 1560
 	}
1561 1561
 	| LOADPATH EQUAL error	{ yyerror("string expected"); }
1562 1562
 	| MODPARAM LPAREN STRING COMMA STRING COMMA STRING RPAREN {
1563
+	#ifdef SHM_MEM
1564
+		if (!shm_initialized() && init_shm()<0) {
1565
+			yyerror("Can't initialize shared memory");
1566
+			YYABORT;
1567
+		}
1568
+	#endif /* SHM_MEM */
1563 1569
 		if (set_mod_param_regex($3, $5, PARAM_STRING, $7) != 0) {
1564 1570
 			 yyerror("Can't set module parameter");
1565 1571
 		}
1566 1572
 	}
1567
-        | MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
1573
+	| MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
1574
+	#ifdef SHM_MEM
1575
+		if (!shm_initialized() && init_shm()<0) {
1576
+			yyerror("Can't initialize shared memory");
1577
+			YYABORT;
1578
+		}
1579
+	#endif /* SHM_MEM */
1568 1580
 		if (set_mod_param_regex($3, $5, PARAM_INT, (void*)$7) != 0) {
1569 1581
 			 yyerror("Can't set module parameter");
1570 1582
 		}
... ...
@@ -1645,8 +1689,22 @@ route_name:		NUMBER	{
1645 1645
 ;
1646 1646
 
1647 1647
 route_stm:
1648
-	ROUTE LBRACE actions RBRACE { push($3, &main_rt.rlist[DEFAULT_RT]); }
1648
+	ROUTE LBRACE actions RBRACE {
1649
+	#ifdef SHM_MEM
1650
+		if (!shm_initialized() && init_shm()<0) {
1651
+			yyerror("Can't initialize shared memory");
1652
+			YYABORT;
1653
+		}
1654
+	#endif /* SHM_MEM */
1655
+		push($3, &main_rt.rlist[DEFAULT_RT]);
1656
+	}
1649 1657
 	| ROUTE LBRACK route_name RBRACK LBRACE actions RBRACE {
1658
+	#ifdef SHM_MEM
1659
+		if (!shm_initialized() && init_shm()<0) {
1660
+			yyerror("Can't initialize shared memory");
1661
+			YYABORT;
1662
+		}
1663
+	#endif /* SHM_MEM */
1650 1664
 		i_tmp=route_get(&main_rt, $3);
1651 1665
 		if (i_tmp==-1){
1652 1666
 			yyerror("internal error");
... ...
@@ -1662,9 +1720,21 @@ route_stm:
1662 1662
 	;
1663 1663
 failure_route_stm:
1664 1664
 	ROUTE_FAILURE LBRACE actions RBRACE {
1665
-									push($3, &failure_rt.rlist[DEFAULT_RT]);
1666
-										}
1665
+	#ifdef SHM_MEM
1666
+		if (!shm_initialized() && init_shm()<0) {
1667
+			yyerror("Can't initialize shared memory");
1668
+			YYABORT;
1669
+		}
1670
+	#endif /* SHM_MEM */
1671
+		push($3, &failure_rt.rlist[DEFAULT_RT]);
1672
+	}
1667 1673
 	| ROUTE_FAILURE LBRACK route_name RBRACK LBRACE actions RBRACE {
1674
+	#ifdef SHM_MEM
1675
+		if (!shm_initialized() && init_shm()<0) {
1676
+			yyerror("Can't initialize shared memory");
1677
+			YYABORT;
1678
+		}
1679
+	#endif /* SHM_MEM */
1668 1680
 		i_tmp=route_get(&failure_rt, $3);
1669 1681
 		if (i_tmp==-1){
1670 1682
 			yyerror("internal error");
... ...
@@ -1680,9 +1750,21 @@ failure_route_stm:
1680 1680
 	;
1681 1681
 onreply_route_stm:
1682 1682
 	ROUTE_ONREPLY LBRACE actions RBRACE {
1683
-									push($3, &onreply_rt.rlist[DEFAULT_RT]);
1684
-										}
1683
+	#ifdef SHM_MEM
1684
+		if (!shm_initialized() && init_shm()<0) {
1685
+			yyerror("Can't initialize shared memory");
1686
+			YYABORT;
1687
+		}
1688
+	#endif /* SHM_MEM */
1689
+		push($3, &onreply_rt.rlist[DEFAULT_RT]);
1690
+	}
1685 1691
 	| ROUTE_ONREPLY LBRACK route_name RBRACK LBRACE actions RBRACE {
1692
+	#ifdef SHM_MEM
1693
+		if (!shm_initialized() && init_shm()<0) {
1694
+			yyerror("Can't initialize shared memory");
1695
+			YYABORT;
1696
+		}
1697
+	#endif /* SHM_MEM */
1686 1698
 		i_tmp=route_get(&onreply_rt, $3);
1687 1699
 		if (i_tmp==-1){
1688 1700
 			yyerror("internal error");
... ...
@@ -1698,9 +1780,21 @@ onreply_route_stm:
1698 1698
 	;
1699 1699
 branch_route_stm:
1700 1700
 	ROUTE_BRANCH LBRACE actions RBRACE {
1701
-									push($3, &branch_rt.rlist[DEFAULT_RT]);
1702
-										}
1701
+	#ifdef SHM_MEM
1702
+		if (!shm_initialized() && init_shm()<0) {
1703
+			yyerror("Can't initialize shared memory");
1704
+			YYABORT;
1705
+		}
1706
+	#endif /* SHM_MEM */
1707
+		push($3, &branch_rt.rlist[DEFAULT_RT]);
1708
+	}
1703 1709
 	| ROUTE_BRANCH LBRACK route_name RBRACK LBRACE actions RBRACE {
1710
+	#ifdef SHM_MEM
1711
+		if (!shm_initialized() && init_shm()<0) {
1712
+			yyerror("Can't initialize shared memory");
1713
+			YYABORT;
1714
+		}
1715
+	#endif /* SHM_MEM */
1704 1716
 		i_tmp=route_get(&branch_rt, $3);
1705 1717
 		if (i_tmp==-1){
1706 1718
 			yyerror("internal error");
... ...
@@ -1715,9 +1809,21 @@ branch_route_stm:
1715 1715
 	| ROUTE_BRANCH error { yyerror("invalid branch_route statement"); }
1716 1716
 	;
1717 1717
 send_route_stm: ROUTE_SEND LBRACE actions RBRACE {
1718
-									push($3, &onsend_rt.rlist[DEFAULT_RT]);
1719
-												}
1718
+	#ifdef SHM_MEM
1719
+		if (!shm_initialized() && init_shm()<0) {
1720
+			yyerror("Can't initialize shared memory");
1721
+			YYABORT;
1722
+		}
1723
+	#endif /* SHM_MEM */
1724
+		push($3, &onsend_rt.rlist[DEFAULT_RT]);
1725
+	}
1720 1726
 	| ROUTE_SEND LBRACK route_name RBRACK LBRACE actions RBRACE {
1727
+	#ifdef SHM_MEM
1728
+		if (!shm_initialized() && init_shm()<0) {
1729
+			yyerror("Can't initialize shared memory");
1730
+			YYABORT;
1731
+		}
1732
+	#endif /* SHM_MEM */
1721 1733
 		i_tmp=route_get(&onsend_rt, $3);
1722 1734
 		if (i_tmp==-1){
1723 1735
 			yyerror("internal error");
... ...
@@ -1732,6 +1838,12 @@ send_route_stm: ROUTE_SEND LBRACE actions RBRACE {
1732 1732
 	| ROUTE_SEND error { yyerror("invalid onsend_route statement"); }
1733 1733
 	;
1734 1734
 event_route_stm: ROUTE_EVENT LBRACK EVENT_RT_NAME RBRACK LBRACE actions RBRACE {
1735
+	#ifdef SHM_MEM
1736
+		if (!shm_initialized() && init_shm()<0) {
1737
+			yyerror("Can't initialize shared memory");
1738
+			YYABORT;
1739
+		}
1740
+	#endif /* SHM_MEM */
1735 1741
 		i_tmp=route_get(&event_rt, $3);
1736 1742
 		if (i_tmp==-1){
1737 1743
 			yyerror("internal error");
... ...
@@ -1,117 +1,132 @@
1
+#!KAMAILIO
1 2
 #
2 3
 # $Id$
3 4
 #
4
-# Kamailio (OpenSER) SIP Server - basic configuration script
5
+# Kamailio (OpenSER) SIP Server v3.0 - basic configuration script
5 6
 #     - web: http://www.kamailio.org
6
-#     - svn: http://openser.svn.sourceforge.net/viewvc/openser/
7
+#     - git: http://sip-router.org
7 8
 #
8 9
 # Direct your questions about this file to: <users@lists.kamailio.org>
9 10
 #
10 11
 # Refer to the Core CookBook at http://www.kamailio.org/dokuwiki/doku.php
11 12
 # for an explanation of possible statements, functions and parameters.
12 13
 #
13
-# There are comments showing how to enable different features in th econfig
14
-# file. Such commented code starts with #X# where X is a letter to identify
15
-# a feature. Delete entire #X# if you want to enable that feature. Next are
16
-# sed commands that help you enable such features.
14
+# Several features can be enabled using '#!define WITH_FEATURE' directives:
17 15
 #
18
-# *** To enamble mysql execute:
19
-#     sed -i 's/#m#//g' kamailio.cfg
16
+# *** To run in debug mode: 
17
+#     - define WITH_DEBUG
20 18
 #
21
-# *** To enamble authentication execute:
19
+# *** To enable mysql: 
20
+#     - define WITH_MYSQL
21
+#
22
+# *** To enable authentication execute:
22 23
 #     - enable mysql
23
-#     sed -i 's/#a#//g' kamailio.cfg
24
+#     - define WITH_AUTH
24 25
 #     - add users using 'kamctl'
25 26
 #
26
-# *** To enamble persistent user location execute:
27
+# *** To enable persistent user location execute:
27 28
 #     - enable mysql
28
-#     sed -i 's/#u#//g' kamailio.cfg
29
+#     - define WITH_USRLOCDB
29 30
 #
30
-# *** To enamble presence server execute:
31
+# *** To enable presence server execute:
31 32
 #     - enable mysql
32
-#     sed -i 's/#p#//g' kamailio.cfg
33
+#     - define WITH_PRESENCE
33 34
 #
34
-# *** To enamble nat traversal execute:
35
-#     sed -i 's/#n#//g' kamailio.cfg
35
+# *** To enable nat traversal execute:
36
+#     - define WITH_NAT
36 37
 #     - install RTPProxy: http://www.rtpproxy.org
37 38
 #     - start RTPProxy:
38 39
 #        rtpproxy -l _your_public_ip_ -s udp:localhost:7722
39 40
 #
41
+# *** To enable PSTN gateway routing execute:
42
+#     - define WITH_PSTN
43
+#     - set the value of pstn.gw_ip
44
+#     - check route[PSTN] for regexp routing condition
45
+#
40 46
 # *** To enhance accounting execute:
41 47
 #     - enable mysql
42
-#     sed -i 's/#c#//g' kamailio.cfg
48
+#     - define WITH_ACCDB
43 49
 #     - add following columns to database
44
-# ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
45
-# ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
46
-# ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
47
-# ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
48
-# ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
49
-# ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
50
-# ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
51
-# ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
52
-# ALTER TABLE missed_call ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
53
-# ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
54
-#
50
+#!ifdef ACCDB_COMMENT
51
+  ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
52
+  ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
53
+  ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
54
+  ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
55
+  ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
56
+  ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
57
+  ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
58
+  ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
59
+  ALTER TABLE missed_call ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
60
+  ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
61
+#!endif
55 62
 
56 63
 
57 64
 ####### Global Parameters #########
58 65
 
59
-debug=3
66
+#!ifdef WITH_DEBUG
67
+debug=4
68
+log_stderror=yes
69
+#!else
70
+debug=2
60 71
 log_stderror=no
72
+#!endif
73
+
74
+memdbg=5
75
+memlog=5
76
+
61 77
 log_facility=LOG_LOCAL0
62 78
 
63 79
 fork=yes
64 80
 children=4
65 81
 
66
-/* uncomment the following lines to enable debugging */
67
-#debug=6
68
-#fork=no
69
-#log_stderror=yes
70
-
71 82
 /* uncomment the next line to disable TCP (default on) */
72 83
 #disable_tcp=yes
73 84
 
74
-/* uncomment the next line to enable the auto temporary blacklisting of 
75
-   not available destinations (default disabled) */
76
-#disable_dns_blacklist=no
77
-
78
-/* uncomment the next line to enable IPv6 lookup after IPv4 dns 
79
-   lookup failures (default disabled) */
80
-#dns_try_ipv6=yes
81
-
82 85
 /* uncomment the next line to disable the auto discovery of local aliases
83 86
    based on revers DNS on IPs (default on) */
84 87
 #auto_aliases=no
85 88
 
86
-/* uncomment the following lines to enable TLS support  (default off) */
87
-#disable_tls = no
88
-#listen = tls:your_IP:5061
89
-#tls_verify_server = 1
90
-#tls_verify_client = 1
91
-#tls_require_client_certificate = 0
92
-#tls_method = TLSv1
93
-#tls_certificate = "/usr/local/etc/kamailio/tls/user/user-cert.pem"
94
-#tls_private_key = "/usr/local/etc/kamailio/tls/user/user-privkey.pem"
95
-#tls_ca_list     = "/usr/local/etc/kamailio/tls/user/user-calist.pem"
96
-
97
-
98 89
 port=5060
99 90
 
100 91
 /* uncomment and configure the following line if you want Kamailio to 
101 92
    bind on a specific interface/port/proto (default bind on all available) */
102
-#listen=udp:192.168.1.2:5060
93
+#listen=udp:10.0.0.10:5060
94
+
95
+
96
+####### Custom Parameters #########
97
+
98
+# These parameters can be modified runtime via RPC interface
99
+# - see the documentation of 'cfg_rpc' module.
100
+#
101
+# Format: group.id = value 'desc' description
102
+# Access: $sel(cfg_get.group.id) or @cfg_get.group.id
103
+#
104
+
105
+#!ifdef WITH_PSTN
106
+# PSTN GW Routing
107
+#
108
+# - pstn.gw_ip: valid IP or hostname as string value, example:
109
+# pstn.gw_ip = "10.0.0.101" desc "My PSTN GW Address"
110
+#
111
+# - by default is empty to avoid misrouting
112
+pstn.gw_ip = "" desc "PSTN GW Address"
113
+#!endif
103 114
 
104 115
 
105 116
 ####### Modules Section ########
106 117
 
107 118
 #set module path
108
-mpath="/usr/local/lib/kamailio/modules/"
119
+mpath="/usr/local/lib/kamailio/modules_k/:/usr/local/lib/kamailio/modules/"
109 120
 
110 121
 /* uncomment next line for MySQL DB support */
111
-#m#loadmodule "db_mysql.so"
122
+#!ifdef WITH_MYSQL
123
+loadmodule "db_mysql.so"
124
+#!endif
112 125
 loadmodule "mi_fifo.so"
113
-loadmodule "sl.so"
126
+loadmodule "kex.so"
114 127
 loadmodule "tm.so"
128
+loadmodule "tmx.so"
129
+loadmodule "sl.so"
115 130
 loadmodule "rr.so"
116 131
 loadmodule "pv.so"
117 132
 loadmodule "maxfwd.so"
... ...
@@ -121,11 +136,14 @@ loadmodule "textops.so"
121 121
 loadmodule "uri_db.so"
122 122
 loadmodule "siputils.so"
123 123
 loadmodule "xlog.so"
124
+loadmodule "sanity.so"
125
+loadmodule "ctl.so"
126
+loadmodule "mi_rpc.so"
124 127
 loadmodule "acc.so"
125
-/* uncomment next lines for MySQL based authentication support 
126
-   NOTE: a DB (like db_mysql) module must be also loaded */
127
-#a#loadmodule "auth.so"
128
-#a#loadmodule "auth_db.so"
128
+#!ifdef WITH_AUTH
129
+loadmodule "auth.so"
130
+loadmodule "auth_db.so"
131
+#!endif
129 132
 /* uncomment next line for aliases support
130 133
    NOTE: a DB (like db_mysql) module must be also loaded */
131 134
 #loadmodule "alias_db.so"
... ...
@@ -134,12 +152,14 @@ loadmodule "acc.so"
134 134
    NOTE: be sure and enable multi-domain support in all used modules
135 135
          (see "multi-module params" section ) */
136 136
 #loadmodule "domain.so"
137
-/* uncomment the next two lines for presence server support
138
-   NOTE: a DB (like db_mysql) module must be also loaded */
139
-#p#loadmodule "presence.so"
140
-#p#loadmodule "presence_xml.so"
137
+#!ifdef WITH_PRESENCE
138
+loadmodule "presence.so"
139
+loadmodule "presence_xml.so"
140
+#!endif
141 141
 
142
-#n#loadmodule "nathelper.so"
142
+#!ifdef WITH_NAT
143
+loadmodule "nathelper.so"
144
+#!endif
143 145
 
144 146
 # ----------------- setting module-specific parameters ---------------
145 147
 
... ...
@@ -185,31 +205,33 @@ modparam("acc", "log_flag", 1)
185 185
 modparam("acc", "log_missed_flag", 2)
186 186
 modparam("acc", "log_extra", 
187 187
 	"src_user=$fU;src_domain=$fd;dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
188
-/* uncomment the following lines to enable DB accounting also */
189
-#c#modparam("acc", "db_flag", 1)
190
-#c#modparam("acc", "db_missed_flag", 2)
191
-#c#modparam("acc", "db_url",
192
-#c#	"mysql://openser:openserrw@localhost/openser")
193
-#c#modparam("acc", "db_extra",
194
-#c#	"src_user=$fU;src_domain=$fd;dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
195
-
188
+/* enhanced DB accounting */
189
+#!ifdef WITH_ACCDB
190
+modparam("acc", "db_flag", 1)
191
+modparam("acc", "db_missed_flag", 2)
192
+modparam("acc", "db_url",
193
+	"mysql://openser:openserrw@localhost/openser")
194
+modparam("acc", "db_extra",
195
+	"src_user=$fU;src_domain=$fd;dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
196
+#!endif
196 197
 
197 198
 # ----- usrloc params -----
198
-/* uncomment the following lines if you want to enable DB persistency
199
-   for location entries */
200
-#u#modparam("usrloc", "db_mode",   2)
201
-#u#modparam("usrloc", "db_url",
202
-#u#	"mysql://openser:openserrw@localhost/openser")
199
+/* enable DB persistency for location entries */
200
+#!ifdef WITH_USRLOCDB
201
+modparam("usrloc", "db_mode",   2)
202
+modparam("usrloc", "db_url",
203
+	"mysql://openser:openserrw@localhost/openser")
204
+#!endif
203 205
 
204 206
 # ----- auth_db params -----
205
-/* uncomment the following lines if you want to enable the DB based
206
-   authentication */
207
-#a#modparam("auth_db", "calculate_ha1", yes)
208
-#a#modparam("auth_db", "password_column", "password")
209
-#a#modparam("auth_db", "db_url",
210
-#a#	"mysql://openser:openserrw@localhost/openser")
211
-#a#modparam("auth_db", "load_credentials", "")
212
-
207
+/* enable the DB based authentication */
208
+#!ifdef WITH_AUTH
209
+modparam("auth_db", "calculate_ha1", yes)
210
+modparam("auth_db", "password_column", "password")
211
+modparam("auth_db", "db_url",
212
+	"mysql://openser:openserrw@localhost/openser")
213
+modparam("auth_db", "load_credentials", "")
214
+#!endif
213 215
 
214 216
 # ----- alias_db params -----
215 217
 /* uncomment the following lines if you want to enable the DB based
... ...
@@ -233,20 +255,24 @@ modparam("acc", "log_extra",
233 233
 
234 234
 
235 235
 # ----- presence params -----
236
-/* uncomment the following lines if you want to enable presence */
237
-#p#modparam("presence|presence_xml", "db_url",
238
-#p#	"mysql://openser:openserrw@localhost/openser")
239
-#p#modparam("presence_xml", "force_active", 1)
240
-#p#modparam("presence", "server_address", "sip:192.168.1.2:5060")
241
-
242
-# -- nathelper
243
-#n#modparam("nathelper", "rtpproxy_sock", "udp:127.0.0.1:7722")
244
-#n#modparam("nathelper", "natping_interval", 30)
245
-#n#modparam("nathelper", "ping_nated_only", 1)
246
-#n#modparam("nathelper", "sipping_bflag", 7)
247
-#n#modparam("nathelper", "sipping_from", "sip:pinger@kamailio.org")
248
-#n#modparam("registrar|nathelper", "received_avp", "$avp(i:80)")
249
-#n#modparam("usrloc", "nat_bflag", 6)
236
+/* enable presence server support */
237
+#!ifdef WITH_PRESENCE
238
+modparam("presence|presence_xml", "db_url",
239
+	"mysql://openser:openserrw@localhost/openser")
240
+modparam("presence_xml", "force_active", 1)
241
+modparam("presence", "server_address", "sip:10.0.0.10:5060")
242
+#!endif
243
+
244
+# ----- nathelper -----
245
+#!ifdef WITH_NAT
246
+modparam("nathelper", "rtpproxy_sock", "udp:127.0.0.1:7722")
247
+modparam("nathelper", "natping_interval", 30)
248
+modparam("nathelper", "ping_nated_only", 1)
249
+modparam("nathelper", "sipping_bflag", 7)
250
+modparam("nathelper", "sipping_from", "sip:pinger@kamailio.org")
251
+modparam("registrar|nathelper", "received_avp", "$avp(i:80)")
252
+modparam("usrloc", "nat_bflag", 6)
253
+#!endif
250 254
 
251 255
 ####### Routing Logic ########
252 256
 
... ...
@@ -260,8 +286,14 @@ route{
260 260
 		exit;
261 261
 	}
262 262
 
263
+	if(!sanity_check("1511", "7"))
264
+	{
265
+		xlog("Malformed SIP message from $si:$sp\n");
266
+		exit;
267
+	}
268
+
263 269
 	# NAT detection
264
-	route(4);
270
+	route(NAT);
265 271
 
266 272
 	if (has_totag()) {
267 273
 		# sequential request withing a dialog should
... ...
@@ -271,11 +303,11 @@ route{
271 271
 				setflag(1); # do accounting ...
272 272
 				setflag(3); # ... even if the transaction fails
273 273
 			}
274
-			route(1);
274
+			route(RELAY);
275 275
 		} else {
276 276
 			if (is_method("SUBSCRIBE") && uri == myself) {
277 277
 				# in-dialog subscribe requests
278
-				route(2);
278
+				route(PRESENCE);
279 279
 				exit;
280 280
 			}
281 281
 			if ( is_method("ACK") ) {
... ...
@@ -306,9 +338,11 @@ route{
306 306
 	t_check_trans();
307 307
 
308 308
 	# authentication
309
-	route(3);
309
+	route(AUTH);
310 310
 
311 311
 	# record routing for dialog forming requests (in case they are routed)
312
+	# - remove preloaded route headers
313
+	remove_hf("Route");
312 314
 	if (is_method("INVITE|SUBSCRIBE"))
313 315
 		record_route();
314 316
 
... ...
@@ -321,41 +355,41 @@ route{
321 321
 	##if (!is_uri_host_local())
322 322
 	{
323 323
 		append_hf("P-hint: outbound\r\n"); 
324
-		# if you have some interdomain connections via TLS
325
-		##if($rd=="tls_domain1.net") {
326
-		##	t_relay("tls:domain1.net");
327
-		##	exit;
328
-		##} else if($rd=="tls_domain2.net") {
329
-		##	t_relay("tls:domain2.net");
330
-		##	exit;
331
-		##}
332
-		route(1);
324
+		route(RELAY);
333 325
 	}
334 326
 
335 327
 	# requests for my domain
336 328
 
337 329
 	if( is_method("PUBLISH|SUBSCRIBE"))
338
-		route(2);
330
+		route(PRESENCE);
339 331
 
340 332
 	if (is_method("REGISTER"))
341 333
 	{
334
+		if(isflagset(5))
335
+		{
336
+			setbflag("6");
337
+			# uncomment next line to do SIP NAT pinging 
338
+			## setbflag("7");
339
+		}
342 340
 		if (!save("location"))
343 341
 			sl_reply_error();
344 342
 
345 343
 		exit;
346 344
 	}
347 345
 
348
-	if ($rU==NULL) {
346
+	if ($rU==$null) {
349 347
 		# request with no Username in RURI
350 348
 		sl_send_reply("484","Address Incomplete");
351 349
 		exit;
352 350
 	}
353 351
 
352
+	route(PSTN);
353
+
354 354
 	# apply DB based aliases (uncomment to enable)
355 355
 	##alias_db_lookup("dbaliases");
356 356
 
357 357
 	if (!lookup("location")) {
358
-		switch ($retcode) {
358
+		switch ($rc) {
359 359
 			case -1:
360 360
 			case -3:
361 361
 				t_newtran();
... ...
@@ -370,23 +404,25 @@ route{
370 370
 	# when routing via usrloc, log the missed calls also
371 371
 	setflag(2);
372 372
 
373
-	route(1);
373
+	route(RELAY);
374 374
 }
375 375
 
376 376
 
377
-route[1] {
378
-#n#	if (check_route_param("nat=yes")) {
379
-#n#		setbflag(6);
380
-#n#	}
381
-#n#	if (isflagset(5) || isbflagset(6)) {
382
-#n#		route(5);
383
-#n#	}
377
+route[RELAY] {
378
+#!ifdef WITH_NAT
379
+	if (check_route_param("nat=yes")) {
380
+		setbflag("6");
381
+	}
382
+	if (isflagset(5) || isbflagset("6")) {
383
+		route(RTPPROXY);
384
+	}
385
+#!endif
384 386
 
385 387
 	/* example how to enable some additional event routes */
386 388
 	if (is_method("INVITE")) {
387
-		#t_on_branch("1");
388
-		t_on_reply("1");
389
-		t_on_failure("1");
389
+		#t_on_branch("BRANCH_ONE");
390
+		t_on_reply("REPLY_ONE");
391
+		t_on_failure("FAIL_ONE");
390 392
 	}
391 393
 
392 394
 	if (!t_relay()) {
... ...
@@ -396,31 +432,32 @@ route[1] {
396 396
 }
397 397
 
398 398
 
399
-# Presence route
400
-/* uncomment the whole following route for enabling presence server */
401
-route[2]
399
+# Presence server route
400
+route[PRESENCE]
402 401
 {
403
-#p#	if (!t_newtran())
404
-#p#	{
405
-#p#		sl_reply_error();
406
-#p#		exit;
407
-#p#	};
408
-#p#
409
-#p#	if(is_method("PUBLISH"))
410
-#p#	{
411
-#p#		handle_publish();
412
-#p#		t_release();
413
-#p#	}
414
-#p#	else
415
-#p#	if( is_method("SUBSCRIBE"))
416
-#p#	{
417
-#p#		handle_subscribe();
418
-#p#		t_release();
419
-#p#	}
420
-#p#	exit;
402
+#!ifdef WITH_PRESENCE
403
+	if (!t_newtran())
404
+	{
405
+		sl_reply_error();
406
+		exit;
407
+	};
408
+
409
+	if(is_method("PUBLISH"))
410
+	{
411
+		handle_publish();
412
+		t_release();
413
+	}
414
+	else
415
+	if( is_method("SUBSCRIBE"))
416
+	{
417
+		handle_subscribe();
418
+		t_release();
419
+	}
420
+	exit;
421
+#!endif
421 422
 	
422 423
 	# if presence enabled, this part will not be executed
423
-	if (is_method("PUBLISH") || $rU==null)
424
+	if (is_method("PUBLISH") || $rU==$null)
424 425
 	{
425 426
 		sl_send_reply("404", "Not here");
426 427
 		exit;
... ...
@@ -429,101 +466,138 @@ route[2]
429 429
 }
430 430
 
431 431
 # Authentication route
432
-/* uncomment the whole following route for enabling authentication */
433
-route[3] {
434
-#a#	if (is_method("REGISTER"))
435
-#a#	{
436
-#a#		# authenticate the REGISTER requests (uncomment to enable auth)
437
-#a#		if (!www_authorize("", "subscriber"))
438
-#a#		{
439
-#a#			www_challenge("", "0");
440
-#a#			exit;
441
-#a#		}
442
-#a#
443
-#a#		if ($au!=$tU) 
444
-#a#		{
445
-#a#			sl_send_reply("403","Forbidden auth ID");
446
-#a#			exit;
447
-#a#		}
448
-#a#	} else {
449
-#a#		# authenticate if from local subscriber (uncomment to enable auth)
450
-#a#		if (from_uri==myself)
451
-#a#		{
452
-#a#			if (!proxy_authorize("", "subscriber")) {
453
-#a#				proxy_challenge("", "0");
454
-#a#				exit;
455
-#a#			}
456
-#a#			if (is_method("PUBLISH"))
457
-#a#			{
458
-#a#				if ($au!=$tU) {
459
-#a#					sl_send_reply("403","Forbidden auth ID");
460
-#a#					exit;
461
-#a#				}
462
-#a#			} else {
463
-#a#				if ($au!=$fU) {
464
-#a#					sl_send_reply("403","Forbidden auth ID");
465
-#a#					exit;
466
-#a#				}
467
-#a#			}
468
-#a#
469
-#a#			consume_credentials();
470
-#a#			# caller authenticated
471
-#a#		}
472
-#a#	}
432
+route[AUTH] {
433
+#!ifdef WITH_AUTH
434
+	if (is_method("REGISTER"))
435
+	{
436
+		# authenticate the REGISTER requests (uncomment to enable auth)
437
+		if (!www_authorize("", "subscriber"))
438
+		{
439
+			www_challenge("", "0");
440
+			exit;
441
+		}
442
+
443
+		if ($au!=$tU)
444
+		{
445
+			sl_send_reply("403","Forbidden auth ID");
446
+			exit;
447
+		}
448
+	} else {
449
+		# authenticate if from local subscriber (uncomment to enable auth)
450
+		if (from_uri==myself)
451
+		{
452
+			if (!proxy_authorize("", "subscriber")) {
453
+				proxy_challenge("", "0");
454
+				exit;
455
+			}
456
+			if (is_method("PUBLISH"))
457
+			{
458
+				if ($au!=$tU) {
459
+					sl_send_reply("403","Forbidden auth ID");
460
+					exit;
461
+				}
462
+			} else {
463
+				if ($au!=$fU) {
464
+					sl_send_reply("403","Forbidden auth ID");
465
+					exit;
466
+				}
467
+			}
468
+
469
+			consume_credentials();
470
+			# caller authenticated
471
+		}
472
+	}
473
+#!endif
473 474
 	return;
474 475
 }
475 476
 
476 477
 # Caller NAT detection route
477
-/* uncomment the whole following route for enabling Caller NAT Detection */
478
-route[4]{
479
-#n#	force_rport();
480
-#n#	if (nat_uac_test("19")) {
481
-#n#		if (method=="REGISTER") {
482
-#n#			fix_nated_register();
483
-#n#		} else {
484
-#n#			fix_nated_contact();
485
-#n#		}
486
-#n#		setflag(5);
487
-#n#	}
478
+route[NAT]{
479
+#!ifdef WITH_NAT
480
+	force_rport();
481
+	if (nat_uac_test("19")) {
482
+		if (method=="REGISTER") {
483
+			fix_nated_register();
484
+		} else {
485
+			fix_nated_contact();
486
+		}
487
+		setflag(5);
488
+	}
489
+#!endif
488 490
 	return;
489 491
 }
490 492
 
491 493
 # RTPProxy control
492
-/* uncomment the whole following route for enabling RTPProxy Control */
493
-route[5] {
494
-#n#	if (is_method("BYE")) {
495
-#n#		unforce_rtp_proxy();
496
-#n#	} else if (is_method("INVITE")){
497
-#n#		force_rtp_proxy();
498
-#n#	}
499
-#n#	if (!has_totag()) add_rr_param(";nat=yes");
494
+route[RTPPROXY] {
495
+#!ifdef WITH_NAT
496
+	if (is_method("BYE")) {
497
+		unforce_rtp_proxy();
498
+	} else if (is_method("INVITE")){
499
+		force_rtp_proxy();
500
+	}
501
+	if (!has_totag()) add_rr_param(";nat=yes");
502
+#!endif
500 503
 	return;
501 504
 }
502 505
 
503
-branch_route[1] {
504
-	xdbg("new branch at $ru\n");
505
-}
506
+# PSTN GW routing
507
+route[PSTN] {
508
+#!ifdef WITH_PSTN
509
+	# check if PSTN GW IP is defined
510
+	if (strempty($sel(cfg_get.pstn.gw_ip))) {
511
+		xlog("SCRIPT: PSTN rotuing enabled but pstn.gw_ip not defined\n");
512
+		return;
513
+	}
506 514
 
515
+	# route to PSTN dialed numbers starting with '+' or '00'
516
+	#     (international format)
517
+	# - update the condition to match your dialing rules for PSTN routing
518
+	if(!($rU=~"^(\+|00)[1-9][0-9]{3,20}$"))
519
+		return;
507 520
 
508
-onreply_route[1] {
509
-	xdbg("incoming reply\n");
521
+	# only local users allowed to call
522
+	if(from_uri!=myself) {
523
+		sl_send_reply("403", "Not Allowed");
524
+		exit;
525
+	}
526
+
527
+	$ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip);
528
+
529
+	route(RELAY);
530
+	exit;
531
+#!endif
510 532
 
511
-#n#	if ((isflagset(5) || isbflagset(6)) && status=~"(183)|(2[0-9][0-9])") {
512
-#n#		force_rtp_proxy();
513
-#n#	}
514
-#n#	if (isbflagset(6)) {
515
-#n#		fix_nated_contact();
516
-#n#	}
533
+	return;
534
+}
535
+
536
+# Sample branch router
537
+branch_route[BRANCH_ONE] {
538
+	xdbg("new branch at $ru\n");
517 539
 }
518 540
 
541
+# Sample onreply route
542
+onreply_route[REPLY_ONE] {
543
+	xdbg("incoming reply\n");
544
+#!ifdef WITH_NAT
545
+	if ((isflagset(5) || isbflagset("6")) && status=~"(183)|(2[0-9][0-9])") {
546
+		force_rtp_proxy();
547
+	}
548
+	if (isbflagset("6")) {
549
+		fix_nated_contact();
550
+	}
551
+#!endif
552
+}
519 553
 
520
-failure_route[1] {
521
-#n#	if (is_method("INVITE")
522
-#n#			&& (isbflagset(6) || isflagset(5))) {
523
-#n#		unforce_rtp_proxy();
524
-#n#	}
554
+# Sample failure route
555
+failure_route[FAIL_ONE] {
556
+#!ifdef WITH_NAT
557
+	if (is_method("INVITE")
558
+			&& (isbflagset("6") || isflagset(5))) {
559
+		unforce_rtp_proxy();
560
+	}
561
+#!endif
525 562
 
526
-	if (t_was_cancelled()) {
563
+	if (t_is_canceled()) {
527 564
 		exit;
528 565
 	}
529 566
 
... ...
@@ -9,7 +9,7 @@
9 9
 
10 10
 <table id="dialog" xmlns:db="http://docbook.org/ns/docbook">
11 11
 	<name>dialog</name>
12
-	<version>3</version>
12
+	<version>4</version>
13 13
 	<type db="mysql">&MYSQL_TABLE_TYPE;</type>
14 14
 	<description>
15 15
 		<db:para>Persistent dialog information for the dialog module. More 
... ...
@@ -185,6 +185,13 @@
185 185
 		</description>
186 186
 	</column>
187 187
 
188
+	<column>
189
+		<name>req_uri</name>
190
+		<type>string</type>
191
+		<size>&uri_len;</size>
192
+		<description>The URI of initial request in dialog</description>
193
+	</column>