Browse code

core: added core.tcp_list rpc command

- print details about tcp connections

Daniel-Constantin Mierla authored on 13/12/2011 14:02:02
Showing 1 changed files
... ...
@@ -40,6 +40,7 @@
40 40
 #include "pt.h"
41 41
 #include "ut.h"
42 42
 #include "tcp_info.h"
43
+#include "tcp_conn.h"
43 44
 #include "tcp_options.h"
44 45
 #include "core_cmd.h"
45 46
 #include "cfg_core.h"
... ...
@@ -739,6 +740,89 @@ static void core_tcp_options(rpc_t* rpc, void* c)
739 739
 }
740 740
 
741 741
 
742
+static const char* core_tcp_list_doc[] = {
743
+	"Returns tcp connections details.",    /* Documentation string */
744
+	0                               /* Method signature(s) */
745
+};
746
+
747
+extern gen_lock_t* tcpconn_lock;
748
+extern struct tcp_connection** tcpconn_id_hash;
749
+
750
+static void core_tcp_list(rpc_t* rpc, void* c)
751
+{
752
+#ifdef USE_TCP
753
+	char src_ip[IP_ADDR_MAX_STR_SIZE];
754
+	char dst_ip[IP_ADDR_MAX_STR_SIZE];
755
+	void* handle;
756
+	char* state;
757
+	char* type;
758
+	struct tcp_connection* con;
759
+	int i, len, timeout;
760
+
761
+	TCPCONN_LOCK;
762
+	for(i = 0; i < TCP_ID_HASH_SIZE; i++) {
763
+		for (con = tcpconn_id_hash[i]; con; con = con->id_next) {
764
+			rpc->add(c, "{", &handle);
765
+			/* tcp data */
766
+			if (con->rcv.proto == PROTO_TCP)
767
+				type = "TCP";
768
+			else if (con->rcv.proto == PROTO_TCP)
769
+				type = "TLS";
770
+			else
771
+				type = "UNKNOWN";
772
+
773
+			if ((len = ip_addr2sbuf(&con->rcv.src_ip, src_ip, sizeof(src_ip)))
774
+					== 0)
775
+				BUG("failed to convert source ip");
776
+			src_ip[len] = 0;
777
+			if ((len = ip_addr2sbuf(&con->rcv.dst_ip, dst_ip, sizeof(dst_ip)))
778
+					== 0)
779
+				BUG("failed to convert destination ip");
780
+			dst_ip[len] = 0;
781
+			timeout = TICKS_TO_S(con->timeout - get_ticks_raw());
782
+			switch(con->state) {
783
+				case S_CONN_ERROR:
784
+					state = "CONN_ERROR";
785
+				break;
786
+				case S_CONN_BAD:
787
+					state = "CONN_BAD";
788
+				break;
789
+				case S_CONN_OK:
790
+					state = "CONN_OK";
791
+				break;
792
+				case S_CONN_INIT:
793
+					state = "CONN_INIT";
794
+				break;
795
+				case S_CONN_EOF:
796
+					state = "CONN_EOF";
797
+				break;
798
+				case S_CONN_ACCEPT:
799
+					state = "CONN_ACCEPT";
800
+				break;
801
+				case S_CONN_CONNECT:
802
+					state = "CONN_CONNECT";
803
+				break;
804
+				default:
805
+					state = "UNKNOWN";
806
+			}
807
+			rpc->struct_add(handle, "dssdsdsd",
808
+					"id", con->id,
809
+					"type", type,
810
+					"state", state,
811
+					"timeout", timeout,
812
+					"src_ip", src_ip,
813
+					"src_port", con->rcv.src_port,
814
+					"dst_ip", dst_ip,
815
+					"dst_port", con->rcv.dst_port);
816
+		}
817
+	}
818
+	TCPCONN_UNLOCK;
819
+#else
820
+	rpc->fault(c, 500, "tcp support not compiled");
821
+#endif
822
+}
823
+
824
+
742 825
 
743 826
 static const char* core_sctp_options_doc[] = {
744 827
 	"Returns active sctp options. With one parameter"
... ...
@@ -920,6 +1004,7 @@ static rpc_export_t core_rpc_methods[] = {
920 920
 #endif
921 921
 	{"core.tcp_info",          core_tcpinfo,           core_tcpinfo_doc,    0},
922 922
 	{"core.tcp_options",       core_tcp_options,       core_tcp_options_doc,0},
923
+	{"core.tcp_list",          core_tcp_list,          core_tcp_list_doc,0},
923 924
 	{"core.sctp_options",      core_sctp_options,      core_sctp_options_doc,
924 925
 		0},
925 926
 	{"core.sctp_info",         core_sctpinfo,          core_sctpinfo_doc,   0},