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 1018
 			$(foreach m,$(modules_dirs),\
954 1019
 				-e "s#/usr/lib/$(CFG_NAME)/$(m)\([^_]\)#$($(m)_target)\1#g") \
955 1020
 			-e "s#/usr/share/doc/$(CFG_NAME)/#$(doc_target)#g" \
956
-			< $(CFG_NAME).8 >  \
1021
+			-e "s#$(SRC_NAME)#$(MAIN_NAME)#g" \
1022
+			< $(SRC_NAME).8 >  \
957 1023
 							$(man_prefix)/$(man_dir)/man8/$(MAIN_NAME).8
958 1024
 		@chmod 644  $(man_prefix)/$(man_dir)/man8/$(MAIN_NAME).8
959 1025
 		@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 1027
 			$(foreach m,$(modules_dirs),\
962 1028
 				-e "s#/usr/lib/$(CFG_NAME)/$(m)\([^_]\)#$($(m)_target)\1#g") \
963 1029
 			-e "s#/usr/share/doc/$(CFG_NAME)/#$(doc_target)#g" \
964
-			< $(CFG_NAME).cfg.5 >  \
1030
+			-e "s#$(SRC_NAME)#$(MAIN_NAME)#g" \
1031
+			< $(SRC_NAME).cfg.5 >  \
965 1032
 			$(man_prefix)/$(man_dir)/man5/$(MAIN_NAME).cfg.5
966 1033
 		@chmod 644  $(man_prefix)/$(man_dir)/man5/$(MAIN_NAME).cfg.5
967 1034
 
... ...
@@ -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 212
 ifeq ($(OS), linux)
211 213
 	doc_dir = share/doc/$(MAIN_NAME)/
212 214
 	man_dir = share/man/
215
+	data_dir = share/$(MAIN_NAME)/
213 216
 	LOCALBASE ?= /usr/local
214 217
 else
215 218
 ifeq ($(OS), freebsd)
216 219
 	doc_dir = share/doc/$(MAIN_NAME)/
217 220
 	man_dir = man/
221
+	data_dir = share/$(MAIN_NAME)/
218 222
 	LOCALBASE ?= /usr/local
219 223
 else
220 224
 ifeq ($(OS), openbsd)
221 225
 	doc_dir = share/doc/$(MAIN_NAME)/
222 226
 	man_dir = man/
227
+	data_dir = share/$(MAIN_NAME)/
223 228
 	LOCALBASE ?= /usr/local
224 229
 else
225 230
 ifeq ($(OS), netbsd)
226 231
 	doc_dir = share/doc/$(MAIN_NAME)/
227 232
 	man_dir = man/
233
+	data_dir = share/$(MAIN_NAME)/
228 234
 	LOCALBASE ?= /usr/pkg
229 235
 else
230 236
 ifeq ($(OS), darwin)
231 237
 	doc_dir = share/doc/$(MAIN_NAME)/
232 238
 	man_dir = man/
239
+	data_dir = share/$(MAIN_NAME)/
233 240
 	LOCALBASE ?= /usr/local
234 241
 else
235 242
 	doc_dir = doc/$(MAIN_NAME)/
236 243
 	man_dir = man/
244
+	data_dir = $(MAIN_NAME)/
237 245
 	LOCALBASE ?= /usr/local
238 246
 endif
239 247
 endif
... ...
@@ -265,6 +273,7 @@ doc_prefix = $(basedir)$(prefix)
265 273
 man_prefix = $(basedir)$(prefix)
266 274
 ut_prefix = $(basedir)$(prefix)
267 275
 share_prefix = $(basedir)$(prefix)
276
+data_prefix = $(basedir)$(prefix)
268 277
 
269 278
 
270 279
 # target dirs for various stuff
... ...
@@ -273,6 +282,7 @@ bin_target = $(prefix)/$(bin_dir)
273 282
 #modules_target = $(prefix)/$(modules_dir)
274 283
 lib_target = $(prefix)/$(lib_dir)
275 284
 doc_target = $(prefix)/$(doc_dir)
285
+data_target = $(prefix)/$(data_dir)
276 286
 
277 287
 
278 288
 
... ...
@@ -1919,7 +1929,7 @@ export exported_vars
1919 1929
 #  cannot be overwritten from environment or command line, unless make cfg
1920 1930
 #  is run)
1921 1931
 saved_fixed_vars:=	MAIN_NAME  CFG_NAME SCR_NAME FLAVOUR INSTALL_FLAVOUR \
1922
-		RELEASE OS ARCH \
1932
+		SRC_NAME RELEASE OS ARCH \
1923 1933
 		C_DEFS DEFS_RM PROFILE CC LD MKDEP MKTAGS LDFLAGS C_INCLUDES \
1924 1934
 		MOD_LDFLAGS LIB_LDFLAGS UTILS_LDFLAGS LIB_SONAME LD_RPATH \
1925 1935
 		LIB_SUFFIX LIB_PREFIX \
... ...
@@ -1939,8 +1949,8 @@ saved_chg_vars:=\
1939 1949
 		PREFIX prefix\
1940 1950
 		cfg_prefix cfg_dir bin_prefix bin_dir modules_prefix modules_dir \
1941 1951
 		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
1952
+		share_prefix share_dir lib_prefix lib_dir data_prefix data_dir \
1953
+		cfg_target lib_target data_target
1944 1954
 
1945 1955
 
1946 1956
 #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 81
            compiled with DBG_QM_MALLOC or DBG_F_MALLOC).
80 82
        Default: 1.
81 83
        Can be changed at runtime.
84
+  - shm = number  or shm_mem = number  - size of shared memory in MB. It's
85
+       overwritten if a value is specified on the command line (-m val).
86
+       Default: 32 Mb. Must appear prior to any modparam() or route block.
82 87
 
83 88
 new script commands:
84 89
   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 107
 	#define PVAR_P_S                7  /* pvar: $(...)  or $foo(...)*/
107 108
 	#define PVARID_S                8  /* $foo.bar...*/
108 109
 	#define STR_BETWEEN_S		9
110
+	#define LINECOMMENT_S            10
111
+	#define DEFINE_S                11
112
+	#define DEFINE_EOL_S            12
113
+	#define IFDEF_S                    13
114
+	#define IFDEF_EOL_S                14
115
+	#define IFDEF_SKIP_S            15
109 116
 
110 117
 	#define STR_BUF_ALLOC_UNIT	128
111 118
 	struct str_buf{
... ...
@@ -153,11 +160,19 @@
153 160
 		struct sr_yy_fname *next;
154 161
 	} *sr_yy_fname_list = 0;
155 162
 
163
+	static int  pp_define(int len, const char * text);
164
+	static int  pp_ifdef_type(int pos);
165
+	static void pp_ifdef_var(int len, const char * text);
166
+	static void pp_ifdef();
167
+	static void pp_else();
168
+	static void pp_endif();
169
+
156 170
 %}
157 171
 
158 172
 /* start conditions */
159 173
 %x STRING1 STRING2 STR_BETWEEN COMMENT COMMENT_LN ATTR SELECT AVP_PVAR PVAR_P 
160 174
 %x PVARID INCLF
175
+%x LINECOMMENT DEFINE_ID DEFINE_EOL IFDEF_ID IFDEF_EOL IFDEF_SKIP
161 176
 
162 177
 /* config script types : #!SER  or #!KAMAILIO or #!MAX_COMPAT */
163 178
 SER_CFG			SER
... ...
@@ -430,6 +445,7 @@ ADVERTISED_ADDRESS	"advertised_address"
430 445
 ADVERTISED_PORT		"advertised_port"
431 446
 DISABLE_CORE		"disable_core_dump"
432 447
 OPEN_FD_LIMIT		"open_files_limit"
448
+SHM_MEM_SZ		"shm"|"shm_mem"|"shm_mem_size"
433 449
 SHM_FORCE_ALLOC		"shm_force_alloc"
434 450
 MLOCK_PAGES			"mlock_pages"
435 451
 REAL_TIME			"real_time"
... ...
@@ -475,8 +491,9 @@ TLSv1			"tlsv1"|"TLSv1"|"TLSV1"
475 491
 
476 492
 LETTER		[a-zA-Z]
477 493
 DIGIT		[0-9]
478
-ALPHANUM	{LETTER}|{DIGIT}|[_]
479
-ID			{LETTER}{ALPHANUM}*
494
+LETTER_     {LETTER}|[_]
495
+ALPHANUM    {LETTER_}|{DIGIT}
496
+ID          {LETTER_}{ALPHANUM}*
480 497
 NUM_ID		{ALPHANUM}+
481 498
 HEX			[0-9a-fA-F]
482 499
 HEXNUMBER	0x{HEX}+
... ...
@@ -507,6 +524,12 @@ COM_LINE	#
507 524
 COM_START	"/\*"
508 525
 COM_END		"\*/"
509 526
 
527
+DEFINE       define
528
+IFDEF        ifdef
529
+IFNDEF       ifndef
530
+ENDIF        endif
531
+/* else is already defined */
532
+
510 533
 EAT_ABLE	[\ \t\b\r]
511 534
 
512 535
 %%
... ...
@@ -818,6 +841,8 @@ EAT_ABLE	[\ \t\b\r]
818 841
 									return DISABLE_CORE; }
819 842
 <INITIAL>{OPEN_FD_LIMIT}		{	count(); yylval.strval=yytext;
820 843
 									return OPEN_FD_LIMIT; }
844
+<INITIAL>{SHM_MEM_SZ}		{	count(); yylval.strval=yytext;
845
+									return SHM_MEM_SZ; }
821 846
 <INITIAL>{SHM_FORCE_ALLOC}		{	count(); yylval.strval=yytext;
822 847
 									return SHM_FORCE_ALLOC; }
823 848
 <INITIAL>{MLOCK_PAGES}		{	count(); yylval.strval=yytext;
... ...
@@ -1129,7 +1154,38 @@ EAT_ABLE	[\ \t\b\r]
1129 1154
 											sr_cfg_compat=SR_COMPAT_KAMAILIO;}
1130 1155
 <INITIAL>{COM_LINE}!{MAXCOMPAT_CFG}{CR}	{ count(); 
1131 1156
 												sr_cfg_compat=SR_COMPAT_MAX;}
1132
-<INITIAL>{COM_LINE}.*{CR}	{ count(); }
1157
+
1158
+<INITIAL>{COM_LINE}!{DEFINE}{EAT_ABLE}+        { count();
1159
+										state = DEFINE_S; BEGIN(DEFINE_ID); }
1160
+<DEFINE_ID>{ID}                                { count();
1161
+								if (pp_define(yyleng, yytext)) return 1;
1162
+								state = DEFINE_EOL_S; BEGIN(DEFINE_EOL); }
1163
+<DEFINE_EOL>{EAT_ABLE}*{CR}                    { count();
1164
+									state = INITIAL; BEGIN(INITIAL); }
1165
+
1166
+<INITIAL,IFDEF_SKIP>{COM_LINE}!{IFDEF}{EAT_ABLE}+    { count();
1167
+								if (pp_ifdef_type(1)) return 1;
1168
+								state = IFDEF_S; BEGIN(IFDEF_ID); }
1169
+<INITIAL,IFDEF_SKIP>{COM_LINE}!{IFNDEF}{EAT_ABLE}+    { count();
1170
+								if (pp_ifdef_type(0)) return 1;
1171
+								state = IFDEF_S; BEGIN(IFDEF_ID); }
1172
+<IFDEF_ID>{ID}                { count();
1173
+                                pp_ifdef_var(yyleng, yytext);
1174
+                                state = IFDEF_EOL_S; BEGIN(IFDEF_EOL); }
1175
+<IFDEF_EOL>{EAT_ABLE}*{CR}    { count(); pp_ifdef(); }
1176
+
1177
+<INITIAL,IFDEF_SKIP>{COM_LINE}!{ELSE}{EAT_ABLE}*{CR}    { count(); pp_else(); }
1178
+
1179
+<INITIAL,IFDEF_SKIP>{COM_LINE}!{ENDIF}{EAT_ABLE}*{CR}    { count();
1180
+															pp_endif(); }
1181
+
1182
+ /* we're in an ifdef that evaluated to false -- throw it away */
1183
+<IFDEF_SKIP>.|{CR}    { count(); }
1184
+
1185
+ /* this is split so the shebangs match more, giving them priority */
1186
+<INITIAL>{COM_LINE}        { count(); state = LINECOMMENT_S;
1187
+								BEGIN(LINECOMMENT); }
1188
+<LINECOMMENT>.*{CR}        { count(); state = INITIAL_S; BEGIN(INITIAL); }
1133 1189
 
1134 1190
 <INITIAL>{ID}			{ count(); addstr(&s_buf, yytext, yyleng);
1135 1191
 									yylval.strval=s_buf.s;
... ...
@@ -1480,3 +1536,102 @@ static int sr_pop_yy_state()
1480 1536
 	return 0;
1481 1537
 }
1482 1538
 
1539
+/* define/ifdef support */
1540
+
1541
+#define MAX_DEFINES    1024
1542
+static str pp_defines[MAX_DEFINES];
1543
+static int pp_num_defines = 0;
1544
+
1545
+/* pp_ifdef_stack[i] is 1 if the ifdef test at depth i is either
1546
+ * ifdef(defined), ifndef(undefined), or the opposite of these
1547
+ * two, but in an else branch
1548
+ */
1549
+#define MAX_IFDEFS    128
1550
+static int pp_ifdef_stack[MAX_IFDEFS];
1551
+static int pp_sptr = 0; /* stack pointer */
1552
+
1553
+static int pp_lookup(int len, const char * text)
1554
+{
1555
+	str var = {(char *)text, len};
1556
+	int i;
1557
+
1558
+	for (i=0; i<pp_num_defines; i++)
1559
+		if (STR_EQ(pp_defines[i], var))
1560
+			return i;
1561
+
1562
+	return -1;
1563
+}
1564
+
1565
+static int pp_define(int len, const char * text)
1566
+{
1567
+	if (pp_num_defines == MAX_DEFINES) {
1568
+		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
1569
+		return -1;
1570
+	}
1571
+
1572
+	if (pp_lookup(len, text) >= 0) {
1573
+		LOG(L_CRIT, "ERROR: already defined: %.*s\n", len, text);
1574
+		return -1;
1575
+	}
1576
+
1577
+	pp_defines[pp_num_defines].len = len;
1578
+	pp_defines[pp_num_defines].s = (char*)pkg_malloc(len+1);
1579
+	memcpy(pp_defines[pp_num_defines].s, text, len);
1580
+	pp_num_defines++;
1581
+
1582
+	return 0;
1583
+}
1584
+
1585
+static int pp_ifdef_type(int type)
1586
+{
1587
+	if (pp_sptr == MAX_IFDEFS) {
1588
+		LOG(L_CRIT, "ERROR: too many nested ifdefs -- adjust MAX_IFDEFS\n");
1589
+		return -1;
1590
+	}
1591
+
1592
+	pp_ifdef_stack[pp_sptr] = type;
1593
+	return 0;
1594
+}
1595
+
1596
+/* this sets the result of the if[n]def expr:
1597
+ * ifdef  defined   -> 1
1598
+ * ifdef  undefined -> 0
1599
+ * ifndef defined   -> 0
1600
+ * ifndef undefined -> 1
1601
+ */
1602
+static void pp_ifdef_var(int len, const char * text)
1603
+{
1604
+	pp_ifdef_stack[pp_sptr] ^= (pp_lookup(len, text) < 0);
1605
+}
1606
+
1607
+static void pp_update_state()
1608
+{
1609
+	int i;
1610
+
1611
+	for (i=0; i<pp_sptr; i++)
1612
+		if (! pp_ifdef_stack[i]) {
1613
+			state = IFDEF_SKIP_S; BEGIN(IFDEF_SKIP);
1614
+			return;
1615
+		}
1616
+
1617
+	state = INITIAL; BEGIN(INITIAL);
1618
+}
1619
+
1620
+static void pp_ifdef()
1621
+{
1622
+	pp_sptr++;
1623
+	pp_update_state();
1624
+}
1625
+
1626
+static void pp_else()
1627
+{
1628
+	pp_ifdef_stack[pp_sptr-1] ^= 1;
1629
+	pp_update_state();
1630
+}
1631
+
1632
+static void pp_endif()
1633
+{
1634
+	pp_sptr--;
1635
+	pp_update_state();
1636
+}
1637
+
... ...
@@ -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 113
 #include <errno.h>
112 114
 #include "route_struct.h"
113 115
 #include "globals.h"
116
+#ifdef SHM_MEM
117
+#include "shm_init.h"
118
+#endif /* SHM_MEM */
114 119
 #include "route.h"
115 120
 #include "switch.h"
116 121
 #include "dprint.h"
... ...
@@ -490,6 +495,7 @@ extern char *finame;
490 495
 %token ADVERTISED_PORT
491 496
 %token DISABLE_CORE
492 497
 %token OPEN_FD_LIMIT
498
+%token SHM_MEM_SZ
493 499
 %token SHM_FORCE_ALLOC
494 500
 %token MLOCK_PAGES
495 501
 %token REAL_TIME
... ...
@@ -867,8 +873,20 @@ assign_stm:
867 873
 	| MEMSUM EQUAL error { yyerror("int value expected"); }
868 874
 	| SIP_WARNING EQUAL NUMBER { sip_warning=$3; }
869 875
 	| SIP_WARNING EQUAL error { yyerror("boolean value expected"); }
870
-	| USER EQUAL STRING     { user=$3; }
871
-	| USER EQUAL ID         { user=$3; }
876
+	| USER EQUAL STRING     {
877
+		if (shm_initialized())
878
+			yyerror("user must be before any modparam or the"
879
+					" route blocks");
880
+		else if (user==0)
881
+			user=$3; 
882
+	}
883
+	| USER EQUAL ID         {
884
+		if (shm_initialized())
885
+			yyerror("user must be before any modparam or the"
886
+					" route blocks");
887
+		else if (user==0)
888
+			user=$3;
889
+	}
872 890
 	| USER EQUAL error      { yyerror("string value expected"); }
873 891
 	| GROUP EQUAL STRING     { group=$3; }
874 892
 	| GROUP EQUAL ID         { group=$3; }
... ...
@@ -1447,7 +1465,21 @@ assign_stm:
1447 1465
 	| DISABLE_CORE EQUAL error { yyerror("boolean value expected"); }
1448 1466
 	| OPEN_FD_LIMIT EQUAL NUMBER { open_files_limit=$3; }
1449 1467
 	| OPEN_FD_LIMIT EQUAL error { yyerror("number expected"); }
1450
-	| SHM_FORCE_ALLOC EQUAL NUMBER { shm_force_alloc=$3; }
1468
+	| SHM_MEM_SZ EQUAL NUMBER {
1469
+		if (shm_initialized())
1470
+			yyerror("shm/shm_mem_size must be before any modparam or the"
1471
+					" route blocks");
1472
+		else if (shm_mem_size == 0)
1473
+			shm_mem_size=$3 * 1024 * 1024;
1474
+	}
1475
+	| SHM_MEM_SZ EQUAL error { yyerror("number expected"); }
1476
+	| SHM_FORCE_ALLOC EQUAL NUMBER {
1477
+		if (shm_initialized())
1478
+			yyerror("shm_force_alloc must be before any modparam or the"
1479
+					" route blocks");
1480
+		else
1481
+			shm_force_alloc=$3;
1482
+	}
1451 1483
 	| SHM_FORCE_ALLOC EQUAL error { yyerror("boolean value expected"); }
1452 1484
 	| MLOCK_PAGES EQUAL NUMBER { mlock_pages=$3; }
1453 1485
 	| MLOCK_PAGES EQUAL error { yyerror("boolean value expected"); }
... ...
@@ -1560,11 +1592,23 @@ module_stm:
1560 1592
 	}
1561 1593
 	| LOADPATH EQUAL error	{ yyerror("string expected"); }
1562 1594
 	| MODPARAM LPAREN STRING COMMA STRING COMMA STRING RPAREN {
1595
+	#ifdef SHM_MEM
1596
+		if (!shm_initialized() && init_shm()<0) {
1597
+			yyerror("Can't initialize shared memory");
1598
+			YYABORT;
1599
+		}
1600
+	#endif /* SHM_MEM */
1563 1601
 		if (set_mod_param_regex($3, $5, PARAM_STRING, $7) != 0) {
1564 1602
 			 yyerror("Can't set module parameter");
1565 1603
 		}
1566 1604
 	}
1567
-        | MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
1605
+	| MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
1606
+	#ifdef SHM_MEM
1607
+		if (!shm_initialized() && init_shm()<0) {
1608
+			yyerror("Can't initialize shared memory");
1609
+			YYABORT;
1610
+		}
1611
+	#endif /* SHM_MEM */
1568 1612
 		if (set_mod_param_regex($3, $5, PARAM_INT, (void*)$7) != 0) {
1569 1613
 			 yyerror("Can't set module parameter");
1570 1614
 		}
... ...
@@ -1645,8 +1689,22 @@ route_name:		NUMBER	{
1645 1689
 ;
1646 1690
 
1647 1691
 route_stm:
1648
-	ROUTE LBRACE actions RBRACE { push($3, &main_rt.rlist[DEFAULT_RT]); }
1692
+	ROUTE LBRACE actions RBRACE {
1693
+	#ifdef SHM_MEM
1694
+		if (!shm_initialized() && init_shm()<0) {
1695
+			yyerror("Can't initialize shared memory");
1696
+			YYABORT;
1697
+		}
1698
+	#endif /* SHM_MEM */
1699
+		push($3, &main_rt.rlist[DEFAULT_RT]);
1700
+	}
1649 1701
 	| ROUTE LBRACK route_name RBRACK LBRACE actions RBRACE {
1702
+	#ifdef SHM_MEM
1703
+		if (!shm_initialized() && init_shm()<0) {
1704
+			yyerror("Can't initialize shared memory");
1705
+			YYABORT;
1706
+		}
1707
+	#endif /* SHM_MEM */
1650 1708
 		i_tmp=route_get(&main_rt, $3);
1651 1709
 		if (i_tmp==-1){
1652 1710
 			yyerror("internal error");
... ...
@@ -1662,9 +1720,21 @@ route_stm:
1662 1720
 	;
1663 1721
 failure_route_stm:
1664 1722
 	ROUTE_FAILURE LBRACE actions RBRACE {
1665
-									push($3, &failure_rt.rlist[DEFAULT_RT]);
1666
-										}
1723
+	#ifdef SHM_MEM
1724
+		if (!shm_initialized() && init_shm()<0) {
1725
+			yyerror("Can't initialize shared memory");
1726
+			YYABORT;
1727
+		}
1728
+	#endif /* SHM_MEM */
1729
+		push($3, &failure_rt.rlist[DEFAULT_RT]);
1730
+	}
1667 1731
 	| ROUTE_FAILURE LBRACK route_name RBRACK LBRACE actions RBRACE {
1732
+	#ifdef SHM_MEM
1733
+		if (!shm_initialized() && init_shm()<0) {
1734
+			yyerror("Can't initialize shared memory");
1735
+			YYABORT;
1736
+		}
1737
+	#endif /* SHM_MEM */
1668 1738
 		i_tmp=route_get(&failure_rt, $3);
1669 1739
 		if (i_tmp==-1){
1670 1740
 			yyerror("internal error");
... ...
@@ -1680,9 +1750,21 @@ failure_route_stm:
1680 1750
 	;
1681 1751
 onreply_route_stm:
1682 1752
 	ROUTE_ONREPLY LBRACE actions RBRACE {
1683
-									push($3, &onreply_rt.rlist[DEFAULT_RT]);
1684
-										}
1753
+	#ifdef SHM_MEM
1754
+		if (!shm_initialized() && init_shm()<0) {
1755
+			yyerror("Can't initialize shared memory");
1756
+			YYABORT;
1757
+		}
1758
+	#endif /* SHM_MEM */
1759
+		push($3, &onreply_rt.rlist[DEFAULT_RT]);
1760
+	}
1685 1761
 	| ROUTE_ONREPLY LBRACK route_name RBRACK LBRACE actions RBRACE {
1762
+	#ifdef SHM_MEM
1763
+		if (!shm_initialized() && init_shm()<0) {
1764
+			yyerror("Can't initialize shared memory");
1765
+			YYABORT;
1766
+		}
1767
+	#endif /* SHM_MEM */
1686 1768
 		i_tmp=route_get(&onreply_rt, $3);
1687 1769
 		if (i_tmp==-1){
1688 1770
 			yyerror("internal error");
... ...
@@ -1698,9 +1780,21 @@ onreply_route_stm:
1698 1780
 	;
1699 1781
 branch_route_stm:
1700 1782
 	ROUTE_BRANCH LBRACE actions RBRACE {
1701
-									push($3, &branch_rt.rlist[DEFAULT_RT]);
1702
-										}
1783
+	#ifdef SHM_MEM
1784
+		if (!shm_initialized() && init_shm()<0) {
1785
+			yyerror("Can't initialize shared memory");
1786
+			YYABORT;
1787
+		}
1788
+	#endif /* SHM_MEM */
1789
+		push($3, &branch_rt.rlist[DEFAULT_RT]);
1790
+	}
1703 1791
 	| ROUTE_BRANCH LBRACK route_name RBRACK LBRACE actions RBRACE {
1792
+	#ifdef SHM_MEM
1793
+		if (!shm_initialized() && init_shm()<0) {
1794
+			yyerror("Can't initialize shared memory");
1795
+			YYABORT;
1796
+		}
1797
+	#endif /* SHM_MEM */
1704 1798
 		i_tmp=route_get(&branch_rt, $3);
1705 1799
 		if (i_tmp==-1){
1706 1800
 			yyerror("internal error");
... ...
@@ -1715,9 +1809,21 @@ branch_route_stm:
1715 1809
 	| ROUTE_BRANCH error { yyerror("invalid branch_route statement"); }
1716 1810
 	;
1717 1811
 send_route_stm: ROUTE_SEND LBRACE actions RBRACE {
1718
-									push($3, &onsend_rt.rlist[DEFAULT_RT]);
1719
-												}
1812
+	#ifdef SHM_MEM
1813
+		if (!shm_initialized() && init_shm()<0) {
1814
+			yyerror("Can't initialize shared memory");
1815
+			YYABORT;
1816
+		}
1817
+	#endif /* SHM_MEM */
1818
+		push($3, &onsend_rt.rlist[DEFAULT_RT]);
1819
+	}
1720 1820
 	| ROUTE_SEND LBRACK route_name RBRACK LBRACE actions RBRACE {
1821
+	#ifdef SHM_MEM
1822
+		if (!shm_initialized() && init_shm()<0) {
1823
+			yyerror("Can't initialize shared memory");
1824
+			YYABORT;
1825
+		}
1826
+	#endif /* SHM_MEM */
1721 1827
 		i_tmp=route_get(&onsend_rt, $3);
1722 1828
 		if (i_tmp==-1){
1723 1829
 			yyerror("internal error");
... ...
@@ -1732,6 +1838,12 @@ send_route_stm: ROUTE_SEND LBRACE actions RBRACE {
1732 1838
 	| ROUTE_SEND error { yyerror("invalid onsend_route statement"); }
1733 1839
 	;
1734 1840
 event_route_stm: ROUTE_EVENT LBRACK EVENT_RT_NAME RBRACK LBRACE actions RBRACE {
1841
+	#ifdef SHM_MEM
1842
+		if (!shm_initialized() && init_shm()<0) {
1843
+			yyerror("Can't initialize shared memory");
1844
+			YYABORT;
1845
+		}
1846
+	#endif /* SHM_MEM */
1735 1847
 		i_tmp=route_get(&event_rt, $3);
1736 1848
 		if (i_tmp==-1){
1737 1849
 			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 136
 loadmodule "uri_db.so"
122 137
 loadmodule "siputils.so"
123 138
 loadmodule "xlog.so"
139
+loadmodule "sanity.so"
140
+loadmodule "ctl.so"
141
+loadmodule "mi_rpc.so"
124 142
 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"
143
+#!ifdef WITH_AUTH
144
+loadmodule "auth.so"
145
+loadmodule "auth_db.so"
146
+#!endif
129 147
 /* uncomment next line for aliases support
130 148
    NOTE: a DB (like db_mysql) module must be also loaded */
131 149
 #loadmodule "alias_db.so"
... ...
@@ -134,12 +152,14 @@ loadmodule "acc.so"
134 152
    NOTE: be sure and enable multi-domain support in all used modules
135 153
          (see "multi-module params" section ) */
136 154
 #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"
155
+#!ifdef WITH_PRESENCE
156
+loadmodule "presence.so"
157
+loadmodule "presence_xml.so"
158
+#!endif
141 159
 
142
-#n#loadmodule "nathelper.so"
160
+#!ifdef WITH_NAT
161
+loadmodule "nathelper.so"
162
+#!endif
143 163
 
144 164
 # ----------------- setting module-specific parameters ---------------
145 165
 
... ...
@@ -185,31 +205,33 @@ modparam("acc", "log_flag", 1)
185 205
 modparam("acc", "log_missed_flag", 2)
186 206
 modparam("acc", "log_extra", 
187 207
 	"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
-
208
+/* enhanced DB accounting */
209
+#!ifdef WITH_ACCDB
210
+modparam("acc", "db_flag", 1)
211
+modparam("acc", "db_missed_flag", 2)
212
+modparam("acc", "db_url",
213
+	"mysql://openser:openserrw@localhost/openser")
214
+modparam("acc", "db_extra",
215
+	"src_user=$fU;src_domain=$fd;dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
216
+#!endif
196 217
 
197 218
 # ----- 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")
219
+/* enable DB persistency for location entries */
220
+#!ifdef WITH_USRLOCDB
221
+modparam("usrloc", "db_mode",   2)
222
+modparam("usrloc", "db_url",
223
+	"mysql://openser:openserrw@localhost/openser")
224
+#!endif
203 225
 
204 226
 # ----- 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
-
227
+/* enable the DB based authentication */
228
+#!ifdef WITH_AUTH
229
+modparam("auth_db", "calculate_ha1", yes)
230
+modparam("auth_db", "password_column", "password")
231
+modparam("auth_db", "db_url",
232
+	"mysql://openser:openserrw@localhost/openser")
233
+modparam("auth_db", "load_credentials", "")
234
+#!endif
213 235
 
214 236
 # ----- alias_db params -----
215 237
 /* uncomment the following lines if you want to enable the DB based
... ...
@@ -233,20 +255,24 @@ modparam("acc", "log_extra",
233 255
 
234 256
 
235 257
 # ----- 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)
258
+/* enable presence server support */
259
+#!ifdef WITH_PRESENCE
260
+modparam("presence|presence_xml", "db_url",
261
+	"mysql://openser:openserrw@localhost/openser")
262
+modparam("presence_xml", "force_active", 1)
263
+modparam("presence", "server_address", "sip:10.0.0.10:5060")
264
+#!endif
265
+
266
+# ----- nathelper -----
267
+#!ifdef WITH_NAT
268
+modparam("nathelper", "rtpproxy_sock", "udp:127.0.0.1:7722")
269
+modparam("nathelper", "natping_interval", 30)
270
+modparam("nathelper", "ping_nated_only", 1)
271
+modparam("nathelper", "sipping_bflag", 7)
272
+modparam("nathelper", "sipping_from", "sip:pinger@kamailio.org")
273
+modparam("registrar|nathelper", "received_avp", "$avp(i:80)")
274
+modparam("usrloc", "nat_bflag", 6)
275
+#!endif
250 276
 
251 277
 ####### Routing Logic ########
252 278
 
... ...
@@ -260,8 +286,14 @@ route{
260 286
 		exit;
261 287
 	}
262 288
 
289
+	if(!sanity_check("1511", "7"))
290
+	{
291
+		xlog("Malformed SIP message from $si:$sp\n");
292
+		exit;
293
+	}
294
+
263 295
 	# NAT detection
264
-	route(4);
296
+	route(NAT);
265 297
 
266 298
 	if (has_totag()) {
267 299
 		# sequential request withing a dialog should
... ...
@@ -271,11 +303,11 @@ route{
271 303
 				setflag(1); # do accounting ...
272 304
 				setflag(3); # ... even if the transaction fails
273 305
 			}
274
-			route(1);
306
+			route(RELAY);
275 307
 		} else {
276 308
 			if (is_method("SUBSCRIBE") && uri == myself) {
277 309
 				# in-dialog subscribe requests
278
-				route(2);
310
+				route(PRESENCE);
279 311
 				exit;
280 312
 			}
281 313
 			if ( is_method("ACK") ) {
... ...
@@ -306,9 +338,11 @@ route{
306 338
 	t_check_trans();
307 339
 
308 340
 	# authentication
309
-	route(3);
341
+	route(AUTH);
310 342
 
311 343
 	# record routing for dialog forming requests (in case they are routed)
344
+	# - remove preloaded route headers
345
+	remove_hf("Route");
312 346
 	if (is_method("INVITE|SUBSCRIBE"))
313 347
 		record_route();
314 348
 
... ...
@@ -321,41 +355,41 @@ route{
321 355
 	##if (!is_uri_host_local())
322 356
 	{
323 357
 		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);
358
+		route(RELAY);
333 359
 	}
334 360
 
335 361
 	# requests for my domain
336 362
 
337 363
 	if( is_method("PUBLISH|SUBSCRIBE"))
338
-		route(2);
364
+		route(PRESENCE);
339 365
 
340 366
 	if (is_method("REGISTER"))
341 367
 	{
368
+		if(isflagset(5))
369
+		{
370
+			setbflag("6");
371
+			# uncomment next line to do SIP NAT pinging 
372
+			## setbflag("7");
373
+		}
342 374
 		if (!save("location"))
343 375
 			sl_reply_error();
344 376
 
345 377
 		exit;
346 378
 	}
347 379
 
348
-	if ($rU==NULL) {
380
+	if ($rU==$null) {
349 381
 		# request with no Username in RURI
350 382
 		sl_send_reply("484","Address Incomplete");
351 383
 		exit;
352 384
 	}
353 385
 
386
+	route(PSTN);
387
+
354 388
 	# apply DB based aliases (uncomment to enable)
355 389
 	##alias_db_lookup("dbaliases");
356 390
 
357 391
 	if (!lookup("location")) {
358
-		switch ($retcode) {
392
+		switch ($rc) {
359 393
 			case -1:
360 394
 			case -3:
361 395
 				t_newtran();
... ...
@@ -370,23 +404,25 @@ route{
370 404
 	# when routing via usrloc, log the missed calls also
371 405
 	setflag(2);
372 406
 
373
-	route(1);
407
+	route(RELAY);
374 408
 }
375 409
 
376 410
 
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#	}
411
+route[RELAY] {
412
+#!ifdef WITH_NAT
413
+	if (check_route_param("nat=yes")) {
414
+		setbflag("6");
415
+	}
416
+	if (isflagset(5) || isbflagset("6")) {
417
+		route(RTPPROXY);
418
+	}
419
+#!endif
384 420
 
385 421
 	/* example how to enable some additional event routes */
386 422
 	if (is_method("INVITE")) {
387
-		#t_on_branch("1");
388
-		t_on_reply("1");
389
-		t_on_failure("1");
423
+		#t_on_branch("BRANCH_ONE");
424
+		t_on_reply("REPLY_ONE");
425
+		t_on_failure("FAIL_ONE");
390 426
 	}
391 427
 
392 428
 	if (!t_relay()) {
... ...
@@ -396,31 +432,32 @@ route[1] {
396 432
 }
397 433
 
398 434
 
399
-# Presence route
400
-/* uncomment the whole following route for enabling presence server */
401
-route[2]
435
+# Presence server route
436
+route[PRESENCE]
402 437
 {
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;
438
+#!ifdef WITH_PRESENCE
439
+	if (!t_newtran())
440
+	{
441
+		sl_reply_error();
442
+		exit;
443
+	};
444
+
445
+	if(is_method("PUBLISH"))
446
+	{
447
+		handle_publish();
448
+		t_release();
449
+	}
450
+	else
451
+	if( is_method("SUBSCRIBE"))
452
+	{
453
+		handle_subscribe();
454
+		t_release();
455
+	}
456
+	exit;
457
+#!endif
421 458
 	
422 459
 	# if presence enabled, this part will not be executed
423
-	if (is_method("PUBLISH") || $rU==null)
460
+	if (is_method("PUBLISH") || $rU==$null)
424 461
 	{
425 462
 		sl_send_reply("404", "Not here");
426 463
 		exit;
... ...
@@ -429,101 +466,138 @@ route[2]
429 466
 }
430 467
 
431 468
 # 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#	}
469
+route[AUTH] {
470
+#!ifdef WITH_AUTH
471
+	if (is_method("REGISTER"))
472
+	{
473
+		# authenticate the REGISTER requests (uncomment to enable auth)
474
+		if (!www_authorize("", "subscriber"))
475
+		{
476
+			www_challenge("", "0");
477
+			exit;
478
+		}
479
+
480
+		if ($au!=$tU)
481
+		{
482
+			sl_send_reply("403","Forbidden auth ID");
483
+			exit;
484
+		}
485
+	} else {
486
+		# authenticate if from local subscriber (uncomment to enable auth)
487
+		if (from_uri==myself)
488
+		{
489
+			if (!proxy_authorize("", "subscriber")) {
490
+				proxy_challenge("", "0");
491
+				exit;
492
+			}
493
+			if (is_method("PUBLISH"))
494
+			{
495
+				if ($au!=$tU) {
496
+					sl_send_reply("403","Forbidden auth ID");
497
+					exit;
498
+				}
499
+			} else {
500
+				if ($au!=$fU) {
501
+					sl_send_reply("403","Forbidden auth ID");
502
+					exit;
503
+				}
504
+			}
505
+
506
+			consume_credentials();
507
+			# caller authenticated
508
+		}
509
+	}
510
+#!endif
473 511
 	return;
474 512
 }
475 513
 
476 514
 # 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#	}
515
+route[NAT]{
516
+#!ifdef WITH_NAT
517
+	force_rport();
518
+	if (nat_uac_test("19")) {
519
+		if (method=="REGISTER") {
520
+			fix_nated_register();
521
+		} else {
522
+			fix_nated_contact();
523
+		}
524
+		setflag(5);
525
+	}
526
+#!endif
488 527
 	return;
489 528
 }
490 529
 
491 530
 # 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");
531
+route[RTPPROXY] {
532
+#!ifdef WITH_NAT
533
+	if (is_method("BYE")) {
534
+		unforce_rtp_proxy();
535
+	} else if (is_method("INVITE")){
536
+		force_rtp_proxy();
537
+	}
538
+	if (!has_totag()) add_rr_param(";nat=yes");
539
+#!endif
500 540
 	return;
501 541
 }
502 542
 
503
-branch_route[1] {
504
-	xdbg("new branch at $ru\n");
505
-}
543
+# PSTN GW routing
544
+route[PSTN] {
545
+#!ifdef WITH_PSTN
546
+	# check if PSTN GW IP is defined
547
+	if (strempty($sel(cfg_get.pstn.gw_ip))) {
548
+		xlog("SCRIPT: PSTN rotuing enabled but pstn.gw_ip not defined\n");
549
+		return;
550
+	}
506 551
 
552
+	# route to PSTN dialed numbers starting with '+' or '00'
553
+	#     (international format)
554
+	# - update the condition to match your dialing rules for PSTN routing
555
+	if(!($rU=~"^(\+|00)[1-9][0-9]{3,20}$"))
556
+		return;
507 557
 
508
-onreply_route[1] {
509
-	xdbg("incoming reply\n");
558
+	# only local users allowed to call
559
+	if(from_uri!=myself) {
560
+		sl_send_reply("403", "Not Allowed");
561
+		exit;
562
+	}
563
+
564
+	$ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip);
565
+
566
+	route(RELAY);
567
+	exit;
568
+#!endif
510 569
 
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#	}
570
+	return;
571
+}
572
+
573
+# Sample branch router
574
+branch_route[BRANCH_ONE] {
575
+	xdbg("new branch at $ru\n");
517 576
 }
518 577
 
578
+# Sample onreply route
579
+onreply_route[REPLY_ONE] {
580
+	xdbg("incoming reply\n");
581
+#!ifdef WITH_NAT
582
+	if ((isflagset(5) || isbflagset("6")) && status=~"(183)|(2[0-9][0-9])") {
583
+		force_rtp_proxy();
584
+	}
585
+	if (isbflagset("6")) {
586
+		fix_nated_contact();
587
+	}
588
+#!endif
589
+}
519 590
 
520
-failure_route[1] {
521
-#n#	if (is_method("INVITE")
522
-#n#			&& (isbflagset(6) || isflagset(5))) {
523
-#n#		unforce_rtp_proxy();
524
-#n#	}
591
+# Sample failure route
592
+failure_route[FAIL_ONE] {
593
+#!ifdef WITH_NAT
594
+	if (is_method("INVITE")
595
+			&& (isbflagset("6") || isflagset(5))) {
596
+		unforce_rtp_proxy();
597
+	}
598
+#!endif
525 599
 
526
-	if (t_was_cancelled()) {
600
+	if (t_is_canceled()) {
527 601
 		exit;
528 602
 	}
529 603
 
... ...
@@ -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>