//#define USE_MSG_BINDING using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel.Channels; using System.ServiceModel; using System.Security.Cryptography.X509Certificates; using System.Net; using System.ServiceModel.Description; namespace HeuristicLab.Hive.Contracts { public static class WcfSettings { public const string SlaveServiceName = "SlaveService"; public const string ServerConsoleServiceName = "ServerConsoleService"; public const string ClientServiceName = "ClientService"; public const int DefaultPort = 9000; /// /// Gets a pre-defined binding using TCP for secure transport. /// /// A binding type of public static Binding GetBinding() { #if USE_MSG_BINDING NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message); #else NetTcpBinding binding = new NetTcpBinding(); #endif binding.MaxBufferSize = int.MaxValue; binding.MaxReceivedMessageSize = int.MaxValue; binding.ReaderQuotas.MaxArrayLength = int.MaxValue; binding.ReaderQuotas.MaxStringContentLength = int.MaxValue; binding.CloseTimeout = new TimeSpan(0, 5, 0); binding.ReceiveTimeout = new TimeSpan(0, 5, 0); binding.SendTimeout = new TimeSpan(0, 5, 0); return binding; } public static Binding GetStreamedBinding() { #if USE_MSG_BINDING NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message); #else NetTcpBinding binding = new NetTcpBinding(); #endif binding.TransferMode = TransferMode.Streamed; binding.MaxReceivedMessageSize = int.MaxValue; binding.ReaderQuotas.MaxArrayLength = int.MaxValue; binding.ReaderQuotas.MaxStringContentLength = int.MaxValue; binding.CloseTimeout = new TimeSpan(0, 5, 0); binding.ReceiveTimeout = new TimeSpan(0, 5, 0); binding.SendTimeout = new TimeSpan(0, 5, 0); //Disabling security mode, for the moment binding.Security.Mode = SecurityMode.None; return binding; } /// /// Defines the used certificate for authentification located in a certification store. /// /// A service for which this certificate is applicable. public static void SetServiceCertificate(ServiceHost svchost) { #if USE_MSG_BINDING svchost.Credentials.ServiceCertificate.SetCertificate( StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, SERVERCERT); #endif } /// /// Gets the currently active IP address. /// If more than one IP connections is active, the first one will be used. /// /// public static IPAddress GetActiveIP() { //return IPAddress.Parse("127.0.0.1"); return System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()[0].LocalEndPoint.Address; //IPAddress[] addresses; //addresses = Dns.GetHostAddresses(Dns.GetHostName()); //int index = 0; //if (System.Environment.OSVersion.Version.Major >= 6) { // for (index = addresses.Length - 1; index >= 0; index--) // if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) // break; //} //return addresses[index]; } /// /// Gets the default port used for HIVE services. /// /// public static int GetDefaultPort() { return DefaultPort; } /// /// This method changes the endpoint-address while preserving the identity-certificate defined in the config file /// public static void SetEndpointAddress(ServiceEndpoint endpoint, string address) { EndpointAddressBuilder builder = new EndpointAddressBuilder(endpoint.Address); builder.Uri = new Uri(address); endpoint.Address = builder.ToEndpointAddress(); } } /// /// This class verifies the certificate defined by method. Normally, /// the verification process is managed by the underlying operating system. /// /// /// WARNUNG: Dieser Code wird nur für Testzertifikate benötigt, wie sie beispielsweise von makecert erstellt werden. /// Sie sollten diesen Code nicht in einer Produktionsumgebung verwenden. /// /* public class PermissiveCertificatePolicy { string subjectName; static PermissiveCertificatePolicy currentPolicy; PermissiveCertificatePolicy(string subjectName) { this.subjectName = subjectName; ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate); } public static void Enact(string subjectName) { currentPolicy = new PermissiveCertificatePolicy(subjectName); } bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) { if (cert.Subject == subjectName) { return true; } return false; } } */ }