summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax <msuraev@sysmocom.de>2018-11-20 15:18:31 +0100
committerMax <msuraev@sysmocom.de>2018-12-07 10:33:29 +0000
commit9d7a247da4e20acc6638fcd06198bfe673d0c384 (patch)
treed70896c6b7413d79aaf3feeb347a1b542c3d53fb
parent72dfd437995cd5af6da6d8701e81c984744bc5ae (diff)
Add osmo_sockaddr_in_to_str_and_uint()
It's similar to osmo_sockaddr_to_str_and_uint() but does not require odd typecasting for AF_INET case. Make osmo_sockaddr_to_str_and_uint() into wrapper around new function and make sure to check for address family before typecasting. Also use proper return type. Change-Id: Ie384483124d407a960ab6732e6a7fd90554389d2
-rw-r--r--include/osmocom/core/socket.h3
-rw-r--r--src/socket.c25
2 files changed, 23 insertions, 5 deletions
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 28f89a50..79a61bb0 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -11,6 +11,7 @@
#include <stdbool.h>
#include <stddef.h>
+struct sockaddr_in;
struct sockaddr;
struct osmo_fd;
@@ -49,6 +50,8 @@ int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen);
unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port,
const struct sockaddr *sa);
+size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port,
+ const struct sockaddr_in *sin);
int osmo_sock_unix_init(uint16_t type, uint8_t proto,
const char *socket_path, unsigned int flags);
diff --git a/src/socket.c b/src/socket.c
index 0e17a28d..e804ab53 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -569,6 +569,25 @@ int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen)
return 0;
}
+/*! Convert sockaddr_in to IP address as char string and port as uint16_t.
+ * \param[out] addr String buffer to write IP address to, or NULL.
+ * \param[out] addr_len Size of \a addr.
+ * \param[out] port Pointer to uint16_t to write the port number to, or NULL.
+ * \param[in] sin Sockaddr to convert.
+ * \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL.
+ */
+size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port,
+ const struct sockaddr_in *sin)
+{
+ if (port)
+ *port = ntohs(sin->sin_port);
+
+ if (addr)
+ return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len);
+
+ return 0;
+}
+
/*! Convert sockaddr to IP address as char string and port as uint16_t.
* \param[out] addr String buffer to write IP address to, or NULL.
* \param[out] addr_len Size of \a addr.
@@ -581,11 +600,7 @@ unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, ui
{
const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
- if (port)
- *port = ntohs(sin->sin_port);
- if (addr)
- return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len);
- return 0;
+ return osmo_sockaddr_in_to_str_and_uint(addr, addr_len, port, sin);
}
/*! Initialize a unix domain socket (including bind/connect)