Exemple #1
0
        AsyncOperationStatus ProcessClose(AsyncProtocolRequest asyncRequest, AsyncOperationStatus status)
        {
            Debug("ProcessClose: {0}", status);

            lock (ioLock) {
                if (xobileTlsContext == null)
                {
                    return(AsyncOperationStatus.Complete);
                }

                xobileTlsContext.Close();
                xobileTlsContext = null;
                return(AsyncOperationStatus.Continue);
            }
        }
Exemple #2
0
        int InternalRead(AsyncProtocolRequest asyncRequest, BufferOffsetSize internalBuffer, byte[] buffer, int offset, int size, out bool wantMore)
        {
            if (asyncRequest == null)
            {
                throw new InvalidOperationException();
            }

            Debug("InternalRead: {0} {1} {2}", internalBuffer, offset, size);

            /*
             * One of Apple's native functions wants to read 'size' bytes of data.
             *
             * First, we check whether we already have enough in the internal buffer.
             *
             * If the internal buffer is empty (it will be the first time we're called), we save
             * the amount of bytes that were requested and return 'SslStatus.WouldBlock' to our
             * native caller.  This native function will then return this code to managed code,
             * where we read the requested amount of data into the internal buffer, then call the
             * native function again.
             */
            if (internalBuffer.Size == 0 && !internalBuffer.Complete)
            {
                Debug("InternalRead #1: {0} {1}", internalBuffer.Offset, internalBuffer.TotalBytes);
                internalBuffer.Offset = internalBuffer.Size = 0;
                asyncRequest.RequestRead(size);
                wantMore = true;
                return(0);
            }

            /*
             * The second time we're called, the native buffer will contain the exact amount of data that the
             * previous call requested from us, so we should be able to return it all here.  However, just in
             * case that Apple's native function changed its mind, we can also return less.
             *
             * In either case, if we have any data buffered, then we return as much of it as possible - if the
             * native code isn't satisfied, then it will call us again to request more.
             */
            var len = Math.Min(internalBuffer.Size, size);

            Buffer.BlockCopy(internalBuffer.Buffer, internalBuffer.Offset, buffer, offset, len);
            internalBuffer.Offset += len;
            internalBuffer.Size   -= len;
            wantMore = !internalBuffer.Complete && len < size;
            return(len);
        }
Exemple #3
0
        public override void Close()
        {
            /*
             * SSLClose() is a little bit tricky as it might attempt to send a close_notify alert
             * and thus call our write callback.
             *
             * It is also not thread-safe with SSLRead() or SSLWrite(), so we need to take the I/O lock here.
             */
            if (Interlocked.Exchange(ref closeRequested, 1) == 1)
            {
                return;
            }
            if (xobileTlsContext == null)
            {
                return;
            }

            var asyncRequest = new AsyncProtocolRequest(this, null);

            StartOperation(ref asyncWriteRequest, ref writeBuffer, ProcessClose, asyncRequest, "close");
        }
Exemple #4
0
        AsyncOperationStatus ProcessHandshake(AsyncProtocolRequest asyncRequest, AsyncOperationStatus status)
        {
            Debug("ProcessHandshake: {0}", status);

            /*
             * The first time we're called (AsyncOperationStatus.Initialize), we need to setup the SslContext and
             * start the handshake.
             */
            if (status == AsyncOperationStatus.Initialize)
            {
                xobileTlsContext.StartHandshake();
                return(AsyncOperationStatus.Continue);
            }
            else if (status == AsyncOperationStatus.ReadDone)
            {
                // remote prematurely closed connection.
                throw new IOException("Remote prematurely closed connection.");
            }
            else if (status != AsyncOperationStatus.Continue)
            {
                throw new InvalidOperationException();
            }

            /*
             * SSLHandshake() will return repeatedly with 'SslStatus.WouldBlock', we then need
             * to take care of I/O and call it again.
             */
            if (!xobileTlsContext.ProcessHandshake())
            {
                /*
                 * Flush the internal write buffer.
                 */
                InnerFlush();
                return(AsyncOperationStatus.Continue);
            }

            xobileTlsContext.FinishHandshake();
            return(AsyncOperationStatus.Complete);
        }
Exemple #5
0
        AsyncOperationStatus ProcessWrite(AsyncProtocolRequest asyncRequest, AsyncOperationStatus status)
        {
            Debug("ProcessWrite - write user: {0} {1}", status, asyncRequest.UserBuffer);

            if (asyncRequest.UserBuffer.Size == 0)
            {
                asyncRequest.UserResult = asyncRequest.CurrentSize;
                return(AsyncOperationStatus.Complete);
            }

            int  ret;
            bool wantMore;

            lock (ioLock) {
                ret = Context.Write(asyncRequest.UserBuffer.Buffer, asyncRequest.UserBuffer.Offset, asyncRequest.UserBuffer.Size, out wantMore);
            }
            Debug("ProcessWrite - write user done: {0} - {1} {2}", asyncRequest.UserBuffer, ret, wantMore);

            if (ret < 0)
            {
                asyncRequest.UserResult = -1;
                return(AsyncOperationStatus.Complete);
            }

            asyncRequest.CurrentSize       += ret;
            asyncRequest.UserBuffer.Offset += ret;
            asyncRequest.UserBuffer.Size   -= ret;

            if (wantMore || writeBuffer.Size > 0)
            {
                return(AsyncOperationStatus.WantWrite);
            }

            asyncRequest.UserResult = asyncRequest.CurrentSize;
            return(AsyncOperationStatus.Complete);
        }
Exemple #6
0
        internal void ProcessAuthentication(LazyAsyncResult lazyResult)
        {
            var asyncRequest = new AsyncProtocolRequest(this, lazyResult);

            if (Interlocked.CompareExchange(ref asyncHandshakeRequest, asyncRequest, null) != null)
            {
                throw new InvalidOperationException("Invalid nested call.");
            }

            try {
                if (lastException != null)
                {
                    throw lastException;
                }
                if (xobileTlsContext == null)
                {
                    throw new InvalidOperationException();
                }

                readBuffer.Reset();
                writeBuffer.Reset();

                try {
                    asyncRequest.StartOperation(ProcessHandshake);
                } catch (Exception ex) {
                    throw SetException(ex);
                }
            } finally {
                if (lazyResult == null || lastException != null)
                {
                    readBuffer.Reset();
                    writeBuffer.Reset();
                    asyncHandshakeRequest = null;
                }
            }
        }
Exemple #7
0
 AsyncOperationStatus ProcessFlush(AsyncProtocolRequest asyncRequest, AsyncOperationStatus status)
 {
     Debug("ProcessFlush: {0}", status);
     return(AsyncOperationStatus.Complete);
 }
Exemple #8
0
        int StartOperation(ref AsyncProtocolRequest nestedRequest, ref BufferOffsetSize2 internalBuffer, AsyncOperation operation, AsyncProtocolRequest asyncRequest, string name)
        {
            if (Interlocked.CompareExchange(ref nestedRequest, asyncRequest, null) != null)
            {
                throw new InvalidOperationException("Invalid nested call.");
            }

            bool failed = false;

            try {
                internalBuffer.Reset();
                asyncRequest.StartOperation(operation);
                return(asyncRequest.UserResult);
            } catch (Exception e) {
                failed = true;
                if (e is IOException)
                {
                    throw;
                }
                throw new IOException(name + " failed", e);
            } finally {
                if (asyncRequest.UserAsyncResult == null || failed)
                {
                    internalBuffer.Reset();
                    nestedRequest = null;
                }
            }
        }
        int InternalWrite(AsyncProtocolRequest asyncRequest, BufferOffsetSize internalBuffer, byte[] buffer, int offset, int size, out bool wantMore)
        {
            if (asyncRequest == null)
            {
                /*
                 * The only situation where 'asyncRequest' could possibly be 'null' is when we're called
                 * from within SSLClose() - which might attempt to send the close_notity notification.
                 * Since this notification message is very small, it should definitely fit into our internal
                 * buffer, so we just save it in there and after SSLClose() returns, the final call to
                 * InternalFlush() - just before closing the underlying stream - will send it out.
                 */
                if (lastException != null)
                {
                    wantMore = false;
                    return(-1);
                }

                if (Interlocked.Exchange(ref closeRequested, 1) == 0)
                {
                    internalBuffer.Reset();
                }
                else if (internalBuffer.Remaining == 0)
                {
                    throw new InvalidOperationException();
                }
            }

            /*
             * Normal write - can be either SSLWrite() or SSLHandshake().
             *
             * Copy as much of the data into the internal buffer and only return SslStatus.WouldBlock when
             * it's full.
             *
             */
            if (internalBuffer.Remaining == 0)
            {
                // Internal buffer is full, so we must actually write all the data now.
                asyncRequest.RequestWrite();
                wantMore = true;
                return(0);
            }

            var len = Math.Min(internalBuffer.Remaining, size);

            Buffer.BlockCopy(buffer, offset, internalBuffer.Buffer, internalBuffer.EndOffset, len);
            internalBuffer.Size += len;
            wantMore             = len < size;

            /*
             * Calling 'asyncRequest.RequestWrite()' here ensures that ProcessWrite() is called next
             * time we regain control from native code.
             *
             * During the handshake, the native code won't actually realize (unless if attempts to send
             * so much that the write buffer gets full) that we only buffered the data.
             *
             * However, it doesn't matter because it will either return with a completed handshake
             * (and doesn't care whether the remote actually received the data) or it will expect more
             * data from the remote and request a read.  In either case, we regain control in managed
             * code and can flush out the data.
             *
             * Note that a calling RequestWrite() followed by RequestRead() will first flush the write
             * queue once we return to managed code - before attempting to read anything.
             */
            if (asyncRequest != null)
            {
                asyncRequest.RequestWrite();
            }

            return(len);
        }