utils.c: Remove all usages of ast_gethostbyname()

gethostbyname() and gethostbyname_r() are deprecated in favor of
getaddrinfo() which we use in the ast_sockaddr family of functions.

ASTERISK-29819 #close

Change-Id: Ie277c0ef768d753b169c121ef570a71665692ab7
This commit is contained in:
Sean Bright
2021-12-24 11:26:31 -05:00
committed by Kevin Harwell
parent 262a4053ff
commit 0d62735f99
7 changed files with 85 additions and 70 deletions

View File

@@ -1949,10 +1949,8 @@ static int process_sdp(struct mgcp_subchannel *sub, struct mgcp_request *req)
int portno;
struct ast_format_cap *peercap;
int peerNonCodecCapability;
struct sockaddr_in sin;
struct ast_sockaddr sin_tmp;
struct ast_sockaddr addr = { {0,} };
char *codecs;
struct ast_hostent ahp; struct hostent *hp;
int codec, codec_count=0;
int iterator;
struct mgcp_endpoint *p = sub->parent;
@@ -1972,8 +1970,7 @@ static int process_sdp(struct mgcp_subchannel *sub, struct mgcp_request *req)
return -1;
}
/* XXX This could block for a long time, and block the main thread! XXX */
hp = ast_gethostbyname(host, &ahp);
if (!hp) {
if (ast_sockaddr_resolve_first_af(&addr, host, PARSE_PORT_FORBID, AF_INET)) {
ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
return -1;
}
@@ -1981,12 +1978,9 @@ static int process_sdp(struct mgcp_subchannel *sub, struct mgcp_request *req)
ast_log(LOG_WARNING, "Malformed media stream descriptor: %s\n", m);
return -1;
}
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
sin.sin_port = htons(portno);
ast_sockaddr_from_sin(&sin_tmp, &sin);
ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
ast_debug(3, "Peer RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
ast_sockaddr_set_port(&addr, portno);
ast_rtp_instance_set_remote_address(sub->rtp, &addr);
ast_debug(3, "Peer RTP is at port %s\n", ast_sockaddr_stringify(&addr));
/* Scan through the RTP payload types specified in a "m=" line: */
codecs = ast_strdupa(m + len);
while (!ast_strlen_zero(codecs)) {
@@ -4651,6 +4645,30 @@ static struct ast_variable *copy_vars(struct ast_variable *src)
return res;
}
/*!
* \brief Resolve the given hostname and save its IPv4 address.
*
* \param[in] hostname The hostname to resolve.
* \param[out] sin_addr Pointer to a <tt>struct in_addr</tt> in which to
* store the resolved IPv4 address. \c sin_addr will
* not be changed if resolution fails.
*
* \retval 0 if successful
* \retval 1 on failure
*/
static int resolve_first_addr(const char *hostname, struct in_addr *sin_addr)
{
struct ast_sockaddr addr = { {0,} };
struct sockaddr_in tmp;
if (ast_sockaddr_resolve_first_af(&addr, hostname, PARSE_PORT_FORBID, AF_INET)) {
return 1;
}
ast_sockaddr_to_sin(&addr, &tmp);
*sin_addr = tmp.sin_addr;
return 0;
}
static int reload_config(int reload)
{
@@ -4659,8 +4677,6 @@ static int reload_config(int reload)
struct mgcp_gateway *g;
struct mgcp_endpoint *e;
char *cat;
struct ast_hostent ahp;
struct hostent *hp;
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
if (gethostname(ourhost, sizeof(ourhost)-1)) {
@@ -4694,10 +4710,8 @@ static int reload_config(int reload)
/* Create the interface list */
if (!strcasecmp(v->name, "bindaddr")) {
if (!(hp = ast_gethostbyname(v->value, &ahp))) {
if (resolve_first_addr(v->value, &bindaddr.sin_addr)) {
ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
} else {
memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
}
} else if (!strcasecmp(v->name, "allow")) {
ast_format_cap_update_by_allow_disallow(global_capability, v->value, 1);
@@ -4765,13 +4779,11 @@ static int reload_config(int reload)
if (ntohl(bindaddr.sin_addr.s_addr)) {
memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip));
} else {
hp = ast_gethostbyname(ourhost, &ahp);
if (!hp) {
if (resolve_first_addr(ourhost, &__ourip)) {
ast_log(LOG_WARNING, "Unable to get our IP address, MGCP disabled\n");
ast_config_destroy(cfg);
return 0;
}
memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
}
if (!ntohs(bindaddr.sin_port))
bindaddr.sin_port = htons(DEFAULT_MGCP_CA_PORT);