diff options
Diffstat (limited to 'qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking')
5 files changed, 454 insertions, 0 deletions
diff --git a/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/BlockingSocketTransport.cs b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/BlockingSocketTransport.cs new file mode 100644 index 0000000000..f336d8a80a --- /dev/null +++ b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/BlockingSocketTransport.cs @@ -0,0 +1,150 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; +using System.Collections; +using System.IO; +using System.Threading; +using Apache.Qpid.Client.Qms; +using Apache.Qpid.Client.Protocol; +using Apache.Qpid.Codec; +using Apache.Qpid.Framing; + +namespace Apache.Qpid.Client.Transport.Socket.Blocking +{ + /// <summary> + /// TCP Socket transport supporting both + /// SSL and non-SSL connections. + /// </summary> + public class BlockingSocketTransport : ITransport + { + // Configuration variables. + IProtocolListener _protocolListener; + + // Runtime variables. + private ISocketConnector _connector; + private IoHandler _ioHandler; + private AmqpChannel _amqpChannel; + private ManualResetEvent _stopEvent; + + public IProtocolWriter ProtocolWriter + { + get { return _amqpChannel; } + } + public string LocalEndpoint + { + get { return _connector.LocalEndpoint; } + } + + + /// <summary> + /// Connect to the specified broker + /// </summary> + /// <param name="broker">The broker to connect to</param> + /// <param name="connection">The AMQ connection</param> + public void Connect(IBrokerInfo broker, AMQConnection connection) + { + _stopEvent = new ManualResetEvent(false); + _protocolListener = connection.ProtocolListener; + + _ioHandler = MakeBrokerConnection(broker, connection); + // todo: get default read size from config! + + IProtocolDecoderOutput decoderOutput = + new ProtocolDecoderOutput(_protocolListener); + _amqpChannel = + new AmqpChannel(new ByteChannel(_ioHandler), decoderOutput); + + // post an initial async read + _amqpChannel.BeginRead(new AsyncCallback(OnAsyncReadDone), this); + } + + /// <summary> + /// Close the broker connection + /// </summary> + public void Close() + { + StopReading(); + CloseBrokerConnection(); + } + + private void StopReading() + { + _stopEvent.Set(); + } + + private void CloseBrokerConnection() + { + if ( _ioHandler != null ) + { + _ioHandler.Dispose(); + _ioHandler = null; + } + if ( _connector != null ) + { + _connector.Dispose(); + _connector = null; + } + } + + private IoHandler MakeBrokerConnection(IBrokerInfo broker, AMQConnection connection) + { + if ( broker.UseSSL ) + { + _connector = new SslSocketConnector(); + } else + { + _connector = new SocketConnector(); + } + + Stream stream = _connector.Connect(broker); + return new IoHandler(stream, connection.ProtocolListener); + } + + private void OnAsyncReadDone(IAsyncResult result) + { + try + { + _amqpChannel.EndRead(result); + + bool stopping = _stopEvent.WaitOne(0, false); + if ( !stopping ) + _amqpChannel.BeginRead(new AsyncCallback(OnAsyncReadDone), null); + } catch ( Exception e ) + { + // ignore any errors during closing + bool stopping = _stopEvent.WaitOne(0, false); + if ( !stopping ) + _protocolListener.OnException(e); + } + } + + #region IProtocolDecoderOutput Members + + public void Write(object message) + { + _protocolListener.OnMessage((IDataBlock)message); + } + + #endregion + } +} + + diff --git a/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/ByteChannel.cs b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/ByteChannel.cs new file mode 100644 index 0000000000..4540f01f4e --- /dev/null +++ b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/ByteChannel.cs @@ -0,0 +1,92 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; +using log4net; +using Apache.Qpid.Buffer; + +namespace Apache.Qpid.Client.Transport.Socket.Blocking +{ + class ByteChannel : IByteChannel + { + // Warning: don't use this log for regular logging. + private static readonly ILog _ioTraceLog = LogManager.GetLogger("TRACE.Qpid.Client.ByteChannel"); + + private IByteChannel _lowerChannel; + + public ByteChannel(IByteChannel lowerChannel) + { + _lowerChannel = lowerChannel; + } + + public ByteBuffer Read() + { + ByteBuffer result = _lowerChannel.Read(); + + // TODO: Move into decorator. + if (_ioTraceLog.IsDebugEnabled) + { + _ioTraceLog.Debug(String.Format("READ {0}", result)); + } + + return result; + } + + public IAsyncResult BeginRead(AsyncCallback callback, object state) + { + return _lowerChannel.BeginRead(callback, state); + } + + public ByteBuffer EndRead(IAsyncResult result) + { + ByteBuffer buffer = _lowerChannel.EndRead(result); + if ( _ioTraceLog.IsDebugEnabled ) + { + _ioTraceLog.Debug(String.Format("READ {0}", buffer)); + } + return buffer; + } + + public void Write(ByteBuffer buffer) + { + // TODO: Move into decorator. + if (_ioTraceLog.IsDebugEnabled) + { + _ioTraceLog.Debug(String.Format("WRITE {0}", buffer)); + } + + _lowerChannel.Write(buffer); + } + + public IAsyncResult BeginWrite(ByteBuffer buffer, AsyncCallback callback, object state) + { + if ( _ioTraceLog.IsDebugEnabled ) + { + _ioTraceLog.Debug(String.Format("WRITE {0}", buffer)); + } + return _lowerChannel.BeginWrite(buffer, callback, state); + } + + public void EndWrite(IAsyncResult result) + { + _lowerChannel.EndWrite(result); + } + } +} diff --git a/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/ISocketConnector.cs b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/ISocketConnector.cs new file mode 100644 index 0000000000..137fa19c0d --- /dev/null +++ b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/ISocketConnector.cs @@ -0,0 +1,34 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; +using System.IO; +using Apache.Qpid.Client.Qms; + +namespace Apache.Qpid.Client.Transport.Socket.Blocking +{ + interface ISocketConnector : IDisposable + { + string LocalEndpoint { get; } + Stream Connect(IBrokerInfo broker); + } +} + + diff --git a/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/SocketConnector.cs b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/SocketConnector.cs new file mode 100644 index 0000000000..b6dd8c3be1 --- /dev/null +++ b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/SocketConnector.cs @@ -0,0 +1,71 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System.IO; +using System.Net; +using System.Net.Sockets; +using Apache.Qpid.Client.Qms; + +namespace Apache.Qpid.Client.Transport.Socket.Blocking +{ + /// <summary> + /// Implements a TCP connection over regular sockets. + /// </summary> + class SocketConnector : ISocketConnector + { + private MyTcpClient _tcpClient; + + public string LocalEndpoint + { + get { return _tcpClient.LocalEndpoint.ToString(); } + } + + public Stream Connect(IBrokerInfo broker) + { + _tcpClient = new MyTcpClient(broker.Host, broker.Port); + return _tcpClient.GetStream(); + } + + public void Dispose() + { + if ( _tcpClient != null ) + { + _tcpClient.Close(); + _tcpClient = null; + } + } + + class MyTcpClient : TcpClient + { + public MyTcpClient(string host, int port) + : base(host, port) + { + } + + public EndPoint LocalEndpoint + { + get { return Client.LocalEndPoint; } + } + } + + } +} + + diff --git a/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/SslSocketConnector.cs b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/SslSocketConnector.cs new file mode 100644 index 0000000000..8436e6fc4f --- /dev/null +++ b/qpid/dotnet/Qpid.Client/Client/Transport/Socket/Blocking/SslSocketConnector.cs @@ -0,0 +1,107 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System.IO; +using System.Net; +using log4net; +using Apache.Qpid.Client.Qms; +using Org.Mentalis.Security.Ssl; +using MCertificate = Org.Mentalis.Security.Certificates.Certificate; +using MCertificateChain = Org.Mentalis.Security.Certificates.CertificateChain; + +namespace Apache.Qpid.Client.Transport.Socket.Blocking +{ + /// <summary> + /// Implements a TLS v1.0 connection using the Mentalis.org library + /// </summary> + /// <remarks> + /// It would've been easier to implement this at the StreamFilter + /// level, but unfortunately the Mentalis library doesn't support + /// a passthrough SSL stream class and is tied directly + /// to socket-like classes. + /// </remarks> + class SslSocketConnector : ISocketConnector + { + private static ILog _logger = LogManager.GetLogger(typeof(SslSocketConnector)); + private MyTcpClient _tcpClient; + + public string LocalEndpoint + { + get { return _tcpClient.LocalEndpoint.ToString(); } + } + + public Stream Connect(IBrokerInfo broker) + { + MCertificate cert = GetClientCert(broker); + SecurityOptions options = new SecurityOptions( + SecureProtocol.Tls1, cert, ConnectionEnd.Client + ); + if ( broker.SslOptions != null + && broker.SslOptions.IgnoreValidationErrors ) + { + _logger.Warn("Ignoring any certificate validation errors during SSL handshake..."); + options.VerificationType = CredentialVerification.None; + } + + _tcpClient = new MyTcpClient(broker.Host, broker.Port, options); + return _tcpClient.GetStream(); + } + + public void Dispose() + { + if ( _tcpClient != null ) + { + _tcpClient.Close(); + _tcpClient = null; + } + } + + private static MCertificate GetClientCert(IBrokerInfo broker) + { + // if a client certificate is configured, + // use that to enable mutual authentication + MCertificate cert = null; + if ( broker.SslOptions != null + && broker.SslOptions.ClientCertificate != null ) + { + cert = MCertificate.CreateFromX509Certificate( + broker.SslOptions.ClientCertificate + ); + _logger.DebugFormat("Using Client Certificate for SSL '{0}'", cert.ToString(true)); + } + return cert; + } + + class MyTcpClient : SecureTcpClient + { + public MyTcpClient(string host, int port, SecurityOptions options) + : base(host, port, options) + { + } + + public EndPoint LocalEndpoint + { + get { return Client.LocalEndPoint; } + } + + } + + } +} |