[Sheepdog] [PATCH 01/18] add addr_to_str to format address easily

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Mar 11 07:48:00 CET 2010


Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/group.c      |   18 +++++++++---------
 collie/store.c      |   20 +++++---------------
 include/net.h       |    2 ++
 lib/net.c           |   29 ++++++++++++++++-------------
 shepherd/shepherd.c |   12 ++----------
 5 files changed, 34 insertions(+), 47 deletions(-)

diff --git a/collie/group.c b/collie/group.c
index 9775648..c3508e8 100644
--- a/collie/group.c
+++ b/collie/group.c
@@ -231,12 +231,12 @@ static void group_handler(int listen_fd, int events, void *data)
 static void print_node_list(void)
 {
 	struct node *node;
+	char name[128];
 	list_for_each_entry(node, &sys->node_list, list) {
-		dprintf("%c nodeid: %x, pid: %d, ip: %d.%d.%d.%d:%d\n",
+		dprintf("%c nodeid: %x, pid: %d, ip: %s\n",
 			node_cmp(&node->ent, &sys->this_node) ? ' ' : 'l',
 			node->nodeid, node->pid,
-			node->ent.addr[12], node->ent.addr[13],
-			node->ent.addr[14], node->ent.addr[15], node->ent.port);
+			addr_to_str(name, sizeof(name), node->ent.addr, node->ent.port));
 	}
 }
 
@@ -454,11 +454,11 @@ static void __sd_deliver(struct work *work, int idx)
 {
 	struct work_deliver *w = container_of(work, struct work_deliver, work);
 	struct message_header *m = w->msg;
+	char name[128];
 
-	dprintf("op: %d, done: %d, size: %d, from: %d.%d.%d.%d:%d\n",
+	dprintf("op: %d, done: %d, size: %d, from: %s\n",
 		m->op, m->done, m->msg_length,
-		m->from.addr[12], m->from.addr[13],
-		m->from.addr[14], m->from.addr[15], m->from.port);
+		addr_to_str(name, sizeof(name), m->from.addr, m->from.port));
 
 	if (!m->done) {
 		if (!is_master())
@@ -517,11 +517,11 @@ static void sd_deliver(cpg_handle_t handle, const struct cpg_name *group_name,
 {
 	struct work_deliver *w;
 	struct message_header *m = msg;
+	char name[128];
 
-	dprintf("op: %d, done: %d, size: %d, from: %d.%d.%d.%d:%d\n",
+	dprintf("op: %d, done: %d, size: %d, from: %s\n",
 		m->op, m->done, m->msg_length,
-		m->from.addr[12], m->from.addr[13],
-		m->from.addr[14], m->from.addr[15], m->from.port);
+		addr_to_str(name, sizeof(name), m->from.addr, m->from.port));
 
 	w = zalloc(sizeof(*w));
 	if (!w)
diff --git a/collie/store.c b/collie/store.c
index 6776254..05b19c6 100644
--- a/collie/store.c
+++ b/collie/store.c
@@ -153,9 +153,7 @@ again:
 	for (i = 0; i < nr; i++) {
 		n = obj_to_sheep(e, nr, oid, i);
 
-		snprintf(name, sizeof(name), "%d.%d.%d.%d",
-			 e[n].addr[12], e[n].addr[13],
-			 e[n].addr[14], e[n].addr[15]);
+		addr_to_str(name, sizeof(name), e[n].addr, 0);
 
 		/* FIXME: do like store_queue_request_local() */
 		if (e[n].id == sys->this_node.id)
@@ -242,9 +240,7 @@ again:
 	for (i = 0; i < copies; i++) {
 		n = obj_to_sheep(e, nr, oid, i);
 
-		snprintf(name, sizeof(name), "%d.%d.%d.%d",
-			 e[n].addr[12], e[n].addr[13],
-			 e[n].addr[14], e[n].addr[15]);
+		addr_to_str(name, sizeof(name), e[n].addr, 0);
 
 		/* TODO: we can do better; we need to chech this first */
 		if (e[n].id == sys->this_node.id) {
@@ -771,9 +767,7 @@ void so_queue_request(struct work *work, int idx)
 
 			n = obj_to_sheep(e, nr, SD_DIR_OID, 0);
 
-			snprintf(name, sizeof(name), "%d.%d.%d.%d",
-				 e[n].addr[12], e[n].addr[13],
-				 e[n].addr[14], e[n].addr[15]);
+			addr_to_str(name, sizeof(name), e[n].addr, 0);
 
 			eprintf("%s %d\n", name, e[n].port);
 
@@ -1082,9 +1076,7 @@ static void recover_one(struct work *work, int idx)
 
 	eprintf("%d %d, %16lx\n", rw->done, rw->count, oid);
 
-	snprintf(name, sizeof(name), "%d.%d.%d.%d",
-		 e->addr[12], e->addr[13],
-		 e->addr[14], e->addr[15]);
+	addr_to_str(name, sizeof(name), e->addr, 0);
 
 	fd = connect_to(name, e->port);
 	if (fd < 0) {
@@ -1170,9 +1162,7 @@ static int fill_obj_list(struct recovery_work *rw,
 	struct sd_obj_req hdr;
 	struct sd_obj_rsp *rsp;
 
-	snprintf(name, sizeof(name), "%d.%d.%d.%d",
-		 e->addr[12], e->addr[13],
-		 e->addr[14], e->addr[15]);
+	addr_to_str(name, sizeof(name), e->addr, 0);
 
 	dprintf("%s %d\n", name, e->port);
 
diff --git a/include/net.h b/include/net.h
index 7bf0dbb..f456eb0 100644
--- a/include/net.h
+++ b/include/net.h
@@ -51,4 +51,6 @@ int exec_reqs(struct sheepdog_node_list_entry *e,
 
 int create_listen_ports(int port, int (*callback)(int fd, void *), void *data);
 
+char *addr_to_str(char *str, int size, uint8_t *addr, uint16_t port);
+
 #endif
diff --git a/lib/net.c b/lib/net.c
index 767af47..c85ee2d 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -361,9 +361,7 @@ int write_object(struct sheepdog_node_list_entry *e,
 
 		n = obj_to_sheep(e, nodes, oid, i);
 
-		snprintf(name, sizeof(name), "%d.%d.%d.%d",
-			 e[n].addr[12], e[n].addr[13],
-			 e[n].addr[14], e[n].addr[15]);
+		addr_to_str(name, sizeof(name), e[n].addr, 0);
 
 		fd = connect_to(name, e[n].port);
 		if (fd < 0) {
@@ -411,11 +409,7 @@ int read_object(struct sheepdog_node_list_entry *e,
 
 		n = obj_to_sheep(e, nodes, oid, i);
 
-		snprintf(name, sizeof(name), "%d.%d.%d.%d",
-			 e[n].addr[12],
-			 e[n].addr[13],
-			 e[n].addr[14],
-			 e[n].addr[15]);
+		addr_to_str(name, sizeof(name), e[n].addr, 0);
 
 		fd = connect_to(name, e[n].port);
 		if (fd < 0)
@@ -461,11 +455,7 @@ int exec_reqs(struct sheepdog_node_list_entry *e,
 
 		n = obj_to_sheep(e, nodes, oid, i);
 
-		snprintf(name, sizeof(name), "%d.%d.%d.%d",
-			 e[n].addr[12],
-			 e[n].addr[13],
-			 e[n].addr[14],
-			 e[n].addr[15]);
+		addr_to_str(name, sizeof(name), e[n].addr, 0);
 
 		fd = connect_to(name, e[n].port);
 		if (fd < 0) {
@@ -508,3 +498,16 @@ int exec_reqs(struct sheepdog_node_list_entry *e,
 	else
 		return wlen;
 }
+
+/* TODO: support IPv6 */
+char *addr_to_str(char *str, int size, uint8_t *addr, uint16_t port)
+{
+	if (port)
+		snprintf(str, size, "%d.%d.%d.%d:%d",
+			 addr[12], addr[13], addr[14], addr[15], port);
+	else
+		snprintf(str, size, "%d.%d.%d.%d",
+			 addr[12], addr[13], addr[14], addr[15]);
+
+	return str;
+}
diff --git a/shepherd/shepherd.c b/shepherd/shepherd.c
index db28f77..f639fe2 100644
--- a/shepherd/shepherd.c
+++ b/shepherd/shepherd.c
@@ -764,11 +764,7 @@ static void parse_objs(uint64_t oid, obj_parser_func_t func, void *data)
 		struct sd_obj_req hdr;
 		struct sd_obj_rsp *rsp = (struct sd_obj_rsp *)&hdr;
 
-		snprintf(name, sizeof(name), "%d.%d.%d.%d",
-			 node_list_entries[i].addr[12],
-			 node_list_entries[i].addr[13],
-			 node_list_entries[i].addr[14],
-			 node_list_entries[i].addr[15]);
+		addr_to_str(name, sizeof(name), node_list_entries[i].addr, 0);
 
 		fd = connect_to(name, node_list_entries[i].port);
 		if (fd < 0)
@@ -914,11 +910,7 @@ rerun:
 			struct sd_node_rsp *rsp = (struct sd_node_rsp *)&req;
 			char store_str[8], free_str[8];
 
-			snprintf(name, sizeof(name), "%d.%d.%d.%d",
-				 node_list_entries[i].addr[12],
-				 node_list_entries[i].addr[13],
-				 node_list_entries[i].addr[14],
-				 node_list_entries[i].addr[15]);
+			addr_to_str(name, sizeof(name), node_list_entries[i].addr, 0);
 
 			fd = connect_to(name, node_list_entries[i].port);
 			if (fd < 0)
-- 
1.5.6.5




More information about the sheepdog mailing list