Browse code

New RPC commands to manipulate the destination blacklist:

- dst_blacklist.view: dumps the content of the blacklist
- dst_blacklist.delete_all: deletes all the entries
(except the permanent ones)
- dst_blacklist.add: Adds a new entry to the blacklsit

Miklos Tirpak authored on 26/07/2007 11:42:53
Showing 3 changed files
... ...
@@ -119,6 +119,9 @@ static const char* dns_set_server_state_doc[] = {
119 119
 #ifdef USE_DST_BLACKLIST
120 120
 void dst_blst_debug(rpc_t* rpc, void* ctx);
121 121
 void dst_blst_mem_info(rpc_t* rpc, void* ctx);
122
+void dst_blst_view(rpc_t* rpc, void* ctx);
123
+void dst_blst_delete_all(rpc_t* rpc, void* ctx);
124
+void dst_blst_add(rpc_t* rpc, void* ctx);
122 125
 
123 126
 static const char* dst_blst_mem_info_doc[] = {
124 127
 	"dst blacklist memory usage info.",  /* Documentation string */
... ...
@@ -128,6 +131,19 @@ static const char* dst_blst_debug_doc[] = {
128 128
 	"dst blacklist  debug  info.",  /* Documentation string */
129 129
 	0                               /* Method signature(s) */
130 130
 };
131
+static const char* dst_blst_view_doc[] = {
132
+	"dst blacklist dump in human-readable format.",  /* Documentation string */
133
+	0                               /* Method signature(s) */
134
+};
135
+static const char* dst_blst_delete_all_doc[] = {
136
+	"Deletes all the entries from the dst blacklist except the permanent ones.",  /* Documentation string */
137
+	0                               /* Method signature(s) */
138
+};
139
+static const char* dst_blst_add_doc[] = {
140
+	"Adds a new entry to the dst blacklist.",  /* Documentation string */
141
+	0                               /* Method signature(s) */
142
+};
143
+
131 144
 #endif
132 145
 
133 146
 
... ...
@@ -542,6 +558,9 @@ rpc_export_t core_rpc_methods[] = {
542 542
 #ifdef USE_DST_BLACKLIST
543 543
 	{"dst_blacklist.mem_info",  dst_blst_mem_info,     dst_blst_mem_info_doc,     0	},
544 544
 	{"dst_blacklist.debug",    dst_blst_debug,         dst_blst_debug_doc,        0	},
545
+	{"dst_blacklist.view",     dst_blst_view,         dst_blst_view_doc,         0	},
546
+	{"dst_blacklist.delete_all", dst_blst_delete_all, dst_blst_delete_all_doc,   0	},
547
+	{"dst_blacklist.add",      dst_blst_add,          dst_blst_add_doc,          0	},
545 548
 #endif
546 549
 	{0, 0, 0, 0}
547 550
 };
... ...
@@ -46,6 +46,7 @@
46 46
 #include "error.h"
47 47
 #include "rpc.h"
48 48
 #include "compiler_opt.h"
49
+#include "resolve.h" /* for str2ip */
49 50
 
50 51
 
51 52
 
... ...
@@ -786,6 +787,110 @@ void dst_blst_hash_stats(rpc_t* rpc, void* ctx)
786 786
 		}
787 787
 }
788 788
 
789
+/* dumps the content of the blacklist in a human-readable format */
790
+void dst_blst_view(rpc_t* rpc, void* ctx)
791
+{
792
+	int h;
793
+	struct dst_blst_entry* e;
794
+	ticks_t now;
795
+	struct ip_addr ip;
796
+	void* handle;
797
+	
798
+	now=get_ticks_raw();
799
+		for(h=0; h<DST_BLST_HASH_SIZE; h++){
800
+			LOCK_BLST(h);
801
+			for(e=dst_blst_hash[h].first; e; e=e->next){
802
+				rpc->add(ctx, "{", &handle);
803
+				dst_blst_entry2ip(&ip, e);
804
+				rpc->struct_add(handle, "s", "protocol",
805
+							get_proto_name(e->proto));
806
+				rpc->struct_add(handle, "s", "ip",
807
+							ip_addr2a(&ip));
808
+				rpc->struct_add(handle, "d", "port",
809
+							e->port);
810
+				rpc->struct_add(handle, "d", "expires in (s)",
811
+							(s_ticks_t)(now-e->expire)<=0?
812
+							TICKS_TO_S(e->expire-now):
813
+							-TICKS_TO_S(now-e->expire));
814
+				rpc->struct_add(handle, "d", "flags",
815
+							e->flags);
816
+			}
817
+			UNLOCK_BLST(h);
818
+		}
819
+}
820
+
821
+/* deletes all the entries from the blacklist except the permanent ones
822
+ * (which are marked with BLST_PERMANENT)
823
+ */
824
+void dst_blst_flush(void)
825
+{
826
+	int h;
827
+	struct dst_blst_entry* e;
828
+	struct dst_blst_entry** last;
829
+
830
+	for(h=0; h<DST_BLST_HASH_SIZE; h++){
831
+		LOCK_BLST(h);
832
+		last = &dst_blst_hash[h].first;
833
+		for(e=dst_blst_hash[h].first; e; e=e->next){
834
+			if (e->flags & BLST_PERMANENT) {
835
+				/* permanent entry, do not remove it from the list */
836
+				*last = e;
837
+				last = &e->next;
838
+			} else {
839
+				/* remove the entry from the list */
840
+				*blst_mem_used-=DST_BLST_ENTRY_SIZE(*e);
841
+				blst_destroy_entry(e);
842
+				BLST_HASH_STATS_DEC(h);
843
+			}
844
+		}
845
+		*last = NULL;
846
+		UNLOCK_BLST(h);
847
+	}
848
+}
849
+
850
+/* rpc wrapper function for dst_blst_flush() */
851
+void dst_blst_delete_all(rpc_t* rpc, void* ctx)
852
+{
853
+	dst_blst_flush();
854
+}
855
+
856
+/* Adds a new entry to the blacklist */
857
+void dst_blst_add(rpc_t* rpc, void* ctx)
858
+{
859
+	str ip;
860
+	int port, proto, flags;
861
+	unsigned char err_flags;
862
+	struct ip_addr *ip_addr;
863
+
864
+	if (rpc->scan(ctx, "Sddd", &ip, &port, &proto, &flags) < 4)
865
+		return;
866
+
867
+	err_flags = (unsigned char)flags;
868
+	/* sanity checks */
869
+	if ((unsigned char)proto > PROTO_SCTP) {
870
+		rpc->fault(ctx, 400, "Unknown protocol");
871
+		return;
872
+	}
873
+
874
+	if (err_flags & BLST_IS_IPV6) {
875
+		/* IPv6 address is specified */
876
+		ip_addr = str2ip6(&ip);
877
+	} else {
878
+		/* try IPv4 first, than IPv6 */
879
+		ip_addr = str2ip(&ip);
880
+		if (!ip_addr) {
881
+			ip_addr = str2ip6(&ip);
882
+			err_flags |= BLST_IS_IPV6;
883
+		}
884
+	}
885
+	if (!ip_addr) {
886
+		rpc->fault(ctx, 400, "Malformed ip address");
887
+		return;
888
+	}
889
+
890
+	if (dst_blacklist_add_ip(err_flags, proto, ip_addr, port))
891
+		rpc->fault(ctx, 400, "Failed to add the entry to the blacklist");
892
+}
789 893
 
790 894
 #endif /* USE_DST_BLACKLIST */
791 895
 
... ...
@@ -72,4 +72,10 @@ void destroy_dst_blacklist();
72 72
 int dst_blacklist_add(unsigned char err_flags, struct dest_info* si);
73 73
 
74 74
 int dst_is_blacklisted(struct dest_info* si);
75
+
76
+/* deletes all the entries from the blacklist except the permanent ones
77
+ * (which are marked with BLST_PERMANENT)
78
+ */
79
+void dst_blst_flush(void);
80
+
75 81
 #endif