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 25d46af..ee60b6d 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(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 b4f7176..0bbd89c 100644 --- a/lib/net.c +++ b/lib/net.c @@ -406,6 +406,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(const char *ipstr, uint8_t *addr) { int addr_start_idx = 0, af = strstr(ipstr, ":") ? AF_INET6 : AF_INET; -- 1.7.2.5 |