Browse code

- new "declare" m4 macro helper to define all numbers. Just add rules.m4 and pass to m4 processor.

Usage:

declare(flags, FLAG_1, FLAG_2)
declare(route, ...)

setflag(FLAG_1);
if (isflagset(FLAG_2))

etc.

Tomas Mandys authored on 02/02/2006 12:05:45
Showing 2 changed files
... ...
@@ -6,6 +6,12 @@
6 6
 # Rules to process ser.cfg templates
7 7
 #
8 8
 
9
+#id generator, usage: gen_id(`name'))
10
+define(`gen_id', `ifdef(`$1',,`define(`$1', 0)')define(`$1',eval($1 + 1))')
11
+
12
+#declare enum constants, usage: declare(route, R_MAIN, R_NAT, ...)  declare(route, R_REGISTER)
13
+define(`declare', `ifelse($#, 1, , `gen_id(`$1'_cnt) define(`$2', indir(`$1'_cnt)) ifelse($#, 2, ,`declare(`$1', shift(shift($@)))')')')
14
+
9 15
 define(`FROM_GW', `(_FROM_GW(1))')
10 16
 define(`_FROM_GW', `ifdef(`GW_IP_$1', `_FROM_GW(incr($1))(src_ip == GW_IP_$1)ifelse($1, 1, , ` || ')')')
11 17
 
... ...
@@ -18,12 +24,3 @@ define(`SER_HOSTNAME_REGEX', `patsubst(SER_HOSTNAME, `\.', `\\.')')
18 18
 define(`SER_HOST_REGEX', `((SER_IP_REGEX)|(SER_HOSTNAME_REGEX))')
19 19
 
20 20
 define(`FROM_MYSELF', `(src_ip == SER_IP)')
21
-
22
-define(`ACC_FLAG', 1)
23
-define(`MISSED_FLAG', 3)
24
-define(`VM_FLAG', 4)
25
-define(`NAT_FLAG', 6)
26
-
27
-define(`PSTN', 3)
28
-define(`NAT', 1)
29
-define(`VOICEMAIL', 4)
... ...
@@ -1,11 +1,18 @@
1 1
 ### m4 macros to make the configuration easier
2 2
 
3
+include(`rules.m4')
4
+
3 5
 define(`SER_IP', `192.168.0.1')
4 6
 define(`SER_HOSTNAME', `foo.bar')
5 7
 
6 8
 define(`GW_IP_1', `192.168.0.2')
7 9
 define(`GW_IP_2', `192.168.0.3')
8 10
 
11
+declare(flags, ACC_FLAG, MISSED_FLAG, VM_FLAG, NAT_FLAG)
12
+declare(route, PSTN_ROUTE, NAT_ROUTE, VOICEMAIL_ROUTE, PSTN2_ROUTE)
13
+declare(onreply, NAT_REPLY)
14
+declare(failure, PSTN_FAILURE, _1_FAILURE)
15
+
9 16
 ### End of m4 macro section
10 17
 
11 18
 #
... ...
@@ -234,12 +241,12 @@ route {
234 234
                 # check if someone has not introduced a pre-loaded INVITE -- if so,
235 235
                 # verify caller's privileges before accepting rr-ing
236 236
                 if ((method=="INVITE" || method=="ACK" || method=="CANCEL") && uri =~ "TO_GW") {
237
-                        route(PSTN); # Forward to PSTN gateway
237
+                        route(PSTN_ROUTE); # Forward to PSTN gateway
238 238
                 } else {
239 239
                         append_hf("P-hint: rr-enforced\r\n");
240 240
                         # account all BYEs 
241 241
                         if (method=="BYE") setflag(ACC_FLAG);
242
-                        route(NAT);  # Generic forward
242
+                        route(NAT_ROUTE);  # Generic forward
243 243
                 };
244 244
                 break;
245 245
         };
... ...
@@ -250,7 +257,7 @@ route {
250 250
                 # its address may show up in subsequent requests after
251 251
                 # rewriteFromRoute
252 252
                 append_hf("P-hint: OUTBOUND\r\n");
253
-                route(NAT);
253
+                route(NAT_ROUTE);
254 254
                 break;
255 255
         };
256 256
 
... ...
@@ -306,7 +313,7 @@ route {
306 306
         # check again, if it is still for our domain after aliases
307 307
         if (!(uri == myself || uri =~ "TO_GW")) {
308 308
                 append_hf("P-hint: ALIASED-OUTBOUND\r\n");
309
-                route(NAT);
309
+                route(NAT_ROUTE);
310 310
                 break;
311 311
         };
312 312
 
... ...
@@ -319,7 +326,7 @@ route {
319 319
 	if (!does_uri_exist()) {
320 320
 		# Try numeric destinations through the gateway
321 321
 		if (uri =~ "^[a-zA-Z]+:[0-9]+@") {
322
-			route(PSTN);
322
+			route(PSTN_ROUTE);
323 323
 		} else {
324 324
 			sl_send_reply("604", "Does Not Exist Anywhere");
325 325
 		};
... ...
@@ -336,7 +343,7 @@ route {
336 336
         # native SIP destinations are handled using our USRLOC DB
337 337
         if (!lookup("location")) {
338 338
                 # handle user which was not found
339
-                route(VOICEMAIL);
339
+                route(VOICEMAIL_ROUTE);
340 340
                 break;
341 341
         };
342 342
 
... ...
@@ -344,14 +351,14 @@ route {
344 344
         # contacts to UsrLoc to bypass our authorization logic
345 345
         if (uri =~ "TO_GW") {
346 346
                 log(1, "LOG: Weird! Gateway address in UsrLoc!\n");
347
-                route(PSTN);
347
+                route(PSTN_ROUTE);
348 348
                 break;
349 349
         };
350 350
 
351 351
         # if user is on-line and is in voicemail group, enable redirection
352 352
         /* no voicemail currently activated
353 353
         if (method == "INVITE" && isflagset(VM_FLAG)) {
354
-                t_on_failure("1");
354
+                t_on_failure(_1_FAILURE);    # failure_route() not defined
355 355
         };
356 356
         */
357 357
 
... ...
@@ -361,13 +368,13 @@ route {
361 361
 
362 362
         # we now know we may, we know where, let it go out now!
363 363
         append_hf("P-hint: USRLOC\r\n");
364
-        route(NAT);
364
+        route(NAT_ROUTE);
365 365
 }
366 366
 
367 367
 #
368 368
 # Forcing media relay if necesarry
369 369
 #
370
-route[NAT] {
370
+route[NAT_ROUTE] {
371 371
     if (uri=~"[@:](192\.168\.|10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.)" && !search("^Route:")) {
372 372
             sl_send_reply("479", "We don't forward to private IP addresses");
373 373
             break;
... ...
@@ -384,7 +391,7 @@ route[NAT] {
384 384
     # re-INVITEs from public to private UA are hard to identify as
385 385
     # natted at the moment of request processing); look at replies
386 386
 
387
-    t_on_reply("1");
387
+    t_on_reply(NAT_REPLY);
388 388
 
389 389
     if (!t_relay()) {
390 390
             sl_reply_error();
... ...
@@ -393,7 +400,7 @@ route[NAT] {
393 393
 }
394 394
 
395 395
 
396
-onreply_route[1] {
396
+onreply_route[NAT_REPLY] {
397 397
         # natted transaction ?
398 398
         if (isflagset(NAT_FLAG) && status =~ "(183)|2[0-9][0-9]") {
399 399
                 fix_nated_contact();
... ...
@@ -404,8 +411,8 @@ onreply_route[1] {
404 404
                 fix_nated_contact();
405 405
         };
406 406
 
407
-        # keep Cisco gatweay sending keep-alives
408
-        if (isflagset(7) && status=~"2[0-9][0-9]") {
407
+        # keep Cisco gateway sending keep-alives
408
+        if (isflagset(7) && status=~"2[0-9][0-9]") {   # flag(7) is mentioned NAT_FLAG ??
409 409
                 remove_hf("Session-Expires");
410 410
                 append_hf("Session-Expires: 60;refresher=UAC\r\n");
411 411
                 fix_nated_sdp("1");
... ...
@@ -416,7 +423,7 @@ onreply_route[1] {
416 416
 #
417 417
 # logic for calls to the PSTN
418 418
 #
419
-route[PSTN] {
419
+route[PSTN_ROUTE] {
420 420
 
421 421
         # discard non-PSTN methods
422 422
         if (!(method == "INVITE" || method == "ACK" || method == "CANCEL" || method == "OPTIONS" || method == "BYE")) {
... ...
@@ -451,7 +458,7 @@ route[PSTN] {
451 451
                 	};
452 452
 		} else {
453 453
 			# Allow free-pstn destinations without any checks
454
-			route(5);
454
+			route(PSTN2_ROUTE);
455 455
 			break;
456 456
 		};
457 457
 
... ...
@@ -460,7 +467,7 @@ route[PSTN] {
460 460
 			    sl_send_reply("403", "International numbers not allowed");
461 461
 			    break;
462 462
 			};
463
-			route(5);
463
+			route(PSTN2_ROUTE);
464 464
 		} else {
465 465
 			sl_send_reply("403", "Invalid Number");
466 466
 			break;
... ...
@@ -469,13 +476,13 @@ route[PSTN] {
469 469
 	break;
470 470
 }
471 471
 
472
-route[5] {
472
+route[PSTN2_ROUTE] {
473 473
 	rewritehostport("GW_IP_1:5060");
474 474
 	consume_credentials();
475 475
 	append_hf("P-Hint: GATEWAY\r\n");
476 476
 
477 477
 	# Try alternative gateway on failure
478
-	t_on_failure("7");
478
+	t_on_failure(PSTN_FAILURE);
479 479
         # Our PSTN gateway is symmetric and can handle direction=active flag
480 480
         # properly, therefore we don't have to use RTP proxy
481 481
 	t_relay();
... ...
@@ -483,7 +490,7 @@ route[5] {
483 483
 
484 484
 
485 485
 
486
-failure_route[7] {
486
+failure_route[PSTN_FAILURE] {
487 487
 	rewritehostport("GW_IP_2:5060");
488 488
 	append_branch();
489 489
 	t_relay();	
... ...
@@ -491,7 +498,7 @@ failure_route[7] {
491 491
 
492 492
 
493 493
 # ------------- handling of unavailable user ------------------
494
-route[VOICEMAIL] {
494
+route[VOICEMAIL_ROUTE] {
495 495
         # message store
496 496
         if (method == "MESSAGE") {
497 497
                 if (!t_newtran()) {