From 64c77c37a5d58d6c90ea110ba5d33c10f958ac04 Mon Sep 17 00:00:00 2001 From: malcomvetter Date: Sat, 3 Aug 2019 12:46:37 -0500 Subject: [PATCH] capture IP address on exceptions --- DNS/Server/DnsServer.cs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/DNS/Server/DnsServer.cs b/DNS/Server/DnsServer.cs index 7e6e63f..5ab1921 100644 --- a/DNS/Server/DnsServer.cs +++ b/DNS/Server/DnsServer.cs @@ -65,7 +65,7 @@ public async Task Listen(IPEndPoint endpoint) { udp.Client.IOControl(SIO_UDP_CONNRESET, new byte[4], new byte[4]); } } catch (SocketException e) { - OnError(e); + OnError(e, endpoint); return; } } @@ -73,9 +73,10 @@ public async Task Listen(IPEndPoint endpoint) { AsyncCallback receiveCallback = null; receiveCallback = result => { byte[] data; + IPEndPoint remote = null; try { - IPEndPoint remote = new IPEndPoint(0, 0); + remote = new IPEndPoint(0, 0); data = udp.EndReceive(result, ref remote); HandleRequest(data, remote); } @@ -84,7 +85,7 @@ public async Task Listen(IPEndPoint endpoint) { run = false; } catch (SocketException e) { - OnError(e); + OnError(e, remote); } if (run) udp.BeginReceive(receiveCallback, null); @@ -115,8 +116,8 @@ protected virtual void Dispose(bool disposing) { } } - private void OnError(Exception e) { - OnEvent(Errored, new ErroredEventArgs(e)); + private void OnError(Exception e, IPEndPoint remote) { + OnEvent(Errored, new ErroredEventArgs(e, remote)); } private async void HandleRequest(byte[] data, IPEndPoint remote) { @@ -133,12 +134,12 @@ await udp .SendAsync(response.ToArray(), response.Size, remote) .WithCancellationTimeout(TimeSpan.FromMilliseconds(UDP_TIMEOUT)); } - catch (SocketException e) { OnError(e); } - catch (ArgumentException e) { OnError(e); } - catch (IndexOutOfRangeException e) { OnError(e); } - catch (OperationCanceledException e) { OnError(e); } - catch (IOException e) { OnError(e); } - catch (ObjectDisposedException e) { OnError(e); } + catch (SocketException e) { OnError(e, remote); } + catch (ArgumentException e) { OnError(e, remote); } + catch (IndexOutOfRangeException e) { OnError(e, remote); } + catch (OperationCanceledException e) { OnError(e, remote); } + catch (IOException e) { OnError(e, remote); } + catch (ObjectDisposedException e) { OnError(e, remote); } catch (ResponseException e) { IResponse response = e.Response; @@ -153,7 +154,7 @@ await udp } catch (SocketException) {} catch (OperationCanceledException) {} - finally { OnError(e); } + finally { OnError(e, remote); } } } @@ -210,14 +211,20 @@ public IPEndPoint Remote { } public class ErroredEventArgs : EventArgs { - public ErroredEventArgs(Exception e) { + public ErroredEventArgs(Exception e, IPEndPoint remote) { Exception = e; + Remote = remote; } public Exception Exception { get; private set; } + + public IPEndPoint Remote { + get; + private set; + } } private class FallbackRequestResolver : IRequestResolver {