Skip to content

Commit

Permalink
refactor: do not resolve ip addresses
Browse files Browse the repository at this point in the history
Make sure that if a remote IP address is specified, do not DNS resolve it.
  • Loading branch information
FantasticFiasco committed Sep 10, 2018
1 parent 73bbc75 commit 99a2dea
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 16 deletions.
17 changes: 17 additions & 0 deletions src/Serilog.Sinks.Udp/Sinks/Udp/Private/IUdpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Net;
using System.Threading.Tasks;

namespace Serilog.Sinks.Udp.Private
Expand All @@ -21,6 +22,22 @@ namespace Serilog.Sinks.Udp.Private
/// </summary>
public interface IUdpClient
{
/// <summary>
/// Sends a UDP datagram asynchronously to a remote host.
/// </summary>
/// <param name="datagram">
/// An array of type <see cref="byte"/> that specifies the UDP datagram that you intend to
/// send represented as an array of bytes.
/// </param>
/// <param name="bytes">
/// The number of bytes in the datagram.
/// </param>
/// <param name="endPoint">
/// An <see cref="IPEndPoint"/> that represents the host and port to which to send the datagram.
/// </param>
/// <returns></returns>
Task<int> SendAsync(byte[] datagram, int bytes, IPEndPoint endPoint);

/// <summary>
/// Sends a UDP datagram asynchronously to a remote host.
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions src/Serilog.Sinks.Udp/Sinks/Udp/Private/RemoteEndPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Net;

namespace Serilog.Sinks.Udp.Private
{
internal class RemoteEndPoint
{
public RemoteEndPoint(string address, int port)
{
if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort) throw new ArgumentOutOfRangeException(nameof(port));

Address = address ?? throw new ArgumentNullException(nameof(address));
Port = port;

if (IPAddress.TryParse(address, out var ipAddress))
{
IPEndPoint = new IPEndPoint(ipAddress, port);
}
}

public string Address { get; }

public int Port { get; }

public IPEndPoint IPEndPoint { get; }
}
}
5 changes: 5 additions & 0 deletions src/Serilog.Sinks.Udp/Sinks/Udp/Private/UdpClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public UdpClientWrapper(int localPort)
: new UdpClient(localPort, AddressFamily.InterNetwork);
}

public Task<int> SendAsync(byte[] datagram, int bytes, IPEndPoint endPoint)
{
return client.SendAsync(datagram, bytes, endPoint);
}

public Task<int> SendAsync(byte[] datagram, int bytes, string hostname, int port)
{
return client.SendAsync(datagram, bytes, hostname, port);
Expand Down
24 changes: 14 additions & 10 deletions src/Serilog.Sinks.Udp/Sinks/Udp/Private/UdpSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Serilog.Debugging;
Expand All @@ -31,8 +30,7 @@ namespace Serilog.Sinks.Udp.Private
internal class UdpSink : PeriodicBatchingSink
{
private readonly IUdpClient client;
private readonly string remoteAddress;
private readonly int remotePort;
private readonly RemoteEndPoint remoteEndPoint;
private readonly ITextFormatter formatter;

public UdpSink(
Expand All @@ -42,11 +40,8 @@ public UdpSink(
ITextFormatter formatter)
: base(1000, TimeSpan.FromSeconds(0.5))
{
if (remotePort < IPEndPoint.MinPort || remotePort > IPEndPoint.MaxPort) throw new ArgumentOutOfRangeException(nameof(remotePort));

this.client = client ?? throw new ArgumentNullException(nameof(client));
this.remoteAddress = remoteAddress ?? throw new ArgumentNullException(nameof(remoteAddress));
this.remotePort = remotePort;
remoteEndPoint = new RemoteEndPoint(remoteAddress, remotePort);
this.formatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
}

Expand All @@ -69,9 +64,18 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
.Trim()
.ToCharArray());

await client
.SendAsync(buffer, buffer.Length, remoteAddress, remotePort)
.ConfigureAwait(false);
if (remoteEndPoint.IPEndPoint != null)
{
await client
.SendAsync(buffer, buffer.Length, remoteEndPoint.IPEndPoint)
.ConfigureAwait(false);
}
else
{
await client
.SendAsync(buffer, buffer.Length, remoteEndPoint.Address, remoteEndPoint.Port)
.ConfigureAwait(false);
}
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using Serilog.Core;
using Serilog.Core;
using Serilog.Support;

namespace Serilog
Expand All @@ -10,7 +9,7 @@ public class OutputTemplateGivenCodeConfigurationShould : SinkFixture

public OutputTemplateGivenCodeConfigurationShould()
{
var remoteAddress = IPAddress.Loopback.ToString();
var remoteAddress = "localhost";
var remotePort = 7071;

RemoteAddress = remoteAddress;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using Serilog.Core;
using Serilog.Core;
using Serilog.Support.TextFormatters;

namespace Serilog
Expand All @@ -8,7 +7,7 @@ public class TextFormatterGivenCodeConfigurationShould : SinkFixture
{
public TextFormatterGivenCodeConfigurationShould()
{
var remoteAddress = IPAddress.Loopback.ToString();
var remoteAddress = "localhost";
var remotePort = 7071;

RemoteAddress = remoteAddress;
Expand Down

0 comments on commit 99a2dea

Please sign in to comment.