Bugzilla bug #26506: getipnodebyname and getipnodebyaddr are thread
authorwtc%netscape.com
Tue, 23 May 2000 00:51:09 +0000
changeset 1376 a285dc6438f904e122429cb168f487c104078dc4
parent 1375 53ab283f4728b80e961a52238ad267c217d5f1d9
child 1377 f3a07faafb41555d5d3864e2b0b0d7adbb1d0c95
push idunknown
push userunknown
push dateunknown
bugs26506
Bugzilla bug #26506: getipnodebyname and getipnodebyaddr are thread safe, so no need to lock around them.
pr/src/misc/prnetdb.c
--- a/pr/src/misc/prnetdb.c
+++ b/pr/src/misc/prnetdb.c
@@ -380,33 +380,33 @@ PR_IMPLEMENT(PRStatus) PR_GetIPNodeByNam
     if (!_pr_initialized) _PR_ImplicitInitialization();
 
     PR_ASSERT(af == PR_AF_INET || af == PR_AF_INET6);
     if (af != PR_AF_INET && af != PR_AF_INET6) {
         PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
         return PR_FAILURE;
     }
 
-	LOCK_DNS();
-
 #if defined(_PR_HAVE_GETIPNODEBYNAME)
 	if (flags & PR_AI_V4MAPPED)
 		tmp_flags |= AI_V4MAPPED;
 	if (flags & PR_AI_ADDRCONFIG)
 		tmp_flags |= AI_ADDRCONFIG;
 	if (flags & PR_AI_ALL)
 		tmp_flags |= AI_ALL;
     if (af == PR_AF_INET6)
     	md_af = AF_INET6;
 	else
     	md_af = af;
 #endif
 
+    /* Do not need to lock the DNS lock if getipnodebyname() is called */
 #ifdef _PR_INET6
 #ifdef _PR_HAVE_GETHOSTBYNAME2
+    LOCK_DNS();
     if (af == PR_AF_INET6)
     {
 #ifdef _PR_INET6_PROBE
       if (_pr_ipv6_is_present == PR_TRUE)
 #endif
         h = gethostbyname2(name, AF_INET6); 
         if ((NULL == h) && (flags & PR_AI_V4MAPPED))
         {
@@ -422,19 +422,23 @@ PR_IMPLEMENT(PRStatus) PR_GetIPNodeByNam
 #elif defined(_PR_HAVE_GETIPNODEBYNAME)
     h = getipnodebyname(name, md_af, tmp_flags, &error_num);
 #else
 #error "Unknown name-to-address translation function"
 #endif	/* _PR_HAVE_GETHOSTBYNAME2 */
 #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME)
     if (_pr_ipv6_is_present == PR_TRUE)
     	h = (*((_pr_getipnodebyname_t)_pr_getipnodebyname_fp))(name, md_af, tmp_flags, &error_num);
-	else
+    else
+    {
+        LOCK_DNS();
     	h = gethostbyname(name);
+    }
 #else /* _PR_INET6 */
+    LOCK_DNS();
 #ifdef XP_OS2_VACPP
 	h = gethostbyname((char *)name);
 #else
     h = gethostbyname(name);
 #endif
 #endif /* _PR_INET6 */
     
 	if (NULL == h)
@@ -492,17 +496,28 @@ PR_IMPLEMENT(PRStatus) PR_GetIPNodeByNam
 				if (!hp->h_addr_list[na]) return PR_FAILURE;
 				MakeIPv4MappedAddr(*ap, hp->h_addr_list[na]);
 			}
 			hp->h_addr_list[na] = 0;
 		}
 #endif
 	}
 
-	UNLOCK_DNS();
+    /* Must match the convoluted logic above for LOCK_DNS() */
+#ifdef _PR_INET6
+#ifdef _PR_HAVE_GETHOSTBYNAME2
+    UNLOCK_DNS();
+#endif	/* _PR_HAVE_GETHOSTBYNAME2 */
+#elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME)
+    if (_pr_ipv6_is_present == PR_FALSE)
+        UNLOCK_DNS();
+#else /* _PR_INET6 */
+    UNLOCK_DNS();
+#endif /* _PR_INET6 */
+
 	return rv;
 }
 
 PR_IMPLEMENT(PRStatus) PR_GetHostByAddr(
     const PRNetAddr *hostaddr, char *buf, PRIntn bufsize, PRHostEnt *hostentry)
 {
 	struct hostent *h;
 	PRStatus rv = PR_FAILURE;
@@ -511,17 +526,16 @@ PR_IMPLEMENT(PRStatus) PR_GetHostByAddr(
 	int addrlen;
 	PRInt32 af;
 #if defined(_PR_HAVE_GETIPNODEBYADDR)
 	int error_num;
 #endif
 
     if (!_pr_initialized) _PR_ImplicitInitialization();
 
-	LOCK_DNS();
 	if (hostaddr->raw.family == PR_AF_INET6)
 	{
 #if defined(_PR_INET6_PROBE)
 		if (_pr_ipv6_is_present == PR_TRUE)
 			af = AF_INET6;
 		else
 			af = AF_INET;
 #elif defined(_PR_INET6)
@@ -556,25 +570,30 @@ PR_IMPLEMENT(PRStatus) PR_GetHostByAddr(
 		}
 	} else {
 		PR_ASSERT(hostaddr->raw.family == AF_INET);
 		PR_ASSERT(af == AF_INET);
 		addr = &hostaddr->inet.ip;
 		addrlen = sizeof(hostaddr->inet.ip);
 	}
 
+    /* Do not need to lock the DNS lock if getipnodebyaddr() is called */
 #if defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6)
 	h = getipnodebyaddr(addr, addrlen, af, &error_num);
 #elif defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6_PROBE)
     if (_pr_ipv6_is_present == PR_TRUE)
     	h = (*((_pr_getipnodebyaddr_t)_pr_getipnodebyaddr_fp))(addr, addrlen,
 				af, &error_num);
 	else
+    {
+        LOCK_DNS();
 		h = gethostbyaddr(addr, addrlen, af);
+    }
 #else	/* _PR_HAVE_GETIPNODEBYADDR */
+    LOCK_DNS();
 #ifdef XP_OS2_VACPP
 	h = gethostbyaddr((char *)addr, addrlen, af);
 #else
 	h = gethostbyaddr(addr, addrlen, af);
 #endif
 #endif /* _PR_HAVE_GETIPNODEBYADDR */
 	if (NULL == h)
 	{
@@ -609,17 +628,26 @@ PR_IMPLEMENT(PRStatus) PR_GetHostByAddr(
 		}
 #if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYADDR)
 		freehostent(h);
 #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYADDR)
     	if (_pr_ipv6_is_present == PR_TRUE)
 			(*((_pr_freehostent_t)_pr_freehostent_fp))(h);
 #endif
 	}
-	UNLOCK_DNS();
+
+    /* Must match the convoluted logic above for LOCK_DNS() */
+#if defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6)
+#elif defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6_PROBE)
+    if (_pr_ipv6_is_present == PR_FALSE)
+        UNLOCK_DNS();
+#else	/* _PR_HAVE_GETIPNODEBYADDR */
+    UNLOCK_DNS();
+#endif /* _PR_HAVE_GETIPNODEBYADDR */
+
 	return rv;
 }
 
 /******************************************************************************/
 /*
  * Some systems define a reentrant version of getprotobyname(). Too bad
  * the signature isn't always the same. But hey, they tried. If there
  * is such a definition, use it. Otherwise, grab a lock and do it here.