From 9f9dce05b25d05c924ee23f8dd8b17b0c9e3362a Mon Sep 17 00:00:00 2001 From: Matthew Fredrickson Date: Mon, 14 May 2018 06:07:34 -0500 Subject: [PATCH] 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 --- channels/chan_sip.c | 26 ------------------------- include/asterisk/netsock2.h | 38 +++++++++++++++++++++++++++++++++++++ main/netsock2.c | 21 ++++++++++++++++++++ res/res_hep.c | 21 -------------------- res/res_pjsip_logger.c | 25 ------------------------ 5 files changed, 59 insertions(+), 72 deletions(-) diff --git a/channels/chan_sip.c b/channels/chan_sip.c index 46f9ad699b..dc60deb609 100644 --- a/channels/chan_sip.c +++ b/channels/chan_sip.c @@ -1380,8 +1380,6 @@ static int sip_dtmfmode(struct ast_channel *chan, const char *data); 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, @@ -34332,30 +34330,6 @@ static int reload(void) 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 * * \warning Using this function probably means you have a faulty design. diff --git a/include/asterisk/netsock2.h b/include/asterisk/netsock2.h index 2d2cfdf5ae..fc3a0c005f 100644 --- a/include/asterisk/netsock2.h +++ b/include/asterisk/netsock2.h @@ -437,6 +437,44 @@ int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags); 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 function only returns the + * first address into the ast_sockaddr. Allowed formats for name 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 name 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 specify any address family. Behavior is ultimately determined + * by getaddrinfo in how it orders return results. First result is selected to + * be returned. + * + * \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 fedbd94a04..783a5064f1 100644 --- a/main/netsock2.c +++ b/main/netsock2.c @@ -333,6 +333,27 @@ cleanup: 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 cc7028bd57..376fc4e0bb 100644 --- a/res/res_hep.c +++ b/res/res_hep.c @@ -367,27 +367,6 @@ static void hepv3_data_dtor(void *obj) } } -/*! \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 49ad6faec4..fb58f2bbe1 100644 --- a/res/res_pjsip_logger.c +++ b/res/res_pjsip_logger.c @@ -41,31 +41,6 @@ enum pjsip_logging_mode { 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) {