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.

235 lines
5.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996.
  5. //
  6. // File: net.cxx
  7. //
  8. // Contents:
  9. // Net helper functions.
  10. //
  11. // History:
  12. //--------------------------------------------------------------------------
  13. #include "act.hxx"
  14. #if 1 // #ifndef _CHICAGO_
  15. DWORD APIENTRY ScmWNetGetUniversalName(
  16. LPCWSTR lpLocalPath,
  17. DWORD dwInfoLevel,
  18. LPVOID lpBuffer,
  19. LPDWORD lpBufferSize
  20. )
  21. {
  22. static GET_UNIVERSAL_NAME_FUNC pfnWNetGetUniversalName = 0;
  23. HINSTANCE hLib;
  24. if ( pfnWNetGetUniversalName == 0 )
  25. {
  26. hLib = LoadLibraryT( TEXT("mpr.dll") );
  27. if ( ! hLib )
  28. return GetLastError();
  29. pfnWNetGetUniversalName =
  30. (GET_UNIVERSAL_NAME_FUNC) GetProcAddress( hLib, "WNetGetUniversalNameW" );
  31. if ( pfnWNetGetUniversalName == 0 )
  32. return GetLastError();
  33. }
  34. return (*pfnWNetGetUniversalName)( lpLocalPath, dwInfoLevel, lpBuffer, lpBufferSize );
  35. }
  36. NET_API_STATUS NET_API_FUNCTION ScmNetShareGetInfo(
  37. LPTSTR servername,
  38. LPTSTR netname,
  39. DWORD level,
  40. LPBYTE *bufptr
  41. )
  42. {
  43. static NET_SHARE_GET_INFO_FUNC pfnNetShareGetInfo = 0;
  44. HINSTANCE hLib;
  45. if ( pfnNetShareGetInfo == 0 )
  46. {
  47. hLib = LoadLibraryT( TEXT("netapi32.dll") );
  48. if ( ! hLib )
  49. return GetLastError();
  50. pfnNetShareGetInfo =
  51. (NET_SHARE_GET_INFO_FUNC) GetProcAddress( hLib, "NetShareGetInfo" );
  52. if ( pfnNetShareGetInfo == 0 )
  53. return GetLastError();
  54. }
  55. return (*pfnNetShareGetInfo)( servername, netname, level, bufptr );
  56. }
  57. HRESULT ScmGetUniversalName(
  58. LPCWSTR lpLocalPath, // original path
  59. LPWSTR lpBuffer, // buffer for UNC path
  60. LPDWORD lpBufferSize // size of buffer in WCHARs
  61. )
  62. {
  63. // The local path is assumed to be absolute -- either UNC or drive-based
  64. if (lpLocalPath[0] == L'\\' && lpLocalPath[1] == L'\\') // UNC path
  65. {
  66. DWORD dwLocal = lstrlenW(lpLocalPath);
  67. if (*lpBufferSize - dwLocal > 0)
  68. {
  69. lstrcpyW(lpBuffer,lpLocalPath);
  70. return NO_ERROR;
  71. }
  72. else
  73. {
  74. *lpBufferSize = dwLocal + 1;
  75. return ERROR_MORE_DATA;
  76. }
  77. }
  78. else if (lpLocalPath[1] == L':' && lpLocalPath[2] == L'\\') // drive-based path
  79. {
  80. WCHAR drive[3];
  81. drive[0] = lpLocalPath[0];
  82. drive[1] = L':';
  83. drive[2] = 0;
  84. WCHAR remConnection[MAX_PATH+1];
  85. WCHAR *pszPath = remConnection;
  86. DWORD dwSize = (MAX_PATH+1) * sizeof(WCHAR);
  87. DWORD Status = OleWNetGetConnection(drive, pszPath, &dwSize);
  88. if (Status != NO_ERROR)
  89. {
  90. return CO_E_BAD_PATH;
  91. }
  92. lpLocalPath += 2;
  93. DWORD dwNetLength = lstrlenW(pszPath);
  94. DWORD dwFullLength = dwNetLength + lstrlenW(lpLocalPath);
  95. if (*lpBufferSize - dwFullLength > 0)
  96. {
  97. lstrcpyW(lpBuffer,pszPath);
  98. lpBuffer += dwNetLength;
  99. lstrcpyW(lpBuffer,lpLocalPath);
  100. return NO_ERROR;
  101. }
  102. else
  103. {
  104. *lpBufferSize = dwFullLength + 1;
  105. return ERROR_MORE_DATA;
  106. }
  107. }
  108. else
  109. {
  110. return CO_E_BAD_PATH;
  111. }
  112. }
  113. #else // _CHICAGO_
  114. // From objex, which doesn't have shared source code for NT & win9x.
  115. RPC_STATUS
  116. IP_BuildAddressVector(
  117. OUT NETWORK_ADDRESS_VECTOR **ppAddressVector
  118. )
  119. /*++
  120. Routine Description:
  121. Builds a vector of IP addresses supported by this machine.
  122. Arguments:
  123. ppAddressVector - A place to store the vector.
  124. Return Value:
  125. RPC_S_OK
  126. RPC_S_OUT_OF_MEMORY
  127. RPC_S_OUT_OF_RESOURCES
  128. --*/
  129. {
  130. // Figure out all of our IP addresses
  131. CHAR hostname[IP_MAXIMUM_PRETTY_NAME];
  132. PHOSTENT phostent;
  133. UINT i;
  134. if (gethostname(hostname, IP_MAXIMUM_PRETTY_NAME) != 0)
  135. {
  136. return(RPC_S_OUT_OF_RESOURCES);
  137. }
  138. if ( (phostent = gethostbyname(hostname)) == 0)
  139. {
  140. return(RPC_S_OUT_OF_RESOURCES);
  141. }
  142. // Count ip addresses
  143. UINT cAddrs = 0;
  144. while(phostent->h_addr_list[cAddrs])
  145. cAddrs++;
  146. NETWORK_ADDRESS_VECTOR *pVector;
  147. pVector = (NETWORK_ADDRESS_VECTOR *)
  148. PrivMemAlloc( sizeof(NETWORK_ADDRESS_VECTOR)
  149. + (cAddrs * sizeof(WCHAR *))
  150. + (cAddrs * IP_MAXIMUM_RAW_NAME * sizeof(WCHAR)) );
  151. if (pVector == NULL)
  152. {
  153. return(RPC_S_OUT_OF_MEMORY);
  154. }
  155. pVector->Count = 0;
  156. pVector->NetworkAddresses = (WCHAR **) &pVector[1];
  157. pVector->NetworkAddresses[0] = (WCHAR *)&pVector->NetworkAddresses[cAddrs];
  158. char * pszAddress;
  159. int Status;
  160. for ( i = 0; (i < cAddrs) && (phostent->h_addr_list[i]); i++ )
  161. {
  162. pszAddress = inet_ntoa(*(struct in_addr *)phostent->h_addr_list[i]);
  163. Status = MultiByteToWideChar(
  164. CP_ACP,
  165. 0,
  166. pszAddress,
  167. -1,
  168. pVector->NetworkAddresses[i],
  169. IP_MAXIMUM_RAW_NAME
  170. );
  171. if ( 0 == Status )
  172. {
  173. PrivMemFree(pVector);
  174. return(RPC_S_OUT_OF_RESOURCES);
  175. }
  176. pVector->Count++;
  177. if (i != cAddrs - 1)
  178. {
  179. // Setup for next address, if any
  180. pVector->NetworkAddresses[i+1] = pVector->NetworkAddresses[i];
  181. pVector->NetworkAddresses[i+1] += lstrlenW(pVector->NetworkAddresses[i]) + 1;
  182. }
  183. }
  184. *ppAddressVector = pVector;
  185. return(RPC_S_OK);
  186. }
  187. #endif // _CHICAGO_