| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.IO.Pipelines; |
| | | 7 | | using System.Net.Sockets; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels.Framing |
| | | 12 | | { |
| | | 13 | | internal class UnixDomainSocketExceptionConvertingDuplexPipe : IDuplexPipe |
| | | 14 | | { |
| | 0 | 15 | | public UnixDomainSocketExceptionConvertingDuplexPipe(IDuplexPipe innerDuplexPipe) |
| | | 16 | | { |
| | 0 | 17 | | Input = new UnixDomainSocketExceptionConvertingPipeReader(innerDuplexPipe.Input); |
| | 0 | 18 | | Output = new UnixDomainSocketExceptionConvertingPipeWriter(innerDuplexPipe.Output); |
| | 0 | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | public PipeReader Input { get; } |
| | 0 | 22 | | public PipeWriter Output { get; } |
| | | 23 | | |
| | | 24 | | private class UnixDomainSocketExceptionConvertingPipeReader : PipeReader |
| | | 25 | | { |
| | | 26 | | private PipeReader _input; |
| | | 27 | | |
| | 0 | 28 | | public UnixDomainSocketExceptionConvertingPipeReader(PipeReader input) => _input = input; |
| | | 29 | | |
| | 0 | 30 | | public override void AdvanceTo(SequencePosition consumed) => _input.AdvanceTo(consumed); |
| | 0 | 31 | | public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) => _input.AdvanceTo(con |
| | 0 | 32 | | public override void CancelPendingRead() => _input.CancelPendingRead(); |
| | 0 | 33 | | public override void Complete(Exception exception = null) => _input.Complete(exception); |
| | | 34 | | public override async ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | | 36 | | try |
| | | 37 | | { |
| | 0 | 38 | | return await _input.ReadAsync(cancellationToken); |
| | | 39 | | } |
| | 0 | 40 | | catch(SocketException socketException) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelper( |
| | 0 | 43 | | ConvertReceiveException(socketException), TraceEventType.Error); |
| | | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | public override bool TryRead(out ReadResult result) => _input.TryRead(out result); |
| | | 48 | | |
| | | 49 | | private Exception ConvertReceiveException(SocketException socketException) |
| | | 50 | | { |
| | 0 | 51 | | return ConvertTransferException(socketException, socketException, aborted: false); |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | private class UnixDomainSocketExceptionConvertingPipeWriter : PipeWriter |
| | | 56 | | { |
| | | 57 | | private PipeWriter _output; |
| | | 58 | | |
| | 0 | 59 | | public UnixDomainSocketExceptionConvertingPipeWriter(PipeWriter output) => _output = output; |
| | | 60 | | |
| | 0 | 61 | | public override void Advance(int bytes) => _output.Advance(bytes); |
| | 0 | 62 | | public override void CancelPendingFlush() => _output.CancelPendingFlush(); |
| | 0 | 63 | | public override void Complete(Exception exception = null) => _output.Complete(exception); |
| | | 64 | | public override async ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default) |
| | | 65 | | { |
| | | 66 | | try |
| | | 67 | | { |
| | 0 | 68 | | return await _output.FlushAsync(cancellationToken); |
| | | 69 | | } |
| | 0 | 70 | | catch (SocketException socketException) |
| | | 71 | | { |
| | 0 | 72 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelper( |
| | 0 | 73 | | ConvertSendException(socketException), TraceEventType.Error); |
| | | 74 | | } |
| | 0 | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | public override Memory<byte> GetMemory(int sizeHint = 0) => _output.GetMemory(sizeHint); |
| | 0 | 78 | | public override Span<byte> GetSpan(int sizeHint = 0) => _output.GetSpan(sizeHint); |
| | | 79 | | |
| | | 80 | | private Exception ConvertSendException(SocketException socketException) |
| | | 81 | | { |
| | 0 | 82 | | return ConvertTransferException(socketException, socketException, aborted: false); |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static Exception ConvertTransferException(SocketException socketException, Exception originalException, |
| | | 87 | | { |
| | 0 | 88 | | if (socketException.ErrorCode == UnsafeNativeMethods.ERROR_INVALID_HANDLE) |
| | | 89 | | { |
| | 0 | 90 | | return new CommunicationObjectAbortedException(socketException.Message, socketException); |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | if (socketException.ErrorCode == UnsafeNativeMethods.WSAENETRESET || |
| | 0 | 94 | | socketException.ErrorCode == UnsafeNativeMethods.WSAECONNABORTED || |
| | 0 | 95 | | socketException.ErrorCode == UnsafeNativeMethods.WSAECONNRESET) |
| | | 96 | | { |
| | 0 | 97 | | if (aborted) |
| | | 98 | | { |
| | 0 | 99 | | return new CommunicationObjectAbortedException(SR.TcpLocalConnectionAborted, originalException); |
| | | 100 | | } |
| | | 101 | | else |
| | | 102 | | { |
| | 0 | 103 | | CommunicationException communicationException = new CommunicationException(SR.Format(SR.TcpConnectio |
| | | 104 | | //if (TD.TcpConnectionResetErrorIsEnabled()) |
| | | 105 | | //{ |
| | | 106 | | // if (socketConnection != null) |
| | | 107 | | // { |
| | | 108 | | // int socketId = (socketConnection.socket != null) ? socketConnection.socket.GetHashCode() : |
| | | 109 | | // TD.TcpConnectionResetError(socketId, socketConnection.RemoteEndpointAddress); |
| | | 110 | | // } |
| | | 111 | | //} |
| | | 112 | | //if (DiagnosticUtility.ShouldTrace(exceptionEventType)) |
| | | 113 | | //{ |
| | | 114 | | // TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpConnectionResetError, GetEndpointStri |
| | | 115 | | //} |
| | 0 | 116 | | return communicationException; |
| | | 117 | | } |
| | | 118 | | } |
| | 0 | 119 | | else if (socketException.ErrorCode == UnsafeNativeMethods.WSAETIMEDOUT) |
| | | 120 | | { |
| | 0 | 121 | | TimeoutException timeoutException = new TimeoutException(SR.Format(SR.TcpConnectionTimedOut, "unknown"), |
| | | 122 | | //if (DiagnosticUtility.ShouldTrace(exceptionEventType)) |
| | | 123 | | //{ |
| | | 124 | | // TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpConnectionTimedOut, GetEndpointString(SR. |
| | | 125 | | //} |
| | 0 | 126 | | return timeoutException; |
| | | 127 | | } |
| | | 128 | | else |
| | | 129 | | { |
| | 0 | 130 | | if (aborted) |
| | | 131 | | { |
| | 0 | 132 | | return new CommunicationObjectAbortedException(SR.Format(SR.TcpTransferError, socketException.ErrorC |
| | | 133 | | } |
| | | 134 | | else |
| | | 135 | | { |
| | 0 | 136 | | CommunicationException communicationException = new CommunicationException(SR.Format(SR.TcpTransferE |
| | | 137 | | //if (DiagnosticUtility.ShouldTrace(exceptionEventType)) |
| | | 138 | | //{ |
| | | 139 | | // TraceUtility.TraceEvent(exceptionEventType, TraceCode.TcpTransferError, GetEndpointString(SR.T |
| | | 140 | | //} |
| | 0 | 141 | | return communicationException; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | internal static class UnsafeNativeMethods |
| | | 147 | | { |
| | | 148 | | public const int ERROR_SUCCESS = 0; |
| | | 149 | | public const int ERROR_FILE_NOT_FOUND = 2; |
| | | 150 | | public const int ERROR_ACCESS_DENIED = 5; |
| | | 151 | | public const int ERROR_INVALID_HANDLE = 6; |
| | | 152 | | public const int ERROR_NOT_ENOUGH_MEMORY = 8; |
| | | 153 | | public const int ERROR_OUTOFMEMORY = 14; |
| | | 154 | | public const int ERROR_SHARING_VIOLATION = 32; |
| | | 155 | | public const int ERROR_NETNAME_DELETED = 64; |
| | | 156 | | public const int ERROR_INVALID_PARAMETER = 87; |
| | | 157 | | public const int ERROR_BROKEN_PIPE = 109; |
| | | 158 | | public const int ERROR_ALREADY_EXISTS = 183; |
| | | 159 | | public const int ERROR_PIPE_BUSY = 231; |
| | | 160 | | public const int ERROR_NO_DATA = 232; |
| | | 161 | | public const int ERROR_MORE_DATA = 234; |
| | | 162 | | public const int WAIT_TIMEOUT = 258; |
| | | 163 | | public const int ERROR_PIPE_CONNECTED = 535; |
| | | 164 | | public const int ERROR_OPERATION_ABORTED = 995; |
| | | 165 | | public const int ERROR_IO_PENDING = 997; |
| | | 166 | | public const int ERROR_SERVICE_ALREADY_RUNNING = 1056; |
| | | 167 | | public const int ERROR_SERVICE_DISABLED = 1058; |
| | | 168 | | public const int ERROR_NO_TRACKING_SERVICE = 1172; |
| | | 169 | | public const int ERROR_ALLOTTED_SPACE_EXCEEDED = 1344; |
| | | 170 | | public const int ERROR_NO_SYSTEM_RESOURCES = 1450; |
| | | 171 | | |
| | | 172 | | // When querying for the token length |
| | | 173 | | private const int ERROR_INSUFFICIENT_BUFFER = 122; |
| | | 174 | | |
| | | 175 | | public const int STATUS_PENDING = 0x103; |
| | | 176 | | |
| | | 177 | | // socket errors |
| | | 178 | | public const int WSAACCESS = 10013; |
| | | 179 | | public const int WSAEMFILE = 10024; |
| | | 180 | | public const int WSAEMSGSIZE = 10040; |
| | | 181 | | public const int WSAEADDRINUSE = 10048; |
| | | 182 | | public const int WSAEADDRNOTAVAIL = 10049; |
| | | 183 | | public const int WSAENETDOWN = 10050; |
| | | 184 | | public const int WSAENETUNREACH = 10051; |
| | | 185 | | public const int WSAENETRESET = 10052; |
| | | 186 | | public const int WSAECONNABORTED = 10053; |
| | | 187 | | public const int WSAECONNRESET = 10054; |
| | | 188 | | public const int WSAENOBUFS = 10055; |
| | | 189 | | public const int WSAESHUTDOWN = 10058; |
| | | 190 | | public const int WSAETIMEDOUT = 10060; |
| | | 191 | | public const int WSAECONNREFUSED = 10061; |
| | | 192 | | public const int WSAEHOSTDOWN = 10064; |
| | | 193 | | public const int WSAEHOSTUNREACH = 10065; |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | } |