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.

277 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 1991-1993 Microsoft Corporation
  3. Module Name:
  4. Conn.c
  5. Abstract:
  6. This file contains the RpcXlate code to handle the Connection APIs.
  7. Author:
  8. John Rogers (JohnRo) 23-Jul-1991
  9. Environment:
  10. Portable to any flat, 32-bit environment. (Uses Win32 typedefs.)
  11. Requires ANSI C extensions: slash-slash comments, long external names.
  12. Revision History:
  13. 23-Jul-1991 JohnRo
  14. Created.
  15. 15-Oct-1991 JohnRo
  16. Be paranoid about possible infinite loop.
  17. 21-Nov-1991 JohnRo
  18. Removed NT dependencies to reduce recompiles.
  19. 01-Apr-1992 JohnRo
  20. Use NetApiBufferAllocate() instead of private version.
  21. 03-Feb-1993 JohnRo
  22. RAID 8926: NetConnectionEnum to downlevel: memory leak on error.
  23. Also prevent possible infinite loop.
  24. Also set buffer pointer to NULL if success but no entries returned.
  25. --*/
  26. // These must be included first:
  27. #include <windef.h> // IN, DWORD, etc.
  28. #include <lmcons.h> // DEVLEN, NET_API_STATUS, etc.
  29. // These may be included in any order:
  30. #include <apinums.h> // API_ equates.
  31. #include <lmapibuf.h> // NetApiBufferAllocate(), NetApiBufferFree().
  32. #include <lmerr.h> // ERROR_ and NERR_ equates.
  33. #include <lmshare.h> // API's data structures.
  34. #include <netdebug.h> // DBGSTATIC, NetpKdPrint(), FORMAT_ equates.
  35. #include <netlib.h> // NetpAdjustPreferredMaximum().
  36. #include <prefix.h> // PREFIX_ equates.
  37. #include <rap.h> // LPDESC.
  38. #include <remdef.h> // REM16_, REM32_, REMSmb_ equates.
  39. #include <rx.h> // RxRemoteApi().
  40. #include <rxp.h> // RxpFatalErrorCode().
  41. #include <rxconn.h> // My prototype(s).
  42. #define MAX_CONNECTION_INFO_0_STRING_LEN \
  43. 0
  44. #define MAX_CONNECTION_INFO_1_STRING_LEN \
  45. ( LM20_UNLEN+1 + LM20_NNLEN+1 )
  46. #define MAX_CONNECTION_INFO_0_STRING_SIZE \
  47. ( MAX_CONNECTION_INFO_0_STRING_LEN * sizeof(TCHAR) )
  48. #define MAX_CONNECTION_INFO_1_STRING_SIZE \
  49. ( MAX_CONNECTION_INFO_1_STRING_LEN * sizeof(TCHAR) )
  50. #define ENUM_ARRAY_OVERHEAD_SIZE 0
  51. DBGSTATIC NET_API_STATUS
  52. RxpGetConnectionDataDescs(
  53. IN DWORD Level,
  54. OUT LPDESC * DataDesc16,
  55. OUT LPDESC * DataDesc32,
  56. OUT LPDESC * DataDescSmb,
  57. OUT LPDWORD ApiBufferSize32 OPTIONAL
  58. )
  59. {
  60. switch (Level) {
  61. case 0 :
  62. *DataDesc16 = REM16_connection_info_0;
  63. *DataDesc32 = REM32_connection_info_0;
  64. *DataDescSmb = REMSmb_connection_info_0;
  65. NetpSetOptionalArg(
  66. ApiBufferSize32,
  67. sizeof(CONNECTION_INFO_0)
  68. + MAX_CONNECTION_INFO_0_STRING_SIZE);
  69. return (NERR_Success);
  70. case 1 :
  71. *DataDesc16 = REM16_connection_info_1;
  72. *DataDesc32 = REM32_connection_info_1;
  73. *DataDescSmb = REMSmb_connection_info_1;
  74. NetpSetOptionalArg(
  75. ApiBufferSize32,
  76. sizeof(CONNECTION_INFO_1)
  77. + MAX_CONNECTION_INFO_1_STRING_SIZE);
  78. return (NERR_Success);
  79. default :
  80. return (ERROR_INVALID_LEVEL);
  81. }
  82. /* NOTREACHED */
  83. } // RxpGetConnectionDataDescs
  84. NET_API_STATUS
  85. RxNetConnectionEnum (
  86. IN LPTSTR UncServerName,
  87. IN LPTSTR Qualifier,
  88. IN DWORD Level,
  89. OUT LPBYTE *BufPtr,
  90. IN DWORD PreferedMaximumSize,
  91. OUT LPDWORD EntriesRead,
  92. OUT LPDWORD TotalEntries,
  93. IN OUT LPDWORD ResumeHandle OPTIONAL
  94. )
  95. /*++
  96. Routine Description:
  97. RxNetConnectionEnum performs the same function as NetConnectionEnum,
  98. except that the server name is known to refer to a downlevel server.
  99. Arguments:
  100. (Same as NetConnectionEnum, except UncServerName must not be null, and
  101. must not refer to the local computer.)
  102. Return Value:
  103. (Same as NetConnectionEnum.)
  104. --*/
  105. {
  106. LPDESC DataDesc16;
  107. LPDESC DataDesc32;
  108. LPDESC DataDescSmb;
  109. DWORD EntriesToAllocate;
  110. LPVOID InfoArray;
  111. DWORD InfoArraySize;
  112. DWORD MaxEntrySize;
  113. NET_API_STATUS Status;
  114. UNREFERENCED_PARAMETER(ResumeHandle);
  115. // Make sure caller didn't mess up.
  116. NetpAssert(UncServerName != NULL);
  117. if (BufPtr == NULL) {
  118. return (ERROR_INVALID_PARAMETER);
  119. }
  120. // Assume something might go wrong, and make error paths easier to
  121. // code. Also, check for a bad pointer before we do anything.
  122. *BufPtr = NULL;
  123. Status = RxpGetConnectionDataDescs(
  124. Level,
  125. & DataDesc16,
  126. & DataDesc32,
  127. & DataDescSmb,
  128. & MaxEntrySize); // API buffer size 32
  129. if (Status != NERR_Success) {
  130. return (Status);
  131. }
  132. //
  133. // Downlevel servers don't support resume handles, and we don't
  134. // have a way to say "close this resume handle" even if we wanted to
  135. // emulate them here. Therefore we have to do everthing in one shot.
  136. // So, the first time around, we'll try using the caller's prefered
  137. // maximum, but we will enlarge that until we can get everything in one
  138. // buffer.
  139. //
  140. // First time: try caller's prefered maximum.
  141. NetpAdjustPreferedMaximum (
  142. PreferedMaximumSize, // caller's request
  143. MaxEntrySize, // byte count per array element
  144. ENUM_ARRAY_OVERHEAD_SIZE, // num bytes overhead to show array end
  145. NULL, // we'll compute byte counts ourselves.
  146. & EntriesToAllocate); // num of entries we can get.
  147. //
  148. // Loop until we have enough memory or we die for some other reason.
  149. //
  150. do {
  151. //
  152. // Figure out how much memory we need.
  153. //
  154. InfoArraySize = (EntriesToAllocate * MaxEntrySize)
  155. + ENUM_ARRAY_OVERHEAD_SIZE;
  156. if (InfoArraySize > MAX_TRANSACT_RET_DATA_SIZE) {
  157. InfoArraySize = MAX_TRANSACT_RET_DATA_SIZE;
  158. }
  159. //
  160. // Alloc memory for the array.
  161. //
  162. Status = NetApiBufferAllocate( InfoArraySize, & InfoArray );
  163. if (Status != NERR_Success) {
  164. NetpAssert( Status == ERROR_NOT_ENOUGH_MEMORY );
  165. return (Status);
  166. }
  167. NetpAssert( InfoArray != NULL );
  168. //
  169. // Remote the API, and see if we've got enough space in the array.
  170. //
  171. Status = RxRemoteApi(
  172. API_WConnectionEnum, // api number
  173. UncServerName, // \\servername
  174. REMSmb_NetConnectionEnum_P, // parm desc (SMB version)
  175. DataDesc16,
  176. DataDesc32,
  177. DataDescSmb,
  178. NULL, // no aux desc 16
  179. NULL, // no aux desc 32
  180. NULL, // no aux desc SMB
  181. 0, // flags: not a null session API
  182. // rest of API's arguments in 32-bit LM 2.x format:
  183. Qualifier, // Which item to get connections for.
  184. Level, // Level: info level
  185. InfoArray, // Buffer: info lvl array
  186. InfoArraySize, // Buffer: info lvl array len
  187. EntriesRead, // EntriesRead
  188. TotalEntries); // TotalAvail
  189. //
  190. // If the server returned ERROR_MORE_DATA, free the buffer and try
  191. // again. (Actually, if we already tried 64K, then forget it.)
  192. //
  193. NetpAssert( InfoArraySize <= MAX_TRANSACT_RET_DATA_SIZE );
  194. if (Status != ERROR_MORE_DATA) {
  195. break;
  196. } else if (InfoArraySize == MAX_TRANSACT_RET_DATA_SIZE) {
  197. NetpKdPrint(( PREFIX_NETAPI
  198. "RxNetConnectionEnum: "
  199. "**WARNING** protocol limit reached (64KB).\n" ));
  200. break;
  201. }
  202. (void) NetApiBufferFree( InfoArray );
  203. InfoArray = NULL;
  204. NetpAssert( EntriesToAllocate < *TotalEntries );
  205. EntriesToAllocate = *TotalEntries;
  206. } while (Status == ERROR_MORE_DATA);
  207. if ( (Status == NO_ERROR) && ((*EntriesRead) > 0) ) {
  208. *BufPtr = InfoArray;
  209. } else {
  210. (VOID) NetApiBufferFree( InfoArray );
  211. NetpAssert( *BufPtr == NULL );
  212. }
  213. return (Status);
  214. } // RxNetConnectionEnum