Esempio n. 1
0
        private void SendAndClose(ref StateObject state, IByteArraySerializable response)
        {
            var handler = state.WorkSocket;

            if (response != null)
            {
                // Convert the string data to byte data using ASCII encoding.
                var byteData = response.ToByteArray();

                // Begin sending the data to the remote device.
                if (handler.Connected)
                {
                    LOG.Debug($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Sending {byteData.Length} bytes and then closing connection.");
                    handler.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendAndCloseCallback), state);
                }
                else
                {
                    LOG.Debug($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Could not send {byteData.Length} bytes and then close connection because no longer connected.");
                }
            }
            else if (handler.Connected)
            {
                LOG.Debug($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Sending nothing and then closing connection.");
                handler.BeginSend(new byte[] { }, 0, 0, SocketFlags.None, new AsyncCallback(SendAndCloseCallback), state);
            }
            else
            {
                LOG.Debug($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Client disconnected before response could be sent and the connection closed.");
                state.Client.State = State.Disconnected;
                _activeConnections.TryRemove(state.Client.RemoteEndpoint, out var junk);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
        }
Esempio n. 2
0
        internal static string EntryAsString(IndexEntry entry, string fileName, string indexName)
        {
            IByteArraySerializable keyValue  = null;
            IByteArraySerializable dataValue = null;

            // Try to guess the type of data in the key and data fields from the filename and index name
            if (indexName == "$I30")
            {
                keyValue  = new FileNameRecord();
                dataValue = new FileRecordReference();
            }
            else if (fileName == "$ObjId" && indexName == "$O")
            {
                keyValue  = new ObjectIds.IndexKey();
                dataValue = new ObjectIdRecord();
            }
            else if (fileName == "$Reparse" && indexName == "$R")
            {
                keyValue  = new ReparsePoints.Key();
                dataValue = new ReparsePoints.Data();
            }
            else if (fileName == "$Quota")
            {
                if (indexName == "$O")
                {
                    keyValue  = new Quotas.OwnerKey();
                    dataValue = new Quotas.OwnerRecord();
                }
                else if (indexName == "$Q")
                {
                    keyValue  = new Quotas.OwnerRecord();
                    dataValue = new Quotas.QuotaRecord();
                }
            }
            else if (fileName == "$Secure")
            {
                if (indexName == "$SII")
                {
                    keyValue  = new SecurityDescriptors.IdIndexKey();
                    dataValue = new SecurityDescriptors.IdIndexData();
                }
                else if (indexName == "$SDH")
                {
                    keyValue  = new SecurityDescriptors.HashIndexKey();
                    dataValue = new SecurityDescriptors.IdIndexData();
                }
            }

            try
            {
                if (keyValue != null && dataValue != null)
                {
                    keyValue.ReadFrom(entry.KeyBuffer, 0);
                    dataValue.ReadFrom(entry.DataBuffer, 0);
                    return("{" + keyValue + "-->" + dataValue + "}");
                }
            }
            catch
            {
                return("{Parsing-Error}");
            }

            return("{Unknown-Index-Type}");
        }
Esempio n. 3
0
 private static BuilderExtent ExtentForStruct(IByteArraySerializable structure, long position)
 {
     byte[] buffer = new byte[structure.Size];
     structure.WriteTo(buffer, 0);
     return new BuilderBufferExtent(position, buffer);
 }
Esempio n. 4
0
 private static BuilderExtent ExtentForStruct(IByteArraySerializable structure, long position)
 {
     byte[] buffer = new byte[structure.Size];
     structure.WriteTo(buffer, 0);
     return(new BuilderBufferExtent(position, buffer));
 }
Esempio n. 5
0
        private void ReadCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            var state   = (StateObject)ar.AsyncState;
            var handler = state.WorkSocket;

            // Read data from the client socket.
            int bytesRead = 0;

            try {
                bytesRead = handler.EndReceive(ar);
            }
            catch (Exception e) {
                LOG.Warn($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Exception caught reading data.", e);
                Send(ref state, new ServerResponseMsg(ServerResponseMsg.ResponseCode.RC_ERROR, Guid.Empty));
                StartReceive(ref state, state.Client.State == State.Challenged ? (IByteArrayAppendable) new AuthResponseMsg() : new ClientRequestMsg(), ReadCallback);
                return;
            }

            if (bytesRead > 0)
            {
                LOG.Debug($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Reading {bytesRead} bytes.");

                var complete = false;
                try {
                    complete = state.Message.AddRange(state.Buffer);
                }
                catch (Exception e) {
                    LOG.Warn($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Exception caught while extracting data from inbound message.", e);
                    Send(ref state, new ServerResponseMsg(ServerResponseMsg.ResponseCode.RC_ERROR, Guid.Empty));
                    StartReceive(ref state, state.Client.State == State.Challenged ? (IByteArrayAppendable) new AuthResponseMsg() : new ClientRequestMsg(), ReadCallback);
                    return;
                }

                if (complete)
                {
                    IByteArraySerializable response = null;

                    if (state.Client.State == State.Ready)
                    {
                        // There might be more data, so store the data received so far.
                        var message = state.Message as ClientRequestMsg;

                        state.Client.RequestInfo = $"{message.Type.ToString().Substring(3)} {message.AssetId}";                         // Substring removes the "RT_"
                        LOG.Debug($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Request message completed: {message?.GetHeaderSummary()}");

                        try {
                            _requestHandler(message, RequestResponseCallback, state);
                        }
                        catch (Exception e) {
                            LOG.Warn($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Exception caught from request handler while processing message.", e);
                            Send(ref state, new ServerResponseMsg(ServerResponseMsg.ResponseCode.RC_ERROR, message.AssetId));
                            StartReceive(ref state, new ClientRequestMsg(), ReadCallback);
                            return;
                        }
                    }
                    else                       // State.Challenged, or not recognized.
                                               // Wants to know status, reply accordingly.
                    {
                        var message = state.Message as AuthResponseMsg;

                        var hashCorrect = message?.ChallengeHash == state.CorrectChallengeHash;

                        LOG.Debug($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Auth response completed: " + (hashCorrect ? "Hash correct." : "Hash not correct, auth failed."));

                        response = new AuthStatusMsg(hashCorrect ? AuthStatusMsg.StatusType.AS_SUCCESS : AuthStatusMsg.StatusType.AS_FAILURE);

                        if (hashCorrect)
                        {
                            state.Message      = new ClientRequestMsg();
                            state.Client.State = State.Ready;
                        }

                        try {
                            if (hashCorrect)
                            {
                                Send(ref state, response);
                                StartReceive(ref state, new ClientRequestMsg(), ReadCallback);
                            }
                            else
                            {
                                SendAndClose(ref state, response);
                            }
                        }
                        catch (Exception e) {
                            LOG.Warn($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Exception caught responding to client.", e);
                            Send(ref state, new ServerResponseMsg(ServerResponseMsg.ResponseCode.RC_ERROR, Guid.Empty));
                            StartReceive(ref state, new ClientRequestMsg(), ReadCallback);
                            return;
                        }
                    }
                }
                else
                {
                    // Not all data received. Get more.
                    LOG.Debug($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Message incomplete, getting next packet.");

                    ContinueReceive(ref state, ReadCallback);
                }
            }
            else
            {
                LOG.Debug($"{_localEndPoint}, {state.Client.RemoteEndpoint}, {state.Client.State} - Zero bytes received. Client must have closed the connection.");
                state.Client.State = State.Disconnected;
                _activeConnections.TryRemove(state.Client.RemoteEndpoint, out var junk);
            }
        }