[sheepdog] [PATCH UPDATE] net: cleanup str_to_addr()

Liu Yuan namei.unix at gmail.com
Tue Jan 15 07:15:52 CET 2013


From: Liu Yuan <tailai.ly at taobao.com>

We'd better embed inet type in it, then reduce one parameter passed in.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 update: correct str_to_addr()

 include/net.h   |    2 +-
 lib/net.c       |    7 +++----
 sheep/cluster.h |    8 ++------
 sheep/sheep.c   |    9 +++------
 4 files changed, 9 insertions(+), 17 deletions(-)

diff --git a/include/net.h b/include/net.h
index e0bbabd..25d46af 100644
--- a/include/net.h
+++ b/include/net.h
@@ -49,7 +49,7 @@ int create_unix_domain_socket(const char *unix_path,
 			      int (*callback)(int, void *), void *data);
 
 char *addr_to_str(char *str, int size, const uint8_t *addr, uint16_t port);
-uint8_t *str_to_addr(int af, const char *ipstr, uint8_t *addr);
+uint8_t *str_to_addr(const char *ipstr, uint8_t *addr);
 int set_nonblocking(int fd);
 int set_nodelay(int fd);
 int set_keepalive(int fd);
diff --git a/lib/net.c b/lib/net.c
index 77da430..d676960 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -406,17 +406,16 @@ char *addr_to_str(char *str, int size, const uint8_t *addr, uint16_t port)
 	return str;
 }
 
-uint8_t *str_to_addr(int af, const char *ipstr, uint8_t *addr)
+uint8_t *str_to_addr(const char *ipstr, uint8_t *addr)
 {
-	int addr_start_idx = 0;
+	int addr_start_idx = 0, af = strstr(ipstr, ":") ? AF_INET6 : AF_INET;
 
 	if (af == AF_INET)
 		addr_start_idx = 12;
-
-	memset(addr, 0, addr_start_idx);
 	if (!inet_pton(af, ipstr, addr + addr_start_idx))
 		return NULL;
 
+	memset(addr, 0, addr_start_idx);
 	return addr;
 }
 
diff --git a/sheep/cluster.h b/sheep/cluster.h
index d1e711d..d078f48 100644
--- a/sheep/cluster.h
+++ b/sheep/cluster.h
@@ -178,16 +178,12 @@ static inline char *node_to_str(const struct sd_node *id)
 
 static inline struct sd_node *str_to_node(const char *str, struct sd_node *id)
 {
-	int port, af = AF_INET6;
+	int port;
 	char v[8], ip[256];
 
 	sscanf(str, "%s ip:%s port:%d", v, ip, &port);
 	id->nid.port = port;
-
-	if (strcmp(v, "IPv4") == 0)
-		af = AF_INET;
-
-	if (!str_to_addr(af, ip, id->nid.addr))
+	if (!str_to_addr(ip, id->nid.addr))
 		return NULL;
 
 	return id;
diff --git a/sheep/sheep.c b/sheep/sheep.c
index bd33968..7d8e1d2 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -367,7 +367,7 @@ static int init_work_queues(void)
 int main(int argc, char **argv)
 {
 	int ch, longindex, ret, port = SD_LISTEN_PORT, io_port = SD_LISTEN_PORT;
-	int log_level = SDOG_INFO, af, nr_vnodes = SD_DEFAULT_VNODES;
+	int log_level = SDOG_INFO, nr_vnodes = SD_DEFAULT_VNODES;
 	const char *dirp = DEFAULT_OBJECT_DIR, *short_options;
 	char *dir, *p, *pid_file = NULL, *bindaddr = NULL, path[PATH_MAX];
 	char *io_addr = NULL, *io_pt;
@@ -408,8 +408,7 @@ int main(int argc, char **argv)
 			}
 			break;
 		case 'y':
-			af = strstr(optarg, ":") ? AF_INET6 : AF_INET;
-			if (!str_to_addr(af, optarg, sys->this_node.nid.addr)) {
+			if (!str_to_addr(optarg, sys->this_node.nid.addr)) {
 				fprintf(stderr, "Invalid address: '%s'\n",
 					optarg);
 				exit(1);
@@ -477,9 +476,7 @@ int main(int argc, char **argv)
 				fprintf(stderr, "Bad addr '%s'\n", optarg);
 				exit(1);
 			}
-			af = strstr(io_addr, ":") ? AF_INET6 : AF_INET;
-			if (!str_to_addr(af, io_addr,
-					 sys->this_node.nid.io_addr)) {
+			if (!str_to_addr(io_addr, sys->this_node.nid.io_addr)) {
 				fprintf(stderr, "Bad addr: '%s'\n",
 					io_addr);
 				exit(1);
-- 
1.7.9.5




More information about the sheepdog mailing list