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 3142
 	struct dns_hash_entry* e;
3143 3143
 	ticks_t now;
3144 3144
 
3145
+	if (!use_dns_cache){
3146
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3147
+		return;
3148
+	}
3145 3149
 	now=get_ticks_raw();
3146 3150
 	LOCK_DNS_HASH();
3147 3151
 		for (h=0; h<DNS_HASH_SIZE; h++){
... ...
@@ -3159,6 +3167,39 @@ void dns_cache_debug(rpc_t* rpc, void* ctx)
3159 3159
 
3160 3160
 
3161 3161
 #ifdef USE_DNS_CACHE_STATS
3162
+static unsigned long  stat_sum(int ivar, int breset)
3163
+{
3164
+	unsigned long isum=0;
3165
+	int i1=0;
3166
+
3167
+	for (; i1 < get_max_procs(); i1++)
3168
+		switch (ivar) {
3169
+			case 0:
3170
+				isum+=dns_cache_stats[i1].dns_req_cnt;
3171
+				if (breset)
3172
+					dns_cache_stats[i1].dns_req_cnt=0;
3173
+				break;
3174
+			case 1:
3175
+				isum+=dns_cache_stats[i1].dc_hits_cnt;
3176
+				if (breset)
3177
+					dns_cache_stats[i1].dc_hits_cnt=0;
3178
+				break;
3179
+			case 2:
3180
+				isum+=dns_cache_stats[i1].dc_neg_hits_cnt;
3181
+				if (breset)
3182
+					dns_cache_stats[i1].dc_neg_hits_cnt=0;
3183
+				break;
3184
+			case 3:
3185
+				isum+=dns_cache_stats[i1].dc_lru_cnt;
3186
+				if (breset)
3187
+					dns_cache_stats[i1].dc_lru_cnt=0;
3188
+				break;
3189
+		}
3190
+
3191
+	return isum;
3192
+}
3193
+
3194
+
3162 3195
 void dns_cache_stats_get(rpc_t* rpc, void* c)
3163 3196
 {
3164 3197
 	char *name=NULL;
... ...
@@ -3172,36 +3213,6 @@ void dns_cache_stats_get(rpc_t* rpc, void* c)
3172 3172
 		"dc_lru_cnt",
3173 3173
 		NULL
3174 3174
 	};
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 3175
 
3206 3176
 
3207 3177
 	if (!use_dns_cache) {
... ...
@@ -3249,6 +3260,10 @@ void dns_cache_debug_all(rpc_t* rpc, void* ctx)
3249 3249
 	int i;
3250 3250
 	ticks_t now;
3251 3251
 
3252
+	if (!use_dns_cache){
3253
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3254
+		return;
3255
+	}
3252 3256
 	now=get_ticks_raw();
3253 3257
 	LOCK_DNS_HASH();
3254 3258
 		for (h=0; h<DNS_HASH_SIZE; h++){
... ...
@@ -3295,6 +3310,7 @@ void dns_cache_debug_all(rpc_t* rpc, void* ctx)
3295 3295
 	UNLOCK_DNS_HASH();
3296 3296
 }
3297 3297
 
3298
+
3298 3299
 static char *print_type(unsigned short type)
3299 3300
 {
3300 3301
 	switch (type) {
... ...
@@ -3323,6 +3339,10 @@ void dns_cache_view(rpc_t* rpc, void* ctx)
3323 3323
 	ticks_t now;
3324 3324
 	str s;
3325 3325
 
3326
+	if (!use_dns_cache){
3327
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3328
+		return;
3329
+	}
3326 3330
 	now=get_ticks_raw();
3327 3331
 	LOCK_DNS_HASH();
3328 3332
 	for (h=0; h<DNS_HASH_SIZE; h++){
... ...
@@ -3425,6 +3445,10 @@ void dns_cache_flush(void)
3425 3425
 /* deletes all the entries from the cache */
3426 3426
 void dns_cache_delete_all(rpc_t* rpc, void* ctx)
3427 3427
 {
3428
+	if (!use_dns_cache){
3429
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3430
+		return;
3431
+	}
3428 3432
 	dns_cache_flush();
3429 3433
 }
3430 3434
 
... ...
@@ -3570,6 +3594,11 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
3570 3570
 	ip_addr = 0;
3571 3571
 	size = 0;
3572 3572
 
3573
+	if (!use_dns_cache){
3574
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3575
+		return;
3576
+	}
3577
+	
3573 3578
 	switch(type) {
3574 3579
 	case T_A:
3575 3580
 	case T_AAAA:
... ...
@@ -3772,6 +3801,11 @@ static void dns_cache_delete_record(rpc_t* rpc, void* ctx, unsigned short type)
3772 3772
 	str name;
3773 3773
 	int err, h, found=0;
3774 3774
 
3775
+	if (!use_dns_cache){
3776
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3777
+		return;
3778
+	}
3779
+	
3775 3780
 	if (rpc->scan(ctx, "S", &name) < 1)
3776 3781
 		return;
3777 3782
 
... ...
@@ -3834,6 +3868,10 @@ void dns_set_server_state_rpc(rpc_t* rpc, void* ctx)
3834 3834
 {
3835 3835
 	int	state;
3836 3836
 
3837
+	if (!use_dns_cache){
3838
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3839
+		return;
3840
+	}
3837 3841
 	if (rpc->scan(ctx, "d", &state) < 1)
3838 3842
 		return;
3839 3843
 	dns_set_server_state(state);
... ...
@@ -3842,6 +3880,10 @@ void dns_set_server_state_rpc(rpc_t* rpc, void* ctx)
3842 3842
 /* prints the DNS server state */
3843 3843
 void dns_get_server_state_rpc(rpc_t* rpc, void* ctx)
3844 3844
 {
3845
+	if (!use_dns_cache){
3846
+		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
3847
+		return;
3848
+	}
3845 3849
 	rpc->add(ctx, "d", dns_get_server_state());
3846 3850
 }
3847 3851
 #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 834
 /* rpc functions */
835 835
 void dst_blst_mem_info(rpc_t* rpc, void* ctx)
836 836
 {
837
+	if (!use_dst_blacklist){
838
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
839
+		return;
840
+	}
837 841
 	rpc->add(ctx, "dd",  *blst_mem_used, blst_max_mem);
838 842
 }
839 843
 
... ...
@@ -857,6 +862,29 @@ static char* get_proto_name(unsigned char proto)
857 857
 
858 858
 
859 859
 #ifdef USE_DST_BLACKLIST_STATS
860
+
861
+static unsigned long  stat_sum(int ivar, int breset) {
862
+	unsigned long isum=0;
863
+	int i1=0;
864
+
865
+	for (; i1 < get_max_procs(); i1++)
866
+		switch (ivar) {
867
+			case 0:
868
+				isum+=dst_blacklist_stats[i1].bkl_hit_cnt;
869
+				if (breset)
870
+					dst_blacklist_stats[i1].bkl_hit_cnt=0;
871
+				break;
872
+			case 1:
873
+				isum+=dst_blacklist_stats[i1].bkl_lru_cnt;
874
+				if (breset)
875
+					dst_blacklist_stats[i1].bkl_lru_cnt=0;
876
+				break;
877
+		}
878
+
879
+		return isum;
880
+}
881
+
882
+
860 883
 void dst_blst_stats_get(rpc_t* rpc, void* c)
861 884
 {
862 885
 	char *name=NULL;
... ...
@@ -868,27 +896,11 @@ void dst_blst_stats_get(rpc_t* rpc, void* c)
868 868
 		"bkl_lru_cnt",
869 869
 		NULL
870 870
 	};
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;
871
+	
872
+	if (!use_dst_blacklist){
873
+		rpc->fault(c, 500, "dst blacklist support disabled");
874
+		return;
890 875
 	}
891
-
892 876
 	if (rpc->scan(c, "s", &name) < 0)
893 877
 		return;
894 878
 	if (rpc->scan(c, "d", &reset) < 0)
... ...
@@ -928,6 +940,10 @@ void dst_blst_debug(rpc_t* rpc, void* ctx)
928 928
 	ticks_t now;
929 929
 	struct ip_addr ip;
930 930
 
931
+	if (!use_dst_blacklist){
932
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
933
+		return;
934
+	}
931 935
 	now=get_ticks_raw();
932 936
 		for(h=0; h<DST_BLST_HASH_SIZE; h++){
933 937
 			LOCK_BLST(h);
... ...
@@ -954,7 +970,10 @@ void dst_blst_hash_stats(rpc_t* rpc, void* ctx)
954 954
 
955 955
 	n=0;
956 956
 #endif
957
-
957
+	if (!use_dst_blacklist){
958
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
959
+		return;
960
+	}
958 961
 		for(h=0; h<DST_BLST_HASH_SIZE; h++){
959 962
 #ifdef BLST_HASH_STATS
960 963
 			LOCK_BLST(h);
... ...
@@ -975,6 +994,10 @@ void dst_blst_view(rpc_t* rpc, void* ctx)
975 975
 	ticks_t now;
976 976
 	struct ip_addr ip;
977 977
 
978
+	if (!use_dst_blacklist){
979
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
980
+		return;
981
+	}
978 982
 	now=get_ticks_raw();
979 983
 	for(h=0; h<DST_BLST_HASH_SIZE; h++) {
980 984
 		LOCK_BLST(h);
... ...
@@ -1025,6 +1048,10 @@ void dst_blst_flush(void)
1025 1025
 /* rpc wrapper function for dst_blst_flush() */
1026 1026
 void dst_blst_delete_all(rpc_t* rpc, void* ctx)
1027 1027
 {
1028
+	if (!use_dst_blacklist){
1029
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
1030
+		return;
1031
+	}
1028 1032
 	dst_blst_flush();
1029 1033
 }
1030 1034
 
... ...
@@ -1036,6 +1063,10 @@ void dst_blst_add(rpc_t* rpc, void* ctx)
1036 1036
 	unsigned char err_flags;
1037 1037
 	struct ip_addr *ip_addr;
1038 1038
 
1039
+	if (!use_dst_blacklist){
1040
+		rpc->fault(ctx, 500, "dst blacklist support disabled");
1041
+		return;
1042
+	}
1039 1043
 	if (rpc->scan(ctx, "Sddd", &ip, &port, &proto, &flags) < 4)
1040 1044
 		return;
1041 1045