[WS2_32]
[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 = Catalog->ProtocolList.Flink;
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 while (NextEntry != &Catalog->ProtocolList)
398 {
399 /* Get the Current Entry */
400 Entry = CONTAINING_RECORD(NextEntry, TCATALOG_ENTRY, CatalogLink);
401 NextEntry = NextEntry->Flink;
402
403 /* Check if this is the Catalog Entry ID we want */
404 if (Entry->ProtocolInfo.iAddressFamily == AddressFamily)
405 {
406 /* Check if it doesn't already have a provider */
407 if (!Entry->Provider)
408 {
409 /* Match, load the Provider */
410 ErrorCode = WsTcLoadProvider(Catalog, Entry);
411
412 /* Make sure this didn't fail */
413 if (ErrorCode != ERROR_SUCCESS) break;
414 }
415
416 /* Reference the entry and return it */
417 InterlockedIncrement(&Entry->RefCount);
418 *CatalogEntry = Entry;
419 ErrorCode = ERROR_SUCCESS;
420 break;
421 }
422 }
423
424 /* Release the catalog */
425 WsTcUnlock();
426
427 /* Return */
428 return ErrorCode;
429 }
430
431 DWORD
432 WSAAPI
433 WsTcGetEntryFromCatalogEntryId(IN PTCATALOG Catalog,
434 IN DWORD CatalogEntryId,
435 IN PTCATALOG_ENTRY *CatalogEntry)
436 {
437 INT ErrorCode = WSAEINVAL;
438 PLIST_ENTRY NextEntry = Catalog->ProtocolList.Flink;
439 PTCATALOG_ENTRY Entry;
440
441 /* Lock the catalog */
442 WsTcLock();
443
444 /* Match the Id with all the entries in the List */
445 while (NextEntry != &Catalog->ProtocolList)
446 {
447 /* Get the Current Entry */
448 Entry = CONTAINING_RECORD(NextEntry, TCATALOG_ENTRY, CatalogLink);
449 NextEntry = NextEntry->Flink;
450
451 /* Check if this is the Catalog Entry ID we want */
452 if (Entry->ProtocolInfo.dwCatalogEntryId == CatalogEntryId)
453 {
454 /* Check if it doesn't already have a provider */
455 if (!Entry->Provider)
456 {
457 /* Match, load the Provider */
458 WsTcLoadProvider(Catalog, Entry);
459 }
460
461 /* Reference the entry and return it */
462 InterlockedIncrement(&Entry->RefCount);
463 *CatalogEntry = Entry;
464 ErrorCode = ERROR_SUCCESS;
465 break;
466 }
467 }
468
469 /* Release the catalog */
470 WsTcUnlock();
471
472 /* Return */
473 return ErrorCode;
474 }
475
476 DWORD
477 WSAAPI
478 WsTcGetEntryFromTriplet(IN PTCATALOG Catalog,
479 IN INT af,
480 IN INT type,
481 IN INT protocol,
482 IN DWORD StartId,
483 IN PTCATALOG_ENTRY *CatalogEntry)
484 {
485 INT ErrorCode = WSAEINVAL;
486 PLIST_ENTRY NextEntry = Catalog->ProtocolList.Flink;
487 PTCATALOG_ENTRY Entry;
488 DPRINT("WsTcGetEntryFromTriplet: %lx, %lx, %lx, %lx\n", af, type, protocol, StartId);
489
490 /* Assume failure */
491 *CatalogEntry = NULL;
492
493 /* Lock the catalog */
494 WsTcLock();
495
496 /* Check if we are starting past 0 */
497 if (StartId)
498 {
499 /* Loop the list */
500 while (NextEntry != &Catalog->ProtocolList)
501 {
502 /* Get the Current Entry */
503 Entry = CONTAINING_RECORD(NextEntry, TCATALOG_ENTRY, CatalogLink);
504 NextEntry = NextEntry->Flink;
505
506 /* Check if this is the ID where we are starting */
507 if (Entry->ProtocolInfo.dwCatalogEntryId == StartId) break;
508 }
509 }
510
511 /* Match the Id with all the entries in the List */
512 while (NextEntry != &Catalog->ProtocolList)
513 {
514 /* Get the Current Entry */
515 Entry = CONTAINING_RECORD(NextEntry, TCATALOG_ENTRY, CatalogLink);
516 NextEntry = NextEntry->Flink;
517
518 /* Check if Address Family Matches or if it's wildcard */
519 if ((Entry->ProtocolInfo.iAddressFamily == af) || (af == AF_UNSPEC))
520 {
521 /* Check if Socket Type Matches or if it's wildcard */
522 if ((Entry->ProtocolInfo.iSocketType == type) || (type == 0))
523 {
524 /* Check if Protocol is In Range or if it's wildcard */
525 if (((Entry->ProtocolInfo.iProtocol <= protocol) &&
526 ((Entry->ProtocolInfo.iProtocol +
527 Entry->ProtocolInfo.iProtocolMaxOffset) >= protocol)) ||
528 (protocol == 0))
529 {
530 /* Check if it doesn't already have a provider */
531 if (!Entry->Provider)
532 {
533 /* Match, load the Provider */
534 ErrorCode = WsTcLoadProvider(Catalog, Entry);
535
536 /* Make sure this didn't fail */
537 if (ErrorCode != ERROR_SUCCESS) break;
538 }
539
540 /* Reference the entry and return it */
541 InterlockedIncrement(&Entry->RefCount);
542 *CatalogEntry = Entry;
543 ErrorCode = ERROR_SUCCESS;
544 break;
545 }
546 else
547 {
548 ErrorCode = WSAEPROTONOSUPPORT;
549 }
550 }
551 else
552 {
553 ErrorCode = WSAESOCKTNOSUPPORT;
554 }
555 }
556 else
557 {
558 ErrorCode = WSAEAFNOSUPPORT;
559 }
560 }
561
562 /* Release the catalog */
563 WsTcUnlock();
564
565 /* Return */
566 return ErrorCode;
567 }
568
569 PTPROVIDER
570 WSAAPI
571 WsTcFindProvider(IN PTCATALOG Catalog,
572 IN LPGUID ProviderId)
573 {
574 PTPROVIDER Provider;
575 PLIST_ENTRY Entry;
576 PTCATALOG_ENTRY CatalogEntry;
577
578 /* Loop the provider list */
579 Entry = Catalog->ProtocolList.Flink;
580 while (Entry != &Catalog->ProtocolList)
581 {
582 /* Get the entry */
583 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
584
585 /* Move to the next one, get the provider */
586 Entry = Entry->Flink;
587 Provider = CatalogEntry->Provider;
588
589 /* Check for a match */
590 if ((Provider) &&
591 !(memcmp(&CatalogEntry->ProtocolInfo.ProviderId,
592 ProviderId,
593 sizeof(GUID))))
594 {
595 /* Found a match */
596 return Provider;
597 }
598 }
599
600 /* No match here */
601 return NULL;
602 }
603
604 DWORD
605 WSAAPI
606 WsTcLoadProvider(IN PTCATALOG Catalog,
607 IN PTCATALOG_ENTRY CatalogEntry)
608 {
609 INT ErrorCode = ERROR_SUCCESS;
610 PTPROVIDER Provider;
611 DPRINT("WsTcLoadProvider: %p, %p\n", Catalog, CatalogEntry);
612
613 /* Lock the catalog */
614 WsTcLock();
615
616 /* Check if we have a provider already */
617 if (!CatalogEntry->Provider)
618 {
619 /* Try to find another instance */
620 Provider = WsTcFindProvider(Catalog,
621 &CatalogEntry->ProtocolInfo.ProviderId);
622
623 /* Check if we found one now */
624 if (Provider)
625 {
626 /* Set this one as the provider */
627 WsTcEntrySetProvider(CatalogEntry, Provider);
628 ErrorCode = ERROR_SUCCESS;
629 }
630 else
631 {
632 /* Nothing found, Allocate a provider */
633 if ((Provider = WsTpAllocate()))
634 {
635 /* Initialize it */
636 ErrorCode = WsTpInitialize(Provider,
637 CatalogEntry->DllPath,
638 &CatalogEntry->ProtocolInfo);
639
640 /* Ensure success */
641 if (ErrorCode == ERROR_SUCCESS)
642 {
643 /* Set the provider */
644 WsTcEntrySetProvider(CatalogEntry, Provider);
645 }
646
647 /* Dereference it */
648 WsTpDereference(Provider);
649 }
650 else
651 {
652 /* No memory */
653 ErrorCode = WSA_NOT_ENOUGH_MEMORY;
654 }
655 }
656 }
657
658 /* Release the lock */
659 WsTcUnlock();
660 return ErrorCode;
661 }
662
663 VOID
664 WSAAPI
665 WsTcUpdateProtocolList(IN PTCATALOG Catalog,
666 IN PLIST_ENTRY List)
667 {
668 LIST_ENTRY TempList;
669 PTCATALOG_ENTRY CatalogEntry, OldCatalogEntry;
670 PLIST_ENTRY Entry;
671
672 /* First move from our list to the old one */
673 InsertHeadList(&Catalog->ProtocolList, &TempList);
674 RemoveEntryList(&Catalog->ProtocolList);
675 InitializeListHead(&Catalog->ProtocolList);
676
677 /* Loop every item on the list */
678 while (!IsListEmpty(List))
679 {
680 /* Get the catalog entry */
681 Entry = RemoveHeadList(List);
682 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
683
684 /* Check if this item is already on our list */
685 Entry = TempList.Flink;
686 while (Entry != &TempList)
687 {
688 /* Get the catalog entry */
689 OldCatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
690 Entry = Entry->Flink;
691
692 /* Check if they match */
693 if (CatalogEntry->ProtocolInfo.dwCatalogEntryId ==
694 OldCatalogEntry->ProtocolInfo.dwCatalogEntryId)
695 {
696 /* We have a match, use the old item instead */
697 WsTcEntryDereference(CatalogEntry);
698 CatalogEntry = OldCatalogEntry;
699 RemoveEntryList(&CatalogEntry->CatalogLink);
700
701 /* Decrease the number of protocols we have */
702 Catalog->ItemCount--;
703 break;
704 }
705 }
706
707 /* Add this item */
708 InsertTailList(&Catalog->ProtocolList, &CatalogEntry->CatalogLink);
709 Catalog->ItemCount++;
710 }
711
712 /* If there's anything left on the temporary list */
713 while (!IsListEmpty(&TempList))
714 {
715 /* Get the entry */
716 Entry = RemoveHeadList(&TempList);
717 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
718
719 /* Remove it */
720 Catalog->ItemCount--;
721 WsTcEntryDereference(CatalogEntry);
722 }
723 }
724
725 VOID
726 WSAAPI
727 WsTcEnumerateCatalogItems(IN PTCATALOG Catalog,
728 IN PTCATALOG_ENUMERATE_PROC Callback,
729 IN PVOID Context)
730 {
731 PLIST_ENTRY Entry;
732 PTCATALOG_ENTRY CatalogEntry;
733 BOOL GoOn = TRUE;
734
735 /* Lock the catalog */
736 WsTcLock();
737
738 /* Loop the entries */
739 Entry = Catalog->ProtocolList.Flink;
740 while (GoOn && (Entry != &Catalog->ProtocolList))
741 {
742 /* Get the entry */
743 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
744
745 /* Move to the next one and call the callback */
746 Entry = Entry->Flink;
747 GoOn = Callback(Context, CatalogEntry);
748 }
749
750 /* Release lock */
751 WsTcUnlock();
752 }
753
754 DWORD
755 WSAAPI
756 WsTcFindIfsProviderForSocket(IN PTCATALOG Catalog,
757 IN SOCKET Handle)
758 {
759 PTPROVIDER Provider;
760 IN SOCKET NewHandle;
761 INT Error;
762 INT OptionLength;
763 PLIST_ENTRY Entry;
764 WSAPROTOCOL_INFOW ProtocolInfo;
765 DWORD UniqueId;
766 INT ErrorCode;
767 PTCATALOG_ENTRY CatalogEntry;
768
769 /* Get the catalog lock */
770 WsTcLock();
771
772 /* Loop as long as the catalog changes */
773 CatalogChanged:
774
775 /* Loop every provider */
776 Entry = Catalog->ProtocolList.Flink;
777 while (Entry != &Catalog->ProtocolList)
778 {
779 /* Get the catalog entry */
780 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
781
782 /* Move to the next entry */
783 Entry = Entry->Flink;
784
785 /* Skip it if it doesn't support IFS */
786 if (!(CatalogEntry->ProtocolInfo.dwServiceFlags1 & XP1_IFS_HANDLES)) continue;
787
788 /* Check if we need to load it */
789 if (!(Provider = CatalogEntry->Provider))
790 {
791 /* Load it */
792 ErrorCode = WsTcLoadProvider(Catalog, CatalogEntry);
793
794 /* Skip it if we failed to load it */
795 if (ErrorCode != ERROR_SUCCESS) continue;
796
797 /* Get the provider again */
798 Provider = CatalogEntry->Provider;
799 }
800
801 /* Reference the entry and get our unique id */
802 InterlockedIncrement(&CatalogEntry->RefCount);
803 UniqueId = Catalog->UniqueId;
804
805 /* Release the lock now */
806 WsTcUnlock();
807
808 /* Get the catalog entry ID */
809 OptionLength = sizeof(ProtocolInfo);
810 ErrorCode = Provider->Service.lpWSPGetSockOpt(Handle,
811 SOL_SOCKET,
812 SO_PROTOCOL_INFOW,
813 (PCHAR)&ProtocolInfo,
814 &OptionLength,
815 &Error);
816
817 /* Dereference the entry and check the result */
818 WsTcEntryDereference(CatalogEntry);
819 if (ErrorCode != ERROR_SUCCESS)
820 {
821 /* Lock and make sure this provider is still valid */
822 WsTcLock();
823 if (UniqueId == Catalog->UniqueId) continue;
824
825 /* It changed! We need to start over */
826 goto CatalogChanged;
827 }
828
829 /* Now get the IFS handle */
830 NewHandle = WPUModifyIFSHandle(ProtocolInfo.dwCatalogEntryId,
831 Handle,
832 &Error);
833
834 /* Check if the socket is invalid */
835 if (NewHandle == INVALID_SOCKET) return WSAENOTSOCK;
836
837 /* We succeeded, get out of here */
838 return ERROR_SUCCESS;
839 }
840
841 /* Unrecognized socket if we get here: note, we still have the lock */
842 WsTcUnlock();
843 return WSAENOTSOCK;
844 }
845
846 VOID
847 WSAAPI
848 WsTcRemoveCatalogItem(IN PTCATALOG Catalog,
849 IN PTCATALOG_ENTRY Entry)
850 {
851 /* Remove the entry from the list */
852 RemoveEntryList(&Entry->CatalogLink);
853
854 /* Decrease our count */
855 Catalog->ItemCount--;
856 }
857
858 VOID
859 WSAAPI
860 WsTcDelete(IN PTCATALOG Catalog)
861 {
862 PLIST_ENTRY Entry;
863 PTCATALOG_ENTRY CatalogEntry;
864
865 /* Check if we're initialized */
866 if (!Catalog->ProtocolList.Flink) return;
867
868 /* Acquire lock */
869 WsTcLock();
870
871 /* Loop every entry */
872 Entry = Catalog->ProtocolList.Flink;
873 while (Entry != &Catalog->ProtocolList)
874 {
875 /* Get this entry */
876 CatalogEntry = CONTAINING_RECORD(Entry, TCATALOG_ENTRY, CatalogLink);
877
878 /* Remove it */
879 WsTcRemoveCatalogItem(Catalog, CatalogEntry);
880
881 /* Dereference it */
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 }