Blame SOURCES/rsyslog-8.24.0-rhbz1056548-getaddrinfo.patch

fde8c1
diff --git a/runtime/net.c b/runtime/net.c
fde8c1
index 3610fc5..2d8de94 100644
fde8c1
--- a/runtime/net.c
fde8c1
+++ b/runtime/net.c
fde8c1
@@ -1181,26 +1181,24 @@ getLocalHostname(uchar **ppName)
fde8c1
 	}
fde8c1
 
fde8c1
 	char *dot = strstr(hnbuf, ".");
fde8c1
+	struct addrinfo *res = NULL;
fde8c1
 	if(!empty_hostname && dot == NULL) {
fde8c1
 		/* we need to (try) to find the real name via resolver */
fde8c1
-		struct hostent *hent = gethostbyname((char*)hnbuf);
fde8c1
-		if(hent) {
fde8c1
-			int i = 0;
fde8c1
-			if(hent->h_aliases) {
fde8c1
-				const size_t hnlen = strlen(hnbuf);
fde8c1
-				for(i = 0; hent->h_aliases[i]; i++) {
fde8c1
-					if(!strncmp(hent->h_aliases[i], hnbuf, hnlen)
fde8c1
-					   && hent->h_aliases[i][hnlen] == '.') {
fde8c1
-						break; /* match! */
fde8c1
-					}
fde8c1
-				}
fde8c1
-			}
fde8c1
-			if(hent->h_aliases && hent->h_aliases[i]) {
fde8c1
-				CHKmalloc(fqdn = (uchar*)strdup(hent->h_aliases[i]));
fde8c1
-			} else {
fde8c1
-				CHKmalloc(fqdn = (uchar*)strdup(hent->h_name));
fde8c1
+		struct addrinfo flags;
fde8c1
+		memset(&flags, 0, sizeof(flags));
fde8c1
+		flags.ai_flags = AI_CANONNAME;
fde8c1
+		int error = getaddrinfo((char*)hnbuf, NULL, &flags, &res;;
fde8c1
+		if (error != 0) {
fde8c1
+			dbgprintf("getaddrinfo: %s\n", gai_strerror(error));
fde8c1
+			ABORT_FINALIZE(RS_RET_IO_ERROR);
fde8c1
+		}
fde8c1
+		if (res != NULL) {
fde8c1
+			/* When AI_CANONNAME is set first member of res linked-list */
fde8c1
+			/* should contain what we need */
fde8c1
+			if (res->ai_canonname != NULL && res->ai_canonname[0] != '\0') {
fde8c1
+				CHKmalloc(fqdn = (uchar*)strdup(res->ai_canonname));
fde8c1
+				dot = strstr((char*)fqdn, ".");
fde8c1
 			}
fde8c1
-			dot = strstr((char*)fqdn, ".");
fde8c1
 		}
fde8c1
 	}
fde8c1
 
fde8c1
@@ -1215,6 +1213,9 @@ getLocalHostname(uchar **ppName)
fde8c1
 
fde8c1
 	*ppName = fqdn;
fde8c1
 finalize_it:
fde8c1
+	if (res != NULL) {
fde8c1
+		freeaddrinfo(res);
fde8c1
+	}
fde8c1
 	RETiRet;
fde8c1
 }
fde8c1