daniel has submitted this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/31172 )

Change subject: Add osmo_sockaddr_size() to return the size of the variant used
......................................................................

Add osmo_sockaddr_size() to return the size of the variant used

Change-Id: I952b6bb752441fe019fc18f89bce4bbfbe58994a
---
M include/osmocom/core/socket.h
1 file changed, 32 insertions(+), 0 deletions(-)

Approvals:
  laforge: Looks good to me, but someone else must approve
  pespin: Looks good to me, approved
  Jenkins Builder: Verified




diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 3ed9cc6..6bac87d 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -33,6 +33,29 @@
 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,

--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/31172
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I952b6bb752441fe019fc18f89bce4bbfbe58994a
Gerrit-Change-Number: 31172
Gerrit-PatchSet: 4
Gerrit-Owner: daniel <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: daniel <[email protected]>
Gerrit-Reviewer: laforge <[email protected]>
Gerrit-Reviewer: msuraev <[email protected]>
Gerrit-Reviewer: pespin <[email protected]>
Gerrit-MessageType: merged

Reply via email to