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)
9 /* INCLUDES ******************************************************************/
16 /* DATA **********************************************************************/
18 #define WsTcLock() EnterCriticalSection((LPCRITICAL_SECTION)&Catalog->Lock);
19 #define WsTcUnlock() LeaveCriticalSection((LPCRITICAL_SECTION)&Catalog->Lock);
21 /* FUNCTIONS *****************************************************************/
29 /* Allocate the object */
30 Catalog
= HeapAlloc(WsSockHeap
, HEAP_ZERO_MEMORY
, sizeof(*Catalog
));
38 WsTcOpen(IN PTCATALOG Catalog
,
42 DWORD CreateDisposition
;
43 HKEY CatalogKey
, NewKey
;
44 //DWORD CatalogEntries = 0;
45 DWORD RegType
= REG_DWORD
;
46 DWORD RegSize
= sizeof(DWORD
);
51 /* Initialize the catalog lock and namespace list */
52 InitializeCriticalSection((LPCRITICAL_SECTION
)&Catalog
->Lock
);
53 InitializeListHead(&Catalog
->ProtocolList
);
55 /* Read the catalog name */
56 ErrorCode
= RegQueryValueEx(ParentKey
,
57 "Current_Protocol_Catalog",
63 CatalogKeyName
= HeapAlloc(WsSockHeap
, 0, RegSize
);
65 /* Read the catalog name */
66 ErrorCode
= RegQueryValueEx(ParentKey
,
67 "Current_Protocol_Catalog",
70 (LPBYTE
)CatalogKeyName
,
73 /* Open the Catalog Key */
74 ErrorCode
= RegOpenKeyEx(ParentKey
,
80 /* If we didn't find the key, create it */
81 if (ErrorCode
== ERROR_SUCCESS
)
83 /* Fake that we opened an existing key */
84 CreateDisposition
= REG_OPENED_EXISTING_KEY
;
86 else if (ErrorCode
== ERROR_FILE_NOT_FOUND
)
88 /* Create the Catalog Name */
89 ErrorCode
= RegCreateKeyEx(ParentKey
,
93 REG_OPTION_NON_VOLATILE
,
100 HeapFree(WsSockHeap
, 0, CatalogKeyName
);
102 RegSize
= sizeof(DWORD
);
104 /* Fail if that didn't work */
105 if (ErrorCode
!= ERROR_SUCCESS
) return FALSE
;
107 /* Check if we had to create the key */
108 if (CreateDisposition
== REG_CREATED_NEW_KEY
)
110 /* Write the count of entries (0 now) */
111 ErrorCode
= RegSetValueEx(CatalogKey
,
112 "Num_Catalog_Entries",
117 if (ErrorCode
!= ERROR_SUCCESS
)
119 /* Close the key and fail */
120 RegCloseKey(CatalogKey
);
124 /* Write the first catalog entry ID */
126 ErrorCode
= RegSetValueEx(CatalogKey
,
127 "Next_Catalog_Entry_ID",
132 if (ErrorCode
!= ERROR_SUCCESS
)
134 /* Close the key and fail */
135 RegCloseKey(CatalogKey
);
139 /* Write the first catalog entry Uniqe ID */
141 ErrorCode
= RegSetValueEx(CatalogKey
,
147 if (ErrorCode
!= ERROR_SUCCESS
)
149 /* Close the key and fail */
150 RegCloseKey(CatalogKey
);
154 /* Create a key for this entry */
155 ErrorCode
= RegCreateKeyEx(CatalogKey
,
159 REG_OPTION_NON_VOLATILE
,
164 if (ErrorCode
!= ERROR_SUCCESS
)
166 /* Close the key and fail */
167 RegCloseKey(CatalogKey
);
171 /* Close the key since we don't need it */
176 RegSize
= sizeof(DWORD
);
177 /* Read the serial number */
178 ErrorCode
= RegQueryValueEx(CatalogKey
,
185 /* Check if it's missing for some reason */
186 if (ErrorCode
!= ERROR_SUCCESS
)
188 /* Write the first catalog entry Unique ID */
190 ErrorCode
= RegSetValueEx(CatalogKey
,
199 /* Set the Catalog Key */
200 Catalog
->CatalogKey
= CatalogKey
;
206 WsTcInitializeFromRegistry(IN PTCATALOG Catalog
,
208 IN HANDLE CatalogEvent
)
210 INT ErrorCode
= WSASYSCALLFAILURE
;
212 /* Open the catalog */
213 if (WsTcOpen(Catalog
, ParentKey
))
216 ErrorCode
= WsTcRefreshFromRegistry(Catalog
, CatalogEvent
);
219 /* Return the status */
225 WsTcRefreshFromRegistry(IN PTCATALOG Catalog
,
226 IN HANDLE CatalogEvent
)
229 BOOLEAN LocalEvent
= FALSE
;
230 LIST_ENTRY LocalList
;
233 DWORD CatalogEntries
;
234 PTCATALOG_ENTRY CatalogEntry
;
235 DWORD NextCatalogEntry
;
238 DWORD RegType
= REG_DWORD
;
239 DWORD RegSize
= sizeof(DWORD
);
242 /* Check if we got an event */
245 /* Create an event ourselves */
246 CatalogEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
247 if (!CatalogEvent
) return WSASYSCALLFAILURE
;
251 /* Lock the catalog */
254 /* Initialize our local list for the loop */
255 InitializeListHead(&LocalList
);
260 /* Setup notifications for the catalog entry */
261 ErrorCode
= WsSetupCatalogProtection(Catalog
->CatalogKey
,
264 if (ErrorCode
!= ERROR_SUCCESS
) break;
266 /* Check if we've changed till now */
267 if (UniqueId
== Catalog
->UniqueId
)
269 /* We haven't, so return */
270 ErrorCode
= ERROR_SUCCESS
;
274 /* Now Open the Entries */
275 ErrorCode
= RegOpenKeyEx(Catalog
->CatalogKey
,
280 if (ErrorCode
!= ERROR_SUCCESS
)
282 /* Critical failure */
283 ErrorCode
= WSASYSCALLFAILURE
;
287 /* Get the next entry */
288 ErrorCode
= RegQueryValueEx(Catalog
->CatalogKey
,
289 "Next_Catalog_Entry_ID",
292 (LPBYTE
)&NextCatalogEntry
,
294 if (ErrorCode
!= ERROR_SUCCESS
)
296 /* Critical failure */
297 ErrorCode
= WSASYSCALLFAILURE
;
301 /* Find out how many there are */
302 ErrorCode
= RegQueryValueEx(Catalog
->CatalogKey
,
303 "Num_Catalog_Entries",
306 (LPBYTE
)&CatalogEntries
,
308 if (ErrorCode
!= ERROR_SUCCESS
)
310 /* Critical failure */
311 ErrorCode
= WSASYSCALLFAILURE
;
315 /* Initialize them all */
316 for (i
= 1; i
<= CatalogEntries
; i
++)
318 /* Allocate a Catalog Entry Structure */
319 CatalogEntry
= WsTcEntryAllocate();
322 /* Not enough memory, fail */
323 ErrorCode
= WSA_NOT_ENOUGH_MEMORY
;
327 /* Initialize it from the Registry Key */
328 ErrorCode
= WsTcEntryInitializeFromRegistry(CatalogEntry
,
331 if (ErrorCode
!= ERROR_SUCCESS
)
333 /* We failed to get it, dereference the entry and leave */
334 WsTcEntryDereference(CatalogEntry
);
338 /* Insert it to our List */
339 InsertTailList(&LocalList
, &CatalogEntry
->CatalogLink
);
342 /* Close the catalog key */
343 RegCloseKey(EntriesKey
);
345 /* Check if we changed during our read and if we have success */
346 NewChangesMade
= WsCheckCatalogState(CatalogEvent
);
347 if (!NewChangesMade
&& ErrorCode
== ERROR_SUCCESS
)
349 /* All is good, update the protocol list */
350 WsTcUpdateProtocolList(Catalog
, &LocalList
);
352 /* Update and return */
353 Catalog
->UniqueId
= UniqueId
;
354 Catalog
->NextId
= NextCatalogEntry
;
358 /* We failed and/or catalog data changed, free what we did till now */
359 while (!IsListEmpty(&LocalList
))
361 /* Get the LP Catalog Item */
362 Entry
= RemoveHeadList(&LocalList
);
363 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
366 WsTcEntryDereference(CatalogEntry
);
368 } while (NewChangesMade
);
370 /* Release the lock */
373 /* Close the event, if any was created by us */
374 if (LocalEvent
) CloseHandle(CatalogEvent
);
382 WsTcGetEntryFromAf(IN PTCATALOG Catalog
,
383 IN INT AddressFamily
,
384 IN PTCATALOG_ENTRY
*CatalogEntry
)
386 INT ErrorCode
= WSAEINVAL
;
387 PLIST_ENTRY NextEntry
= Catalog
->ProtocolList
.Flink
;
388 PTCATALOG_ENTRY Entry
;
391 *CatalogEntry
= NULL
;
393 /* Lock the catalog */
396 /* Match the Id with all the entries in the List */
397 while (NextEntry
!= &Catalog
->ProtocolList
)
399 /* Get the Current Entry */
400 Entry
= CONTAINING_RECORD(NextEntry
, TCATALOG_ENTRY
, CatalogLink
);
401 NextEntry
= NextEntry
->Flink
;
403 /* Check if this is the Catalog Entry ID we want */
404 if (Entry
->ProtocolInfo
.iAddressFamily
== AddressFamily
)
406 /* Check if it doesn't already have a provider */
407 if (!Entry
->Provider
)
409 /* Match, load the Provider */
410 ErrorCode
= WsTcLoadProvider(Catalog
, Entry
);
412 /* Make sure this didn't fail */
413 if (ErrorCode
!= ERROR_SUCCESS
) break;
416 /* Reference the entry and return it */
417 InterlockedIncrement(&Entry
->RefCount
);
418 *CatalogEntry
= Entry
;
419 ErrorCode
= ERROR_SUCCESS
;
424 /* Release the catalog */
433 WsTcGetEntryFromCatalogEntryId(IN PTCATALOG Catalog
,
434 IN DWORD CatalogEntryId
,
435 IN PTCATALOG_ENTRY
*CatalogEntry
)
437 INT ErrorCode
= WSAEINVAL
;
438 PLIST_ENTRY NextEntry
= Catalog
->ProtocolList
.Flink
;
439 PTCATALOG_ENTRY Entry
;
441 /* Lock the catalog */
444 /* Match the Id with all the entries in the List */
445 while (NextEntry
!= &Catalog
->ProtocolList
)
447 /* Get the Current Entry */
448 Entry
= CONTAINING_RECORD(NextEntry
, TCATALOG_ENTRY
, CatalogLink
);
449 NextEntry
= NextEntry
->Flink
;
451 /* Check if this is the Catalog Entry ID we want */
452 if (Entry
->ProtocolInfo
.dwCatalogEntryId
== CatalogEntryId
)
454 /* Check if it doesn't already have a provider */
455 if (!Entry
->Provider
)
457 /* Match, load the Provider */
458 WsTcLoadProvider(Catalog
, Entry
);
461 /* Reference the entry and return it */
462 InterlockedIncrement(&Entry
->RefCount
);
463 *CatalogEntry
= Entry
;
464 ErrorCode
= ERROR_SUCCESS
;
469 /* Release the catalog */
478 WsTcGetEntryFromTriplet(IN PTCATALOG Catalog
,
483 IN PTCATALOG_ENTRY
*CatalogEntry
)
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
);
491 *CatalogEntry
= NULL
;
493 /* Lock the catalog */
496 /* Check if we are starting past 0 */
500 while (NextEntry
!= &Catalog
->ProtocolList
)
502 /* Get the Current Entry */
503 Entry
= CONTAINING_RECORD(NextEntry
, TCATALOG_ENTRY
, CatalogLink
);
504 NextEntry
= NextEntry
->Flink
;
506 /* Check if this is the ID where we are starting */
507 if (Entry
->ProtocolInfo
.dwCatalogEntryId
== StartId
) break;
511 /* Match the Id with all the entries in the List */
512 while (NextEntry
!= &Catalog
->ProtocolList
)
514 /* Get the Current Entry */
515 Entry
= CONTAINING_RECORD(NextEntry
, TCATALOG_ENTRY
, CatalogLink
);
516 NextEntry
= NextEntry
->Flink
;
518 /* Check if Address Family Matches or if it's wildcard */
519 if ((Entry
->ProtocolInfo
.iAddressFamily
== af
) || (af
== AF_UNSPEC
))
521 /* Check if Socket Type Matches or if it's wildcard */
522 if ((Entry
->ProtocolInfo
.iSocketType
== type
) || (type
== 0))
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
)) ||
530 /* Check if it doesn't already have a provider */
531 if (!Entry
->Provider
)
533 /* Match, load the Provider */
534 ErrorCode
= WsTcLoadProvider(Catalog
, Entry
);
536 /* Make sure this didn't fail */
537 if (ErrorCode
!= ERROR_SUCCESS
) break;
540 /* Reference the entry and return it */
541 InterlockedIncrement(&Entry
->RefCount
);
542 *CatalogEntry
= Entry
;
543 ErrorCode
= ERROR_SUCCESS
;
548 ErrorCode
= WSAEPROTONOSUPPORT
;
553 ErrorCode
= WSAESOCKTNOSUPPORT
;
558 ErrorCode
= WSAEAFNOSUPPORT
;
562 /* Release the catalog */
571 WsTcFindProvider(IN PTCATALOG Catalog
,
572 IN LPGUID ProviderId
)
576 PTCATALOG_ENTRY CatalogEntry
;
578 /* Loop the provider list */
579 Entry
= Catalog
->ProtocolList
.Flink
;
580 while (Entry
!= &Catalog
->ProtocolList
)
583 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
585 /* Move to the next one, get the provider */
586 Entry
= Entry
->Flink
;
587 Provider
= CatalogEntry
->Provider
;
589 /* Check for a match */
591 !(memcmp(&CatalogEntry
->ProtocolInfo
.ProviderId
,
606 WsTcLoadProvider(IN PTCATALOG Catalog
,
607 IN PTCATALOG_ENTRY CatalogEntry
)
609 INT ErrorCode
= ERROR_SUCCESS
;
611 DPRINT("WsTcLoadProvider: %p, %p\n", Catalog
, CatalogEntry
);
613 /* Lock the catalog */
616 /* Check if we have a provider already */
617 if (!CatalogEntry
->Provider
)
619 /* Try to find another instance */
620 Provider
= WsTcFindProvider(Catalog
,
621 &CatalogEntry
->ProtocolInfo
.ProviderId
);
623 /* Check if we found one now */
626 /* Set this one as the provider */
627 WsTcEntrySetProvider(CatalogEntry
, Provider
);
628 ErrorCode
= ERROR_SUCCESS
;
632 /* Nothing found, Allocate a provider */
633 if ((Provider
= WsTpAllocate()))
636 ErrorCode
= WsTpInitialize(Provider
,
637 CatalogEntry
->DllPath
,
638 &CatalogEntry
->ProtocolInfo
);
641 if (ErrorCode
== ERROR_SUCCESS
)
643 /* Set the provider */
644 WsTcEntrySetProvider(CatalogEntry
, Provider
);
648 WsTpDereference(Provider
);
653 ErrorCode
= WSA_NOT_ENOUGH_MEMORY
;
658 /* Release the lock */
665 WsTcUpdateProtocolList(IN PTCATALOG Catalog
,
669 PTCATALOG_ENTRY CatalogEntry
, OldCatalogEntry
;
672 /* First move from our list to the old one */
673 InsertHeadList(&Catalog
->ProtocolList
, &TempList
);
674 RemoveEntryList(&Catalog
->ProtocolList
);
675 InitializeListHead(&Catalog
->ProtocolList
);
677 /* Loop every item on the list */
678 while (!IsListEmpty(List
))
680 /* Get the catalog entry */
681 Entry
= RemoveHeadList(List
);
682 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
684 /* Check if this item is already on our list */
685 Entry
= TempList
.Flink
;
686 while (Entry
!= &TempList
)
688 /* Get the catalog entry */
689 OldCatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
690 Entry
= Entry
->Flink
;
692 /* Check if they match */
693 if (CatalogEntry
->ProtocolInfo
.dwCatalogEntryId
==
694 OldCatalogEntry
->ProtocolInfo
.dwCatalogEntryId
)
696 /* We have a match, use the old item instead */
697 WsTcEntryDereference(CatalogEntry
);
698 CatalogEntry
= OldCatalogEntry
;
699 RemoveEntryList(&CatalogEntry
->CatalogLink
);
701 /* Decrease the number of protocols we have */
702 Catalog
->ItemCount
--;
708 InsertTailList(&Catalog
->ProtocolList
, &CatalogEntry
->CatalogLink
);
709 Catalog
->ItemCount
++;
712 /* If there's anything left on the temporary list */
713 while (!IsListEmpty(&TempList
))
716 Entry
= RemoveHeadList(&TempList
);
717 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
720 Catalog
->ItemCount
--;
721 WsTcEntryDereference(CatalogEntry
);
727 WsTcEnumerateCatalogItems(IN PTCATALOG Catalog
,
728 IN PTCATALOG_ENUMERATE_PROC Callback
,
732 PTCATALOG_ENTRY CatalogEntry
;
735 /* Lock the catalog */
738 /* Loop the entries */
739 Entry
= Catalog
->ProtocolList
.Flink
;
740 while (GoOn
&& (Entry
!= &Catalog
->ProtocolList
))
743 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
745 /* Move to the next one and call the callback */
746 Entry
= Entry
->Flink
;
747 GoOn
= Callback(Context
, CatalogEntry
);
756 WsTcFindIfsProviderForSocket(IN PTCATALOG Catalog
,
764 WSAPROTOCOL_INFOW ProtocolInfo
;
767 PTCATALOG_ENTRY CatalogEntry
;
769 /* Get the catalog lock */
772 /* Loop as long as the catalog changes */
775 /* Loop every provider */
776 Entry
= Catalog
->ProtocolList
.Flink
;
777 while (Entry
!= &Catalog
->ProtocolList
)
779 /* Get the catalog entry */
780 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
782 /* Move to the next entry */
783 Entry
= Entry
->Flink
;
785 /* Skip it if it doesn't support IFS */
786 if (!(CatalogEntry
->ProtocolInfo
.dwServiceFlags1
& XP1_IFS_HANDLES
)) continue;
788 /* Check if we need to load it */
789 if (!(Provider
= CatalogEntry
->Provider
))
792 ErrorCode
= WsTcLoadProvider(Catalog
, CatalogEntry
);
794 /* Skip it if we failed to load it */
795 if (ErrorCode
!= ERROR_SUCCESS
) continue;
797 /* Get the provider again */
798 Provider
= CatalogEntry
->Provider
;
801 /* Reference the entry and get our unique id */
802 InterlockedIncrement(&CatalogEntry
->RefCount
);
803 UniqueId
= Catalog
->UniqueId
;
805 /* Release the lock now */
808 /* Get the catalog entry ID */
809 OptionLength
= sizeof(ProtocolInfo
);
810 ErrorCode
= Provider
->Service
.lpWSPGetSockOpt(Handle
,
813 (PCHAR
)&ProtocolInfo
,
814 (LPINT
)&OptionLength
,
817 /* Dereference the entry and check the result */
818 WsTcEntryDereference(CatalogEntry
);
819 if (ErrorCode
!= ERROR_SUCCESS
)
821 /* Lock and make sure this provider is still valid */
823 if (UniqueId
== Catalog
->UniqueId
) continue;
825 /* It changed! We need to start over */
829 /* Now get the IFS handle */
830 NewHandle
= WPUModifyIFSHandle(ProtocolInfo
.dwCatalogEntryId
,
834 /* Check if the socket is invalid */
835 if (NewHandle
== INVALID_SOCKET
) return WSAENOTSOCK
;
837 /* We suceeded, get out of here */
838 return ERROR_SUCCESS
;
841 /* Unrecognized socket if we get here: note, we still have the lock */
848 WsTcRemoveCatalogItem(IN PTCATALOG Catalog
,
849 IN PTCATALOG_ENTRY Entry
)
851 /* Remove the entry from the list */
852 RemoveEntryList(&Entry
->CatalogLink
);
854 /* Decrease our count */
855 Catalog
->ItemCount
--;
860 WsTcDelete(IN PTCATALOG Catalog
)
863 PTCATALOG_ENTRY CatalogEntry
;
865 /* Check if we're initialized */
866 if (!Catalog
->ProtocolList
.Flink
) return;
871 /* Loop every entry */
872 Entry
= Catalog
->ProtocolList
.Flink
;
873 while (Entry
!= &Catalog
->ProtocolList
)
876 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
879 WsTcRemoveCatalogItem(Catalog
, CatalogEntry
);
882 WsTcEntryDereference(CatalogEntry
);
884 /* Move to the next entry */
885 Entry
= Catalog
->ProtocolList
.Flink
;
888 /* Check if the catalog key is opened */
889 if (Catalog
->CatalogKey
)
892 RegCloseKey(Catalog
->CatalogKey
);
893 Catalog
->CatalogKey
= NULL
;
896 /* Release and delete the lock */
898 DeleteCriticalSection((LPCRITICAL_SECTION
)&Catalog
->Lock
);
900 /* Delete the object */
901 HeapFree(WsSockHeap
, 0, Catalog
);