getdomainname

Pierre A. Humblet Pierre.Humblet@ieee.org
Wed Jun 18 02:05:00 GMT 2003


In the course of working on minires I noticed that Cygwin had
a getdomainname() function, but that it was not fully functional.
Here is a fix.

Note that I have used strncpy to comform to a man page I found
<http://www.freebsd.org/cgi/man.cgi?query=getdomainname&apropos=0&sektion=0&
manpath=NetBSD+1.6.1&format=html>
(alternatives are to return an error if name too long, or use
 strlcpy).

Pierre

2003-06-18  Pierre Humblet  <pierre.humblet@ieee.org>

	* autoload.cc (GetNetworkParams): Add.
	* net.cc (getdomainname): Call GetNetworkParams and read the
	DhcpDomain registry value if warranted.
-------------- next part --------------
Index: autoload.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/autoload.cc,v
retrieving revision 1.70
diff -u -p -r1.70 autoload.cc
--- autoload.cc	25 May 2003 09:18:43 -0000	1.70
+++ autoload.cc	17 Jun 2003 23:41:37 -0000
@@ -492,6 +492,7 @@ LoadDLLfuncEx (WSAEnumNetworkEvents, 12,
 LoadDLLfuncEx (GetIfTable, 12, iphlpapi, 1)
 LoadDLLfuncEx (GetIfEntry, 4, iphlpapi, 1)
 LoadDLLfuncEx (GetIpAddrTable, 12, iphlpapi, 1)
+LoadDLLfuncEx (GetNetworkParams, 8, iphlpapi, 1)

 LoadDLLfunc (CoInitialize, 4, ole32)
 LoadDLLfunc (CoUninitialize, 0, ole32)
Index: net.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/net.cc,v
retrieving revision 1.145
diff -u -p -r1.145 net.cc
--- net.cc	16 Jun 2003 03:24:11 -0000	1.145
+++ net.cc	17 Jun 2003 23:41:41 -0000
@@ -1291,20 +1291,39 @@ getdomainname (char *domain, size_t len)
   if (__check_null_invalid_struct_errno (domain, len))
     return -1;

+  PFIXED_INFO info = NULL;
+  ULONG size = 0;
+
+  if (GetNetworkParams(info, &size) == ERROR_BUFFER_OVERFLOW
+      && (info = (PFIXED_INFO) alloca(size))
+      && GetNetworkParams(info, &size) == ERROR_SUCCESS)
+    {
+      strncpy(domain, info->DomainName, len);
+      return 0;
+    }
+
+  /* This is only used by Win95 and NT <=  4.0.
+     FIXME: Are the registry names language dependent?
+     FIXME: Handle DHCP on Win95. The DhcpDomain(s) may be available
+     in ..VxD\DHCP\DhcpInfoXX\OptionInfo, RFC 1533 format */
+
   reg_key r (HKEY_LOCAL_MACHINE, KEY_READ,
 	     (!wincap.is_winnt ()) ? "System" : "SYSTEM",
 	     "CurrentControlSet", "Services",
 	     (!wincap.is_winnt ()) ? "VxD" : "Tcpip",
 	     (!wincap.is_winnt ()) ? "MSTCP" : "Parameters", NULL);

-  /* FIXME: Are registry keys case sensitive? */
-  if (r.error () || r.get_string ("Domain", domain, len, "") != ERROR_SUCCESS)
+  if (!r.error ())
     {
-      __seterrno ();
-      return -1;
+      int res1, res2 = 0; /* Suppress compiler warning */
+      res1 = r.get_string ("Domain", domain, len, "");
+      if (res1 != ERROR_SUCCESS || !domain[0])
+	res2 = r.get_string ("DhcpDomain", domain, len, "");
+      if (res1 == ERROR_SUCCESS || res2 == ERROR_SUCCESS)
+	return 0;
     }
-
-  return 0;
+  __seterrno ();
+  return -1;
 }

 /* Fill out an ifconf struct. */


More information about the Cygwin-patches mailing list