Leaked source code of windows server 2003
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.

983 lines
29 KiB

  1. #include <nt.h> // DbgPrint prototype
  2. #include <ntrtl.h> // DbgPrint
  3. #include <nturtl.h> // Needed by winbase.h
  4. #include <windef.h> // DWORD
  5. #include <winbase.h> // LocalFree
  6. #include <rpcutil.h> // GENERIC_ENUM_STRUCT
  7. #include <lmcons.h> // NET_API_STATUS
  8. #include <lmerr.h> // NetError codes
  9. #include <lmremutl.h> // SUPPORTS_RPC
  10. #include <bowser.h> // generated by the MIDL complier
  11. #include <brnames.h> // Service and interface names
  12. #include <netlib.h>
  13. #include <netdebug.h>
  14. #include <winsvc.h>
  15. #include <lmserver.h>
  16. #include <tstr.h>
  17. #include <ntddbrow.h>
  18. #include <brcommon.h> // Routines common between client & server
  19. VOID
  20. UpdateInterimServerListElement(
  21. IN PINTERIM_SERVER_LIST ServerList,
  22. IN PINTERIM_ELEMENT Element,
  23. IN ULONG Level,
  24. IN BOOLEAN NewElement
  25. );
  26. PINTERIM_ELEMENT
  27. AllocateInterimServerListEntry(
  28. IN PSERVER_INFO_101 ServerInfo,
  29. IN ULONG Level
  30. );
  31. NET_API_STATUS
  32. MergeServerList(
  33. IN OUT PINTERIM_SERVER_LIST InterimServerList,
  34. IN ULONG Level,
  35. IN PVOID NewServerList,
  36. IN ULONG NewEntriesRead,
  37. IN ULONG NewTotalEntries
  38. )
  39. /*++
  40. Routine Description:
  41. This function will merge two server lists. It will reallocate the buffer
  42. for the old list if appropriate.
  43. Arguments:
  44. IN OUT PINTERIM_SERVER_LIST InterimServerList - Supplies an interim server list to merge into.
  45. IN ULONG Level - Supplies the level of the list (100 or 101). Special
  46. level 1010 is really level 101 with the special semantic that no
  47. fields from this the NewServerList override existing fields in the
  48. InterimServerList.
  49. IN ULONG NewServerList - Supplies the list to merge into the interim list
  50. IN ULONG NewEntriesRead - Supplies the entries read in the list.
  51. IN ULONG NewTotalEntries - Supplies the total entries available in the list.
  52. Return Value:
  53. NET_API_STATUS - NERR_Success or reason for failure.
  54. --*/
  55. {
  56. ULONG i;
  57. ULONG ServerElementSize;
  58. PSERVER_INFO_101 ServerInfo = NewServerList;
  59. PINTERIM_ELEMENT InterimEntry = NULL;
  60. PLIST_ENTRY InterimList;
  61. PINTERIM_ELEMENT NewElement = NULL;
  62. if (Level == 100) {
  63. ServerElementSize = sizeof(SERVER_INFO_100);
  64. } else if ( Level == 101 || Level == 1010 ) {
  65. ServerElementSize = sizeof(SERVER_INFO_101);
  66. } else {
  67. return(ERROR_INVALID_LEVEL);
  68. }
  69. //
  70. // Early out if no entries in list.
  71. //
  72. if (NewEntriesRead == 0) {
  73. return NERR_Success;
  74. }
  75. PrepareServerListForMerge(NewServerList, Level, NewEntriesRead);
  76. InterimList = InterimServerList->ServerList.Flink;
  77. //
  78. // Walk the existing structure, packing it into an interim element, and
  79. // sticking the element into the interim table.
  80. //
  81. for (i = 0; i < NewEntriesRead; i ++) {
  82. BOOLEAN EntryInserted = FALSE;
  83. //
  84. // Walk forward in the interim element and find the appropriate place
  85. // to insert this element.
  86. //
  87. while (InterimList != &InterimServerList->ServerList) {
  88. LONG CompareResult;
  89. InterimEntry = CONTAINING_RECORD(InterimList, INTERIM_ELEMENT, NextElement);
  90. // KdPrint(("MergeServerList: Compare %ws and %ws\n", NewElement->Name, InterimEntry->Name));
  91. #if DBG
  92. //
  93. // Make sure that this entry is lexically less than the next
  94. // entry.
  95. //
  96. {
  97. PLIST_ENTRY NextList = InterimList->Flink;
  98. PINTERIM_ELEMENT NextEntry = CONTAINING_RECORD(NextList, INTERIM_ELEMENT, NextElement);
  99. if (NextList != &InterimServerList->ServerList) {
  100. ASSERT (wcscmp(InterimEntry->Name, NextEntry->Name) < 0);
  101. }
  102. //
  103. // Now make sure that the input buffer also doesn't contain
  104. // duplicate entries.
  105. //
  106. if (i < (NewEntriesRead-1)) {
  107. PSERVER_INFO_101 NextServerInfo = (PSERVER_INFO_101)((PCHAR)ServerInfo+ServerElementSize);
  108. ASSERT (wcscmp(ServerInfo->sv101_name, NextServerInfo->sv101_name) <= 0);
  109. }
  110. }
  111. #endif
  112. CompareResult = wcscmp(ServerInfo->sv101_name, InterimEntry->Name);
  113. if (CompareResult == 0) {
  114. // KdPrint(("MergeServerList: Elements equal - update\n"));
  115. //
  116. // If the new information should override the old information,
  117. // copy it on top of the new info.
  118. //
  119. if ( Level != 1010 ) {
  120. InterimEntry->PlatformId = ServerInfo->sv101_platform_id;
  121. if (Level >= 101) {
  122. InterimEntry->MajorVersionNumber = ServerInfo->sv101_version_major;
  123. InterimEntry->MinorVersionNumber = ServerInfo->sv101_version_minor;
  124. InterimEntry->Type = ServerInfo->sv101_type;
  125. InterimServerList->TotalBytesNeeded -= wcslen(InterimEntry->Comment) * sizeof(WCHAR) + sizeof(WCHAR);
  126. wcscpy(InterimEntry->Comment, ServerInfo->sv101_comment);
  127. InterimServerList->TotalBytesNeeded += wcslen(InterimEntry->Comment) * sizeof(WCHAR) + sizeof(WCHAR);
  128. }
  129. }
  130. UpdateInterimServerListElement(InterimServerList, InterimEntry, Level, FALSE);
  131. EntryInserted = TRUE;
  132. break;
  133. } else if (CompareResult > 0) {
  134. // KdPrint(("MergeServerList: Elements greater. Skip element\n"));
  135. InterimList = InterimList->Flink;
  136. } else {
  137. NewElement = AllocateInterimServerListEntry(ServerInfo, Level);
  138. if (NewElement == NULL) {
  139. return ERROR_NOT_ENOUGH_MEMORY;
  140. }
  141. // KdPrint(("MergeServerList: Elements less. Insert at end\n"));
  142. //
  143. // The new entry is < than the previous entry. Insert it
  144. // before this entry.
  145. //
  146. InsertTailList(&InterimEntry->NextElement, &NewElement->NextElement);
  147. //
  148. // Skip to the next element in the list.
  149. //
  150. InterimList = &NewElement->NextElement;
  151. UpdateInterimServerListElement(InterimServerList, NewElement, Level, TRUE);
  152. EntryInserted = TRUE;
  153. break;
  154. }
  155. }
  156. if (!EntryInserted &&
  157. (InterimList == &InterimServerList->ServerList)) {
  158. NewElement = AllocateInterimServerListEntry(ServerInfo, Level);
  159. if (NewElement == NULL) {
  160. return ERROR_NOT_ENOUGH_MEMORY;
  161. }
  162. // KdPrint(("MergeServerList: Insert %ws at end of list\n", NewElement->Name));
  163. InsertTailList(&InterimServerList->ServerList, &NewElement->NextElement);
  164. InterimList = &NewElement->NextElement;
  165. UpdateInterimServerListElement(InterimServerList, NewElement, Level, TRUE);
  166. }
  167. ServerInfo = (PSERVER_INFO_101)((PCHAR)ServerInfo+ServerElementSize);
  168. }
  169. #if 0
  170. {
  171. PLIST_ENTRY InterimList;
  172. ULONG TotalNeededForList = 0;
  173. for (InterimList = InterimServerList->ServerList.Flink ;
  174. InterimList != &InterimServerList->ServerList ;
  175. InterimList = InterimList->Flink ) {
  176. ULONG ServerElementSize;
  177. InterimEntry = CONTAINING_RECORD(InterimList, INTERIM_ELEMENT, NextElement);
  178. if (Level == 100) {
  179. ServerElementSize = sizeof(SERVER_INFO_100);
  180. } else {
  181. ServerElementSize = sizeof(SERVER_INFO_101);
  182. }
  183. ServerElementSize += wcslen(InterimEntry->Name)*sizeof(WCHAR)+sizeof(WCHAR);
  184. ServerElementSize += wcslen(InterimEntry->Comment)*sizeof(WCHAR)+sizeof(WCHAR);
  185. // KdPrint(("MergeInterimServerList: %ws. %ld needed\n", InterimEntry->Name, ServerElementSize));
  186. TotalNeededForList += ServerElementSize;
  187. }
  188. if (TotalNeededForList != InterimServerList->TotalBytesNeeded) {
  189. KdPrint(("UpdateInterimServerList: Wrong number of bytes (%ld) for interim server list. %ld needed\n", InterimServerList->TotalBytesNeeded, TotalNeededForList));
  190. }
  191. }
  192. #endif
  193. // KdPrint(("%lx bytes needed to hold server list\n", InterimServerList->TotalBytesNeeded));
  194. //
  195. // Also, we had better have the whole table locally.
  196. //
  197. ASSERT (InterimServerList->EntriesRead == InterimServerList->TotalEntries);
  198. return(NERR_Success);
  199. }
  200. ULONG
  201. __cdecl
  202. CompareServerInfo(
  203. const void * Param1,
  204. const void * Param2
  205. )
  206. {
  207. const SERVER_INFO_100 * ServerInfo1 = Param1;
  208. const SERVER_INFO_100 * ServerInfo2 = Param2;
  209. return wcscmp(ServerInfo1->sv100_name, ServerInfo2->sv100_name);
  210. }
  211. VOID
  212. PrepareServerListForMerge(
  213. IN PVOID ServerInfoList,
  214. IN ULONG Level,
  215. IN ULONG EntriesInList
  216. )
  217. /*++
  218. Routine Description:
  219. MergeServerList requires that the inputs to the list be in a strictly
  220. sorted order. This routine guarantees that this list will be of
  221. an "appropriate" form to be merged.
  222. Arguments:
  223. IN PVOID ServerInfoList - Supplies the list to munge.
  224. IN ULONG Level - Supplies the level of the list (100 or 101).
  225. (Or level 1010 which is identical to level 101.)
  226. IN ULONG EntriesInList - Supplies the number of entries in the list.
  227. Return Value:
  228. None.
  229. Note:
  230. In 99% of the cases, the list passed in will already be sorted. We want to
  231. take the input list and first check to see if it is sorted. If it is,
  232. we can return immediately. If it is not, we need to sort the list.
  233. We don't just unilaterally sort the list, because the input is mostly
  234. sorted anyway, and there are no good sorting algorithms that handle mostly
  235. sorted inputs. Since we will see unsorted input only rarely (basically,
  236. we will only see it from WfW machines), we just take the penalty of a worst
  237. case quicksort if the input is unsorted.
  238. --*/
  239. {
  240. LONG i;
  241. ULONG ServerElementSize;
  242. PSERVER_INFO_101 ServerInfo = ServerInfoList;
  243. BOOLEAN MisOrderedList = FALSE;
  244. ASSERT (Level == 100 || Level == 101 || Level == 1010);
  245. //
  246. // Figure out the size of each element.
  247. //
  248. if (Level == 100) {
  249. ServerElementSize = sizeof(SERVER_INFO_100);
  250. } else {
  251. ServerElementSize = sizeof(SERVER_INFO_101);
  252. }
  253. //
  254. // Next check to see if the input list is sorted.
  255. //
  256. for (i = 0 ; i < ((LONG)EntriesInList - 1) ; i += 1 ) {
  257. PSERVER_INFO_101 NextServerInfo = (PSERVER_INFO_101)((PCHAR)ServerInfo+ServerElementSize);
  258. if (wcscmp(ServerInfo->sv101_name, NextServerInfo->sv101_name) >= 0) {
  259. MisOrderedList = TRUE;
  260. break;
  261. }
  262. ServerInfo = NextServerInfo;
  263. }
  264. //
  265. // This list is sorted. Return right away, it's fine.
  266. //
  267. if (!MisOrderedList) {
  268. return;
  269. }
  270. //
  271. // This list isn't sorted. We need to sort it.
  272. //
  273. qsort(ServerInfoList, EntriesInList, ServerElementSize, CompareServerInfo);
  274. }
  275. PINTERIM_ELEMENT
  276. AllocateInterimServerListEntry(
  277. IN PSERVER_INFO_101 ServerInfo,
  278. IN ULONG Level
  279. )
  280. {
  281. PINTERIM_ELEMENT NewElement;
  282. NewElement = MIDL_user_allocate(sizeof(INTERIM_ELEMENT));
  283. if (NewElement == NULL) {
  284. return NULL;
  285. }
  286. //
  287. // Initialize TimeLastSeen and Periodicity.
  288. //
  289. NewElement->TimeLastSeen = 0;
  290. NewElement->Periodicity = 0;
  291. NewElement->PlatformId = ServerInfo->sv101_platform_id;
  292. ASSERT (wcslen(ServerInfo->sv101_name) <= CNLEN);
  293. wcscpy(NewElement->Name, ServerInfo->sv101_name);
  294. if (Level == 100) {
  295. NewElement->MajorVersionNumber = 0;
  296. NewElement->MinorVersionNumber = 0;
  297. *NewElement->Comment = L'\0';
  298. NewElement->Type = SV_TYPE_ALL;
  299. } else {
  300. NewElement->MajorVersionNumber = ServerInfo->sv101_version_major;
  301. NewElement->MinorVersionNumber = ServerInfo->sv101_version_minor;
  302. NewElement->Type = ServerInfo->sv101_type;
  303. ASSERT (wcslen(ServerInfo->sv101_comment) <= LM20_MAXCOMMENTSZ);
  304. wcsncpy(NewElement->Comment, ServerInfo->sv101_comment, LM20_MAXCOMMENTSZ);
  305. if (wcslen(ServerInfo->sv101_comment) < LM20_MAXCOMMENTSZ) {
  306. NewElement->Comment[wcslen(ServerInfo->sv101_comment)] = L'\0';
  307. } else {
  308. NewElement->Comment[LM20_MAXCOMMENTSZ] = L'\0';
  309. }
  310. }
  311. return NewElement;
  312. }
  313. VOID
  314. UpdateInterimServerListElement(
  315. IN PINTERIM_SERVER_LIST InterimServerList,
  316. IN PINTERIM_ELEMENT InterimElement,
  317. IN ULONG Level,
  318. IN BOOLEAN NewElement
  319. )
  320. {
  321. #if 0
  322. PINTERIM_ELEMENT InterimEntry;
  323. ULONG TotalNeededForList = 0;
  324. #endif
  325. //
  326. // If this is a new element, update the size of the table to match.
  327. //
  328. if (NewElement) {
  329. ULONG ServerElementSize;
  330. if (Level == 100) {
  331. ServerElementSize = sizeof(SERVER_INFO_100);
  332. } else {
  333. ServerElementSize = sizeof(SERVER_INFO_101);
  334. }
  335. InterimServerList->EntriesRead += 1;
  336. ServerElementSize += wcslen(InterimElement->Name)*sizeof(WCHAR)+sizeof(WCHAR);
  337. if (Level == 100) {
  338. ServerElementSize += sizeof(WCHAR);
  339. } else {
  340. ServerElementSize += wcslen(InterimElement->Comment)*sizeof(WCHAR)+sizeof(WCHAR);
  341. }
  342. InterimServerList->TotalBytesNeeded += ServerElementSize;
  343. InterimServerList->TotalEntries += 1;
  344. if (InterimServerList->NewElementCallback != NULL) {
  345. InterimServerList->NewElementCallback(InterimServerList,
  346. InterimElement);
  347. } else {
  348. InterimElement->Periodicity = 0xffffffff;
  349. InterimElement->TimeLastSeen = 0xffffffff;
  350. }
  351. } else {
  352. if (InterimServerList->ExistingElementCallback != NULL) {
  353. InterimServerList->ExistingElementCallback(InterimServerList,
  354. InterimElement);
  355. } else {
  356. InterimElement->Periodicity = 0xffffffff;
  357. InterimElement->TimeLastSeen = 0xffffffff;
  358. }
  359. }
  360. #if 0
  361. {
  362. PLIST_ENTRY InterimList;
  363. ULONG TotalNeededForList = 0;
  364. for (InterimList = InterimServerList->ServerList.Flink ;
  365. InterimList != &InterimServerList->ServerList ;
  366. InterimList = InterimList->Flink ) {
  367. ULONG ServerElementSize;
  368. InterimEntry = CONTAINING_RECORD(InterimList, INTERIM_ELEMENT, NextElement);
  369. if (Level == 100) {
  370. ServerElementSize = sizeof(SERVER_INFO_100);
  371. } else {
  372. ServerElementSize = sizeof(SERVER_INFO_101);
  373. }
  374. ServerElementSize += wcslen(InterimEntry->Name)*sizeof(WCHAR)+sizeof(WCHAR);
  375. ServerElementSize += wcslen(InterimEntry->Comment)*sizeof(WCHAR)+sizeof(WCHAR);
  376. TotalNeededForList += ServerElementSize;
  377. }
  378. if (TotalNeededForList != InterimServerList->TotalBytesNeeded) {
  379. KdPrint(("UpdateInterimServerList: Wrong number of bytes (%ld) for interim server list. %ld needed\n", InterimServerList->TotalBytesNeeded, TotalNeededForList));
  380. }
  381. }
  382. #endif
  383. return;
  384. }
  385. NET_API_STATUS
  386. PackServerList(
  387. IN PINTERIM_SERVER_LIST InterimServerList,
  388. IN ULONG Level,
  389. IN ULONG ServerType,
  390. IN ULONG PreferedMaximumLength,
  391. OUT PVOID *ServerList,
  392. OUT PULONG EntriesRead,
  393. OUT PULONG TotalEntries,
  394. IN LPCWSTR FirstNameToReturn
  395. )
  396. /*++
  397. Routine Description:
  398. This function will take an interim server list and "pack" it into an array
  399. of server_info_xxx structures.
  400. Arguments:
  401. IN PINTERIM_SERVER_LIST InterimServerList - Supplies an interim server list to merge into.
  402. IN ULONG Level - Supplies the level of the list (100 or 101).
  403. IN ULONG ServerType - Supplies the type to filter on the list.
  404. IN ULONG PreferedMaximumLength - Supplies the prefered size of the list.
  405. OUT PVOID *ServerList - Where to put the destination list.
  406. OUT PULONG EntriesEntries - Receives the entries packed in the list.
  407. OUT PULONG TotalEntries - Receives the total entries available in the list.
  408. FirstNameToReturn - Supplies the name of the first domain or server entry to return.
  409. The caller can use this parameter to implement a resume handle of sorts by passing
  410. the name of the last entry returned on a previous call. (Notice that the specified
  411. entry will, also, be returned on this call unless it has since been deleted.)
  412. Pass NULL to start with the first entry available.
  413. Passed name must be the canonical form of the name.
  414. Return Value:
  415. NET_API_STATUS - NERR_Success or reason for failure.
  416. --*/
  417. {
  418. ULONG EntriesPacked = 0;
  419. PLIST_ENTRY InterimList;
  420. PSERVER_INFO_101 ServerEntry;
  421. ULONG EntrySize = 0;
  422. LPWSTR BufferEnd;
  423. BOOLEAN ReturnWholeList = FALSE;
  424. BOOLEAN TrimmingNames;
  425. BOOLEAN BufferFull = FALSE;
  426. if (Level == 100) {
  427. EntrySize = sizeof(SERVER_INFO_100);
  428. } else if (Level == 101) {
  429. EntrySize = sizeof(SERVER_INFO_101);
  430. } else {
  431. return(ERROR_INVALID_LEVEL);
  432. }
  433. //
  434. // Set the entries read based on the information we collected before.
  435. //
  436. *TotalEntries = 0;
  437. if (PreferedMaximumLength == 0xffffffff) {
  438. *ServerList = MIDL_user_allocate(InterimServerList->TotalBytesNeeded);
  439. BufferEnd = (LPWSTR)((ULONG_PTR)(*ServerList)+InterimServerList->TotalBytesNeeded);
  440. } else {
  441. *ServerList = MIDL_user_allocate(PreferedMaximumLength);
  442. BufferEnd = (LPWSTR)((ULONG_PTR)(*ServerList)+PreferedMaximumLength);
  443. }
  444. if (ServerType == SV_TYPE_ALL || ServerType == SV_TYPE_DOMAIN_ENUM) {
  445. ReturnWholeList = TRUE;
  446. }
  447. if ( *ServerList == NULL ) {
  448. return(ERROR_NOT_ENOUGH_MEMORY);
  449. }
  450. TrimmingNames = (FirstNameToReturn != NULL && *FirstNameToReturn != L'\0');
  451. ServerEntry = *ServerList;
  452. for (InterimList = InterimServerList->ServerList.Flink ;
  453. InterimList != &InterimServerList->ServerList ;
  454. InterimList = InterimList->Flink ) {
  455. PINTERIM_ELEMENT InterimEntry = CONTAINING_RECORD(InterimList, INTERIM_ELEMENT, NextElement);
  456. #if DBG
  457. //
  458. // Make sure that this entry is lexically less than the next
  459. // entry.
  460. //
  461. {
  462. PLIST_ENTRY NextList = InterimList->Flink;
  463. PINTERIM_ELEMENT NextEntry = CONTAINING_RECORD(NextList, INTERIM_ELEMENT, NextElement);
  464. if (NextList != &InterimServerList->ServerList) {
  465. ASSERT (wcscmp(InterimEntry->Name, NextEntry->Name) < 0);
  466. }
  467. }
  468. #endif
  469. //
  470. // Trim the first several names from the list.
  471. //
  472. if ( TrimmingNames ) {
  473. if ( wcscmp( InterimEntry->Name, FirstNameToReturn ) < 0 ) {
  474. continue;
  475. }
  476. TrimmingNames = FALSE;
  477. }
  478. //
  479. // If the server's type matches the type filter, pack it into the buffer.
  480. //
  481. if (InterimEntry->Type & ServerType) {
  482. (*TotalEntries) += 1;
  483. //
  484. // If this entry will fit into the buffer, pack it in.
  485. //
  486. // Please note that we only count an entry if the entire entry
  487. // (server and comment) fits in the buffer. This is NOT
  488. // strictly Lan Manager compatible.
  489. //
  490. if ( !BufferFull &&
  491. ((ULONG_PTR)ServerEntry+EntrySize <= (ULONG_PTR)BufferEnd)) {
  492. ServerEntry->sv101_platform_id = InterimEntry->PlatformId;
  493. ServerEntry->sv101_name = InterimEntry->Name;
  494. if (NetpPackString(&ServerEntry->sv101_name,
  495. (LPBYTE)((PCHAR)ServerEntry)+EntrySize,
  496. &BufferEnd)) {
  497. if (Level == 101) {
  498. ServerEntry->sv101_version_major = InterimEntry->MajorVersionNumber;;
  499. ServerEntry->sv101_version_minor = InterimEntry->MinorVersionNumber;;
  500. ServerEntry->sv101_type = InterimEntry->Type;
  501. ServerEntry->sv101_comment = InterimEntry->Comment;
  502. if (NetpPackString(&ServerEntry->sv101_comment,
  503. (LPBYTE)((PCHAR)ServerEntry)+EntrySize,
  504. &BufferEnd)) {
  505. EntriesPacked += 1;
  506. } else {
  507. BufferFull = TRUE;
  508. }
  509. } else {
  510. EntriesPacked += 1;
  511. }
  512. #if DBG
  513. {
  514. PSERVER_INFO_101 PreviousServerInfo = (PSERVER_INFO_101)((PCHAR)ServerEntry-EntrySize);
  515. if (PreviousServerInfo >= (PSERVER_INFO_101)*ServerList) {
  516. ASSERT (wcscmp(ServerEntry->sv101_name, PreviousServerInfo->sv101_name) > 0);
  517. }
  518. }
  519. #endif
  520. } else {
  521. BufferFull = TRUE;
  522. }
  523. } else {
  524. //
  525. // If we're returning the entire list and we have exceeded
  526. // the amount that fits in the list, we can early out
  527. // now.
  528. //
  529. if (ReturnWholeList) {
  530. *TotalEntries = InterimServerList->TotalEntries;
  531. break;
  532. }
  533. BufferFull = TRUE;
  534. }
  535. //
  536. // Step to the next server entry.
  537. //
  538. ServerEntry = (PSERVER_INFO_101)((PCHAR)ServerEntry+EntrySize);
  539. }
  540. }
  541. ASSERT (InterimServerList->EntriesRead >= EntriesPacked);
  542. *EntriesRead = EntriesPacked;
  543. if (EntriesPacked != *TotalEntries) {
  544. return ERROR_MORE_DATA;
  545. } else {
  546. return NERR_Success;
  547. }
  548. }
  549. NET_API_STATUS
  550. InitializeInterimServerList(
  551. IN PINTERIM_SERVER_LIST InterimServerList,
  552. IN PINTERIM_NEW_CALLBACK NewCallback,
  553. IN PINTERIM_EXISTING_CALLBACK ExistingCallback,
  554. IN PINTERIM_DELETE_CALLBACK DeleteElementCallback,
  555. IN PINTERIM_AGE_CALLBACK AgeElementCallback
  556. )
  557. {
  558. InitializeListHead(&InterimServerList->ServerList);
  559. InterimServerList->TotalBytesNeeded = 0;
  560. InterimServerList->TotalEntries = 0;
  561. InterimServerList->EntriesRead = 0;
  562. InterimServerList->NewElementCallback = NewCallback;
  563. InterimServerList->ExistingElementCallback = ExistingCallback;
  564. InterimServerList->DeleteElementCallback = DeleteElementCallback;
  565. InterimServerList->AgeElementCallback = AgeElementCallback;
  566. return(NERR_Success);
  567. }
  568. NET_API_STATUS
  569. UninitializeInterimServerList(
  570. IN PINTERIM_SERVER_LIST InterimServerList
  571. )
  572. {
  573. PINTERIM_ELEMENT InterimElement;
  574. // KdPrint(("BROWSER: Uninitialize Interim Server List %lx\n", InterimServerList));
  575. //
  576. // Enumerate the elements in the table, deleting them as we go.
  577. //
  578. while (!IsListEmpty(&InterimServerList->ServerList)) {
  579. PLIST_ENTRY Entry;
  580. Entry = RemoveHeadList(&InterimServerList->ServerList);
  581. InterimElement = CONTAINING_RECORD(Entry, INTERIM_ELEMENT, NextElement);
  582. if (InterimServerList->DeleteElementCallback != NULL) {
  583. InterimServerList->DeleteElementCallback(InterimServerList, InterimElement);
  584. }
  585. //
  586. // There is one less element in the list.
  587. //
  588. InterimServerList->EntriesRead -= 1;
  589. InterimServerList->TotalEntries -= 1;
  590. MIDL_user_free(InterimElement);
  591. }
  592. ASSERT (InterimServerList->EntriesRead == 0);
  593. return(NERR_Success);
  594. }
  595. ULONG
  596. NumberInterimServerListElements(
  597. IN PINTERIM_SERVER_LIST InterimServerList
  598. )
  599. {
  600. PLIST_ENTRY InterimList;
  601. ULONG NumberOfEntries = 0;
  602. for (InterimList = InterimServerList->ServerList.Flink ;
  603. InterimList != &InterimServerList->ServerList ;
  604. InterimList = InterimList->Flink ) {
  605. NumberOfEntries += 1;
  606. }
  607. return NumberOfEntries;
  608. }
  609. NET_API_STATUS
  610. AgeInterimServerList(
  611. IN PINTERIM_SERVER_LIST InterimServerList
  612. )
  613. {
  614. PLIST_ENTRY InterimList, NextElement;
  615. PINTERIM_ELEMENT InterimElement;
  616. if (InterimServerList->AgeElementCallback != NULL) {
  617. //
  618. // Enumerate the elements in the table, aging them as we go.
  619. //
  620. for (InterimList = InterimServerList->ServerList.Flink ;
  621. InterimList != &InterimServerList->ServerList ;
  622. InterimList = NextElement) {
  623. InterimElement = CONTAINING_RECORD(InterimList, INTERIM_ELEMENT, NextElement);
  624. //
  625. // Call into the aging routine and if this entry is too old,
  626. // remove it from the interim list.
  627. //
  628. if (InterimServerList->AgeElementCallback(InterimServerList, InterimElement)) {
  629. ULONG ElementSize = sizeof(SERVER_INFO_101) + ((wcslen(InterimElement->Comment) + 1) * sizeof(WCHAR)) + ((wcslen(InterimElement->Name) + 1) * sizeof(WCHAR));
  630. ASSERT (ElementSize <= InterimServerList->TotalBytesNeeded);
  631. NextElement = InterimList->Flink;
  632. //
  633. // Remove this entry from the list.
  634. //
  635. RemoveEntryList(&InterimElement->NextElement);
  636. if (InterimServerList->DeleteElementCallback != NULL) {
  637. InterimServerList->DeleteElementCallback(InterimServerList, InterimElement);
  638. }
  639. //
  640. // There is one less element in the list.
  641. //
  642. InterimServerList->EntriesRead -= 1;
  643. InterimServerList->TotalEntries -= 1;
  644. //
  645. // Since this element isn't in the table any more, we don't
  646. // need to allocate memory for it.
  647. //
  648. InterimServerList->TotalBytesNeeded -= ElementSize;
  649. MIDL_user_free(InterimElement);
  650. } else {
  651. NextElement = InterimList->Flink;
  652. }
  653. }
  654. #if 0
  655. {
  656. PINTERIM_ELEMENT InterimEntry;
  657. ULONG TotalNeededForList = 0;
  658. for (InterimList = InterimServerList->ServerList.Flink ;
  659. InterimList != &InterimServerList->ServerList ;
  660. InterimList = InterimList->Flink ) {
  661. ULONG ServerElementSize;
  662. InterimEntry = CONTAINING_RECORD(InterimList, INTERIM_ELEMENT, NextElement);
  663. ServerElementSize = sizeof(SERVER_INFO_101);
  664. ServerElementSize += wcslen(InterimEntry->Name)*sizeof(WCHAR)+sizeof(WCHAR);
  665. ServerElementSize += wcslen(InterimEntry->Comment)*sizeof(WCHAR)+sizeof(WCHAR);
  666. TotalNeededForList += ServerElementSize;
  667. }
  668. if (TotalNeededForList != InterimServerList->TotalBytesNeeded) {
  669. KdPrint(("AgeInterimServerList: Too few bytes (%ld) for interim server list. %ld needed\n", InterimServerList->TotalBytesNeeded, TotalNeededForList));
  670. }
  671. }
  672. #endif
  673. }
  674. return(NERR_Success);
  675. }
  676. PINTERIM_ELEMENT
  677. LookupInterimServerList(
  678. IN PINTERIM_SERVER_LIST InterimServerList,
  679. IN LPWSTR ServerNameToLookUp
  680. )
  681. {
  682. PLIST_ENTRY InterimList;
  683. for (InterimList = InterimServerList->ServerList.Flink ;
  684. InterimList != &InterimServerList->ServerList ;
  685. InterimList = InterimList->Flink ) {
  686. PINTERIM_ELEMENT InterimEntry = CONTAINING_RECORD(InterimList, INTERIM_ELEMENT, NextElement);
  687. LONG CompareResult;
  688. if ((CompareResult = _wcsicmp(InterimEntry->Name, ServerNameToLookUp) == 0)) {
  689. return InterimEntry;
  690. }
  691. //
  692. // If we went past this guy, return an error.
  693. //
  694. if (CompareResult > 0) {
  695. return NULL;
  696. }
  697. }
  698. return NULL;
  699. }