Exemple #1
0
        internal void InvokeWithCatch(NetworkChannel.CatchableOperation op)
        {
            Exception ex   = null;
            bool      flag = true;

            try
            {
                op();
                flag = false;
            }
            catch (FileIOonSourceException ex2)
            {
                flag = false;
                ex   = ex2;
            }
            catch (IOException ex3)
            {
                if (ex3.InnerException is ObjectDisposedException)
                {
                    ex = new NetworkCancelledException(ex3);
                }
                else
                {
                    ex = new NetworkCommunicationException(this.PartnerNodeName, ex3.Message, ex3);
                }
            }
            catch (SocketException ex4)
            {
                ex = new NetworkCommunicationException(this.PartnerNodeName, ex4.Message, ex4);
            }
            catch (NetworkCommunicationException ex5)
            {
                ex = ex5;
            }
            catch (NetworkTimeoutException ex6)
            {
                ex = ex6;
            }
            catch (NetworkRemoteException ex7)
            {
                flag = false;
                ex   = ex7;
            }
            catch (NetworkEndOfDataException ex8)
            {
                ex = ex8;
            }
            catch (NetworkCorruptDataGenericException)
            {
                ex = new NetworkCorruptDataException(this.PartnerNodeName);
            }
            catch (NetworkTransportException ex9)
            {
                ex = ex9;
            }
            catch (CompressionException innerException)
            {
                ex = new NetworkCorruptDataException(this.PartnerNodeName, innerException);
            }
            catch (DecompressionException innerException2)
            {
                ex = new NetworkCorruptDataException(this.PartnerNodeName, innerException2);
            }
            catch (ObjectDisposedException innerException3)
            {
                ex = new NetworkCancelledException(innerException3);
            }
            catch (SerializationException ex10)
            {
                ex = new NetworkCommunicationException(this.PartnerNodeName, ex10.Message, ex10);
            }
            catch (TargetInvocationException ex11)
            {
                if (ex11.InnerException == null || !(ex11.InnerException is SerializationException))
                {
                    throw;
                }
                ex = new NetworkCommunicationException(this.PartnerNodeName, ex11.Message, ex11);
            }
            catch (InvalidOperationException ex12)
            {
                ex = new NetworkCommunicationException(this.PartnerNodeName, ex12.Message, ex12);
            }
            finally
            {
                if (flag)
                {
                    this.Abort();
                }
            }
            if (ex != null)
            {
                this.TraceError("InvokeWithCatch: Forwarding exception: {0}", new object[]
                {
                    ex
                });
                throw ex;
            }
        }
Exemple #2
0
        internal static bool TryOpenChannel(NetworkPath netPath, int timeoutInMs, out TcpClientChannel channel, out NetworkTransportException networkEx)
        {
            channel   = null;
            networkEx = null;
            Exception       ex              = null;
            Socket          socket          = null;
            Stream          stream          = null;
            NegotiateStream negotiateStream = null;
            Stopwatch       stopwatch       = new Stopwatch();

            stopwatch.Start();
            try
            {
                socket = new Socket(netPath.TargetEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                if (netPath.Purpose == NetworkPath.ConnectionPurpose.Seeding)
                {
                    socket.ReceiveBufferSize = Parameters.CurrentValues.SeedingNetworkTransferSize;
                    socket.SendBufferSize    = Parameters.CurrentValues.SeedingNetworkTransferSize;
                }
                else
                {
                    socket.ReceiveBufferSize = Parameters.CurrentValues.LogCopyNetworkTransferSize;
                    socket.SendBufferSize    = Parameters.CurrentValues.LogCopyNetworkTransferSize;
                }
                if (netPath.HasSourceEndpoint())
                {
                    socket.Bind(netPath.SourceEndPoint);
                }
                TcpClientChannel.ConnectAbandon connectAbandon = new TcpClientChannel.ConnectAbandon(socket);
                IAsyncResult asyncResult = socket.BeginConnect(netPath.TargetEndPoint.Address, netPath.TargetEndPoint.Port, null, connectAbandon);
                if (!asyncResult.AsyncWaitHandle.WaitOne(timeoutInMs, false))
                {
                    socket = null;
                    connectAbandon.Cancel(asyncResult);
                    TcpChannel.ThrowTimeoutException(netPath.TargetNodeName, Strings.NetworkConnectionTimeout(timeoutInMs / 1000));
                }
                socket.EndConnect(asyncResult);
                long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
                ExTraceGlobals.TcpClientTracer.TraceDebug <long>(0L, "Connection took {0}ms", elapsedMilliseconds);
                socket.LingerState = new LingerOption(true, 0);
                if (!netPath.UseSocketStream || Parameters.CurrentValues.DisableSocketStream)
                {
                    stream = new NetworkStream(socket, false);
                }
                else
                {
                    stream = new SocketStream(socket, netPath.SocketStreamBufferPool, netPath.SocketStreamAsyncArgPool, netPath.SocketStreamPerfCounters);
                }
                negotiateStream     = new NegotiateStream(stream, false);
                stream              = null;
                elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
                if (elapsedMilliseconds >= (long)timeoutInMs)
                {
                    TcpChannel.ThrowTimeoutException(netPath.TargetNodeName, Strings.NetworkConnectionTimeout(timeoutInMs / 1000));
                }
                int num = timeoutInMs - (int)elapsedMilliseconds;
                negotiateStream.WriteTimeout = num;
                negotiateStream.ReadTimeout  = num;
                TcpClientChannel.AuthAbandon authAbandon = new TcpClientChannel.AuthAbandon(socket, negotiateStream);
                string targetName;
                if (netPath.UseNullSpn)
                {
                    targetName = "";
                }
                else
                {
                    targetName = "HOST/" + netPath.TargetNodeName;
                }
                bool            encrypt = netPath.Encrypt;
                ProtectionLevel protectionLevel;
                if (encrypt)
                {
                    protectionLevel = ProtectionLevel.EncryptAndSign;
                }
                else if (Parameters.CurrentValues.DisableNetworkSigning)
                {
                    protectionLevel = ProtectionLevel.None;
                }
                else
                {
                    protectionLevel = ProtectionLevel.Sign;
                }
                asyncResult = negotiateStream.BeginAuthenticateAsClient(CredentialCache.DefaultNetworkCredentials, targetName, protectionLevel, TokenImpersonationLevel.Identification, null, authAbandon);
                if (!asyncResult.AsyncWaitHandle.WaitOne(num, false))
                {
                    negotiateStream = null;
                    socket          = null;
                    authAbandon.Abandon(asyncResult);
                    TcpChannel.ThrowTimeoutException(netPath.TargetNodeName, Strings.NetworkConnectionTimeout(timeoutInMs / 1000));
                }
                negotiateStream.EndAuthenticateAsClient(asyncResult);
                bool flag = false;
                if (!negotiateStream.IsAuthenticated)
                {
                    flag = true;
                }
                else if (protectionLevel != ProtectionLevel.None && !negotiateStream.IsMutuallyAuthenticated)
                {
                    if (netPath.IgnoreMutualAuth || MachineName.Comparer.Equals(netPath.TargetNodeName, Environment.MachineName))
                    {
                        ExTraceGlobals.TcpClientTracer.TraceDebug(0L, "Ignoring mutual auth since we are local");
                    }
                    else
                    {
                        flag = true;
                    }
                }
                if (!flag && encrypt && !negotiateStream.IsEncrypted)
                {
                    ExTraceGlobals.TcpClientTracer.TraceError(0L, "Encryption requested, but could not be negotiated");
                    flag = true;
                }
                if (flag)
                {
                    ExTraceGlobals.TcpClientTracer.TraceError <bool, bool, bool>(0L, "Security Negotiation failed. Auth={0},MAuth={1},Encrypt={2}", negotiateStream.IsAuthenticated, negotiateStream.IsMutuallyAuthenticated, negotiateStream.IsEncrypted);
                    throw new NetworkCommunicationException(netPath.TargetNodeName, Strings.NetworkSecurityFailed);
                }
                ExTraceGlobals.TcpClientTracer.TraceDebug <long, bool, ProtectionLevel>(0L, "Authenticated Connection took {0}ms. Encrypt={1} ProtRequested={2}", stopwatch.ElapsedMilliseconds, negotiateStream.IsEncrypted, protectionLevel);
                channel = new TcpClientChannel(netPath.TargetNodeName, socket, negotiateStream, timeoutInMs);
                return(true);
            }
            catch (SocketException ex2)
            {
                ex = ex2;
            }
            catch (IOException ex3)
            {
                ex = ex3;
            }
            catch (AuthenticationException ex4)
            {
                ex = ex4;
            }
            catch (NetworkTransportException ex5)
            {
                ex = ex5;
            }
            finally
            {
                if (channel == null)
                {
                    if (negotiateStream != null)
                    {
                        negotiateStream.Dispose();
                    }
                    else if (stream != null)
                    {
                        stream.Dispose();
                    }
                    if (socket != null)
                    {
                        socket.Close();
                    }
                }
                else
                {
                    ReplayCrimsonEvents.NetworkConnectionSuccess.Log <string, IPEndPoint, IPEndPoint>(netPath.TargetNodeName, netPath.TargetEndPoint, channel.LocalEndpoint);
                }
            }
            ExTraceGlobals.TcpClientTracer.TraceError <Exception>(0L, "TryOpenChannel failed. Ex={0}", ex);
            ReplayCrimsonEvents.NetworkConnectionFailure.Log <string, IPEndPoint, IPEndPoint, string>(netPath.TargetNodeName, netPath.TargetEndPoint, netPath.SourceEndPoint, ex.ToString());
            if (ex is NetworkTransportException)
            {
                networkEx = (NetworkTransportException)ex;
            }
            else
            {
                networkEx = new NetworkCommunicationException(netPath.TargetNodeName, ex.Message, ex);
            }
            return(false);
        }