[Sheepdog] [PATCH] Make connection information more readable

Yunkai Zhang yunkai.me at gmail.com
Sat Apr 28 10:07:20 CEST 2012


From: Yunkai Zhang <qiushu.zyk at taobao.com>

Add ipstr and port in connection structure, and fill these values
in create_client(). We can dprintf them when we need to show us where
the client connection comes from.

To help us debuging, I dprintf these info in client_rx_handler() and
destroy_client().

Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com>
---
 include/net.h |    4 ++++
 sheep/sdnet.c |   21 +++++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/include/net.h b/include/net.h
index 2d087e2..8012eac 100644
--- a/include/net.h
+++ b/include/net.h
@@ -2,6 +2,7 @@
 #define __NET_H__
 
 #include <sys/socket.h>
+#include <arpa/inet.h>
 
 #define DEFAULT_SOCKET_TIMEOUT 5 /* seconds */
 
@@ -17,6 +18,9 @@ struct connection {
 	int fd;
 	unsigned int events;
 
+	uint16_t port;
+	char ipstr[INET6_ADDRSTRLEN];
+
 	enum conn_state c_rx_state;
 	int rx_length;
 	void *rx_buf;
diff --git a/sheep/sdnet.c b/sheep/sdnet.c
index d3ce3f5..7e28f4b 100644
--- a/sheep/sdnet.c
+++ b/sheep/sdnet.c
@@ -408,6 +408,8 @@ static void client_rx_handler(struct client_info *ci)
 	struct sd_req *hdr = &conn->rx_hdr;
 	struct request *req;
 
+	dprintf("connection from: %s:%d\n", ci->conn.ipstr, ci->conn.port);
+
 	if (!ci->rx_req && sys->outstanding_data_size > MAX_OUTSTANDING_DATA_SIZE) {
 		dprintf("too many requests (%p)\n", &ci->conn);
 		conn_rx_off(&ci->conn);
@@ -560,6 +562,7 @@ again:
 
 static void destroy_client(struct client_info *ci)
 {
+	dprintf("connection from: %s:%d\n", ci->conn.ipstr, ci->conn.port);
 	close(ci->conn.fd);
 	free(ci);
 }
@@ -579,11 +582,29 @@ static void client_decref(struct client_info *ci)
 static struct client_info *create_client(int fd, struct cluster_info *cluster)
 {
 	struct client_info *ci;
+	struct sockaddr_storage from;
+	socklen_t namesize = sizeof(from);
 
 	ci = zalloc(sizeof(*ci));
 	if (!ci)
 		return NULL;
 
+	if (getpeername(fd, (struct sockaddr*)&from, &namesize))
+		return NULL;
+
+	switch(from.ss_family) {
+	case AF_INET:
+		ci->conn.port = ntohs(((struct sockaddr_in*)&from)->sin_port);
+		inet_ntop(AF_INET, &((struct sockaddr_in *)&from)->sin_addr,
+				ci->conn.ipstr, sizeof(ci->conn.ipstr));
+		break;
+	case AF_INET6:
+		ci->conn.port = ntohs(((struct sockaddr_in6*)&from)->sin6_port);
+		inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&from)->sin6_addr,
+				ci->conn.ipstr, sizeof(ci->conn.ipstr));
+		break;
+	}
+
 	ci->conn.fd = fd;
 	ci->conn.events = EPOLLIN;
 	ci->refcnt = 1;
-- 
1.7.7.6




More information about the sheepdog mailing list