[Asterisk-code-review] utils.c: Remove all usages of ast_gethostbyname() (asterisk[16])

Sean Bright asteriskteam at digium.com
Fri Dec 24 11:55:17 CST 2021


Sean Bright has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/17713 )


Change subject: utils.c: Remove all usages of ast_gethostbyname()
......................................................................

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
---
M addons/ooh323c/src/ooSocket.c
M apps/app_festival.c
M channels/chan_mgcp.c
M channels/chan_skinny.c
M channels/chan_unistim.c
M channels/iax2/provision.c
M main/config.c
7 files changed, 81 insertions(+), 66 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/13/17713/1

diff --git a/addons/ooh323c/src/ooSocket.c b/addons/ooh323c/src/ooSocket.c
index 151c1cf..6d914f2 100644
--- a/addons/ooh323c/src/ooSocket.c
+++ b/addons/ooh323c/src/ooSocket.c
@@ -537,8 +537,6 @@
 int ooGetLocalIPAddress(char * pIPAddrs)
 {
    int ret;
-   struct hostent *hp;
-   struct ast_hostent phost;
    char hostname[100];
 
    if(pIPAddrs == NULL)
@@ -546,18 +544,10 @@
    ret = gethostname(hostname, 100);
    if(ret == 0)
    {
-      if ((hp = ast_gethostbyname(hostname, &phost))) {
-			if (hp->h_addrtype == AF_INET6) {
-				struct in6_addr i;
-				memcpy(&i, hp->h_addr, sizeof(i));
-				strcpy(pIPAddrs, (inet_ntop(AF_INET6, &i,
-				hostname, sizeof(hostname))) == NULL ? "::1" :
-				inet_ntop(AF_INET6, &i, hostname, sizeof(hostname)));
-			} else {
-	  			struct in_addr i;
-				memcpy(&i, hp->h_addr, sizeof(i));
-			  	strcpy(pIPAddrs, (ast_inet_ntoa(i) == NULL) ? "127.0.0.1" : ast_inet_ntoa(i));
-			}
+      struct ast_sockaddr *addrs;
+      if (ast_sockaddr_resolve(&addrs, hostname, PARSE_PORT_FORBID, AF_UNSPEC)) {
+         strcpy(pIPAddrs, ast_sockaddr_stringify_addr(addrs));
+         ast_free(addrs);
       } else {
          return -1;
       }
diff --git a/apps/app_festival.c b/apps/app_festival.c
index d682b1c..e0f1b0b 100644
--- a/apps/app_festival.c
+++ b/apps/app_festival.c
@@ -281,8 +281,6 @@
 	int usecache;
 	int res = 0;
 	struct sockaddr_in serv_addr;
-	struct hostent *serverhost;
-	struct ast_hostent ahp;
 	int fd;
 	FILE *fs;
 	const char *host;
@@ -398,15 +396,17 @@
 
 	if ((serv_addr.sin_addr.s_addr = inet_addr(host)) == -1) {
 		/* its a name rather than an ipnum */
-		serverhost = ast_gethostbyname(host, &ahp);
+		struct ast_sockaddr addr = { 0, };
 
-		if (serverhost == NULL) {
-			ast_log(LOG_WARNING, "festival_client: gethostbyname failed\n");
+		if (ast_sockaddr_resolve_first_af(&addr, host, PARSE_PORT_FORBID, AF_INET)) {
+			ast_log(LOG_WARNING, "festival_client: ast_sockaddr_resolve_first_af() failed\n");
 			ast_config_destroy(cfg);
 			close(fd);
 			return -1;
 		}
-		memmove(&serv_addr.sin_addr, serverhost->h_addr, serverhost->h_length);
+
+		/* We'll overwrite port and family in a sec */
+		ast_sockaddr_to_sin(&addr, &serv_addr);
 	}
 
 	serv_addr.sin_family = AF_INET;
diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c
index 9404ad1..7156da7 100644
--- a/channels/chan_mgcp.c
+++ b/channels/chan_mgcp.c
@@ -1948,10 +1948,8 @@
 	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;
@@ -1971,8 +1969,7 @@
 		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;
 	}
@@ -1980,12 +1977,9 @@
 		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)) {
@@ -4657,6 +4651,30 @@
 	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)
 {
@@ -4665,8 +4683,6 @@
 	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)) {
@@ -4700,10 +4716,8 @@
 
 		/* 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);
@@ -4771,13 +4785,11 @@
 	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);
diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c
index 4892865..4b91fac 100644
--- a/channels/chan_skinny.c
+++ b/channels/chan_skinny.c
@@ -1234,8 +1234,6 @@
 static char ourhost[256];
 static int ourport;
 static struct in_addr __ourip;
-static struct ast_hostent ahp;
-static struct hostent *hp;
 static int skinnysock = -1;
 static pthread_t accept_t;
 static int callnums = 1;
@@ -7758,6 +7756,31 @@
 	return tmpc;
 }
 
+/*!
+ * \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;
+}
+
 #define TYPE_GENERAL	1
 #define TYPE_DEF_DEVICE 2
 #define TYPE_DEF_LINE	4
@@ -7789,10 +7812,8 @@
 				continue;
 			}
 			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));
 				}
 				continue;
 			} else if (!strcasecmp(v->name, "keepalive")) {
@@ -8487,13 +8508,11 @@
 	if (ntohl(bindaddr.sin_addr.s_addr)) {
 		__ourip = bindaddr.sin_addr;
 	} else {
-		hp = ast_gethostbyname(ourhost, &ahp);
-		if (!hp) {
+		if (resolve_first_addr(ourhost, &__ourip)) {
 			ast_log(LOG_WARNING, "Unable to get our IP address, Skinny 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_SKINNY_PORT);
diff --git a/channels/chan_unistim.c b/channels/chan_unistim.c
index 0240509..bf133e8 100644
--- a/channels/chan_unistim.c
+++ b/channels/chan_unistim.c
@@ -6847,8 +6847,6 @@
 {
 	struct ast_config *cfg;
 	struct ast_variable *v;
-	struct ast_hostent ahp;
-	struct hostent *hp;
 	struct sockaddr_in bindaddr = { 0, };
 	char *config = "unistim.conf";
 	char *cat;
@@ -6916,11 +6914,11 @@
 			}
 		} else if (!strcasecmp(v->name, "public_ip")) {
 			if (!ast_strlen_zero(v->value)) {
-				if (!(hp = ast_gethostbyname(v->value, &ahp))) {
+				struct ast_sockaddr addr = { 0, };
+				if (ast_sockaddr_resolve_first_af(&addr, v->value, PARSE_PORT_FORBID, AF_INET)) {
 					ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
 				} else {
-					memcpy(&public_ip.sin_addr, hp->h_addr, sizeof(public_ip.sin_addr));
-					public_ip.sin_family = AF_INET;
+					ast_sockaddr_to_sin(&addr, &public_ip);
 				}
 			}
 		}
diff --git a/channels/iax2/provision.c b/channels/iax2/provision.c
index f21c4e6..445ccd5 100644
--- a/channels/iax2/provision.c
+++ b/channels/iax2/provision.c
@@ -285,9 +285,6 @@
 	int foundportno = 0;
 	int foundserverportno = 0;
 	int x;
-	struct in_addr ia;
-	struct hostent *hp;
-	struct ast_hostent h;
 	struct iax_template *src, tmp;
 	const char *t;
 	if (def) {
@@ -335,13 +332,12 @@
 			} else
 				ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
 		} else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
-			hp = ast_gethostbyname(v->value, &h);
-			if (hp) {
-				memcpy(&ia, hp->h_addr, sizeof(ia));
+			struct ast_sockaddr addr = { 0, };
+			if (ast_sockaddr_resolve_first_af(&addr, v->value, PARSE_PORT_FORBID, AF_INET)) {
 				if (!strcasecmp(v->name, "server"))
-					cur->server = ntohl(ia.s_addr);
+					cur->server = ast_sockaddr_ipv4(&addr);
 				else
-					cur->altserver = ntohl(ia.s_addr);
+					cur->altserver = ast_sockaddr_ipv4(&addr);
 			} else
 				ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
 		} else if (!strcasecmp(v->name, "codec")) {
diff --git a/main/config.c b/main/config.c
index 4d4f946..b6a749e 100644
--- a/main/config.c
+++ b/main/config.c
@@ -3863,8 +3863,7 @@
 		/* default is either the supplied value or the result itself */
 		struct sockaddr_in *def = (flags & PARSE_DEFAULT) ?
 			va_arg(ap, struct sockaddr_in *) : sa;
-		struct hostent *hp;
-		struct ast_hostent ahp;
+		struct ast_sockaddr addr = { 0, };
 
 		memset(&_sa_buf, '\0', sizeof(_sa_buf)); /* clear buffer */
 		/* duplicate the string to strip away the :port */
@@ -3890,12 +3889,13 @@
 				error = 1;
 		}
 		/* Now deal with host part, even if we have errors before. */
-		hp = ast_gethostbyname(buf, &ahp);
-		if (hp)	/* resolved successfully */
-			memcpy(&sa->sin_addr, hp->h_addr, sizeof(sa->sin_addr));
-		else {
+		if (ast_sockaddr_resolve_first_af(&addr, buf, PARSE_PORT_FORBID, AF_INET)) {
 			error = 1;
 			sa->sin_addr = def->sin_addr;
+		} else {
+			struct sockaddr_in tmp;
+			ast_sockaddr_to_sin(&addr, &tmp);
+			sa->sin_addr = tmp.sin_addr;
 		}
 		ast_debug(3,
 			"extract inaddr from [%s] gives [%s:%d](%d)\n",

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

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: Ie277c0ef768d753b169c121ef570a71665692ab7
Gerrit-Change-Number: 17713
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Bright <sean at seanbright.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20211224/cbe30c35/attachment-0001.html>


More information about the asterisk-code-review mailing list