From 3bc8b36537046bebedccc2cb683ec16c64241485 Mon Sep 17 00:00:00 2001
From: Jaco Kroon <jaco@uls.co.za>
Date: Tue, 3 Dec 2019 20:27:38 +0200
Subject: [PATCH] netsock2: ast_addressfamily_to_sockaddrsize and
 ast_sockaddr_from_sockaddr.

ast_addressfamily_to_sockaddrize will determine the size that's
required, and ast_sockaddr_from_sockaddr then wraps this new function
and ast_sockaddr_copy_sockaddr to copy arbitrary sockaddr's (without
knowing the address family) into the ast_sockaddr structure.

Change-Id: Iee604e96e9096c79b477d6e5ff310cf0b06dae86
Signed-off-by: Jaco Kroon <jaco@uls.co.za>
---
 include/asterisk/netsock2.h | 42 +++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/include/asterisk/netsock2.h b/include/asterisk/netsock2.h
index 3ba15a84d4..f20935242c 100644
--- a/include/asterisk/netsock2.h
+++ b/include/asterisk/netsock2.h
@@ -31,6 +31,8 @@ extern "C" {
 
 #include <netinet/in.h>
 
+#include "asterisk/logger.h"
+
 /*
  * String buffer size that can accommodate a fully stringified representation of a
  * supported IP address & port:
@@ -781,6 +783,46 @@ int _ast_sockaddr_to_sin(const struct ast_sockaddr *addr,
 void _ast_sockaddr_from_sin(struct ast_sockaddr *addr, const struct sockaddr_in *sin,
 		const char *file, int line, const char *func);
 
+/*!
+ * \since 13.31.0, 16.8.0, 17.2.0
+ *
+ * \brief Takes an AF_XXX value as input and returns the size of the underlying
+ * sockaddr structure if known, or zero if not.
+ *
+ * \param family The AF_XXX value to determine the size of
+ * \return Size of the applicable struct sockaddr.
+ */
+#define ast_addressfamily_to_sockaddrsize(family) _ast_addressfamily_to_sockaddrsize(addr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
+static inline int _ast_addressfamily_to_sockaddrsize(int af, const char *file, int line, const char *func)
+{
+	switch (af) {
+	case AF_INET:
+		return sizeof(struct sockaddr_in);
+	case AF_INET6:
+		return sizeof(struct sockaddr_in6);
+	default:
+		ast_log(__LOG_WARNING, file, line, func, "Unknown address family %d encountered.\n", af);
+		return 0;
+	}
+}
+
+/*!
+ * \since 13.31.0, 16.8.0, 17.2.0
+ *
+ * \brief Converts a struct sockaddr to a struct ast_sockaddr.
+ *
+ * Note that there is an underlying assumption that sockaddr data is valid, more specifically,
+ * if sa_family is set to AF_INET that it's actually a sockaddr_in, and in the case of AF_INET6
+ * a valid sockaddr_in6 structure.
+ *
+ * You can check for failure with ast_sockaddr_isnull.
+ *
+ * \param[out] addr The address of the ast_sockaddr to store into
+ * \param sockaddr The sockaddr structure (sockaddr_in or sockaddr_in6) to convert
+ * \return Nothing
+ */
+#define ast_sockaddr_from_sockaddr(addr,sockaddr)	ast_sockaddr_copy_sockaddr(addr, sockaddr, ast_addressfamily_to_sockaddrsize(((const struct sockaddr*)(sockaddr))->sa_family))
+
 /*@}*/
 
 #if defined(__cplusplus) || defined(c_plusplus)
-- 
GitLab