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(&Catalog->Lock)
19 #define WsTcUnlock() LeaveCriticalSection(&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(&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
;
388 PTCATALOG_ENTRY Entry
;
391 *CatalogEntry
= NULL
;
393 /* Lock the catalog */
396 /* Match the Id with all the entries in the List */
397 NextEntry
= Catalog
->ProtocolList
.Flink
;
398 while (NextEntry
!= &Catalog
->ProtocolList
)
400 /* Get the Current Entry */
401 Entry
= CONTAINING_RECORD(NextEntry
, TCATALOG_ENTRY
, CatalogLink
);
402 NextEntry
= NextEntry
->Flink
;
404 /* Check if this is the Catalog Entry ID we want */
405 if (Entry
->ProtocolInfo
.iAddressFamily
== AddressFamily
)
407 /* Check if it doesn't already have a provider */
408 if (!Entry
->Provider
)
410 /* Match, load the Provider */
411 ErrorCode
= WsTcLoadProvider(Catalog
, Entry
);
413 /* Make sure this didn't fail */
414 if (ErrorCode
!= ERROR_SUCCESS
) break;
417 /* Reference the entry and return it */
418 InterlockedIncrement(&Entry
->RefCount
);
419 *CatalogEntry
= Entry
;
420 ErrorCode
= ERROR_SUCCESS
;
425 /* Release the catalog */
434 WsTcGetEntryFromCatalogEntryId(IN PTCATALOG Catalog
,
435 IN DWORD CatalogEntryId
,
436 IN PTCATALOG_ENTRY
*CatalogEntry
)
438 INT ErrorCode
= WSAEINVAL
;
439 PLIST_ENTRY NextEntry
;
440 PTCATALOG_ENTRY Entry
;
442 /* Lock the catalog */
445 /* Match the Id with all the entries in the List */
446 NextEntry
= Catalog
->ProtocolList
.Flink
;
447 while (NextEntry
!= &Catalog
->ProtocolList
)
449 /* Get the Current Entry */
450 Entry
= CONTAINING_RECORD(NextEntry
, TCATALOG_ENTRY
, CatalogLink
);
451 NextEntry
= NextEntry
->Flink
;
453 /* Check if this is the Catalog Entry ID we want */
454 if (Entry
->ProtocolInfo
.dwCatalogEntryId
== CatalogEntryId
)
456 /* Check if it doesn't already have a provider */
457 if (!Entry
->Provider
)
459 /* Match, load the Provider */
460 WsTcLoadProvider(Catalog
, Entry
);
463 /* Reference the entry and return it */
464 InterlockedIncrement(&Entry
->RefCount
);
465 *CatalogEntry
= Entry
;
466 ErrorCode
= ERROR_SUCCESS
;
471 /* Release the catalog */
480 WsTcGetEntryFromTriplet(IN PTCATALOG Catalog
,
485 IN PTCATALOG_ENTRY
*CatalogEntry
)
487 INT ErrorCode
= WSAEINVAL
;
488 PLIST_ENTRY NextEntry
;
489 PTCATALOG_ENTRY Entry
;
490 DPRINT("WsTcGetEntryFromTriplet: %lx, %lx, %lx, %lx\n", af
, type
, protocol
, StartId
);
493 *CatalogEntry
= NULL
;
495 /* Lock the catalog */
498 NextEntry
= Catalog
->ProtocolList
.Flink
;
500 /* Check if we are starting past 0 */
504 while (NextEntry
!= &Catalog
->ProtocolList
)
506 /* Get the Current Entry */
507 Entry
= CONTAINING_RECORD(NextEntry
, TCATALOG_ENTRY
, CatalogLink
);
508 NextEntry
= NextEntry
->Flink
;
510 /* Check if this is the ID where we are starting */
511 if (Entry
->ProtocolInfo
.dwCatalogEntryId
== StartId
) break;
515 /* Match the Id with all the entries in the List */
516 while (NextEntry
!= &Catalog
->ProtocolList
)
518 /* Get the Current Entry */
519 Entry
= CONTAINING_RECORD(NextEntry
, TCATALOG_ENTRY
, CatalogLink
);
520 NextEntry
= NextEntry
->Flink
;
522 /* Check if Address Family Matches or if it's wildcard */
523 if ((Entry
->ProtocolInfo
.iAddressFamily
== af
) || (af
== AF_UNSPEC
))
525 /* Check if Socket Type Matches or if it's wildcard */
526 if ((Entry
->ProtocolInfo
.iSocketType
== type
) || (type
== 0))
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
)) ||
534 /* Check if it doesn't already have a provider */
535 if (!Entry
->Provider
)
537 /* Match, load the Provider */
538 ErrorCode
= WsTcLoadProvider(Catalog
, Entry
);
540 /* Make sure this didn't fail */
541 if (ErrorCode
!= ERROR_SUCCESS
) break;
544 /* Reference the entry and return it */
545 InterlockedIncrement(&Entry
->RefCount
);
546 *CatalogEntry
= Entry
;
547 ErrorCode
= ERROR_SUCCESS
;
552 ErrorCode
= WSAEPROTONOSUPPORT
;
557 ErrorCode
= WSAESOCKTNOSUPPORT
;
562 ErrorCode
= WSAEAFNOSUPPORT
;
566 /* Release the catalog */
575 WsTcFindProvider(IN PTCATALOG Catalog
,
576 IN LPGUID ProviderId
)
580 PTCATALOG_ENTRY CatalogEntry
;
582 /* Loop the provider list */
583 Entry
= Catalog
->ProtocolList
.Flink
;
584 while (Entry
!= &Catalog
->ProtocolList
)
587 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
589 /* Move to the next one, get the provider */
590 Entry
= Entry
->Flink
;
591 Provider
= CatalogEntry
->Provider
;
593 /* Check for a match */
595 IsEqualGUID(&CatalogEntry
->ProtocolInfo
.ProviderId
, ProviderId
))
608 WsTcLoadProvider(IN PTCATALOG Catalog
,
609 IN PTCATALOG_ENTRY CatalogEntry
)
611 INT ErrorCode
= ERROR_SUCCESS
;
613 DPRINT("WsTcLoadProvider: %p, %p\n", Catalog
, CatalogEntry
);
615 /* Lock the catalog */
618 /* Check if we have a provider already */
619 if (!CatalogEntry
->Provider
)
621 /* Try to find another instance */
622 Provider
= WsTcFindProvider(Catalog
,
623 &CatalogEntry
->ProtocolInfo
.ProviderId
);
625 /* Check if we found one now */
628 /* Set this one as the provider */
629 WsTcEntrySetProvider(CatalogEntry
, Provider
);
630 ErrorCode
= ERROR_SUCCESS
;
634 /* Nothing found, Allocate a provider */
635 if ((Provider
= WsTpAllocate()))
638 ErrorCode
= WsTpInitialize(Provider
,
639 CatalogEntry
->DllPath
,
640 &CatalogEntry
->ProtocolInfo
);
643 if (ErrorCode
== ERROR_SUCCESS
)
645 /* Set the provider */
646 WsTcEntrySetProvider(CatalogEntry
, Provider
);
650 WsTpDereference(Provider
);
655 ErrorCode
= WSA_NOT_ENOUGH_MEMORY
;
660 /* Release the lock */
667 WsTcUpdateProtocolList(IN PTCATALOG Catalog
,
671 PTCATALOG_ENTRY CatalogEntry
, OldCatalogEntry
;
674 /* First move from our list to the old one */
675 InsertHeadList(&Catalog
->ProtocolList
, &TempList
);
676 RemoveEntryList(&Catalog
->ProtocolList
);
677 InitializeListHead(&Catalog
->ProtocolList
);
679 /* Loop every item in the list */
680 while (!IsListEmpty(List
))
682 /* Get the catalog entry */
683 Entry
= RemoveHeadList(List
);
684 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
686 /* Check if this item is already in our list */
687 Entry
= TempList
.Flink
;
688 while (Entry
!= &TempList
)
690 /* Get the catalog entry */
691 OldCatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
692 Entry
= Entry
->Flink
;
694 /* Check if they match */
695 if (CatalogEntry
->ProtocolInfo
.dwCatalogEntryId
==
696 OldCatalogEntry
->ProtocolInfo
.dwCatalogEntryId
)
698 /* We have a match, use the old item instead */
699 WsTcEntryDereference(CatalogEntry
);
700 CatalogEntry
= OldCatalogEntry
;
701 RemoveEntryList(&CatalogEntry
->CatalogLink
);
703 /* Decrease the number of protocols we have */
704 Catalog
->ItemCount
--;
710 InsertTailList(&Catalog
->ProtocolList
, &CatalogEntry
->CatalogLink
);
711 Catalog
->ItemCount
++;
714 /* If there's anything left in the temporary list */
715 while (!IsListEmpty(&TempList
))
718 Entry
= RemoveHeadList(&TempList
);
719 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
722 Catalog
->ItemCount
--;
723 WsTcEntryDereference(CatalogEntry
);
729 WsTcEnumerateCatalogItems(IN PTCATALOG Catalog
,
730 IN PTCATALOG_ENUMERATE_PROC Callback
,
734 PTCATALOG_ENTRY CatalogEntry
;
737 /* Lock the catalog */
740 /* Loop the entries */
741 Entry
= Catalog
->ProtocolList
.Flink
;
742 while (GoOn
&& (Entry
!= &Catalog
->ProtocolList
))
745 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
747 /* Move to the next one and call the callback */
748 Entry
= Entry
->Flink
;
749 GoOn
= Callback(Context
, CatalogEntry
);
758 WsTcFindIfsProviderForSocket(IN PTCATALOG Catalog
,
766 WSAPROTOCOL_INFOW ProtocolInfo
;
769 PTCATALOG_ENTRY CatalogEntry
;
771 /* Get the catalog lock */
774 /* Loop as long as the catalog changes */
777 /* Loop every provider */
778 Entry
= Catalog
->ProtocolList
.Flink
;
779 while (Entry
!= &Catalog
->ProtocolList
)
781 /* Get the catalog entry */
782 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
784 /* Move to the next entry */
785 Entry
= Entry
->Flink
;
787 /* Skip it if it doesn't support IFS */
788 if (!(CatalogEntry
->ProtocolInfo
.dwServiceFlags1
& XP1_IFS_HANDLES
)) continue;
790 /* Check if we need to load it */
791 if (!(Provider
= CatalogEntry
->Provider
))
794 ErrorCode
= WsTcLoadProvider(Catalog
, CatalogEntry
);
796 /* Skip it if we failed to load it */
797 if (ErrorCode
!= ERROR_SUCCESS
) continue;
799 /* Get the provider again */
800 Provider
= CatalogEntry
->Provider
;
803 /* Reference the entry and get our unique id */
804 InterlockedIncrement(&CatalogEntry
->RefCount
);
805 UniqueId
= Catalog
->UniqueId
;
807 /* Release the lock now */
810 /* Get the catalog entry ID */
811 OptionLength
= sizeof(ProtocolInfo
);
812 ErrorCode
= Provider
->Service
.lpWSPGetSockOpt(Handle
,
815 (PCHAR
)&ProtocolInfo
,
819 /* Dereference the entry and check the result */
820 WsTcEntryDereference(CatalogEntry
);
821 if (ErrorCode
!= ERROR_SUCCESS
)
823 /* Lock and make sure this provider is still valid */
825 if (UniqueId
== Catalog
->UniqueId
) continue;
827 /* It changed! We need to start over */
831 /* Now get the IFS handle */
832 NewHandle
= WPUModifyIFSHandle(ProtocolInfo
.dwCatalogEntryId
,
836 /* Check if the socket is invalid */
837 if (NewHandle
== INVALID_SOCKET
) return WSAENOTSOCK
;
839 /* We succeeded, get out of here */
840 return ERROR_SUCCESS
;
843 /* Unrecognized socket if we get here: note, we still have the lock */
850 WsTcRemoveCatalogItem(IN PTCATALOG Catalog
,
851 IN PTCATALOG_ENTRY Entry
)
853 /* Remove the entry from the list */
854 RemoveEntryList(&Entry
->CatalogLink
);
856 /* Decrease our count */
857 Catalog
->ItemCount
--;
862 WsTcDelete(IN PTCATALOG Catalog
)
865 PTCATALOG_ENTRY CatalogEntry
;
867 /* Check if we're initialized */
868 if (!Catalog
->ProtocolList
.Flink
) return;
873 /* Loop every entry */
874 Entry
= Catalog
->ProtocolList
.Flink
;
875 while (Entry
!= &Catalog
->ProtocolList
)
878 CatalogEntry
= CONTAINING_RECORD(Entry
, TCATALOG_ENTRY
, CatalogLink
);
880 /* Remove it and dereference it */
881 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(&Catalog
->Lock
);
900 /* Delete the object */
901 HeapFree(WsSockHeap
, 0, Catalog
);