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 123
 
124 124
 /* frees the content of a lump struct */
125 125
 void free_lump(struct lump* l);
126
-/*frees an entire lump list, recursively */
126
+/* frees an entire lump list, recursively */
127 127
 void free_lump_list(struct lump* lump_list);
128
-
128
+/* count applied lumps in a list having a specific type */
129
+unsigned int count_applied_lumps(struct lump *ll, int type);
129 130
 #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 526
 #endif
527 527
 					(ip_addr_cmp(ip, &snd_i->send_sock->address)))
528 528
 				return 0;
529
-			else return 1;
529
+			else {
530
+				LUMP_SET_COND_TRUE(l);
531
+				return 1;
532
+			}
530 533
 		case COND_IF_DIFF_AF:
531 534
 			get_ip_port_proto;
532
-			if (ip->af!=snd_i->send_sock->address.af) return 1;
533
-			else return 0;
535
+			if (ip->af!=snd_i->send_sock->address.af) {
536
+				LUMP_SET_COND_TRUE(l);
537
+				return 1;
538
+			} else return 0;
534 539
 		case COND_IF_DIFF_PROTO:
535 540
 			get_ip_port_proto;
536
-			if (proto!=snd_i->send_sock->proto) return 1;
537
-			else return 0;
541
+			if (proto!=snd_i->send_sock->proto) {
542
+				LUMP_SET_COND_TRUE(l);
543
+				return 1;
544
+			} else return 0;
538 545
 		case COND_IF_DIFF_PORT:
539 546
 			get_ip_port_proto;
540
-			if (port!=snd_i->send_sock->port_no) return 1;
541
-			else return 0;
547
+			if (port!=snd_i->send_sock->port_no) {
548
+				LUMP_SET_COND_TRUE(l);
549
+				return 1;
550
+			} else return 0;
542 551
 		case COND_IF_DIFF_IP:
543 552
 			get_ip_port_proto;
544 553
 			if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
545
-			else return 1;
554
+			else {
555
+				LUMP_SET_COND_TRUE(l);
556
+				return 1;
557
+			}
546 558
 		case COND_IF_RAND:
547
-			return (rand()>=RAND_MAX/2);
559
+			if(rand()>=RAND_MAX/2) {
560
+				LUMP_SET_COND_TRUE(l);
561
+				return 1;
562
+			} else return 0;
548 563
 		default:
549 564
 			LOG(L_CRIT, "BUG: lump:w_check_opt: unknown lump condition %d\n",
550
-					cond);
565
+					l->u.cond);
551 566
 	}
552 567
 	return 0; /* false */
553 568
 }
... ...
@@ -778,7 +794,7 @@ static inline int lumps_len(struct sip_msg* msg, struct lump* lumps,
778 778
 
779 779
 	for(t=lumps;t;t=t->next){
780 780
 		/* 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))
781
+		if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t, msg, send_info))
782 782
 			continue;
783 783
 		for(r=t->before;r;r=r->before){
784 784
 			switch(r->op){
... ...
@@ -791,7 +807,7 @@ static inline int lumps_len(struct sip_msg* msg, struct lump* lumps,
791 791
 				case LUMP_ADD_OPT:
792 792
 					/* skip if this is an OPT lump and the condition is
793 793
 					 * not satisfied */
794
-					if (!lump_check_opt(r->u.cond, msg, send_info))
794
+					if (!lump_check_opt(r, msg, send_info))
795 795
 						goto skip_before;
796 796
 					break;
797 797
 				default:
... ...
@@ -847,7 +863,7 @@ skip_before:
847 847
 				case LUMP_ADD_OPT:
848 848
 					/* skip if this is an OPT lump and the condition is
849 849
 					 * not satisfied */
850
-					if (!lump_check_opt(r->u.cond, msg, send_info))
850
+					if (!lump_check_opt(r, msg, send_info))
851 851
 						goto skip_after;
852 852
 					break;
853 853
 				default:
... ...
@@ -1200,7 +1216,7 @@ static inline void process_lumps(	struct sip_msg* msg,
1200 1200
 				/* skip if this is an OPT lump and the condition is
1201 1201
 				 * not satisfied */
1202 1202
 				if ((t->op==LUMP_ADD_OPT) &&
1203
-						(!lump_check_opt(t->u.cond, msg, send_info)))
1203
+						(!lump_check_opt(t, msg, send_info)))
1204 1204
 					continue;
1205 1205
 				/* just add it here! */
1206 1206
 				/* process before  */
... ...
@@ -1217,7 +1233,7 @@ static inline void process_lumps(	struct sip_msg* msg,
1217 1217
 						case LUMP_ADD_OPT:
1218 1218
 							/* skip if this is an OPT lump and the condition is
1219 1219
 					 		* not satisfied */
1220
-							if (!lump_check_opt(r->u.cond, msg, send_info))
1220
+							if (!lump_check_opt(r, msg, send_info))
1221 1221
 								goto skip_before;
1222 1222
 							break;
1223 1223
 						default:
... ...
@@ -1258,7 +1274,7 @@ skip_before:
1258 1258
 						case LUMP_ADD_OPT:
1259 1259
 							/* skip if this is an OPT lump and the condition is
1260 1260
 					 		* not satisfied */
1261
-							if (!lump_check_opt(r->u.cond, msg, send_info))
1261
+							if (!lump_check_opt(r, msg, send_info))
1262 1262
 								goto skip_after;
1263 1263
 							break;
1264 1264
 						default:
... ...
@@ -1299,7 +1315,7 @@ skip_after:
1299 1299
 						case LUMP_ADD_OPT:
1300 1300
 							/* skip if this is an OPT lump and the condition is
1301 1301
 					 		* not satisfied */
1302
-							if (!lump_check_opt(r->u.cond, msg, send_info))
1302
+							if (!lump_check_opt(r, msg, send_info))
1303 1303
 								goto skip_nop_before;
1304 1304
 							break;
1305 1305
 						default:
... ...
@@ -1328,7 +1344,7 @@ skip_nop_before:
1328 1328
 						case LUMP_ADD_OPT:
1329 1329
 							/* skip if this is an OPT lump and the condition is
1330 1330
 					 		* not satisfied */
1331
-							if (!lump_check_opt(r->u.cond, msg, send_info))
1331
+							if (!lump_check_opt(r, msg, send_info))
1332 1332
 								goto skip_nop_after;
1333 1333
 							break;
1334 1334
 						default: