[Asterisk-code-review] netsock2: ast_addressfamily_to_sockaddrsize and ast_sockaddr_from_soc... (asterisk[13])

Joshua Colp asteriskteam at digium.com
Tue Jan 14 09:08:19 CST 2020


Joshua Colp has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/13361 )

Change subject: netsock2: ast_addressfamily_to_sockaddrsize and ast_sockaddr_from_sockaddr.
......................................................................

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 at uls.co.za>
---
M include/asterisk/netsock2.h
1 file changed, 42 insertions(+), 0 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Joshua Colp: Approved for Submit



diff --git a/include/asterisk/netsock2.h b/include/asterisk/netsock2.h
index b62ca4c..9e3c2fb 100644
--- a/include/asterisk/netsock2.h
+++ b/include/asterisk/netsock2.h
@@ -31,6 +31,8 @@
 
 #include <netinet/in.h>
 
+#include "asterisk/logger.h"
+
 /*
  * String buffer size that can accommodate a fully stringified representation of a
  * supported IP address & port:
@@ -743,6 +745,46 @@
 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)

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

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Change-Id: Iee604e96e9096c79b477d6e5ff310cf0b06dae86
Gerrit-Change-Number: 13361
Gerrit-PatchSet: 11
Gerrit-Owner: Jaco Kroon <jaco at uls.co.za>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-CC: snuffy <snuffy22 at gmail.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200114/41618ce3/attachment-0001.html>


More information about the asterisk-code-review mailing list