[sheepdog] [PATCH 1/2] sheep: move get_addr into lib/net.c

Christoph Hellwig hch at infradead.org
Wed Jun 6 00:32:16 CEST 2012


Instead of duplicating this routine between the accord and zookeeper
drivers move it to lib/net.c as it is generic networking releated code.

Also rename the function to get_local_addr to be a bit more descriptive.

Signed-off-by: Christoph Hellwig <hch at lst.de>

diff --git a/include/net.h b/include/net.h
index 0286ea5..d97984e 100644
--- a/include/net.h
+++ b/include/net.h
@@ -55,5 +55,6 @@ uint8_t *str_to_addr(int af, const char *ipstr, uint8_t *addr);
 int set_nonblocking(int fd);
 int set_nodelay(int fd);
 int set_timeout(int fd);
+int get_local_addr(uint8_t *bytes);
 
 #endif
diff --git a/lib/net.c b/lib/net.c
index 8e46969..db952dd 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -440,3 +440,57 @@ int set_timeout(int fd)
 
 	return 0;
 }
+
+int get_local_addr(uint8_t *bytes)
+{
+	int ret;
+	char name[INET6_ADDRSTRLEN];
+	struct addrinfo hints, *res, *res0;
+
+	gethostname(name, sizeof(name));
+
+	memset(&hints, 0, sizeof(hints));
+
+	hints.ai_family = AF_INET;
+	hints.ai_socktype = SOCK_STREAM;
+	ret = getaddrinfo(name, NULL, &hints, &res0);
+	if (ret)
+		exit(1);
+
+	for (res = res0; res; res = res->ai_next) {
+		if (res->ai_family == AF_INET) {
+			struct sockaddr_in *addr;
+			addr = (struct sockaddr_in *)res->ai_addr;
+
+			if (((char *) &addr->sin_addr)[0] == 127)
+				continue;
+
+			memset(bytes, 0, 12);
+			memcpy(bytes + 12, &addr->sin_addr, 4);
+			break;
+		} else if (res->ai_family == AF_INET6) {
+			struct sockaddr_in6 *addr;
+			uint8_t localhost[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
+						  0, 0, 0, 0, 0, 0, 0, 1 };
+
+			addr = (struct sockaddr_in6 *)res->ai_addr;
+
+			if (memcmp(&addr->sin6_addr, localhost, 16) == 0)
+				continue;
+
+			memcpy(bytes, &addr->sin6_addr, 16);
+			break;
+		} else
+			dprintf("unknown address family\n");
+	}
+
+	if (res == NULL) {
+		eprintf("failed to get address info\n");
+		return -1;
+	}
+
+	freeaddrinfo(res0);
+
+	return 0;
+}
+
diff --git a/sheep/cluster/accord.c b/sheep/cluster/accord.c
index abb6db1..1472b24 100644
--- a/sheep/cluster/accord.c
+++ b/sheep/cluster/accord.c
@@ -11,7 +11,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
-#include <netdb.h>
 #include <search.h>
 #include <assert.h>
 #include <pthread.h>
@@ -292,58 +291,6 @@ out:
 	return 0;
 }
 
-static int get_addr(uint8_t *bytes)
-{
-	int ret;
-	char name[INET6_ADDRSTRLEN];
-	struct addrinfo hints, *res, *res0;
-
-	gethostname(name, sizeof(name));
-
-	memset(&hints, 0, sizeof(hints));
-
-	hints.ai_socktype = SOCK_STREAM;
-	ret = getaddrinfo(name, NULL, &hints, &res0);
-	if (ret)
-		exit(1);
-
-	for (res = res0; res; res = res->ai_next) {
-		if (res->ai_family == AF_INET) {
-			struct sockaddr_in *addr;
-			addr = (struct sockaddr_in *)res->ai_addr;
-
-			if (((char *) &addr->sin_addr)[0] == 127)
-				continue;
-
-			memset(bytes, 0, 12);
-			memcpy(bytes + 12, &addr->sin_addr, 4);
-			break;
-		} else if (res->ai_family == AF_INET6) {
-			struct sockaddr_in6 *addr;
-			uint8_t localhost[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
-						  0, 0, 0, 0, 0, 0, 0, 1 };
-
-			addr = (struct sockaddr_in6 *)res->ai_addr;
-
-			if (memcmp(&addr->sin6_addr, localhost, 16) == 0)
-				continue;
-
-			memcpy(bytes, &addr->sin6_addr, 16);
-			break;
-		} else
-			dprintf("unknown address family\n");
-	}
-
-	if (res == NULL) {
-		eprintf("failed to get address info\n");
-		return -1;
-	}
-
-	freeaddrinfo(res0);
-
-	return 0;
-}
-
 static void find_queue_end(struct acrd_handle *ah, const char *path, void *arg)
 {
 	int max;
@@ -599,7 +546,7 @@ static int accord_init(const char *option, uint8_t *myaddr)
 		return -1;
 	}
 
-	if (get_addr(myaddr) < 0)
+	if (get_local_addr(myaddr) < 0)
 		return -1;
 
 	efd = eventfd(0, EFD_NONBLOCK);
diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index 3dde1e4..a8bff05 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -11,7 +11,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
-#include <netdb.h>
 #include <search.h>
 #include <assert.h>
 #include <sys/epoll.h>
@@ -574,59 +573,6 @@ static void watcher(zhandle_t *zh, int type, int state, const char *path, void*
 	eventfd_write(efd, value);
 }
 
-static int get_addr(uint8_t *bytes)
-{
-	int ret;
-	char name[INET6_ADDRSTRLEN];
-	struct addrinfo hints, *res, *res0;
-
-	gethostname(name, sizeof(name));
-
-	memset(&hints, 0, sizeof(hints));
-
-	hints.ai_family = AF_INET;
-	hints.ai_socktype = SOCK_STREAM;
-	ret = getaddrinfo(name, NULL, &hints, &res0);
-	if (ret)
-		exit(1);
-
-	for (res = res0; res; res = res->ai_next) {
-		if (res->ai_family == AF_INET) {
-			struct sockaddr_in *addr;
-			addr = (struct sockaddr_in *)res->ai_addr;
-
-			if (((char *) &addr->sin_addr)[0] == 127)
-				continue;
-
-			memset(bytes, 0, 12);
-			memcpy(bytes + 12, &addr->sin_addr, 4);
-			break;
-		} else if (res->ai_family == AF_INET6) {
-			struct sockaddr_in6 *addr;
-			uint8_t localhost[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
-						  0, 0, 0, 0, 0, 0, 0, 1 };
-
-			addr = (struct sockaddr_in6 *)res->ai_addr;
-
-			if (memcmp(&addr->sin6_addr, localhost, 16) == 0)
-				continue;
-
-			memcpy(bytes, &addr->sin6_addr, 16);
-			break;
-		} else
-			dprintf("unknown address family\n");
-	}
-
-	if (res == NULL) {
-		eprintf("failed to get address info\n");
-		return -1;
-	}
-
-	freeaddrinfo(res0);
-
-	return 0;
-}
-
 static int zk_join(struct sd_node *myself,
 		   void *opaque, size_t opaque_len)
 {
@@ -861,7 +807,7 @@ static int zk_init(const char *option, uint8_t *myaddr)
 		"negotiated session timeout:%dms\n",
 		SESSION_TIMEOUT, zoo_recv_timeout(zhandle));
 
-	if (get_addr(myaddr) < 0)
+	if (get_local_addr(myaddr) < 0)
 		return -1;
 
 	zk_queue_init(zhandle);



More information about the sheepdog mailing list