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
This commit is contained in:
Matthew Fredrickson 2018-05-14 06:07:34 -05:00
parent a103221de2
commit 9f9dce05b2
5 changed files with 59 additions and 72 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{