[WS2_32] Dereference socket in case of parameter check failure. Thanks Thomas. ROSTES...
[reactos.git] / reactos / dll / win32 / ws2_32 / src / dcatalog.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
4 * FILE: dll/win32/ws2_32_new/src/dcatalog.c
5 * PURPOSE: Transport Catalog Object
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <ws2_32.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* DATA **********************************************************************/
17
18 #define WsTcLock() EnterCriticalSection(&Catalog->Lock)
19 #define WsTcUnlock() LeaveCriticalSection(&Catalog->Lock)
20
21 /* FUNCTIONS *****************************************************************/
22
23 PTCATALOG
24 WSAAPI
25 WsTcAllocate(VOID)
26 {
27 PTCATALOG Catalog;
28
29 /* Allocate the object */
30 Catalog = HeapAlloc(WsSockHeap, HEAP_ZERO_MEMORY, sizeof(*Catalog));
31
32 /* Return it */
33 return Catalog;
34 }
35
36 BOOL
37 WSAAPI
38 WsTcOpen(IN PTCATALOG Catalog,
39 IN HKEY ParentKey)
40 {
41 INT ErrorCode;
42 DWORD CreateDisposition;
43 HKEY CatalogKey, NewKey;
44 //DWORD CatalogEntries = 0;
45 DWORD RegType = REG_DWORD;
46 DWORD RegSize = sizeof(DWORD);
47 DWORD UniqueId = 0;
48 DWORD NewData = 0;
49 CHAR* CatalogKeyName;
50
51 /* Initialize the catalog lock and namespace list */
52 InitializeCriticalSection(&Catalog->Lock);
53 InitializeListHead(&Catalog->ProtocolList);
54
55 /* Read the catalog name */
56 ErrorCode = RegQueryValueEx(ParentKey,
57 "Current_Protocol_Catalog",
58 0,
59 &RegType,
60 NULL,
61 &RegSize);
62
63 CatalogKeyName = HeapAlloc(WsSockHeap, 0, RegSize);
64
65 /* Read the catalog name */
66 ErrorCode = RegQueryValueEx(ParentKey,
67 "Current_Protocol_Catalog",
68 0,
69 &RegType,
70 (LPBYTE)CatalogKeyName,
71 &RegSize);
72
73 /* Open the Catalog Key */
74 ErrorCode = RegOpenKeyEx(ParentKey,
75 CatalogKeyName,
76 0,
77 MAXIMUM_ALLOWED,
78 &CatalogKey);
79
80 /* If we didn't find the key, create it */
81 if (ErrorCode == ERROR_SUCCESS)
82 {
83 /* Fake that we opened an existing key */
84 CreateDisposition = REG_OPENED_EXISTING_KEY;
85 }
86 else if (ErrorCode == ERROR_FILE_NOT_FOUND)
87 {
88 /* Create the Catalog Name */
89 ErrorCode = RegCreateKeyEx(ParentKey,
90 CatalogKeyName,
91 0,
92 NULL,
93 REG_OPTION_NON_VOLATILE,
94 KEY_ALL_ACCESS,
95 NULL,
96 &CatalogKey,
97 &CreateDisposition);
98 }
99
100 HeapFree(WsSockHeap, 0, CatalogKeyName);
101 RegType = REG_DWORD;
102 RegSize = sizeof(DWORD);
103
104 /* Fail if that didn't work */
105 if (ErrorCode != ERROR_SUCCESS) return FALSE;
106
107 /* Check if we had to create the key */
108 if (CreateDisposition == REG_CREATED_NEW_KEY)
109 {
110 /* Write the count of entries (0 now) */
111 ErrorCode = RegSetValueEx(CatalogKey,
112 "Num_Catalog_Entries",
113 0,
114 REG_DWORD,
115 (LPBYTE)&NewData,
116 sizeof(NewData));
117 if (ErrorCode != ERROR_SUCCESS)
118 {
119 /* Close the key and fail */
120 RegCloseKey(CatalogKey);
121 return FALSE;
122 }
123
124 /* Write the first catalog entry ID */
125 NewData = 1001;
126 ErrorCode = RegSetValueEx(CatalogKey,
127 "Next_Catalog_Entry_ID",
128 0,
129 REG_DWORD,
130 (LPBYTE)&NewData,
131 sizeof(NewData));
132 if (ErrorCode != ERROR_SUCCESS)
133 {
134 /* Close the key and fail */
135 RegCloseKey(CatalogKey);
136 return FALSE;
137 }
138
139 /* Write the first catalog entry Uniqe ID */
140 NewData = 1;
141 ErrorCode = RegSetValueEx(CatalogKey,
142 "Serial_Access_Num",
143 0,
144 REG_DWORD,
145 (LPBYTE)&NewData,
146 sizeof(NewData));
147 if (ErrorCode != ERROR_SUCCESS)
148 {
149 /* Close the key and fail */
150 RegCloseKey(CatalogKey);
151 return FALSE;
152 }
153
154 /* Create a key for this entry */
155 ErrorCode = RegCreateKeyEx(CatalogKey,
156 "Catalog_Entries",
157 0,
158 NULL,
159 REG_OPTION_NON_VOLATILE,
160 KEY_ALL_ACCESS,
161 NULL,
162 &NewKey,
163 &CreateDisposition);
164 if (ErrorCode != ERROR_SUCCESS)
165 {
166 /* Close the key and fail */
167 RegCloseKey(CatalogKey);
168 return FALSE;
169 }
170
171 /* Close the key since we don't need it */
172 RegCloseKey(NewKey);
173 }
174 else
175 {
176 RegSize = sizeof(DWORD);
177 /* Read the serial number */
178 ErrorCode = RegQueryValueEx(CatalogKey,
179 "Serial_Access_Num",
180 0,
181 &RegType,
182 (LPBYTE)&UniqueId,
183 &RegSize);
184
185 /* Check if it's missing for some reason */
186 if (ErrorCode != ERROR_SUCCESS)
187 {
188 /* Write the first catalog entry Unique ID */
189 NewData = 1;
190 ErrorCode = RegSetValueEx(CatalogKey,
191 "Serial_Access_Num",
192 0,
193 REG_DWORD,
194 (LPBYTE)&NewData,
195 sizeof(NewData));
196 }
197 }
198
199 /* Set the Catalog Key */
200 Catalog->CatalogKey = CatalogKey;
201 return TRUE;
202 }
203
204 DWORD
205 WSAAPI
206 WsTcInitializeFromRegistry(IN PTCATALOG Catalog,
207 IN HKEY ParentKey,
208 IN HANDLE CatalogEvent)
209 {
210 INT ErrorCode = WSASYSCALLFAILURE;
211
212 /* Open the catalog */
213 if (WsTcOpen(Catalog, ParentKey))
214 {
215 /* Refresh it */
216 ErrorCode = WsTcRefreshFromRegistry(Catalog, CatalogEvent);
217 }
218
219 /* Return the status */
220 return ErrorCode;
221 }
222
223 DWORD
224 WSAAPI
225 WsTcRefreshFromRegistry(IN PTCATALOG Catalog,
226 IN HANDLE CatalogEvent)
227 {
228 INT ErrorCode;
229 BOOLEAN LocalEvent = FALSE;
230 LIST_ENTRY LocalList;
231 DWORD UniqueId;
232 HKEY EntriesKey;
233 DWORD CatalogEntries;
234 PTCATALOG_ENTRY CatalogEntry;
235 DWORD NextCatalogEntry;
236 BOOL NewChangesMade;
237 PLIST_ENTRY Entry;
238 DWORD RegType = REG_DWORD;
239 DWORD RegSize = sizeof(DWORD);
240 DWORD i;
241
242 /* Check if we got an event */
243 if (!CatalogEvent)
244 {
245 /* Create an event ourselves */
246 CatalogEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
247 if (!CatalogEvent) return WSASYSCALLFAILURE;
248 LocalEvent = TRUE;
249 }
250
251 /* Lock the catalog */
252 WsTcLock();
253
254 /* Initialize our local list for the loop */
255 InitializeListHead(&LocalList);
256
257 /* Start looping */
258 do
259 {
260 /* Setup notifications for the catalog entry */
261 ErrorCode = WsSetupCatalogProtection(Catalog->CatalogKey,
262 CatalogEvent,
263 &UniqueId);
264 if (ErrorCode != ERROR_SUCCESS) break;
265
266 /* Check if we've changed till now */
267 if (UniqueId == Catalog->UniqueId)
268 {
269 /* We haven't, so return */
270 ErrorCode = ERROR_SUCCESS;
271 break;
272 }
273
274 /* Now Open the Entries */
275 ErrorCode = RegOpenKeyEx(Catalog->CatalogKey,
276 "Catalog_Entries",
277 0,
278 MAXIMUM_ALLOWED,
279 &EntriesKey);
280 if (ErrorCode != ERROR_SUCCESS)
281 {
282 /* Critical failure */
283 ErrorCode = WSASYSCALLFAILURE;
284 break;
285 }
286
287 /* Get the next entry */
288 ErrorCode = RegQueryValueEx(Catalog->CatalogKey,
289 "Next_Catalog_Entry_ID",
290 0,
291 &RegType,
292 (LPBYTE)&NextCatalogEntry,
293 &RegSize);
294 if (ErrorCode != ERROR_SUCCESS)
295 {
296 /* Critical failure */
297 ErrorCode = WSASYSCALLFAILURE;
298 break;
299 }
300
301 /* Find out how many there are */
302 ErrorCode = RegQueryValueEx(Catalog->CatalogKey,
303 "Num_Catalog_Entries",
304 0,
305 &RegType,
306 (LPBYTE)&CatalogEntries,
307 &RegSize);
308 if (ErrorCode != ERROR_SUCCESS)
309 {
310 /* Critical failure */
311 ErrorCode = WSASYSCALLFAILURE;
312 break;
313 }
314
315 /* Initialize them all */
316 for (i = 1; i <= CatalogEntries; i++)
317 {
318 /* Allocate a Catalog Entry Structure */
319 CatalogEntry = WsTcEntryAllocate();
320 if (!CatalogEntry)
321 {
322 /* Not enough memory, fail */
323 ErrorCode = WSA_NOT_ENOUGH_MEMORY;
324 break;
325 }
326
327 /* Initialize it from the Registry Key */
328 ErrorCode = WsTcEntryInitializeFromRegistry(CatalogEntry,
329 EntriesKey,
330 i);
331 if (ErrorCode != ERROR_SUCCESS)
332 {
333 /* We failed to get it, dereference the entry and leave */
334 WsTcEntryDereference(CatalogEntry);
335 break;
336 }
337
338 /* Insert it to our List */
339 InsertTailList(&LocalList, &CatalogEntry->CatalogLink);
340 }
341
342 /* Close the catalog key */
343 RegCloseKey(EntriesKey);
344
345 /* Check if we changed during our read and if we have success */
346 NewChangesMade = WsCheckCatalogState(CatalogEvent);
347 if (!NewChangesMade && ErrorCode == ERROR_SUCCESS)
348 {
349 /* All is good, update the protocol list */
350 WsTcUpdateProtocolList(Catalog, &LocalList);
351
352 /* Update and return */
353 Catalog->UniqueId = UniqueId;
354 Catalog->NextId = NextCatalogEntry;
355 break;
356 }
357
358 /* We failed and/or catalog data changed, free what we did till now */
359 while (!IsListEmpty(&LocalList))
360 {
361 /* Get the LP Catalog Item */
362 Entry = RemoveHeadList(&LocalList);
363 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
364
365 /* Dereference it */
366 WsTcEntryDereference(CatalogEntry);
367 }
368 } while (NewChangesMade);
369
370 /* Release the lock */
371 WsTcUnlock();
372
373 /* Close the event, if any was created by us */
374 if (LocalEvent) CloseHandle(CatalogEvent);
375
376 /* All Done */
377 return ErrorCode;
378 }
379
380 DWORD
381 WSAAPI
382 WsTcGetEntryFromAf(IN PTCATALOG Catalog,
383 IN INT AddressFamily,
384 IN PTCATALOG_ENTRY *CatalogEntry)
385 {
386 INT ErrorCode = WSAEINVAL;
387 PLIST_ENTRY NextEntry;
388 PTCATALOG_ENTRY Entry;
389
390 /* Assume failure */
391 *CatalogEntry = NULL;
392
393 /* Lock the catalog */
394 WsTcLock();
395
396 /* Match the Id with all the entries in the List */
397 NextEntry = Catalog->ProtocolList.Flink;
398 while (NextEntry != &Catalog->ProtocolList)
399 {
400 /* Get the Current Entry */
401 Entry = CONTAINING_RECORD(NextEntry, TCATALOG_ENTRY, CatalogLink);
402 NextEntry = NextEntry->Flink;
403
404 /* Check if this is the Catalog Entry ID we want */
405 if (Entry->ProtocolInfo.iAddressFamily == AddressFamily)
406 {
407 /* Check if it doesn't already have a provider */
408 if (!Entry->Provider)
409 {
410 /* Match, load the Provider */
411 ErrorCode = WsTcLoadProvider(Catalog, Entry);
412
413 /* Make sure this didn't fail */
414 if (ErrorCode != ERROR_SUCCESS) break;
415 }
416
417 /* Reference the entry and return it */
418 InterlockedIncrement(&Entry->RefCount);
419 *CatalogEntry = Entry;
420 ErrorCode = ERROR_SUCCESS;
421 break;
422 }
423 }
424
425 /* Release the catalog */
426 WsTcUnlock();
427
428 /* Return */
429 return ErrorCode;
430 }
431
432 DWORD
433 WSAAPI
434 WsTcGetEntryFromCatalogEntryId(IN PTCATALOG Catalog,
435 IN DWORD CatalogEntryId,
436 IN PTCATALOG_ENTRY *CatalogEntry)
437 {
438 INT ErrorCode = WSAEINVAL;
439 PLIST_ENTRY NextEntry;
440 PTCATALOG_ENTRY Entry;
441
442 /* Lock the catalog */
443 WsTcLock();
444
445 /* Match the Id with all the entries in the List */
446 NextEntry = Catalog->ProtocolList.Flink;
447 while (NextEntry != &Catalog->ProtocolList)
448 {
449 /* Get the Current Entry */
450 Entry = CONTAINING_RECORD(NextEntry, TCATALOG_ENTRY, CatalogLink);
451 NextEntry = NextEntry->Flink;
452
453 /* Check if this is the Catalog Entry ID we want */
454 if (Entry->ProtocolInfo.dwCatalogEntryId == CatalogEntryId)
455 {
456 /* Check if it doesn't already have a provider */
457 if (!Entry->Provider)
458 {
459 /* Match, load the Provider */
460 WsTcLoadProvider(Catalog, Entry);
461 }
462
463 /* Reference the entry and return it */
464 InterlockedIncrement(&Entry->RefCount);
465 *CatalogEntry = Entry;
466 ErrorCode = ERROR_SUCCESS;
467 break;
468 }
469 }
470
471 /* Release the catalog */
472 WsTcUnlock();
473
474 /* Return */
475 return ErrorCode;
476 }
477
478 DWORD
479 WSAAPI
480 WsTcGetEntryFromTriplet(IN PTCATALOG Catalog,
481 IN INT af,
482 IN INT type,
483 IN INT protocol,
484 IN DWORD StartId,
485 IN PTCATALOG_ENTRY *CatalogEntry)
486 {
487 INT ErrorCode = WSAEINVAL;
488 PLIST_ENTRY NextEntry;
489 PTCATALOG_ENTRY Entry;
490 DPRINT("WsTcGetEntryFromTriplet: %lx, %lx, %lx, %lx\n", af, type, protocol, StartId);
491
492 /* Assume failure */
493 *CatalogEntry = NULL;
494
495 /* Lock the catalog */
496 WsTcLock();
497
498 NextEntry = Catalog->ProtocolList.Flink;
499
500 /* Check if we are starting past 0 */
501 if (StartId)
502 {
503 /* Loop the list */
504 while (NextEntry != &Catalog->ProtocolList)
505 {
506 /* Get the Current Entry */
507 Entry = CONTAINING_RECORD(NextEntry, TCATALOG_ENTRY, CatalogLink);
508 NextEntry = NextEntry->Flink;
509
510 /* Check if this is the ID where we are starting */
511 if (Entry->ProtocolInfo.dwCatalogEntryId == StartId) break;
512 }
513 }
514
515 /* Match the Id with all the entries in the List */
516 while (NextEntry != &Catalog->ProtocolList)
517 {
518 /* Get the Current Entry */
519 Entry = CONTAINING_RECORD(NextEntry, TCATALOG_ENTRY, CatalogLink);
520 NextEntry = NextEntry->Flink;
521
522 /* Check if Address Family Matches or if it's wildcard */
523 if ((Entry->ProtocolInfo.iAddressFamily == af) || (af == AF_UNSPEC))
524 {
525 /* Check if Socket Type Matches or if it's wildcard */
526 if ((Entry->ProtocolInfo.iSocketType == type) || (type == 0))
527 {
528 /* Check if Protocol is In Range or if it's wildcard */
529 if (((Entry->ProtocolInfo.iProtocol <= protocol) &&
530 ((Entry->ProtocolInfo.iProtocol +
531 Entry->ProtocolInfo.iProtocolMaxOffset) >= protocol)) ||
532 (protocol == 0))
533 {
534 /* Check if it doesn't already have a provider */
535 if (!Entry->Provider)
536 {
537 /* Match, load the Provider */
538 ErrorCode = WsTcLoadProvider(Catalog, Entry);
539
540 /* Make sure this didn't fail */
541 if (ErrorCode != ERROR_SUCCESS) break;
542 }
543
544 /* Reference the entry and return it */
545 InterlockedIncrement(&Entry->RefCount);
546 *CatalogEntry = Entry;
547 ErrorCode = ERROR_SUCCESS;
548 break;
549 }
550 else
551 {
552 ErrorCode = WSAEPROTONOSUPPORT;
553 }
554 }
555 else
556 {
557 ErrorCode = WSAESOCKTNOSUPPORT;
558 }
559 }
560 else
561 {
562 ErrorCode = WSAEAFNOSUPPORT;
563 }
564 }
565
566 /* Release the catalog */
567 WsTcUnlock();
568
569 /* Return */
570 return ErrorCode;
571 }
572
573 PTPROVIDER
574 WSAAPI
575 WsTcFindProvider(IN PTCATALOG Catalog,
576 IN LPGUID ProviderId)
577 {
578 PTPROVIDER Provider;
579 PLIST_ENTRY Entry;
580 PTCATALOG_ENTRY CatalogEntry;
581
582 /* Loop the provider list */
583 Entry = Catalog->ProtocolList.Flink;
584 while (Entry != &Catalog->ProtocolList)
585 {
586 /* Get the entry */
587 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
588
589 /* Move to the next one, get the provider */
590 Entry = Entry->Flink;
591 Provider = CatalogEntry->Provider;
592
593 /* Check for a match */
594 if (Provider &&
595 IsEqualGUID(&CatalogEntry->ProtocolInfo.ProviderId, ProviderId))
596 {
597 /* Found a match */
598 return Provider;
599 }
600 }
601
602 /* No match here */
603 return NULL;
604 }
605
606 DWORD
607 WSAAPI
608 WsTcLoadProvider(IN PTCATALOG Catalog,
609 IN PTCATALOG_ENTRY CatalogEntry)
610 {
611 INT ErrorCode = ERROR_SUCCESS;
612 PTPROVIDER Provider;
613 DPRINT("WsTcLoadProvider: %p, %p\n", Catalog, CatalogEntry);
614
615 /* Lock the catalog */
616 WsTcLock();
617
618 /* Check if we have a provider already */
619 if (!CatalogEntry->Provider)
620 {
621 /* Try to find another instance */
622 Provider = WsTcFindProvider(Catalog,
623 &CatalogEntry->ProtocolInfo.ProviderId);
624
625 /* Check if we found one now */
626 if (Provider)
627 {
628 /* Set this one as the provider */
629 WsTcEntrySetProvider(CatalogEntry, Provider);
630 ErrorCode = ERROR_SUCCESS;
631 }
632 else
633 {
634 /* Nothing found, Allocate a provider */
635 if ((Provider = WsTpAllocate()))
636 {
637 /* Initialize it */
638 ErrorCode = WsTpInitialize(Provider,
639 CatalogEntry->DllPath,
640 &CatalogEntry->ProtocolInfo);
641
642 /* Ensure success */
643 if (ErrorCode == ERROR_SUCCESS)
644 {
645 /* Set the provider */
646 WsTcEntrySetProvider(CatalogEntry, Provider);
647 }
648
649 /* Dereference it */
650 WsTpDereference(Provider);
651 }
652 else
653 {
654 /* No memory */
655 ErrorCode = WSA_NOT_ENOUGH_MEMORY;
656 }
657 }
658 }
659
660 /* Release the lock */
661 WsTcUnlock();
662 return ErrorCode;
663 }
664
665 VOID
666 WSAAPI
667 WsTcUpdateProtocolList(IN PTCATALOG Catalog,
668 IN PLIST_ENTRY List)
669 {
670 LIST_ENTRY TempList;
671 PTCATALOG_ENTRY CatalogEntry, OldCatalogEntry;
672 PLIST_ENTRY Entry;
673
674 /* First move from our list to the old one */
675 InsertHeadList(&Catalog->ProtocolList, &TempList);
676 RemoveEntryList(&Catalog->ProtocolList);
677 InitializeListHead(&Catalog->ProtocolList);
678
679 /* Loop every item in the list */
680 while (!IsListEmpty(List))
681 {
682 /* Get the catalog entry */
683 Entry = RemoveHeadList(List);
684 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
685
686 /* Check if this item is already in our list */
687 Entry = TempList.Flink;
688 while (Entry != &TempList)
689 {
690 /* Get the catalog entry */
691 OldCatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
692 Entry = Entry->Flink;
693
694 /* Check if they match */
695 if (CatalogEntry->ProtocolInfo.dwCatalogEntryId ==
696 OldCatalogEntry->ProtocolInfo.dwCatalogEntryId)
697 {
698 /* We have a match, use the old item instead */
699 WsTcEntryDereference(CatalogEntry);
700 CatalogEntry = OldCatalogEntry;
701 RemoveEntryList(&CatalogEntry->CatalogLink);
702
703 /* Decrease the number of protocols we have */
704 Catalog->ItemCount--;
705 break;
706 }
707 }
708
709 /* Add this item */
710 InsertTailList(&Catalog->ProtocolList, &CatalogEntry->CatalogLink);
711 Catalog->ItemCount++;
712 }
713
714 /* If there's anything left in the temporary list */
715 while (!IsListEmpty(&TempList))
716 {
717 /* Get the entry */
718 Entry = RemoveHeadList(&TempList);
719 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
720
721 /* Remove it */
722 Catalog->ItemCount--;
723 WsTcEntryDereference(CatalogEntry);
724 }
725 }
726
727 VOID
728 WSAAPI
729 WsTcEnumerateCatalogItems(IN PTCATALOG Catalog,
730 IN PTCATALOG_ENUMERATE_PROC Callback,
731 IN PVOID Context)
732 {
733 PLIST_ENTRY Entry;
734 PTCATALOG_ENTRY CatalogEntry;
735 BOOL GoOn = TRUE;
736
737 /* Lock the catalog */
738 WsTcLock();
739
740 /* Loop the entries */
741 Entry = Catalog->ProtocolList.Flink;
742 while (GoOn && (Entry != &Catalog->ProtocolList))
743 {
744 /* Get the entry */
745 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
746
747 /* Move to the next one and call the callback */
748 Entry = Entry->Flink;
749 GoOn = Callback(Context, CatalogEntry);
750 }
751
752 /* Release lock */
753 WsTcUnlock();
754 }
755
756 DWORD
757 WSAAPI
758 WsTcFindIfsProviderForSocket(IN PTCATALOG Catalog,
759 IN SOCKET Handle)
760 {
761 PTPROVIDER Provider;
762 IN SOCKET NewHandle;
763 INT Error;
764 INT OptionLength;
765 PLIST_ENTRY Entry;
766 WSAPROTOCOL_INFOW ProtocolInfo;
767 DWORD UniqueId;
768 INT ErrorCode;
769 PTCATALOG_ENTRY CatalogEntry;
770
771 /* Get the catalog lock */
772 WsTcLock();
773
774 /* Loop as long as the catalog changes */
775 CatalogChanged:
776
777 /* Loop every provider */
778 Entry = Catalog->ProtocolList.Flink;
779 while (Entry != &Catalog->ProtocolList)
780 {
781 /* Get the catalog entry */
782 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
783
784 /* Move to the next entry */
785 Entry = Entry->Flink;
786
787 /* Skip it if it doesn't support IFS */
788 if (!(CatalogEntry->ProtocolInfo.dwServiceFlags1 & XP1_IFS_HANDLES)) continue;
789
790 /* Check if we need to load it */
791 if (!(Provider = CatalogEntry->Provider))
792 {
793 /* Load it */
794 ErrorCode = WsTcLoadProvider(Catalog, CatalogEntry);
795
796 /* Skip it if we failed to load it */
797 if (ErrorCode != ERROR_SUCCESS) continue;
798
799 /* Get the provider again */
800 Provider = CatalogEntry->Provider;
801 }
802
803 /* Reference the entry and get our unique id */
804 InterlockedIncrement(&CatalogEntry->RefCount);
805 UniqueId = Catalog->UniqueId;
806
807 /* Release the lock now */
808 WsTcUnlock();
809
810 /* Get the catalog entry ID */
811 OptionLength = sizeof(ProtocolInfo);
812 ErrorCode = Provider->Service.lpWSPGetSockOpt(Handle,
813 SOL_SOCKET,
814 SO_PROTOCOL_INFOW,
815 (PCHAR)&ProtocolInfo,
816 &OptionLength,
817 &Error);
818
819 /* Dereference the entry and check the result */
820 WsTcEntryDereference(CatalogEntry);
821 if (ErrorCode != ERROR_SUCCESS)
822 {
823 /* Lock and make sure this provider is still valid */
824 WsTcLock();
825 if (UniqueId == Catalog->UniqueId) continue;
826
827 /* It changed! We need to start over */
828 goto CatalogChanged;
829 }
830
831 /* Now get the IFS handle */
832 NewHandle = WPUModifyIFSHandle(ProtocolInfo.dwCatalogEntryId,
833 Handle,
834 &Error);
835
836 /* Check if the socket is invalid */
837 if (NewHandle == INVALID_SOCKET) return WSAENOTSOCK;
838
839 /* We succeeded, get out of here */
840 return ERROR_SUCCESS;
841 }
842
843 /* Unrecognized socket if we get here: note, we still have the lock */
844 WsTcUnlock();
845 return WSAENOTSOCK;
846 }
847
848 VOID
849 WSAAPI
850 WsTcRemoveCatalogItem(IN PTCATALOG Catalog,
851 IN PTCATALOG_ENTRY Entry)
852 {
853 /* Remove the entry from the list */
854 RemoveEntryList(&Entry->CatalogLink);
855
856 /* Decrease our count */
857 Catalog->ItemCount--;
858 }
859
860 VOID
861 WSAAPI
862 WsTcDelete(IN PTCATALOG Catalog)
863 {
864 PLIST_ENTRY Entry;
865 PTCATALOG_ENTRY CatalogEntry;
866
867 /* Check if we're initialized */
868 if (!Catalog->ProtocolList.Flink) return;
869
870 /* Acquire lock */
871 WsTcLock();
872
873 /* Loop every entry */
874 Entry = Catalog->ProtocolList.Flink;
875 while (Entry != &Catalog->ProtocolList)
876 {
877 /* Get this entry */
878 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
879
880 /* Remove it and dereference it */
881 WsTcRemoveCatalogItem(Catalog, CatalogEntry);
882 WsTcEntryDereference(CatalogEntry);
883
884 /* Move to the next entry */
885 Entry = Catalog->ProtocolList.Flink;
886 }
887
888 /* Check if the catalog key is opened */
889 if (Catalog->CatalogKey)
890 {
891 /* Close it */
892 RegCloseKey(Catalog->CatalogKey);
893 Catalog->CatalogKey = NULL;
894 }
895
896 /* Release and delete the lock */
897 WsTcUnlock();
898 DeleteCriticalSection(&Catalog->Lock);
899
900 /* Delete the object */
901 HeapFree(WsSockHeap, 0, Catalog);
902 }