[sheepdog] [PATCH v2 2/4] net: cleanup str_to_addr()
Liu Yuan
namei.unix at gmail.com
Tue Jan 15 10:12:06 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>
---
include/net.h | 2 +-
lib/net.c | 5 ++---
sheep/cluster.h | 8 ++------
sheep/sheep.c | 9 +++------
4 files changed, 8 insertions(+), 16 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..b4f7176 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -406,13 +406,12 @@ 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;
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 be78098..737b7ca 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -387,7 +387,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];
bool is_daemon = true, to_stdout = false, explicit_addr = false;
@@ -427,8 +427,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);
@@ -492,9 +491,7 @@ int main(int argc, char **argv)
break;
case 'i':
parse_arg(optarg, ",", init_io_arg);
- 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