[sheepdog] [PATCH v10 3/7] lib: sockaddr_in_to_str(), convert sockaddr_in to string representation

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Mon Apr 8 03:47:03 CEST 2013


This patch adds new function sockaddr_in_to_str() to lib/net.c. This
is for converting variables typed struct sockaddr_in to string
representation.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 include/net.h |    1 +
 lib/net.c     |   19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/include/net.h b/include/net.h
index 75ac197..90ea536 100644
--- a/include/net.h
+++ b/include/net.h
@@ -64,6 +64,7 @@ int create_unix_domain_socket(const char *unix_path,
 
 char *addr_to_str(char *str, int size, const uint8_t *addr, uint16_t port);
 uint8_t *str_to_addr(const char *ipstr, uint8_t *addr);
+char *sockaddr_in_to_str(struct sockaddr_in *sockaddr);
 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 ff0c150..116e55a 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -436,6 +436,25 @@ char *addr_to_str(char *str, int size, const uint8_t *addr, uint16_t port)
 	return str;
 }
 
+char *sockaddr_in_to_str(struct sockaddr_in *sockaddr)
+{
+	int i, si;
+	static char str[32];
+	uint8_t *addr;
+
+	si = 0;
+	memset(str, 0, 32);
+
+	addr = (uint8_t *)&sockaddr->sin_addr.s_addr;
+	for (i = 0; i < 4; i++) {
+		si += snprintf(str + si, 32 - si,
+			i != 3 ? "%d." : "%d", addr[i]);
+	}
+	snprintf(str + si, 32 - si, ":%u", sockaddr->sin_port);
+
+	return str;
+}
+
 uint8_t *str_to_addr(const char *ipstr, uint8_t *addr)
 {
 	int addr_start_idx = 0, af = strstr(ipstr, ":") ? AF_INET6 : AF_INET;
-- 
1.7.2.5




More information about the sheepdog mailing list