tcpip.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <windows.h>
  42. #include "nt/ntos.h"
  43. /*---------------------------------------------------------------------------*\
  44. *
  45. * Function: GetServerDefaultHostName
  46. *
  47. * Purpose: This function gets the default host name
  48. *
  49. * Input:
  50. *
  51. * Returns:
  52. *
  53. * Comments:
  54. \*---------------------------------------------------------------------------*/
  55. DWORD NS_WINAPI
  56. TCPIP_GetDefaultHostName( LPTSTR lpszFullHostName, LPTSTR lpszHostName, LPTSTR lpszDomainName )
  57. {
  58. char * szKey;
  59. char * szName;
  60. DWORD dwValueType;
  61. DWORD dwIpHostSize = 256;
  62. char szIpHost[256];
  63. DWORD dwIpDomainSize = 256;
  64. char szIpDomain[256];
  65. BOOL bWinNT;
  66. /* get operating system */
  67. switch ( INFO_GetOperatingSystem() ) {
  68. case OS_WIN95: bWinNT = FALSE; break;
  69. case OS_WINNT: bWinNT = TRUE; break;
  70. default: return TCPIP_UNSUPPORTED_OS;
  71. }
  72. #if 0
  73. int lastError;
  74. WSADATA WSAData;
  75. if ( WSAStartup( 0x0101, &WSAData ) != 0 ) {
  76. lastError = WSAGetLastError();
  77. m_pMainWnd->MessageBox ( "TCP/IP must be installed.\nUse the Network Icon in Control Panel" );
  78. return FALSE;
  79. }
  80. lastError = gethostname ( szIpHost, sizeof(szIpHost) );
  81. #endif
  82. /* get list of all keys under Netscape */
  83. if ( bWinNT )
  84. szKey = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters";
  85. else
  86. szKey = "SYSTEM\\CurrentControlSet\\Services\\Vxd\\MSTCP";
  87. if( !REG_CheckIfKeyExists( HKEY_LOCAL_MACHINE, szKey ) ) {
  88. return TCPIP_NO_TCPIP;
  89. }
  90. /* get host name for computer. May have to get DHCP host name if empty */
  91. szName = "Hostname";
  92. if( !REG_GetRegistryParameter( HKEY_LOCAL_MACHINE, szKey, szName, &dwValueType, (LPBYTE)szIpHost, &dwIpHostSize ) ) {
  93. szIpHost[0] = '\0';
  94. }
  95. /* get domain name for computer. May have to get DHCP host name if empty */
  96. szName = "Domain";
  97. if( !REG_GetRegistryParameter( HKEY_LOCAL_MACHINE, szKey, szName, &dwValueType, (LPBYTE)szIpDomain, &dwIpDomainSize ) ) {
  98. dwIpDomainSize = 0;
  99. }
  100. if ( dwIpDomainSize == 0 ) {
  101. szName = "DhcpDomain";
  102. if( !REG_GetRegistryParameter( HKEY_LOCAL_MACHINE, szKey, szName, &dwValueType, (LPBYTE)szIpDomain, &dwIpDomainSize ) ) {
  103. dwIpDomainSize = 0;
  104. }
  105. }
  106. if ( lpszHostName )
  107. strcpy ( lpszHostName, szIpHost );
  108. strcpy ( lpszFullHostName, szIpHost );
  109. if ( lpszDomainName ) {
  110. if ( dwIpDomainSize == 0 )
  111. *lpszDomainName = '\0';
  112. else {
  113. strcpy ( lpszDomainName, szIpDomain );
  114. strcat ( lpszFullHostName, "." );
  115. strcat ( lpszFullHostName, lpszDomainName );
  116. }
  117. }
  118. return TCPIP_NO_ERROR;
  119. }
  120. /*---------------------------------------------------------------------------*\
  121. *
  122. * Function: TCPIP_VerifyHostName
  123. *
  124. * Purpose: This function validates the host name
  125. *
  126. * Input:
  127. *
  128. * Returns:
  129. *
  130. * Comments:
  131. \*---------------------------------------------------------------------------*/
  132. DWORD NS_WINAPI
  133. TCPIP_VerifyHostName( LPCTSTR lpszHostName )
  134. {
  135. struct hostent *ent;
  136. WSADATA wsd;
  137. int lastError;
  138. if(WSAStartup(MAKEWORD(1, 1), &wsd) != 0)
  139. return TCPIP_NO_WINSOCK_DLL;
  140. ent = gethostbyname ( lpszHostName );
  141. lastError = WSAGetLastError();
  142. WSACleanup();
  143. if ( ent == NULL ) {
  144. switch ( lastError ) {
  145. case WSANOTINITIALISED: // A successful WSAStartup must occur before using this function.
  146. break;
  147. case WSAENETDOWN: // The Windows Sockets implementation has detected that the network subsystem has failed.
  148. return TCPIP_NETWORK_DOWN;
  149. case WSAHOST_NOT_FOUND: // Authoritative Answer Host not found.
  150. return TCPIP_HOST_NOT_FOUND;
  151. case WSATRY_AGAIN: // Non-Authoritative Host not found, or SERVERFAIL.
  152. return TCPIP_HOST_SERVER_DOWN;
  153. case WSANO_RECOVERY: // Nonrecoverable errors: FORMERR, REFUSED, NOTIMP.
  154. return TCPIP_NETWORK_ERROR;
  155. case WSANO_DATA: // Valid name, no data record of requested type.
  156. return TCPIP_HOST_VALID_NAME;
  157. case WSAEINPROGRESS: // A blocking Windows Sockets operation is in progress.
  158. return TCPIP_NETWORK_ERROR;
  159. case WSAEINTR: // The (blocking) call was canceled using
  160. return TCPIP_NETWORK_ERROR;
  161. default:
  162. return TCPIP_NETWORK_ERROR;
  163. }
  164. }
  165. return TCPIP_NO_ERROR;
  166. }