[Asterisk-code-review] netsock2: Add ast sockaddr resolve first af to netsock2 publ... (asterisk[master])

Matthew Fredrickson asteriskteam at digium.com
Mon May 14 06:14:25 CDT 2018


Matthew Fredrickson has uploaded this change for review. ( https://gerrit.asterisk.org/8987


Change subject: netsock2: Add ast_sockaddr_resolve_first_af to netsock2 public API
......................................................................

netsock2: Add ast_sockaddr_resolve_first_af to netsock2 public API

This function originally was used in chan_sip to enable some simplifying
assumptions and eventually was copy and pasted into res_pjsip_logger and
res_hep.  Since it's replicated in three places, it's probably best to
move it into the public netsock2 API for these modules to use.

Change-Id: Id52e23be885601c51d70259f62de1a5e59d38d04
---
M channels/chan_sip.c
M include/asterisk/netsock2.h
M main/netsock2.c
M res/res_hep.c
M res/res_pjsip_logger.c
5 files changed, 57 insertions(+), 72 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/87/8987/1

diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 46f9ad6..dc60deb 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -1380,8 +1380,6 @@
 static int sip_addheader(struct ast_channel *chan, const char *data);
 static int sip_do_reload(enum channelreloadreason reason);
 static char *sip_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
-static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
-				      const char *name, int flag, int family);
 static int ast_sockaddr_resolve_first(struct ast_sockaddr *addr,
 				      const char *name, int flag);
 static int ast_sockaddr_resolve_first_transport(struct ast_sockaddr *addr,
@@ -34330,30 +34328,6 @@
 {
 	sip_reload(0, 0, NULL);
 	return AST_MODULE_LOAD_SUCCESS;
-}
-
-/*! \brief  Return the first entry from ast_sockaddr_resolve filtered by address family
- *
- * \warning Using this function probably means you have a faulty design.
- */
-static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
-				      const char* name, int flag, int family)
-{
-	struct ast_sockaddr *addrs;
-	int addrs_cnt;
-
-	addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
-	if (addrs_cnt <= 0) {
-		return 1;
-	}
-	if (addrs_cnt > 1) {
-		ast_debug(1, "Multiple addresses, using the first one only\n");
-	}
-
-	ast_sockaddr_copy(addr, &addrs[0]);
-
-	ast_free(addrs);
-	return 0;
 }
 
 /*! \brief  Return the first entry from ast_sockaddr_resolve filtered by family of binddaddr
diff --git a/include/asterisk/netsock2.h b/include/asterisk/netsock2.h
index 2d2cfdf..04f8df0 100644
--- a/include/asterisk/netsock2.h
+++ b/include/asterisk/netsock2.h
@@ -437,6 +437,42 @@
 int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
 			 int flags, int family);
 
+/*! 
+ * \since 16.0
+ *
+ * \brief
+ * Return the first entry from ast_sockaddr_resolve filtered by address family
+ *
+ * \details
+ * Parses a string containing a host name or an IPv4 or IPv6 address followed
+ * by an optional port (separated by a colon).  This funcion only returns the
+ * first address into the ast_sockaddr. Allowed formats for str are the following:
+ *
+ * hostname:port
+ * host.example.com:port
+ * a.b.c.d
+ * a.b.c.d:port
+ * a:b:c:...:d
+ * [a:b:c:...:d]
+ * [a:b:c:...:d]:port
+ *
+ * \param[out] addr The resulting ast_sockaddr
+ * \param str The string to parse
+ * \param flags If set to zero, a port MAY be present. If set to
+ * PARSE_PORT_IGNORE, a port MAY be present but will be ignored. If set to
+ * PARSE_PORT_REQUIRE, a port MUST be present. If set to PARSE_PORT_FORBID, a
+ * port MUST NOT be present.
+ *
+ * \param family Only addresses of the given family will be returned. Use 0 or
+ * AST_AF_UNSPEC to get addresses of all families.
+ *
+ * \retval 0 Success
+ * \retval non-zero Failure
+ * \warning Using this function potentially means you have a faulty design.
+ */
+int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
+				      const char* name, int flag, int family);
+
 /*!
  * \brief
  * Apply a netmask to an address and store the result in a separate structure.
diff --git a/main/netsock2.c b/main/netsock2.c
index fedbd94..783a506 100644
--- a/main/netsock2.c
+++ b/main/netsock2.c
@@ -333,6 +333,27 @@
 	return res_cnt;
 }
 
+/*! \brief Pulls first resolved address and returns it */
+int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
+				      const char* name, int flag, int family)
+{
+	struct ast_sockaddr *addrs;
+	int addrs_cnt;
+
+	addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
+	if (addrs_cnt <= 0) {
+		return 1;
+	}
+	if (addrs_cnt > 1) {
+		ast_debug(1, "Multiple addresses resolving %s, using the first one only\n", name);
+	}
+
+	ast_sockaddr_copy(addr, &addrs[0]);
+
+	ast_free(addrs);
+	return 0;
+}
+
 int ast_sockaddr_apply_netmask(const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask,
 		struct ast_sockaddr *result)
 {
diff --git a/res/res_hep.c b/res/res_hep.c
index cc7028b..376fc4e 100644
--- a/res/res_hep.c
+++ b/res/res_hep.c
@@ -367,27 +367,6 @@
 	}
 }
 
-/*! \brief Pulls first resolved address and returns it */
-static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
-				      const char* name, int flag, int family)
-{
-	struct ast_sockaddr *addrs;
-	int addrs_cnt;
-
-	addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
-	if (addrs_cnt <= 0) {
-		return 1;
-	}
-	if (addrs_cnt > 1) {
-		ast_debug(1, "Multiple addresses resolving %s, using the first one only\n", name);
-	}
-
-	ast_sockaddr_copy(addr, &addrs[0]);
-
-	ast_free(addrs);
-	return 0;
-}
-
 /*! \brief Allocate the HEPv3 run-time data */
 static struct hepv3_runtime_data *hepv3_data_alloc(struct hepv3_global_config *config)
 {
diff --git a/res/res_pjsip_logger.c b/res/res_pjsip_logger.c
index 49ad6fa..fb58f2b 100644
--- a/res/res_pjsip_logger.c
+++ b/res/res_pjsip_logger.c
@@ -41,31 +41,6 @@
 static enum pjsip_logging_mode logging_mode;
 static struct ast_sockaddr log_addr;
 
-/*! \brief  Return the first entry from ast_sockaddr_resolve filtered by address family
- *
- * \warning Using this function probably means you have a faulty design.
- * \note This function was taken from the function of the same name in chan_sip.c
- */
-static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
-				      const char* name, int flag, int family)
-{
-	struct ast_sockaddr *addrs;
-	int addrs_cnt;
-
-	addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
-	if (addrs_cnt <= 0) {
-		return 1;
-	}
-	if (addrs_cnt > 1) {
-		ast_debug(1, "Multiple addresses, using the first one only\n");
-	}
-
-	ast_sockaddr_copy(addr, &addrs[0]);
-
-	ast_free(addrs);
-	return 0;
-}
-
 /*! \brief See if we pass debug IP filter */
 static inline int pjsip_log_test_addr(const char *address, int port)
 {

-- 
To view, visit https://gerrit.asterisk.org/8987
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id52e23be885601c51d70259f62de1a5e59d38d04
Gerrit-Change-Number: 8987
Gerrit-PatchSet: 1
Gerrit-Owner: Matthew Fredrickson <creslin at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180514/0820427b/attachment-0001.html>


More information about the asterisk-code-review mailing list