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 131
 	"dst blacklist  debug  info.",  /* Documentation string */
129 132
 	0                               /* Method signature(s) */
130 133
 };
134
+static const char* dst_blst_view_doc[] = {
135
+	"dst blacklist dump in human-readable format.",  /* Documentation string */
136
+	0                               /* Method signature(s) */
137
+};
138
+static const char* dst_blst_delete_all_doc[] = {
139
+	"Deletes all the entries from the dst blacklist except the permanent ones.",  /* Documentation string */
140
+	0                               /* Method signature(s) */
141
+};
142
+static const char* dst_blst_add_doc[] = {
143
+	"Adds a new entry to the dst blacklist.",  /* Documentation string */
144
+	0                               /* Method signature(s) */
145
+};
146
+
131 147
 #endif
132 148
 
133 149
 
... ...
@@ -542,6 +558,9 @@ rpc_export_t core_rpc_methods[] = {
542 558
 #ifdef USE_DST_BLACKLIST
543 559
 	{"dst_blacklist.mem_info",  dst_blst_mem_info,     dst_blst_mem_info_doc,     0	},
544 560
 	{"dst_blacklist.debug",    dst_blst_debug,         dst_blst_debug_doc,        0	},
561
+	{"dst_blacklist.view",     dst_blst_view,         dst_blst_view_doc,         0	},
562
+	{"dst_blacklist.delete_all", dst_blst_delete_all, dst_blst_delete_all_doc,   0	},
563
+	{"dst_blacklist.add",      dst_blst_add,          dst_blst_add_doc,          0	},
545 564
 #endif
546 565
 	{0, 0, 0, 0}
547 566
 };
... ...
@@ -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 787
 		}
787 788
 }
788 789
 
790
+/* dumps the content of the blacklist in a human-readable format */
791
+void dst_blst_view(rpc_t* rpc, void* ctx)
792
+{
793
+	int h;
794
+	struct dst_blst_entry* e;
795
+	ticks_t now;
796
+	struct ip_addr ip;
797
+	void* handle;
798
+	
799
+	now=get_ticks_raw();
800
+		for(h=0; h<DST_BLST_HASH_SIZE; h++){
801
+			LOCK_BLST(h);
802
+			for(e=dst_blst_hash[h].first; e; e=e->next){
803
+				rpc->add(ctx, "{", &handle);
804
+				dst_blst_entry2ip(&ip, e);
805
+				rpc->struct_add(handle, "s", "protocol",
806
+							get_proto_name(e->proto));
807
+				rpc->struct_add(handle, "s", "ip",
808
+							ip_addr2a(&ip));
809
+				rpc->struct_add(handle, "d", "port",
810
+							e->port);
811
+				rpc->struct_add(handle, "d", "expires in (s)",
812
+							(s_ticks_t)(now-e->expire)<=0?
813
+							TICKS_TO_S(e->expire-now):
814
+							-TICKS_TO_S(now-e->expire));
815
+				rpc->struct_add(handle, "d", "flags",
816
+							e->flags);
817
+			}
818
+			UNLOCK_BLST(h);
819
+		}
820
+}
821
+
822
+/* deletes all the entries from the blacklist except the permanent ones
823
+ * (which are marked with BLST_PERMANENT)
824
+ */
825
+void dst_blst_flush(void)
826
+{
827
+	int h;
828
+	struct dst_blst_entry* e;
829
+	struct dst_blst_entry** last;
830
+
831
+	for(h=0; h<DST_BLST_HASH_SIZE; h++){
832
+		LOCK_BLST(h);
833
+		last = &dst_blst_hash[h].first;
834
+		for(e=dst_blst_hash[h].first; e; e=e->next){
835
+			if (e->flags & BLST_PERMANENT) {
836
+				/* permanent entry, do not remove it from the list */
837
+				*last = e;
838
+				last = &e->next;
839
+			} else {
840
+				/* remove the entry from the list */
841
+				*blst_mem_used-=DST_BLST_ENTRY_SIZE(*e);
842
+				blst_destroy_entry(e);
843
+				BLST_HASH_STATS_DEC(h);
844
+			}
845
+		}
846
+		*last = NULL;
847
+		UNLOCK_BLST(h);
848
+	}
849
+}
850
+
851
+/* rpc wrapper function for dst_blst_flush() */
852
+void dst_blst_delete_all(rpc_t* rpc, void* ctx)
853
+{
854
+	dst_blst_flush();
855
+}
856
+
857
+/* Adds a new entry to the blacklist */
858
+void dst_blst_add(rpc_t* rpc, void* ctx)
859
+{
860
+	str ip;
861
+	int port, proto, flags;
862
+	unsigned char err_flags;
863
+	struct ip_addr *ip_addr;
864
+
865
+	if (rpc->scan(ctx, "Sddd", &ip, &port, &proto, &flags) < 4)
866
+		return;
867
+
868
+	err_flags = (unsigned char)flags;
869
+	/* sanity checks */
870
+	if ((unsigned char)proto > PROTO_SCTP) {
871
+		rpc->fault(ctx, 400, "Unknown protocol");
872
+		return;
873
+	}
874
+
875
+	if (err_flags & BLST_IS_IPV6) {
876
+		/* IPv6 address is specified */
877
+		ip_addr = str2ip6(&ip);
878
+	} else {
879
+		/* try IPv4 first, than IPv6 */
880
+		ip_addr = str2ip(&ip);
881
+		if (!ip_addr) {
882
+			ip_addr = str2ip6(&ip);
883
+			err_flags |= BLST_IS_IPV6;
884
+		}
885
+	}
886
+	if (!ip_addr) {
887
+		rpc->fault(ctx, 400, "Malformed ip address");
888
+		return;
889
+	}
890
+
891
+	if (dst_blacklist_add_ip(err_flags, proto, ip_addr, port))
892
+		rpc->fault(ctx, 400, "Failed to add the entry to the blacklist");
893
+}
789 894
 
790 895
 #endif /* USE_DST_BLACKLIST */
791 896
 
... ...
@@ -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