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

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Wed Jan 9 08:09:05 CET 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     |   17 +++++++++++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/include/net.h b/include/net.h
index 3665f5e..9d025e4 100644
--- a/include/net.h
+++ b/include/net.h
@@ -50,6 +50,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(int af, 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 9b7fea8..b7bfe73 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -412,6 +412,23 @@ 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 += sprintf(str + si, i != 3 ? "%d." : "%d", addr[i]);
+	sprintf(str + si, ":%u", sockaddr->sin_port);
+
+	return str;
+}
+
 uint8_t *str_to_addr(int af, const char *ipstr, uint8_t *addr)
 {
 	int addr_start_idx = 0;
-- 
1.7.2.5




More information about the sheepdog mailing list