[Sheepdog] [PATCH] Enable IPv6 support for addr_to_str()

Narendra Prasad Madanapalli narendramind at gmail.com
Tue Sep 14 04:57:38 CEST 2010


Enable IPv6 support in the function addr_to_str() and modify
print_node_list_entry() to make use of addr_to_str().

Signed-off-by: Narendra <narendramind at gmail.com>
---

 include/sheep.h |    7 ++++---
 lib/net.c       |   24 +++++++++++++++++-------
 2 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/include/sheep.h b/include/sheep.h
index a910219..47ffd4c 100644
--- a/include/sheep.h
+++ b/include/sheep.h
@@ -14,6 +14,7 @@
 #include <stdint.h>
 #include "util.h"
 #include "list.h"
+#include "net.h"

 #define SD_SHEEP_PROTO_VER 0x01

@@ -166,9 +167,9 @@ static inline int obj_to_sheep(struct
sheepdog_node_list_entry *entries,
 static inline void print_node_list_entry(struct sheepdog_node_list_entry *e,
 					 char *str, size_t size)
 {
-	snprintf(str, size, "%016" PRIx64 " - %d.%d.%d.%d:%d",
-		 e->id, e->addr[12], e->addr[13],
-		 e->addr[14], e->addr[15], e->port);
+	char  buf[INET6_ADDRSTRLEN];
+	snprintf(str, size, "%016" PRIx64 " - %s",
+		 e->id, addr_to_str(buf, sizeof(buf), e->addr, e->port));
 }

 static inline int is_sheep_op(uint8_t op)
diff --git a/lib/net.c b/lib/net.c
index 107408c..71d25b8 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -346,15 +346,25 @@ int exec_req(int sockfd, struct sd_req *hdr, void *data,
 	return 0;
 }

-/* 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]);
+	int  af = AF_INET6;
+        int  addr_start_idx = 0;
+
+	/* Find address family type */
+	if (addr[12]) {
+		int  oct_no = 0;
+		while (!addr[oct_no] && oct_no++ < 12 );
+		if (oct_no == 12) {
+			af = AF_INET;
+			addr_start_idx = 12;
+		}
+	}
+	inet_ntop(af, addr+addr_start_idx, str, size);
+	if (port) {
+		int  len = strlen(str);
+		snprintf(str+len, size-len, ":%d", port);
+	}

 	return str;
 }



More information about the sheepdog mailing list