Browse code

- fix: when dns or blacklist are disabled, reply with an error from the corresponding rpc function (way better then crashing)

- un-nested the stat_sum function

Andrei Pelinescu-Onciul authored on 09/10/2007 20:35:50
Showing 2 changed files
... ...
@@ -3132,6 +3132,10 @@ int dns_get_server_state(void)
3132 3132
 /* rpc functions */
3133 3133
 void dns_cache_mem_info(rpc_t* rpc, void* ctx)
3134 3134
 {
3135
+	if (!use_dns_cache){
3136
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3137
+		return;
3138
+	}
3135 3139
 	rpc->add(ctx, "dd",  *dns_cache_mem_used, dns_cache_max_mem);
3136 3140
 }
3137 3141
 
... ...
@@ -3142,6 +3146,10 @@ void dns_cache_debug(rpc_t* rpc, void* ctx)
3142 3146
 	struct dns_hash_entry* e;
3143 3147
 	ticks_t now;
3144 3148
 
3149
+	if (!use_dns_cache){
3150
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3151
+		return;
3152
+	}
3145 3153
 	now=get_ticks_raw();
3146 3154
 	LOCK_DNS_HASH();
3147 3155
 		for (h=0; h<DNS_HASH_SIZE; h++){
... ...
@@ -3159,6 +3167,39 @@ void dns_cache_debug(rpc_t* rpc, void* ctx)
3159 3167
 
3160 3168
 
3161 3169
 #ifdef USE_DNS_CACHE_STATS
3170
+static unsigned long  stat_sum(int ivar, int breset)
3171
+{
3172
+	unsigned long isum=0;
3173
+	int i1=0;
3174
+
3175
+	for (; i1 < get_max_procs(); i1++)
3176
+		switch (ivar) {
3177
+			case 0:
3178
+				isum+=dns_cache_stats[i1].dns_req_cnt;
3179
+				if (breset)
3180
+					dns_cache_stats[i1].dns_req_cnt=0;
3181
+				break;
3182
+			case 1:
3183
+				isum+=dns_cache_stats[i1].dc_hits_cnt;
3184
+				if (breset)
3185
+					dns_cache_stats[i1].dc_hits_cnt=0;
3186
+				break;
3187
+			case 2:
3188
+				isum+=dns_cache_stats[i1].dc_neg_hits_cnt;
3189
+				if (breset)
3190
+					dns_cache_stats[i1].dc_neg_hits_cnt=0;
3191
+				break;
3192
+			case 3:
3193
+				isum+=dns_cache_stats[i1].dc_lru_cnt;
3194
+				if (breset)
3195
+					dns_cache_stats[i1].dc_lru_cnt=0;
3196
+				break;
3197
+		}
3198
+
3199
+	return isum;
3200
+}
3201
+
3202
+
3162 3203
 void dns_cache_stats_get(rpc_t* rpc, void* c)
3163 3204
 {
3164 3205
 	char *name=NULL;
... ...
@@ -3172,36 +3213,6 @@ void dns_cache_stats_get(rpc_t* rpc, void* c)
3172 3213
 		"dc_lru_cnt",
3173 3214
 		NULL
3174 3215
 	};
3175
-	unsigned long  stat_sum(int ivar, int breset) {
3176
-		unsigned long isum=0;
3177
-		int i1=0;
3178
-
3179
-		for (; i1 < get_max_procs(); i1++)
3180
-			switch (ivar) {
3181
-				case 0:
3182
-					isum+=dns_cache_stats[i1].dns_req_cnt;
3183
-					if (breset)
3184
-						dns_cache_stats[i1].dns_req_cnt=0;
3185
-					break;
3186
-				case 1:
3187
-					isum+=dns_cache_stats[i1].dc_hits_cnt;
3188
-					if (breset)
3189
-						dns_cache_stats[i1].dc_hits_cnt=0;
3190
-					break;
3191
-				case 2:
3192
-					isum+=dns_cache_stats[i1].dc_neg_hits_cnt;
3193
-					if (breset)
3194
-						dns_cache_stats[i1].dc_neg_hits_cnt=0;
3195
-					break;
3196
-				case 3:
3197
-					isum+=dns_cache_stats[i1].dc_lru_cnt;
3198
-					if (breset)
3199
-						dns_cache_stats[i1].dc_lru_cnt=0;
3200
-					break;
3201
-			}
3202
-
3203
-		return isum;
3204
-	}
3205 3216
 
3206 3217
 
3207 3218
 	if (!use_dns_cache) {
... ...
@@ -3249,6 +3260,10 @@ void dns_cache_debug_all(rpc_t* rpc, void* ctx)
3249 3260
 	int i;
3250 3261
 	ticks_t now;
3251 3262
 
3263
+	if (!use_dns_cache){
3264
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3265
+		return;
3266
+	}
3252 3267
 	now=get_ticks_raw();
3253 3268
 	LOCK_DNS_HASH();
3254 3269
 		for (h=0; h<DNS_HASH_SIZE; h++){
... ...
@@ -3295,6 +3310,7 @@ void dns_cache_debug_all(rpc_t* rpc, void* ctx)
3295 3310
 	UNLOCK_DNS_HASH();
3296 3311
 }
3297 3312
 
3313
+
3298 3314
 static char *print_type(unsigned short type)
3299 3315
 {
3300 3316
 	switch (type) {
... ...
@@ -3323,6 +3339,10 @@ void dns_cache_view(rpc_t* rpc, void* ctx)
3323 3339
 	ticks_t now;
3324 3340
 	str s;
3325 3341
 
3342
+	if (!use_dns_cache){
3343
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3344
+		return;
3345
+	}
3326 3346
 	now=get_ticks_raw();
3327 3347
 	LOCK_DNS_HASH();
3328 3348
 	for (h=0; h<DNS_HASH_SIZE; h++){
... ...
@@ -3425,6 +3445,10 @@ void dns_cache_flush(void)
3425 3445
 /* deletes all the entries from the cache */
3426 3446
 void dns_cache_delete_all(rpc_t* rpc, void* ctx)
3427 3447
 {
3448
+	if (!use_dns_cache){
3449
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3450
+		return;
3451
+	}
3428 3452
 	dns_cache_flush();
3429 3453
 }
3430 3454
 
... ...
@@ -3570,6 +3594,11 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
3570 3594
 	ip_addr = 0;
3571 3595
 	size = 0;
3572 3596
 
3597
+	if (!use_dns_cache){
3598
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3599
+		return;
3600
+	}
3601
+	
3573 3602
 	switch(type) {
3574 3603
 	case T_A:
3575 3604
 	case T_AAAA:
... ...
@@ -3772,6 +3801,11 @@ static void dns_cache_delete_record(rpc_t* rpc, void* ctx, unsigned short type)
3772 3801
 	str name;
3773 3802
 	int err, h, found=0;
3774 3803
 
3804
+	if (!use_dns_cache){
3805
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3806
+		return;
3807
+	}
3808
+	
3775 3809
 	if (rpc->scan(ctx, "S", &name) < 1)
3776 3810
 		return;
3777 3811
 
... ...
@@ -3834,6 +3868,10 @@ void dns_set_server_state_rpc(rpc_t* rpc, void* ctx)
3834 3868
 {
3835 3869
 	int	state;
3836 3870
 
3871
+	if (!use_dns_cache){
3872
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3873
+		return;
3874
+	}
3837 3875
 	if (rpc->scan(ctx, "d", &state) < 1)
3838 3876
 		return;
3839 3877
 	dns_set_server_state(state);
... ...
@@ -3842,6 +3880,10 @@ void dns_set_server_state_rpc(rpc_t* rpc, void* ctx)
3842 3880
 /* prints the DNS server state */
3843 3881
 void dns_get_server_state_rpc(rpc_t* rpc, void* ctx)
3844 3882
 {
3883
+	if (!use_dns_cache){
3884
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3885
+		return;
3886
+	}
3845 3887
 	rpc->add(ctx, "d", dns_get_server_state());
3846 3888
 }
3847 3889
 #endif /* DNS_WATCHDOG_SUPPORT */
... ...
@@ -39,6 +39,7 @@
39 39
 #ifdef USE_DST_BLACKLIST
40 40
 
41 41
 #include "dst_blacklist.h"
42
+#include "globals.h"
42 43
 #include "mem/shm_mem.h"
43 44
 #include "hashes.h"
44 45
 #include "locking.h"
... ...
@@ -834,6 +835,10 @@ int dst_blacklist_del(struct dest_info* si, struct sip_msg* msg)
834 835
 /* rpc functions */
835 836
 void dst_blst_mem_info(rpc_t* rpc, void* ctx)
836 837
 {
838
+	if (!use_dst_blacklist){
839
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
840
+		return;
841
+	}
837 842
 	rpc->add(ctx, "dd",  *blst_mem_used, blst_max_mem);
838 843
 }
839 844
 
... ...
@@ -857,6 +862,29 @@ static char* get_proto_name(unsigned char proto)
857 862
 
858 863
 
859 864
 #ifdef USE_DST_BLACKLIST_STATS
865
+
866
+static unsigned long  stat_sum(int ivar, int breset) {
867
+	unsigned long isum=0;
868
+	int i1=0;
869
+
870
+	for (; i1 < get_max_procs(); i1++)
871
+		switch (ivar) {
872
+			case 0:
873
+				isum+=dst_blacklist_stats[i1].bkl_hit_cnt;
874
+				if (breset)
875
+					dst_blacklist_stats[i1].bkl_hit_cnt=0;
876
+				break;
877
+			case 1:
878
+				isum+=dst_blacklist_stats[i1].bkl_lru_cnt;
879
+				if (breset)
880
+					dst_blacklist_stats[i1].bkl_lru_cnt=0;
881
+				break;
882
+		}
883
+
884
+		return isum;
885
+}
886
+
887
+
860 888
 void dst_blst_stats_get(rpc_t* rpc, void* c)
861 889
 {
862 890
 	char *name=NULL;
... ...
@@ -868,27 +896,11 @@ void dst_blst_stats_get(rpc_t* rpc, void* c)
868 896
 		"bkl_lru_cnt",
869 897
 		NULL
870 898
 	};
871
-	unsigned long  stat_sum(int ivar, int breset) {
872
-		unsigned long isum=0;
873
-		int i1=0;
874
-
875
-		for (; i1 < get_max_procs(); i1++)
876
-			switch (ivar) {
877
-				case 0:
878
-					isum+=dst_blacklist_stats[i1].bkl_hit_cnt;
879
-					if (breset)
880
-						dst_blacklist_stats[i1].bkl_hit_cnt=0;
881
-					break;
882
-				case 1:
883
-					isum+=dst_blacklist_stats[i1].bkl_lru_cnt;
884
-					if (breset)
885
-						dst_blacklist_stats[i1].bkl_lru_cnt=0;
886
-					break;
887
-			}
888
-
889
-			return isum;
899
+	
900
+	if (!use_dst_blacklist){
901
+		rpc->fault(c, 500, "dst blacklist support disabled");
902
+		return;
890 903
 	}
891
-
892 904
 	if (rpc->scan(c, "s", &name) < 0)
893 905
 		return;
894 906
 	if (rpc->scan(c, "d", &reset) < 0)
... ...
@@ -928,6 +940,10 @@ void dst_blst_debug(rpc_t* rpc, void* ctx)
928 940
 	ticks_t now;
929 941
 	struct ip_addr ip;
930 942
 
943
+	if (!use_dst_blacklist){
944
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
945
+		return;
946
+	}
931 947
 	now=get_ticks_raw();
932 948
 		for(h=0; h<DST_BLST_HASH_SIZE; h++){
933 949
 			LOCK_BLST(h);
... ...
@@ -954,7 +970,10 @@ void dst_blst_hash_stats(rpc_t* rpc, void* ctx)
954 970
 
955 971
 	n=0;
956 972
 #endif
957
-
973
+	if (!use_dst_blacklist){
974
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
975
+		return;
976
+	}
958 977
 		for(h=0; h<DST_BLST_HASH_SIZE; h++){
959 978
 #ifdef BLST_HASH_STATS
960 979
 			LOCK_BLST(h);
... ...
@@ -975,6 +994,10 @@ void dst_blst_view(rpc_t* rpc, void* ctx)
975 994
 	ticks_t now;
976 995
 	struct ip_addr ip;
977 996
 
997
+	if (!use_dst_blacklist){
998
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
999
+		return;
1000
+	}
978 1001
 	now=get_ticks_raw();
979 1002
 	for(h=0; h<DST_BLST_HASH_SIZE; h++) {
980 1003
 		LOCK_BLST(h);
... ...
@@ -1025,6 +1048,10 @@ void dst_blst_flush(void)
1025 1048
 /* rpc wrapper function for dst_blst_flush() */
1026 1049
 void dst_blst_delete_all(rpc_t* rpc, void* ctx)
1027 1050
 {
1051
+	if (!use_dst_blacklist){
1052
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
1053
+		return;
1054
+	}
1028 1055
 	dst_blst_flush();
1029 1056
 }
1030 1057
 
... ...
@@ -1036,6 +1063,10 @@ void dst_blst_add(rpc_t* rpc, void* ctx)
1036 1063
 	unsigned char err_flags;
1037 1064
 	struct ip_addr *ip_addr;
1038 1065
 
1066
+	if (!use_dst_blacklist){
1067
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
1068
+		return;
1069
+	}
1039 1070
 	if (rpc->scan(ctx, "Sddd", &ip, &port, &proto, &flags) < 4)
1040 1071
 		return;
1041 1072