From e0284886b5ea5cc4c6dd2f38ff6fd256e055b687 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Thu, 14 Dec 2017 13:23:12 +0100 Subject: [PATCH] Added the bound IP address to the server options. --- .../Implementations/MqttServerAdapter.cs | 4 +-- .../Server/IMqttServerOptions.cs | 12 ++++--- .../MqttServerDefaultEndpointOptions.cs | 6 +++- .../Server/MqttServerOptionsBuilder.cs | 33 +++++++++++++++---- .../Server/MqttServerTlsEndpointOptions.cs | 6 +++- 5 files changed, 46 insertions(+), 15 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs index c991c23..b4869ff 100644 --- a/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs @@ -39,7 +39,7 @@ namespace MQTTnet.Implementations if (options.DefaultEndpointOptions.IsEnabled) { _defaultEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); - _defaultEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetDefaultEndpointPort())); + _defaultEndpointSocket.Bind(new IPEndPoint(options.DefaultEndpointOptions.BoundIPAddress, options.GetDefaultEndpointPort())); _defaultEndpointSocket.Listen(options.ConnectionBacklog); Task.Run(async () => await AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token).ConfigureAwait(false), _cancellationTokenSource.Token).ConfigureAwait(false); @@ -59,7 +59,7 @@ namespace MQTTnet.Implementations } _tlsEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); - _tlsEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetTlsEndpointPort())); + _tlsEndpointSocket.Bind(new IPEndPoint(options.TlsEndpointOptions.BoundIPAddress, options.GetTlsEndpointPort())); _tlsEndpointSocket.Listen(options.ConnectionBacklog); Task.Run(async () => await AcceptTlsEndpointConnectionsAsync(_cancellationTokenSource.Token).ConfigureAwait(false), _cancellationTokenSource.Token).ConfigureAwait(false); diff --git a/Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs b/Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs index 92c1bfb..d89d915 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/IMqttServerOptions.cs @@ -1,16 +1,20 @@ using System; +using System.Net; namespace MQTTnet.Server { public interface IMqttServerOptions { - Action ApplicationMessageInterceptor { get; } int ConnectionBacklog { get; } - Action ConnectionValidator { get; } TimeSpan DefaultCommunicationTimeout { get; } - MqttServerDefaultEndpointOptions DefaultEndpointOptions { get; } - IMqttServerStorage Storage { get; } + + Action ConnectionValidator { get; } Action SubscriptionInterceptor { get; } + Action ApplicationMessageInterceptor { get; } + + MqttServerDefaultEndpointOptions DefaultEndpointOptions { get; } MqttServerTlsEndpointOptions TlsEndpointOptions { get; } + + IMqttServerStorage Storage { get; } } } \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetStandard/Server/MqttServerDefaultEndpointOptions.cs b/Frameworks/MQTTnet.NetStandard/Server/MqttServerDefaultEndpointOptions.cs index 6dc21b3..545499b 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/MqttServerDefaultEndpointOptions.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/MqttServerDefaultEndpointOptions.cs @@ -1,9 +1,13 @@ -namespace MQTTnet.Server +using System.Net; + +namespace MQTTnet.Server { public sealed class MqttServerDefaultEndpointOptions { public bool IsEnabled { get; set; } = true; public int? Port { get; set; } + + public IPAddress BoundIPAddress { get; set; } = IPAddress.Any; } } diff --git a/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs b/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs index 34f36a9..18b2de2 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/MqttServerOptionsBuilder.cs @@ -1,4 +1,5 @@ using System; +using System.Net; namespace MQTTnet.Server { @@ -18,33 +19,45 @@ namespace MQTTnet.Server return this; } - public MqttServerOptionsBuilder WithDefaultEndpointPort(int value) + public MqttServerOptionsBuilder WithDefaultEndpoint() { - _options.DefaultEndpointOptions.Port = value; + _options.DefaultEndpointOptions.IsEnabled = true; return this; } - public MqttServerOptionsBuilder WithDefaultEndpoint() + public MqttServerOptionsBuilder WithDefaultEndpointPort(int? value) { - _options.DefaultEndpointOptions.IsEnabled = true; + _options.DefaultEndpointOptions.Port = value; return this; } + public MqttServerOptionsBuilder WithDefaultEndpointBoundIPAddress(IPAddress value) + { + _options.DefaultEndpointOptions.BoundIPAddress = value ?? IPAddress.Any; + return this; + } + public MqttServerOptionsBuilder WithoutDefaultEndpoint() { _options.DefaultEndpointOptions.IsEnabled = false; return this; } - + public MqttServerOptionsBuilder WithEncryptedEndpoint() { _options.TlsEndpointOptions.IsEnabled = true; return this; } - public MqttServerOptionsBuilder WithoutEncryptedEndpoint() + public MqttServerOptionsBuilder WithEncryptedEndpointPort(int? value) { - _options.TlsEndpointOptions.IsEnabled = false; + _options.TlsEndpointOptions.Port = value; + return this; + } + + public MqttServerOptionsBuilder WithEncryptedEndpointBoundIPAddress(IPAddress value) + { + _options.TlsEndpointOptions.BoundIPAddress = value; return this; } @@ -54,6 +67,12 @@ namespace MQTTnet.Server return this; } + public MqttServerOptionsBuilder WithoutEncryptedEndpoint() + { + _options.TlsEndpointOptions.IsEnabled = false; + return this; + } + public MqttServerOptionsBuilder WithStorage(IMqttServerStorage value) { _options.Storage = value; diff --git a/Frameworks/MQTTnet.NetStandard/Server/MqttServerTlsEndpointOptions.cs b/Frameworks/MQTTnet.NetStandard/Server/MqttServerTlsEndpointOptions.cs index 6c90a44..73be93c 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/MqttServerTlsEndpointOptions.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/MqttServerTlsEndpointOptions.cs @@ -1,4 +1,6 @@ -namespace MQTTnet.Server +using System.Net; + +namespace MQTTnet.Server { public sealed class MqttServerTlsEndpointOptions { @@ -7,5 +9,7 @@ public int? Port { get; set; } public byte[] Certificate { get; set; } + + public IPAddress BoundIPAddress { get; set; } = IPAddress.Any; } }