Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: vsrvinfo.cxx
  7. //
  8. // Contents: Retrieves the virtual server address in the form of
  9. // L"a.b.c.d" if the process calling this is in the context
  10. // of a virtual server.
  11. //
  12. // History: 9-03-96 srikants Created
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.cxx>
  16. #pragma hdrstop
  17. #include <winsock.h>
  18. #include <webdbg.hxx>
  19. #include <vsrvinfo.hxx>
  20. //+---------------------------------------------------------------------------
  21. //
  22. // Class: CVServerInfo
  23. //
  24. // Purpose: Determines the virtual server ip address (if applicable)
  25. //
  26. // History: 9-03-96 srikants Created
  27. //
  28. //----------------------------------------------------------------------------
  29. class CVServerInfo
  30. {
  31. public:
  32. CVServerInfo( char const * pszServer );
  33. WCHAR * GetVirtualServerIpAddress();
  34. private:
  35. ULONG _GetIpAddress( char const * pszName );
  36. static BOOL _fSocketsInit;
  37. char const * _pszServer;
  38. char _szDefaultServer[MAX_PATH];
  39. ULONG _ipServer;
  40. ULONG _ipDefaultServer;
  41. };
  42. BOOL CVServerInfo::_fSocketsInit = FALSE;
  43. //+---------------------------------------------------------------------------
  44. //
  45. // Member: CVServerInfo::CVServerInfo
  46. //
  47. // Synopsis: Constructor - stores the given name of the server and also
  48. // determines the default server address.
  49. //
  50. // Arguments: [pszName] - Name of the server which launched this program.
  51. //
  52. // History: 9-03-96 srikants Created
  53. //
  54. // Notes:
  55. //
  56. //----------------------------------------------------------------------------
  57. CVServerInfo::CVServerInfo( char const * pszName )
  58. :_pszServer(pszName)
  59. {
  60. _ipServer = (ULONG) SOCKET_ERROR;
  61. _ipDefaultServer = (ULONG) SOCKET_ERROR;
  62. DWORD dwError = 0;
  63. //
  64. // Initialize sockets interface if not already initialized.
  65. //
  66. if ( !_fSocketsInit )
  67. {
  68. INT wsaResult = SOCKET_ERROR; // result of the WSAStartup routine
  69. WSADATA wsadata;
  70. wsaResult = WSAStartup( 0x101, &wsadata );
  71. if( SOCKET_ERROR == wsaResult )
  72. {
  73. dwError = WSAGetLastError();
  74. webDebugOut(( DEB_ERROR, "WSAStartup() failed with error %d\n",
  75. dwError ));
  76. THROW( CException( dwError ) );
  77. }
  78. _fSocketsInit = TRUE;
  79. }
  80. //
  81. // Retrieve the name of the current host.
  82. //
  83. if ( SOCKET_ERROR == gethostname( _szDefaultServer, sizeof(_szDefaultServer)) )
  84. {
  85. dwError = WSAGetLastError();
  86. webDebugOut(( DEB_ERROR, "gethostname failed with error %d\n",
  87. dwError ));
  88. THROW( CException( dwError ) );
  89. }
  90. }
  91. //+---------------------------------------------------------------------------
  92. //
  93. // Member: CVServerInfo::_GetIpAddress
  94. //
  95. // Synopsis: Retrieves the ipaddress of the given server name.
  96. //
  97. // Arguments: [pszName] - Server name. Can be either of the form
  98. // "[email protected]" or "foo" or "1.2.3.4"
  99. //
  100. // Returns: Ipaddress as a ULONG of the given server
  101. //
  102. // History: 9-03-96 srikants Created
  103. //
  104. //----------------------------------------------------------------------------
  105. ULONG CVServerInfo::_GetIpAddress( char const * pszName )
  106. {
  107. Win4Assert( 0 != pszName );
  108. struct hostent * pHostEntry = 0;
  109. ULONG ulIpAddress = inet_addr( pszName );
  110. if ( INADDR_NONE == ulIpAddress )
  111. {
  112. pHostEntry = gethostbyname( pszName );
  113. if ( 0 == pHostEntry )
  114. {
  115. DWORD dwError = WSAGetLastError();
  116. webDebugOut(( DEB_ERROR, "gethostbyname failed with error %d\n",
  117. dwError ));
  118. THROW( CException( dwError ) );
  119. }
  120. RtlCopyMemory( &ulIpAddress, pHostEntry->h_addr,
  121. sizeof(ulIpAddress) );
  122. }
  123. return ulIpAddress;
  124. }
  125. //+---------------------------------------------------------------------------
  126. //
  127. // Member: CVServerInfo::GetVirtualServerIpAddress
  128. //
  129. // Synopsis: Retrieves the virtual server ip address as WCHAR "a.b.c.d"
  130. // if the process is running in the context of a virtual server.
  131. // NULL if it is in the context of a default server.
  132. //
  133. // History: 9-03-96 srikants Created
  134. //
  135. //----------------------------------------------------------------------------
  136. WCHAR * CVServerInfo::GetVirtualServerIpAddress()
  137. {
  138. if ( 0 == _stricmp( _pszServer, _szDefaultServer) )
  139. {
  140. webDebugOut(( DEB_ITRACE, "found default server\n" ));
  141. return 0;
  142. }
  143. _ipServer = _GetIpAddress( _pszServer );
  144. _ipDefaultServer = _GetIpAddress( _szDefaultServer );
  145. #if 0
  146. {
  147. char const * szIpAddress = inet_ntoa( *((struct in_addr *) &_ipServer) );
  148. szIpAddress = inet_ntoa( *((struct in_addr *) &_ipDefaultServer) );
  149. }
  150. #endif // 0
  151. if ( _ipServer == _ipDefaultServer )
  152. return 0;
  153. //
  154. // Convert the ULONG form of ip address to a string form and
  155. // return that.
  156. //
  157. Win4Assert( sizeof(_ipServer) == sizeof(struct in_addr) );
  158. char const * szIpAddress = inet_ntoa( *((struct in_addr *) &_ipServer) );
  159. size_t len = strlen( szIpAddress );
  160. XArray<WCHAR> xIpAddress(len+1);
  161. //
  162. // As the ip address just consists of numbers and periods, we can
  163. // directly copy to the wide char array.
  164. //
  165. for ( unsigned i = 0; i < len; i++ )
  166. xIpAddress[i] = (WCHAR) szIpAddress[i];
  167. xIpAddress[i] = 0;
  168. return xIpAddress.Acquire();
  169. }
  170. WCHAR * GetVirtualServerIpAddress( char const * pszServer )
  171. {
  172. CVServerInfo info( pszServer );
  173. return info.GetVirtualServerIpAddress();
  174. }