Browse code

core: added function to count applied lumps

- new defines to set/test lump flag

Daniel-Constantin Mierla authored on 27/04/2009 12:25:31
Showing 3 changed files
... ...
@@ -596,3 +596,22 @@ void del_nonshm_lump( struct lump** lump_list )
596 596
 	}
597 597
 }
598 598
 
599
+unsigned int count_applied_lumps(struct lump *ll, int type)
600
+{
601
+	unsigned int n = 0;
602
+	struct lump *l = 0;
603
+
604
+	for(l=ll; l; l=l->next) {
605
+		if (l->op==LUMP_NOP && l->type==type) {
606
+			if (l->after && l->after->op==LUMP_ADD_OPT) {
607
+				if (LUMP_IS_COND_TRUE(l->after)) {
608
+					n++;
609
+				}
610
+			} else {
611
+				n++;
612
+			}
613
+		}
614
+	}
615
+	return n;
616
+}
617
+
... ...
@@ -75,8 +75,11 @@ enum lump_conditions {	COND_FALSE,         /* always false */
75 75
 						   proto = protocol (tcp, udp, tls)
76 76
 						*/
77 77
 
78
-enum lump_flag { LUMPFLAG_NONE=0, LUMPFLAG_DUPED=1, LUMPFLAG_SHMEM=2, LUMPFLAG_BRANCH=4 };
78
+enum lump_flag { LUMPFLAG_NONE=0, LUMPFLAG_DUPED=1, LUMPFLAG_SHMEM=2,
79
+	LUMPFLAG_BRANCH=4, LUMPFLAG_COND_TRUE=8 };
79 80
 
81
+#define LUMP_SET_COND_TRUE(_lump)	 (_lump)->flags |= LUMPFLAG_COND_TRUE
82
+#define LUMP_IS_COND_TRUE(_lump)	 ((_lump)->flags & LUMPFLAG_COND_TRUE)
80 83
 
81 84
 struct lump{
82 85
 	enum _hdr_types_t type; /* HDR_VIA_T, HDR_OTHER_T (0), ... */
... ...
@@ -123,7 +126,8 @@ struct lump{
123 126
 
124 127
 /* frees the content of a lump struct */
125 128
 void free_lump(struct lump* l);
126
-/*frees an entire lump list, recursively */
129
+/* frees an entire lump list, recursively */
127 130
 void free_lump_list(struct lump* lump_list);
128
-
131
+/* count applied lumps in a list having a specific type */
132
+unsigned int count_applied_lumps(struct lump *ll, int type);
129 133
 #endif
... ...
@@ -487,7 +487,7 @@ char* clen_builder(	struct sip_msg* msg, int *clen_len, int diff,
487 487
 
488 488
 /* checks if a lump opt condition
489 489
  * returns 1 if cond is true, 0 if false */
490
-static inline int lump_check_opt(	enum lump_conditions cond,
490
+static inline int lump_check_opt(	struct lump *l,
491 491
 									struct sip_msg* msg,
492 492
 									struct dest_info* snd_i
493 493
 									)
... ...
@@ -511,10 +511,11 @@ static inline int lump_check_opt(	enum lump_conditions cond,
511 511
 				proto=msg->rcv.proto; \
512 512
 			} \
513 513
 
514
-	switch(cond){
514
+	switch(l->u.cond){
515 515
 		case COND_FALSE:
516 516
 			return 0;
517 517
 		case COND_TRUE:
518
+			LUMP_SET_COND_TRUE(l);
518 519
 			return 1;
519 520
 		case COND_IF_DIFF_REALMS:
520 521
 			get_ip_port_proto;
... ...
@@ -526,28 +527,43 @@ static inline int lump_check_opt(	enum lump_conditions cond,
526 527
 #endif
527 528
 					(ip_addr_cmp(ip, &snd_i->send_sock->address)))
528 529
 				return 0;
529
-			else return 1;
530
+			else {
531
+				LUMP_SET_COND_TRUE(l);
532
+				return 1;
533
+			}
530 534
 		case COND_IF_DIFF_AF:
531 535
 			get_ip_port_proto;
532
-			if (ip->af!=snd_i->send_sock->address.af) return 1;
533
-			else return 0;
536
+			if (ip->af!=snd_i->send_sock->address.af) {
537
+				LUMP_SET_COND_TRUE(l);
538
+				return 1;
539
+			} else return 0;
534 540
 		case COND_IF_DIFF_PROTO:
535 541
 			get_ip_port_proto;
536
-			if (proto!=snd_i->send_sock->proto) return 1;
537
-			else return 0;
542
+			if (proto!=snd_i->send_sock->proto) {
543
+				LUMP_SET_COND_TRUE(l);
544
+				return 1;
545
+			} else return 0;
538 546
 		case COND_IF_DIFF_PORT:
539 547
 			get_ip_port_proto;
540
-			if (port!=snd_i->send_sock->port_no) return 1;
541
-			else return 0;
548
+			if (port!=snd_i->send_sock->port_no) {
549
+				LUMP_SET_COND_TRUE(l);
550
+				return 1;
551
+			} else return 0;
542 552
 		case COND_IF_DIFF_IP:
543 553
 			get_ip_port_proto;
544 554
 			if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
545
-			else return 1;
555
+			else {
556
+				LUMP_SET_COND_TRUE(l);
557
+				return 1;
558
+			}
546 559
 		case COND_IF_RAND:
547
-			return (rand()>=RAND_MAX/2);
560
+			if(rand()>=RAND_MAX/2) {
561
+				LUMP_SET_COND_TRUE(l);
562
+				return 1;
563
+			} else return 0;
548 564
 		default:
549 565
 			LOG(L_CRIT, "BUG: lump:w_check_opt: unknown lump condition %d\n",
550
-					cond);
566
+					l->u.cond);
551 567
 	}
552 568
 	return 0; /* false */
553 569
 }
... ...
@@ -778,7 +794,7 @@ static inline int lumps_len(struct sip_msg* msg, struct lump* lumps,
778 794
 
779 795
 	for(t=lumps;t;t=t->next){
780 796
 		/* skip if this is an OPT lump and the condition is not satisfied */
781
-		if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t->u.cond, msg, send_info))
797
+		if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t, msg, send_info))
782 798
 			continue;
783 799
 		for(r=t->before;r;r=r->before){
784 800
 			switch(r->op){
... ...
@@ -791,7 +807,7 @@ static inline int lumps_len(struct sip_msg* msg, struct lump* lumps,
791 807
 				case LUMP_ADD_OPT:
792 808
 					/* skip if this is an OPT lump and the condition is
793 809
 					 * not satisfied */
794
-					if (!lump_check_opt(r->u.cond, msg, send_info))
810
+					if (!lump_check_opt(r, msg, send_info))
795 811
 						goto skip_before;
796 812
 					break;
797 813
 				default:
... ...
@@ -847,7 +863,7 @@ skip_before:
847 863
 				case LUMP_ADD_OPT:
848 864
 					/* skip if this is an OPT lump and the condition is
849 865
 					 * not satisfied */
850
-					if (!lump_check_opt(r->u.cond, msg, send_info))
866
+					if (!lump_check_opt(r, msg, send_info))
851 867
 						goto skip_after;
852 868
 					break;
853 869
 				default:
... ...
@@ -1200,7 +1216,7 @@ static inline void process_lumps(	struct sip_msg* msg,
1200 1216
 				/* skip if this is an OPT lump and the condition is
1201 1217
 				 * not satisfied */
1202 1218
 				if ((t->op==LUMP_ADD_OPT) &&
1203
-						(!lump_check_opt(t->u.cond, msg, send_info)))
1219
+						(!lump_check_opt(t, msg, send_info)))
1204 1220
 					continue;
1205 1221
 				/* just add it here! */
1206 1222
 				/* process before  */
... ...
@@ -1217,7 +1233,7 @@ static inline void process_lumps(	struct sip_msg* msg,
1217 1233
 						case LUMP_ADD_OPT:
1218 1234
 							/* skip if this is an OPT lump and the condition is
1219 1235
 					 		* not satisfied */
1220
-							if (!lump_check_opt(r->u.cond, msg, send_info))
1236
+							if (!lump_check_opt(r, msg, send_info))
1221 1237
 								goto skip_before;
1222 1238
 							break;
1223 1239
 						default:
... ...
@@ -1258,7 +1274,7 @@ skip_before:
1258 1274
 						case LUMP_ADD_OPT:
1259 1275
 							/* skip if this is an OPT lump and the condition is
1260 1276
 					 		* not satisfied */
1261
-							if (!lump_check_opt(r->u.cond, msg, send_info))
1277
+							if (!lump_check_opt(r, msg, send_info))
1262 1278
 								goto skip_after;
1263 1279
 							break;
1264 1280
 						default:
... ...
@@ -1299,7 +1315,7 @@ skip_after:
1299 1315
 						case LUMP_ADD_OPT:
1300 1316
 							/* skip if this is an OPT lump and the condition is
1301 1317
 					 		* not satisfied */
1302
-							if (!lump_check_opt(r->u.cond, msg, send_info))
1318
+							if (!lump_check_opt(r, msg, send_info))
1303 1319
 								goto skip_nop_before;
1304 1320
 							break;
1305 1321
 						default:
... ...
@@ -1328,7 +1344,7 @@ skip_nop_before:
1328 1344
 						case LUMP_ADD_OPT:
1329 1345
 							/* skip if this is an OPT lump and the condition is
1330 1346
 					 		* not satisfied */
1331
-							if (!lump_check_opt(r->u.cond, msg, send_info))
1347
+							if (!lump_check_opt(r, msg, send_info))
1332 1348
 								goto skip_nop_after;
1333 1349
 							break;
1334 1350
 						default: