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

Friendly Automation asteriskteam at digium.com
Thu Jan 6 14:04:00 CST 2022


Friendly Automation has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/17830 )

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, 85 insertions(+), 70 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  Kevin Harwell: Looks good to me, approved
  Friendly Automation: Approved for Submit



diff --git a/addons/ooh323c/src/ooSocket.c b/addons/ooh323c/src/ooSocket.c
index 151c1cf..c0457c3 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,20 +544,11 @@
    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));
-			}
-      } else {
+      struct ast_sockaddr addr = { {0,} };
+      if (ast_sockaddr_resolve_first_af(&addr, hostname, PARSE_PORT_FORBID, AF_UNSPEC)) {
          return -1;
+      } else {
+         strcpy(pIPAddrs, ast_sockaddr_stringify_addr(&addr));
       }
    }
    else{
diff --git a/apps/app_festival.c b/apps/app_festival.c
index d682b1c..c18725d 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 3a0c7b3..b2f1e57 100644
--- a/channels/chan_mgcp.c
+++ b/channels/chan_mgcp.c
@@ -1949,10 +1949,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;
@@ -1972,8 +1970,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;
 	}
@@ -1981,12 +1978,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)) {
@@ -4651,6 +4645,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)
 {
@@ -4659,8 +4677,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)) {
@@ -4694,10 +4710,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);
@@ -4765,13 +4779,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 788d3ec..e3a7021 100644
--- a/channels/chan_skinny.c
+++ b/channels/chan_skinny.c
@@ -1235,8 +1235,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;
@@ -7759,6 +7757,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
@@ -7790,10 +7813,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")) {
@@ -8480,13 +8501,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..f813e34 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..d67a73f 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,15 +332,15 @@
 			} 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));
-				if (!strcasecmp(v->name, "server"))
-					cur->server = ntohl(ia.s_addr);
-				else
-					cur->altserver = ntohl(ia.s_addr);
-			} else
+			struct ast_sockaddr addr = { {0,} };
+			if (ast_sockaddr_resolve_first_af(&addr, v->value, PARSE_PORT_FORBID, AF_INET)) {
 				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"))
+					cur->server = ast_sockaddr_ipv4(&addr);
+				else
+					cur->altserver = ast_sockaddr_ipv4(&addr);
+			}
 		} else if (!strcasecmp(v->name, "codec")) {
 			struct ast_format *tmpfmt;
 			if ((tmpfmt = ast_format_cache_get(v->value))) {
diff --git a/main/config.c b/main/config.c
index 4d4f946..7b3b457 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/+/17830
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 19
Gerrit-Change-Id: Ie277c0ef768d753b169c121ef570a71665692ab7
Gerrit-Change-Number: 17830
Gerrit-PatchSet: 2
Gerrit-Owner: Sean Bright <sean at seanbright.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220106/eee95fb1/attachment-0001.html>


More information about the asterisk-code-review mailing list