INT WSAAPI WsApiProlog(OUT PWSPROCESS *Process, OUT PWSTHREAD *Thread) { INT ErrorCode = WSANOTINITIALISED; /* Try to get the current process */ if ((*Process = WsGetProcess())) { /* And the current thread */ ErrorCode = WsThreadGetCurrentThread(*Process, Thread); } /* Return init status */ return ErrorCode; }
PWSSOCKET WSAAPI WsSockFindIfsSocket(IN SOCKET Handle) { INT ErrorCode; DWORD Flags; PWSSOCKET Socket = NULL; PWSPROCESS Process = NULL; PTCATALOG Catalog = NULL; /* Validate the socket and get handle info */ if ((Handle != INVALID_SOCKET) && (GetHandleInformation((HANDLE)Handle, &Flags))) { /* Get the process */ if ((Process = WsGetProcess())) { /* Get the catalog */ Catalog = WsProcGetTCatalog(Process); /* Get the IFS Provider */ ErrorCode = WsTcFindIfsProviderForSocket(Catalog, Handle); /* Check for success */ if (ErrorCode == ERROR_SUCCESS) { /* Get the Socket now */ Socket = WsSockGetSocketNoExport(Handle); /* Mark it as an API Socket */ if (Socket) Socket->ApiSocket = TRUE; } } } /* Return the socket */ return Socket; }
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved) { PWSPROCESS WsProcess; /* Main Entrypoint */ switch (dwReason) { case DLL_PROCESS_ATTACH: /* Save DLL Handle */ WsDllHandle = hModule; /* Get Global Heap */ WsSockHeap = GetProcessHeap(); /* TLS Allocation */ if (GlobalTlsIndex == TLS_OUT_OF_INDEXES) { GlobalTlsIndex = TlsAlloc(); if (GlobalTlsIndex == TLS_OUT_OF_INDEXES) { return FALSE; } } /* Initialize some critical sections */ WsCreateStartupSynchronization(); WsAsyncGlobalInitialize(); WsRasInitializeAutodial(); break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: /* Destroy the attached Winsock Thread */ WsThreadDestroyCurrentThread(); break; case DLL_PROCESS_DETACH: /* Make sure we were initialized */ if (!WsDllHandle) break; /* Check if this was a FreeLibrary call (ie: not process cleanup) */ if (lpReserved) { /* Destroy the thread which is exiting */ WsThreadDestroyCurrentThread(); /* Check if we have a process and destroy it */ WsProcess = WsGetProcess(); if (WsProcess) WsProcDelete(WsProcess); /* Cleanup the Thread and Socket managers */ WsThreadCleanup(); WsSockCleanup(); /* Cleanup critical sections */ WsDestroyStartupSynchronization(); WsAsyncGlobalTerminate(); /* Free the TLS Index */ TlsFree(GlobalTlsIndex); } /* Cleanup RAS auto-dial helper */ WsRasUninitializeAutodial(); /* Clear our handle */ WsDllHandle = NULL; break; } /* Return to OS */ return TRUE; }
/* * @implemented */ INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData) { WORD VersionReturned = 0; DWORD ErrorCode = ERROR_SUCCESS; PWSPROCESS CurrentProcess; DPRINT("WSAStartup: %wx\n", wVersionRequested); /* Make sure that we went through DLL Init */ if (!WsDllHandle) return WSASYSNOTREADY; /* Check which version is being requested */ switch (LOBYTE(wVersionRequested)) { case 0: /* We don't support this unknown version */ ErrorCode = WSAVERNOTSUPPORTED; break; case 1: /* We support only 1.0 and 1.1 */ if (HIBYTE(wVersionRequested) == 0) { /* Caller wants 1.0, return it */ VersionReturned = wVersionRequested; } else { /* The only other version we support is 1.1 */ VersionReturned = MAKEWORD(1, 1); } break; case 2: /* We support only 2.0, 2.1 and 2.2 */ if (HIBYTE(wVersionRequested) <= 2) { /* Caller wants 2.0-2.2, return it */ VersionReturned = MAKEWORD(2, HIBYTE(wVersionRequested)); } else { /* The highest version we support is 2.2 */ VersionReturned = MAKEWORD(2, 2); } break; default: /* Return 2.2 */ VersionReturned = MAKEWORD(2, 2);; break; } /* Return the Version Requested, unless error */ lpWSAData->wVersion = VersionReturned; /* We support Winsock 2.2 */ lpWSAData->wHighVersion = MAKEWORD(2,2); lstrcpy(lpWSAData->szDescription, "WinSock 2.0"); lstrcpy(lpWSAData->szSystemStatus, "Running"); /* * On Winsock 1, the following values are returned. * Taken straight from a Winsock Test app on Windows. */ if (LOBYTE(wVersionRequested) == 1) { lpWSAData->iMaxSockets = 32767; lpWSAData->iMaxUdpDg = 65467; } else { lpWSAData->iMaxSockets = 0; lpWSAData->iMaxUdpDg = 0; } /* Enter the startup synchronization lock */ WsStartupLock(); /* Now setup all our objects */ while (TRUE) { /* Make sure we don't already have a process */ CurrentProcess = WsGetProcess(); if (CurrentProcess) break; /* Setup the process object support */ ErrorCode = WsProcStartup(); if (ErrorCode != ERROR_SUCCESS) break; /* Setup the process object support */ ErrorCode = WsSockStartup(); if (ErrorCode != ERROR_SUCCESS) break; /* Setup the process object support */ ErrorCode = WsThreadStartup(); if (ErrorCode != ERROR_SUCCESS) break; /* Try getting the process now */ CurrentProcess = WsGetProcess(); if (!CurrentProcess) { /* Something is weird... */ ErrorCode = WSASYSNOTREADY; break; } } /* Check if all worked */ if (ErrorCode == ERROR_SUCCESS) { /* Set the requested version */ WsProcSetVersion(CurrentProcess, wVersionRequested); /* Increase the reference count */ InterlockedIncrement(&CurrentProcess->RefCount); } /* Leave the startup lock */ WsStartupUnlock(); /* Return any Error */ return ErrorCode; }
/* * @implemented */ SOCKET WSPAPI WPUModifyIFSHandle(IN DWORD dwCatalogEntryId, IN SOCKET ProposedHandle, OUT LPINT lpErrno) { SOCKET Handle = INVALID_SOCKET; DWORD ErrorCode = ERROR_SUCCESS; PWSPROCESS Process; PTCATALOG Catalog; PTCATALOG_ENTRY Entry; PWSSOCKET Socket; DPRINT("WPUModifyIFSHandle: %lx, %lx\n", dwCatalogEntryId, ProposedHandle); /* Get the current process */ if ((Process = WsGetProcess())) { /* Get the Transport Catalog */ if ((Catalog = WsProcGetTCatalog(Process))) { /* Get the entry for this ID */ ErrorCode = WsTcGetEntryFromCatalogEntryId(Catalog, dwCatalogEntryId, &Entry); /* Check for success */ if (ErrorCode == ERROR_SUCCESS) { /* Create a socket object */ if ((Socket = WsSockAllocate())) { /* Initialize it */ WsSockInitialize(Socket, Entry); /* Associate it */ ErrorCode = WsSockAssociateHandle(Socket, ProposedHandle, TRUE); /* Check for success */ if (ErrorCode == ERROR_SUCCESS) { /* Return */ Handle = ProposedHandle; *lpErrno = ERROR_SUCCESS; } else { /* Fail */ WsSockDereference(Socket); *lpErrno = ErrorCode; } /* Dereference the extra count */ WsSockDereference(Socket); } else { /* No memory to allocate a socket */ *lpErrno = WSAENOBUFS; } /* Dereference the catalog entry */ WsTcEntryDereference(Entry); } else { /* Entry not found */ *lpErrno = ErrorCode; } } else { /* Catalog not found */ *lpErrno = WSANOTINITIALISED; } } else { /* Process not ready */ *lpErrno = WSANOTINITIALISED; } /* Return */ return Handle; }