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 24
 define(`SER_HOST_REGEX', `((SER_IP_REGEX)|(SER_HOSTNAME_REGEX))')
19 25
 
20 26
 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 241
                 # check if someone has not introduced a pre-loaded INVITE -- if so,
235 242
                 # verify caller's privileges before accepting rr-ing
236 243
                 if ((method=="INVITE" || method=="ACK" || method=="CANCEL") && uri =~ "TO_GW") {
237
-                        route(PSTN); # Forward to PSTN gateway
244
+                        route(PSTN_ROUTE); # Forward to PSTN gateway
238 245
                 } else {
239 246
                         append_hf("P-hint: rr-enforced\r\n");
240 247
                         # account all BYEs 
241 248
                         if (method=="BYE") setflag(ACC_FLAG);
242
-                        route(NAT);  # Generic forward
249
+                        route(NAT_ROUTE);  # Generic forward
243 250
                 };
244 251
                 break;
245 252
         };
... ...
@@ -250,7 +257,7 @@ route {
250 257
                 # its address may show up in subsequent requests after
251 258
                 # rewriteFromRoute
252 259
                 append_hf("P-hint: OUTBOUND\r\n");
253
-                route(NAT);
260
+                route(NAT_ROUTE);
254 261
                 break;
255 262
         };
256 263
 
... ...
@@ -306,7 +313,7 @@ route {
306 313
         # check again, if it is still for our domain after aliases
307 314
         if (!(uri == myself || uri =~ "TO_GW")) {
308 315
                 append_hf("P-hint: ALIASED-OUTBOUND\r\n");
309
-                route(NAT);
316
+                route(NAT_ROUTE);
310 317
                 break;
311 318
         };
312 319
 
... ...
@@ -319,7 +326,7 @@ route {
319 326
 	if (!does_uri_exist()) {
320 327
 		# Try numeric destinations through the gateway
321 328
 		if (uri =~ "^[a-zA-Z]+:[0-9]+@") {
322
-			route(PSTN);
329
+			route(PSTN_ROUTE);
323 330
 		} else {
324 331
 			sl_send_reply("604", "Does Not Exist Anywhere");
325 332
 		};
... ...
@@ -336,7 +343,7 @@ route {
336 343
         # native SIP destinations are handled using our USRLOC DB
337 344
         if (!lookup("location")) {
338 345
                 # handle user which was not found
339
-                route(VOICEMAIL);
346
+                route(VOICEMAIL_ROUTE);
340 347
                 break;
341 348
         };
342 349
 
... ...
@@ -344,14 +351,14 @@ route {
344 351
         # contacts to UsrLoc to bypass our authorization logic
345 352
         if (uri =~ "TO_GW") {
346 353
                 log(1, "LOG: Weird! Gateway address in UsrLoc!\n");
347
-                route(PSTN);
354
+                route(PSTN_ROUTE);
348 355
                 break;
349 356
         };
350 357
 
351 358
         # if user is on-line and is in voicemail group, enable redirection
352 359
         /* no voicemail currently activated
353 360
         if (method == "INVITE" && isflagset(VM_FLAG)) {
354
-                t_on_failure("1");
361
+                t_on_failure(_1_FAILURE);    # failure_route() not defined
355 362
         };
356 363
         */
357 364
 
... ...
@@ -361,13 +368,13 @@ route {
361 368
 
362 369
         # we now know we may, we know where, let it go out now!
363 370
         append_hf("P-hint: USRLOC\r\n");
364
-        route(NAT);
371
+        route(NAT_ROUTE);
365 372
 }
366 373
 
367 374
 #
368 375
 # Forcing media relay if necesarry
369 376
 #
370
-route[NAT] {
377
+route[NAT_ROUTE] {
371 378
     if (uri=~"[@:](192\.168\.|10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.)" && !search("^Route:")) {
372 379
             sl_send_reply("479", "We don't forward to private IP addresses");
373 380
             break;
... ...
@@ -384,7 +391,7 @@ route[NAT] {
384 391
     # re-INVITEs from public to private UA are hard to identify as
385 392
     # natted at the moment of request processing); look at replies
386 393
 
387
-    t_on_reply("1");
394
+    t_on_reply(NAT_REPLY);
388 395
 
389 396
     if (!t_relay()) {
390 397
             sl_reply_error();
... ...
@@ -393,7 +400,7 @@ route[NAT] {
393 400
 }
394 401
 
395 402
 
396
-onreply_route[1] {
403
+onreply_route[NAT_REPLY] {
397 404
         # natted transaction ?
398 405
         if (isflagset(NAT_FLAG) && status =~ "(183)|2[0-9][0-9]") {
399 406
                 fix_nated_contact();
... ...
@@ -404,8 +411,8 @@ onreply_route[1] {
404 411
                 fix_nated_contact();
405 412
         };
406 413
 
407
-        # keep Cisco gatweay sending keep-alives
408
-        if (isflagset(7) && status=~"2[0-9][0-9]") {
414
+        # keep Cisco gateway sending keep-alives
415
+        if (isflagset(7) && status=~"2[0-9][0-9]") {   # flag(7) is mentioned NAT_FLAG ??
409 416
                 remove_hf("Session-Expires");
410 417
                 append_hf("Session-Expires: 60;refresher=UAC\r\n");
411 418
                 fix_nated_sdp("1");
... ...
@@ -416,7 +423,7 @@ onreply_route[1] {
416 423
 #
417 424
 # logic for calls to the PSTN
418 425
 #
419
-route[PSTN] {
426
+route[PSTN_ROUTE] {
420 427
 
421 428
         # discard non-PSTN methods
422 429
         if (!(method == "INVITE" || method == "ACK" || method == "CANCEL" || method == "OPTIONS" || method == "BYE")) {
... ...
@@ -451,7 +458,7 @@ route[PSTN] {
451 458
                 	};
452 459
 		} else {
453 460
 			# Allow free-pstn destinations without any checks
454
-			route(5);
461
+			route(PSTN2_ROUTE);
455 462
 			break;
456 463
 		};
457 464
 
... ...
@@ -460,7 +467,7 @@ route[PSTN] {
460 467
 			    sl_send_reply("403", "International numbers not allowed");
461 468
 			    break;
462 469
 			};
463
-			route(5);
470
+			route(PSTN2_ROUTE);
464 471
 		} else {
465 472
 			sl_send_reply("403", "Invalid Number");
466 473
 			break;
... ...
@@ -469,13 +476,13 @@ route[PSTN] {
469 476
 	break;
470 477
 }
471 478
 
472
-route[5] {
479
+route[PSTN2_ROUTE] {
473 480
 	rewritehostport("GW_IP_1:5060");
474 481
 	consume_credentials();
475 482
 	append_hf("P-Hint: GATEWAY\r\n");
476 483
 
477 484
 	# Try alternative gateway on failure
478
-	t_on_failure("7");
485
+	t_on_failure(PSTN_FAILURE);
479 486
         # Our PSTN gateway is symmetric and can handle direction=active flag
480 487
         # properly, therefore we don't have to use RTP proxy
481 488
 	t_relay();
... ...
@@ -483,7 +490,7 @@ route[5] {
483 490
 
484 491
 
485 492
 
486
-failure_route[7] {
493
+failure_route[PSTN_FAILURE] {
487 494
 	rewritehostport("GW_IP_2:5060");
488 495
 	append_branch();
489 496
 	t_relay();	
... ...
@@ -491,7 +498,7 @@ failure_route[7] {
491 498
 
492 499
 
493 500
 # ------------- handling of unavailable user ------------------
494
-route[VOICEMAIL] {
501
+route[VOICEMAIL_ROUTE] {
495 502
         # message store
496 503
         if (method == "MESSAGE") {
497 504
                 if (!t_newtran()) {