JNIEXPORT jint JNICALL Java_com_act365_net_SocketWrenchSession__1shutdown (JNIEnv* env , jclass) { #ifdef WIN32 // Reinstall removed service providers. int errorCode = 0 ; WSCInstallProvider( & protocolBuffer[0].ProviderId , dllPath , protocolBuffer , nProtocols , & errorCode ); if( errorCode > 0 ){ jclass exceptionClass = env -> FindClass("java/net/SocketException"); SocketUtils::throwError( env , exceptionClass , "WSCInstallProvider()" ); env -> DeleteLocalRef( exceptionClass ); return 0 ; } // Clean-up WSACleanup(); #endif return 0 ; }
//------------------------------------------------------------------------ // InstallRestrictedLSP() // //------------------------------------------------------------------------ int InstallRestrictedLSP( DWORD *pdwCatalogId ) { WSAPROTOCOL_INFOW ProtocolInfoW; int iStatus; int iError = ERROR_SUCCESS; memset(&ProtocolInfoW,0,sizeof(WSAPROTOCOL_INFOW)); // Create a PROTOCOL_INFO to install for our provider DLL. // Note: We don't need to fill in chain for a // LAYERED_PROTOCOL or BASE_PROTOCOL. ProtocolInfoW.dwServiceFlags1 = 0; ProtocolInfoW.dwServiceFlags2 = 0; ProtocolInfoW.dwServiceFlags3 = 0; ProtocolInfoW.dwServiceFlags4 = 0; ProtocolInfoW.dwProviderFlags = PFL_HIDDEN; ProtocolInfoW.ProviderId = LayeredProviderGuid; ProtocolInfoW.dwCatalogEntryId = 0; // Assigned by system. ProtocolInfoW.ProtocolChain.ChainLen = LAYERED_PROTOCOL; ProtocolInfoW.iVersion = 0; ProtocolInfoW.iAddressFamily = AF_INET; ProtocolInfoW.iMaxSockAddr = 16; ProtocolInfoW.iMinSockAddr = 16; ProtocolInfoW.iSocketType = SOCK_STREAM; ProtocolInfoW.iProtocol = IPPROTO_TCP; ProtocolInfoW.iProtocolMaxOffset = 0; ProtocolInfoW.iNetworkByteOrder = BIGENDIAN; ProtocolInfoW.iSecurityScheme = SECURITY_PROTOCOL_NONE; ProtocolInfoW.dwMessageSize = 0; // Stream Oriented ProtocolInfoW.dwProviderReserved = 0; wcscpy( ProtocolInfoW.szProtocol, RESTRICTED_LSP_NAME ); iStatus = WSCInstallProvider( (GUID*)&LayeredProviderGuid, LSP_PATH, &ProtocolInfoW, 1, &iError ); *pdwCatalogId = ProtocolInfoW.dwCatalogEntryId; return iError; }
// // Function: InstallProvider // // Description: // This is a wrapper for the WSCInstallProvider function. Depending on // which catalog is specified, this routine calls the correct install // routine. // int InstallProvider( WINSOCK_CATALOG Catalog, // Which catalog are we operating on GUID *Guid, // GUID under which provider will be installed WCHAR *lpwszLspPath, // Path to LSP's DLL WSAPROTOCOL_INFOW *pProvider, // Array of provider structures to install INT iProviderCount // Number of providers in array ) { WSAPROTOCOL_INFOW *pEnumProviders = NULL, *pEntry = NULL; INT iEnumProviderCount, ErrorCode, rc = SOCKET_ERROR; #ifdef _WIN64 if ( LspCatalog32Only == Catalog ) { // Can't install only in 32-bit catalog from 64-bit fprintf( stderr, "InstallProvider: Error! It is not possible to install only " "in 32-bit catalog from 64-bit process!\n\n" ); goto cleanup; } else if ( LspCatalog64Only == Catalog ) { // Just need to call WSCInstallProvider rc = WSCInstallProvider( Guid, lpwszLspPath, pProvider, iProviderCount, &ErrorCode ); } else { // To install in both we must call WSCInstallProviderPath64_32 rc = WSCInstallProvider64_32( Guid, lpwszLspPath, pProvider, iProviderCount, &ErrorCode ); } #else if ( LspCatalog32Only == Catalog ) { // From a 32-bit process we can only install into 32-bit catalog rc = WSCInstallProvider( Guid, lpwszLspPath, pProvider, iProviderCount, &ErrorCode ); } else { // From a 32-bit process, we can't touch the 64-bit catalog at all fprintf( stderr, "InstallProvider: Error! It is not possible to install into " "the 64-bit catalog from a 32-bit process!\n\n" ); goto cleanup; } #endif if ( SOCKET_ERROR == rc ) { fprintf( stderr, "InstallProvider: WSCInstallProvider* failed: %d\n", ErrorCode ); goto cleanup; } // Go back and enumerate what we just installed pEnumProviders = EnumerateProviders( Catalog, &iEnumProviderCount ); if ( NULL == pEnumProviders ) { fprintf( stderr, "InstallProvider: EnumerateProviders failed!\n" ); goto cleanup; } // Make sure our entry is in the catalog pEntry = FindProviderByGuid( Guid, pEnumProviders, iEnumProviderCount ); if ( pEntry ) { printf( "Installed: [%4d] %S\n", pEntry->dwCatalogEntryId, pEntry->szProtocol ); } cleanup: if ( NULL != pEnumProviders ) FreeProviders( pEnumProviders ); return rc; }
//------------------------------------------------------------------------ // InstallNewChain() // //------------------------------------------------------------------------ int InstallNewChain( LPWSAPROTOCOL_INFOW BaseProtocolInfoBuff, DWORD dwLayeredProviderCatalogId ) { int iStatus = ERROR_SUCCESS; int iError = ERROR_SUCCESS; WSAPROTOCOL_INFOW ProtocolChainProtoInfo; // See if we are layering on top of base provider or a chain if (BaseProtocolInfoBuff->ProtocolChain.ChainLen == BASE_PROTOCOL) { // Layering on top of a base protocol: ProtocolChainProtoInfo = *BaseProtocolInfoBuff; ProtocolChainProtoInfo.ProviderId = RestrictedProviderChainId; if ( (wcslen(BaseProtocolInfoBuff->szProtocol) + wcslen(CHAIN_PREFIX)) > WSAPROTOCOL_LEN ) { // The name will be too long use a simpler one... wcscpy( ProtocolChainProtoInfo.szProtocol, SIMPLE_CHAIN_NAME ); } else { wcscpy( ProtocolChainProtoInfo.szProtocol, CHAIN_PREFIX ); wcscat( ProtocolChainProtoInfo.szProtocol, BaseProtocolInfoBuff->szProtocol ); } ProtocolChainProtoInfo.ProtocolChain.ChainLen = 2; ProtocolChainProtoInfo.ProtocolChain.ChainEntries[0] = dwLayeredProviderCatalogId; ProtocolChainProtoInfo.ProtocolChain.ChainEntries[1] = BaseProtocolInfoBuff->dwCatalogEntryId; iStatus = WSCInstallProvider( (GUID*)&RestrictedProviderChainId, LSP_PATH, &ProtocolChainProtoInfo, 1, &iError ); } else { // Layering on top of an existing chain: if (BaseProtocolInfoBuff->ProtocolChain.ChainLen >= MAX_PROTOCOL_CHAIN) { // The chain is going to be too long... return SOCKET_ERROR; } ProtocolChainProtoInfo = *BaseProtocolInfoBuff; ProtocolChainProtoInfo.ProviderId = RestrictedProviderChainId; if ( (wcslen(BaseProtocolInfoBuff->szProtocol) + wcslen(CHAIN_PREFIX)) > WSAPROTOCOL_LEN ) { // The name will be too long use a simpler one... wcscpy( ProtocolChainProtoInfo.szProtocol, SIMPLE_CHAIN_NAME ); } else { wcscpy( ProtocolChainProtoInfo.szProtocol, CHAIN_PREFIX ); wcscat( ProtocolChainProtoInfo.szProtocol, BaseProtocolInfoBuff->szProtocol ); } ProtocolChainProtoInfo.ProtocolChain.ChainLen = 1 + BaseProtocolInfoBuff->ProtocolChain.ChainLen; ProtocolChainProtoInfo.ProtocolChain.ChainEntries[0] = dwLayeredProviderCatalogId; for (int i=0; i<BaseProtocolInfoBuff->ProtocolChain.ChainLen; i++) { ProtocolChainProtoInfo.ProtocolChain.ChainEntries[1+i] = BaseProtocolInfoBuff->ProtocolChain.ChainEntries[i]; } iStatus = WSCInstallProvider( (GUID*)&RestrictedProviderChainId, LSP_PATH, &ProtocolChainProtoInfo, 1, &iError ); } return iStatus; }
BOOL InstallTransportProviderCallback( LPTSTR IniFile, LPTSTR SectionName, DWORD Context ) /*++ Routine Description: Callback routine for EnumProviderSections() that installs the transport service provider described by the given .INI file section. Arguments: IniFile - The name of the .INI file describing the transport provider. SectionName - The name of the .INI file section for this provider. Context - Actually contains behaviour control options (OPTION_FLAG_*). Return Value: BOOL - TRUE if successful, FALSE otherwise. --*/ { TCHAR providerName[MAX_INIFILE_LINE]; TCHAR providerPath[MAX_INIFILE_LINE]; TCHAR providerIdString[MAX_INIFILE_LINE]; TCHAR protocolSectionName[MAX_INIFILE_LINE]; UINT protocolCount; UINT i; DWORD length; LPWSAPROTOCOL_INFO protocolInfo; INT result; INT error; GUID providerId; RPC_STATUS status; BOOL ignoreErrors; // // Let the user know what we're up to. // _tprintf( TEXT("Installing %s\n"), SectionName ); // // Determine if we should ignore errors. If so, then this routine // will always return TRUE. // ignoreErrors = ( ( Context & OPTION_FLAG_IGNORE_ERRORS ) != 0 ); // // Read the fixed information. // length = GetPrivateProfileString( SectionName, TEXT("ProviderName"), TEXT(""), providerName, sizeof(providerName) / sizeof(providerName[0]), IniFile ); if( length == 0 ) { _tprintf( TEXT("ERROR: missing ProviderName key\n") ); return ignoreErrors; } length = GetPrivateProfileString( SectionName, TEXT("ProviderPath"), TEXT(""), providerPath, sizeof(providerPath) / sizeof(providerPath[0]), IniFile ); if( length == 0 ) { _tprintf( TEXT("ERROR: missing ProviderPath key\n") ); return ignoreErrors; } protocolCount = GetPrivateProfileInt( SectionName, TEXT("ProtocolCount"), 0, IniFile ); if( protocolCount == 0 ) { _tprintf( TEXT("ERROR: missing ProtocolCount key\n") ); return ignoreErrors; } length = GetPrivateProfileString( SectionName, TEXT("ProviderId"), TEXT(""), providerIdString, sizeof(providerIdString) / sizeof(providerIdString[0]), IniFile ); if( length == 0 ) { _tprintf( TEXT("ERROR: missing ProviderId key\n") ); return ignoreErrors; } // // Build the GUID. // status = UuidFromString( providerIdString, &providerId ); if( status != RPC_S_OK ) { _tprintf( TEXT("ERROR: invalid ProviderId %s\n"), providerIdString ); return ignoreErrors; } // // Allocate the space for the protocol info structures. // protocolInfo = malloc( protocolCount * sizeof(*protocolInfo) ); if( protocolInfo == NULL ) { _tprintf( TEXT("ERROR: out of memory\n") ); return ignoreErrors; } // // Enumerate the protocols. // for( i = 0 ; i < protocolCount ; i++ ) { // // Build the section name for this protocol. // wsprintf( protocolSectionName, TEXT("%s Protocol %u"), SectionName, i ); // // Read the individual protocol info. // protocolInfo[i].dwServiceFlags1 = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("dwServiceFlags1"), 0, IniFile ); protocolInfo[i].dwServiceFlags2 = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("dwServiceFlags2"), 0, IniFile ); protocolInfo[i].dwServiceFlags3 = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("dwServiceFlags3"), 0, IniFile ); protocolInfo[i].dwServiceFlags4 = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("dwServiceFlags4"), 0, IniFile ); protocolInfo[i].dwProviderFlags = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("dwProviderFlags"), 0, IniFile ); protocolInfo[i].iVersion = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("iVersion"), 0, IniFile ); protocolInfo[i].iAddressFamily = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("iAddressFamily"), 0, IniFile ); protocolInfo[i].iMaxSockAddr = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("iMaxSockAddr"), 0, IniFile ); protocolInfo[i].iMinSockAddr = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("iMinSockAddr"), 0, IniFile ); protocolInfo[i].iSocketType = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("iSocketType"), 0, IniFile ); protocolInfo[i].iProtocol = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("iProtocol"), 0, IniFile ); protocolInfo[i].iProtocolMaxOffset = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("iProtocolMaxOffset"), 0, IniFile ); protocolInfo[i].iNetworkByteOrder = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("iNetworkByteOrder"), 0, IniFile ); protocolInfo[i].iSecurityScheme = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("iSecurityScheme"), 0, IniFile ); protocolInfo[i].dwMessageSize = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("dwMessageSize"), 0, IniFile ); protocolInfo[i].dwProviderReserved = (DWORD)GetPrivateProfileInt( protocolSectionName, TEXT("dwProviderReserved"), 0, IniFile ); length = GetPrivateProfileString( protocolSectionName, TEXT("szProtocol"), TEXT(""), protocolInfo[i].szProtocol, sizeof(protocolInfo[i].szProtocol) / sizeof(protocolInfo[i].szProtocol[0]), IniFile ); if( length == 0 ) { _tprintf( TEXT("ERROR: missing szProtocol key\n") ); free( protocolInfo ); return ignoreErrors; } } // // OK, we've got the protocol data, now just ask WS2_32.DLL to // install 'em. // result = WSCInstallProvider( &providerId, providerPath, protocolInfo, (DWORD)protocolCount, &error ); free( protocolInfo ); if( result == SOCKET_ERROR ) { _tprintf( TEXT("Cannot install %s, error %d\n"), providerName, error ); return ignoreErrors; } return TRUE; } // InstallTransportProviderCallback