public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] bugfix for network/lib6/getaddrinfo_01.c
@ 2021-05-11  9:16 dongshijiang
  2021-05-11 12:21 ` Petr Vorel
  0 siblings, 1 reply; 4+ messages in thread
From: dongshijiang @ 2021-05-11  9:16 UTC (permalink / raw)
  To: ltp

For test cases (test2 ,IPV4 canonical name) and (test13 ,IPV6 canonical name)

The gethostname() API returns the official name of host, not the canonical name of host. The canonical name of host needs to be obtained through the gethostbyname() API;

Signed-off-by: dongshijiang <dongshijiang@inspur.com>
---
 testcases/network/lib6/getaddrinfo_01.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/testcases/network/lib6/getaddrinfo_01.c b/testcases/network/lib6/getaddrinfo_01.c
index db252a998..bf5af7b4a 100644
--- a/testcases/network/lib6/getaddrinfo_01.c
+++ b/testcases/network/lib6/getaddrinfo_01.c
@@ -75,6 +75,14 @@ static void gaiv4(void)
 	if (gethostname(hostname, sizeof(hostname)) < 0)
 		tst_brkm(TBROK | TERRNO, NULL, "gethostname failed");
 	strncpy(shortname, hostname, MAXHOSTNAMELEN);
+
+	//get official name of host
+	struct hostent *phostent = NULL;
+
+	phostent = gethostbyname(hostname);
+	if (phostent == NULL)
+		tst_brkm(TBROK | TERRNO, NULL, "gethostbyname failed");
+
 	shortname[MAXHOSTNAMELEN] = '\0';
 	p = strchr(shortname, '.');
 	if (p)
@@ -134,10 +142,10 @@ static void gaiv4(void)
 				 "entries with canonical name set");
 			freeaddrinfo(aires);
 			return;
-		} else if (strcasecmp(hostname, pai->ai_canonname)) {
+		} else if (strcasecmp(phostent->h_name, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv4 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
-				 pai->ai_canonname, hostname);
+				 pai->ai_canonname, phostent->h_name);
 			freeaddrinfo(aires);
 			return;
 		}
@@ -533,6 +541,14 @@ static void gaiv6(void)
 	if (gethostname(hostname, sizeof(hostname)) < 0)
 		tst_brkm(TBROK, NULL, "gethostname failed - %s",
 			 strerror(errno));
+
+	//get official name of host
+	struct hostent *phostent = NULL;
+
+	phostent = gethostbyname(hostname);
+	if (phostent == NULL)
+		tst_brkm(TBROK | TERRNO, NULL, "gethostbyname failed");
+
 	strncpy(shortname, hostname, MAXHOSTNAMELEN);
 	shortname[MAXHOSTNAMELEN] = '\0';
 	p = strchr(shortname, '.');
@@ -593,10 +609,10 @@ static void gaiv6(void)
 				 "entries with canonical name set");
 			freeaddrinfo(aires);
 			return;
-		} else if (strcasecmp(hostname, pai->ai_canonname)) {
+		} else if (strcasecmp(phostent->h_name, pai->ai_canonname)) {
 			tst_resm(TFAIL, "getaddrinfo IPv6 canonical name "
 				 "(\"%s\") doesn't match hostname (\"%s\")",
-				 pai->ai_canonname, hostname);
+				 pai->ai_canonname, phostent->h_name);
 			freeaddrinfo(aires);
 			return;
 		}
-- 
2.18.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-05-11 13:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-11  9:16 [LTP] [PATCH] bugfix for network/lib6/getaddrinfo_01.c dongshijiang
2021-05-11 12:21 ` Petr Vorel
2021-05-11 12:58   ` Alexey Kodanev
2021-05-11 13:47     ` Petr Vorel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox