From 758666070d5e760bc4d0cc0d316e9b8d3af6afed Mon Sep 17 00:00:00 2001 From: Dmitry Simonenko Date: Thu, 17 Aug 2017 18:15:19 +0300 Subject: [PATCH] odissey: extend getsockname and getaddrname with addr/port --- sources/io.c | 47 ++++++++++++++++++++++++++++++++++++++--------- sources/io.h | 6 +++--- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/sources/io.c b/sources/io.c index b7505f76..d5f304c8 100644 --- a/sources/io.c +++ b/sources/io.c @@ -60,38 +60,67 @@ int od_write(machine_io_t *io, shapito_stream_t *stream) return rc; } -int od_getsockaddrname(struct sockaddr *sa, char *buf, int size) +static int +od_getsockaddrname(struct sockaddr *sa, char *buf, int size, + int add_addr, + int add_port) { char addr[128]; if (sa->sa_family == AF_INET) { struct sockaddr_in *sin = (struct sockaddr_in*)sa; inet_ntop(sa->sa_family, &sin->sin_addr, addr, sizeof(addr)); - snprintf(buf, size, "%s:%d", addr, ntohs(sin->sin_port)); + if (add_addr && add_port) + snprintf(buf, size, "%s:%d", addr, ntohs(sin->sin_port)); + else + if (add_addr) + snprintf(buf, size, "%s", addr); + else + if (add_port) + snprintf(buf, size, "%d", ntohs(sin->sin_port)); return 0; } if (sa->sa_family == AF_INET6) { struct sockaddr_in6 *sin = (struct sockaddr_in6*)sa; inet_ntop(sa->sa_family, &sin->sin6_addr, addr, sizeof(addr)); - snprintf(buf, size, "[%s]:%d", addr, ntohs(sin->sin6_port)); + if (add_addr && add_port) + snprintf(buf, size, "[%s]:%d", addr, ntohs(sin->sin6_port)); + else + if (add_addr) + snprintf(buf, size, "%s", addr); + else + if (add_port) + snprintf(buf, size, "%d", ntohs(sin->sin6_port)); return 0; } - snprintf(buf, size, "unknown"); + snprintf(buf, size, "%s", ""); return -1; } -int od_getaddrname(struct addrinfo *ai, char *buf, int size) +int od_getaddrname(struct addrinfo *ai, char *buf, int size, int add_addr, int add_port) { - return od_getsockaddrname(ai->ai_addr, buf, size); + return od_getsockaddrname(ai->ai_addr, buf, size, add_addr, add_port); } -int od_getpeername(machine_io_t *io, char *buf, int size) +int od_getpeername(machine_io_t *io, char *buf, int size, int add_addr, int add_port) { struct sockaddr_storage sa; int salen = sizeof(sa); int rc = machine_getpeername(io, (struct sockaddr*)&sa, &salen); if (rc < 0) { - snprintf(buf, size, "unknown"); + snprintf(buf, size, "%s", ""); return -1; } - return od_getsockaddrname((struct sockaddr*)&sa, buf, size); + return od_getsockaddrname((struct sockaddr*)&sa, buf, size, add_addr, add_port); +} + +int od_getsockname(machine_io_t *io, char *buf, int size, int add_addr, int add_port) +{ + struct sockaddr_storage sa; + int salen = sizeof(sa); + int rc = machine_getsockname(io, (struct sockaddr*)&sa, &salen); + if (rc < 0) { + snprintf(buf, size, "%s", ""); + return -1; + } + return od_getsockaddrname((struct sockaddr*)&sa, buf, size, add_addr, add_port); } diff --git a/sources/io.h b/sources/io.h index 62ffee49..204d60fe 100644 --- a/sources/io.h +++ b/sources/io.h @@ -9,8 +9,8 @@ int od_read(machine_io_t*, shapito_stream_t*, int); int od_write(machine_io_t*, shapito_stream_t*); -int od_getpeername(machine_io_t*, char*, int); -int od_getsockaddrname(struct sockaddr*, char*, int); -int od_getaddrname(struct addrinfo*, char*, int); +int od_getaddrname(struct addrinfo*, char*, int, int, int); +int od_getpeername(machine_io_t*, char*, int, int, int); +int od_getsockname(machine_io_t*, char*, int, int, int); #endif /* OD_IO_H */