aboutsummaryrefslogtreecommitdiffstats
path: root/include/osmocom/core/socket.h
diff options
context:
space:
mode:
authorDaniel Willmann <dwillmann@sysmocom.de>2023-01-25 09:55:57 +0100
committerdaniel <dwillmann@sysmocom.de>2023-03-02 09:47:41 +0000
commit45ae1c540075cb8b52db6a0c7b89687778a35c77 (patch)
tree3e8c885f202bbef8e9f98d7e6ad1ebd559ead335 /include/osmocom/core/socket.h
parentf2c0fccb58623d9bc2989c76edde226706a73d63 (diff)
Add osmo_sockaddr_size() to return the size of the variant used
Diffstat (limited to 'include/osmocom/core/socket.h')
-rw-r--r--include/osmocom/core/socket.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 3ed9cc65..6bac87d5 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -33,6 +33,29 @@ struct osmo_sockaddr {
int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen);
int osmo_sockaddr_is_any(const struct osmo_sockaddr *addr);
+/*! Return the size of the variant used in the address
+ * NOTE: This does not return the size of the in{,6}_addr, but rather the size of the
+ * surrounding sockaddr_in{,6}.
+ * \param[in] addr the osmo_sockaddr to get the size of
+ * \return the size of the struct variant being used. If the value in sa_family is unsupported it will return
+ * the size of struct osmo_sockaddr. Returns 0 if addr is NULL. This way it can simply be a wrapper for sendto()
+ * which can be called with NULL/0 for dest_addr / addrlen (and then behaves like a send() call).
+ */
+static inline socklen_t osmo_sockaddr_size(const struct osmo_sockaddr *addr)
+{
+ if (!addr)
+ return 0;
+
+ switch (addr->u.sa.sa_family) {
+ case AF_INET:
+ return sizeof(struct sockaddr_in);
+ case AF_INET6:
+ return sizeof(struct sockaddr_in6);
+ default:
+ return sizeof(struct osmo_sockaddr);
+ }
+}
+
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,