Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture Remote IP Address Endpoint on Server Exceptions #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions DNS/Server/DnsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,18 @@ 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;
}
}

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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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;

Expand All @@ -153,7 +154,7 @@ await udp
}
catch (SocketException) {}
catch (OperationCanceledException) {}
finally { OnError(e); }
finally { OnError(e, remote); }
}
}

Expand Down Expand Up @@ -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 {
Expand Down