/// <summary>
 /// Gets the all request message data in bytes.
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public static byte[] GetRequestMessage(this HttpListenerContext context)
 {
     using (EneterTrace.Entering())
     {
         byte[] aRequestMessage = StreamUtil.ReadBytes(context.Request.InputStream, (int)context.Request.ContentLength64);
         return(aRequestMessage);
     }
 }
예제 #2
0
        public static Folder Deserialize(Stream stream)
        {
            Folder folder    = new Folder();
            int    noOfFiles = BitConverter.ToInt32(StreamUtil.ReadBytes(stream, 4), 0);

            for (int i = 0; i < noOfFiles; i++)
            {
                folder.Files.Add(File.Deserialize(stream));
            }

            return(folder);
        }
예제 #3
0
        public static Volume Deserialize(Stream stream, String password, int maxPossibleSize)
        {
            Volume volume = new Volume();
            var    offset = CalculateOffset(password);

            //deserialize header
            stream.Seek(offset, SeekOrigin.Current);                                            //moves stream cursor passed offset
            var IV = StreamUtil.ReadBytes(stream, 32);                                          //read IV
            int encryptedDataLength = BitConverter.ToInt32(StreamUtil.ReadBytes(stream, 4), 0); //read length of the following encrypted data
            var algorithmNo         = Convert.ToInt16(StreamUtil.ReadBytes(stream, 1)[0]);

            volume.AlgorithmNo = algorithmNo;

            if (encryptedDataLength < 32 || encryptedDataLength > maxPossibleSize) //encryptedDataLength is not valid, so the specified password cannot be for this volume.
            {
                return(null);
            }

            byte[] encryptedBytes = StreamUtil.ReadBytes(stream, encryptedDataLength);

            //decrypt volume
            byte[] decryptedBytes = CryptoUtil.Decrypt256(encryptedBytes, IV, password, algorithmNo);

            //deserialize decrypted volume
            MemoryStream decryptedStream = new MemoryStream(decryptedBytes);

            byte[] cryptographicHash = StreamUtil.ReadBytes(decryptedStream, 32);

            //verify volume
            if (!cryptographicHash.SequenceEqual(CryptoUtil.GetSha256Hash(Encoding.UTF8.GetBytes(password))))
            {
                return(null);
            }

            volume.Folder = Folder.Deserialize(decryptedStream);

            return(volume);
        }
예제 #4
0
        private IEnumerator <int> SendInternal(AsyncEnumerator ae)
        {
            try
            {
                using (var client = new TcpClient())
                {
                    try
                    {
                        client.BeginConnect(Destination.Host, Destination.Port,
                                            ae.End(),
                                            null);
                    }
                    catch (Exception exception)
                    {
                        logger.WarnFormat("Failed to connect to {0} because {1}", Destination, exception);
                        Failure(exception);
                        yield break;
                    }

                    yield return(1);

                    try
                    {
                        client.EndConnect(ae.DequeueAsyncResult());
                    }
                    catch (Exception exception)
                    {
                        logger.WarnFormat("Failed to connect to {0} because {1}", Destination, exception);
                        Failure(exception);
                        yield break;
                    }

                    logger.DebugFormat("Successfully connected to {0}", Destination);

                    using (var stream = client.GetStream())
                    {
                        var buffer = Messages.Serialize();

                        var bufferLenInBytes = BitConverter.GetBytes(buffer.Length);

                        logger.DebugFormat("Writing length of {0} bytes to {1}", buffer.Length, Destination);

                        try
                        {
                            stream.BeginWrite(bufferLenInBytes, 0, bufferLenInBytes.Length, ae.End(), null);
                        }
                        catch (Exception exception)
                        {
                            logger.WarnFormat("Could not write to {0} because {1}", Destination,
                                              exception);
                            Failure(exception);
                            yield break;
                        }

                        yield return(1);

                        try
                        {
                            stream.EndWrite(ae.DequeueAsyncResult());
                        }
                        catch (Exception exception)
                        {
                            logger.WarnFormat("Could not write to {0} because {1}", Destination,
                                              exception);
                            Failure(exception);
                            yield break;
                        }

                        logger.DebugFormat("Writing {0} bytes to {1}", buffer.Length, Destination);

                        try
                        {
                            stream.BeginWrite(buffer, 0, buffer.Length, ae.End(), null);
                        }
                        catch (Exception exception)
                        {
                            logger.WarnFormat("Could not write to {0} because {1}", Destination,
                                              exception);
                            Failure(exception);
                            yield break;
                        }

                        yield return(1);

                        try
                        {
                            stream.EndWrite(ae.DequeueAsyncResult());
                        }
                        catch (Exception exception)
                        {
                            logger.WarnFormat("Could not write to {0} because {1}", Destination,
                                              exception);
                            Failure(exception);
                            yield break;
                        }

                        logger.DebugFormat("Successfully wrote to {0}", Destination);

                        var recieveBuffer = new byte[ProtocolConstants.RecievedBuffer.Length];
                        var readConfirmationEnumerator = new AsyncEnumerator();

                        try
                        {
                            readConfirmationEnumerator.BeginExecute(
                                StreamUtil.ReadBytes(recieveBuffer, stream, readConfirmationEnumerator, "recieve confirmation", false), ae.End());
                        }
                        catch (Exception exception)
                        {
                            logger.WarnFormat("Could not read confirmation from {0} because {1}", Destination,
                                              exception);
                            Failure(exception);
                            yield break;
                        }

                        yield return(1);

                        try
                        {
                            readConfirmationEnumerator.EndExecute(ae.DequeueAsyncResult());
                        }
                        catch (Exception exception)
                        {
                            logger.WarnFormat("Could not read confirmation from {0} because {1}", Destination,
                                              exception);
                            Failure(exception);
                            yield break;
                        }

                        var recieveRespone = Encoding.Unicode.GetString(recieveBuffer);
                        if (recieveRespone == ProtocolConstants.QueueDoesNotExists)
                        {
                            logger.WarnFormat(
                                "Response from reciever {0} is that queue does not exists",
                                Destination);
                            Failure(new QueueDoesNotExistsException());
                            yield break;
                        }
                        else if (recieveRespone != ProtocolConstants.Recieved)
                        {
                            logger.WarnFormat(
                                "Response from reciever {0} is not the expected one, unexpected response was: {1}",
                                Destination, recieveRespone);
                            Failure(null);
                            yield break;
                        }

                        try
                        {
                            if (FailToAcknowledgeReceipt)
                            {
                                yield break;
                            }

                            stream.BeginWrite(ProtocolConstants.AcknowledgedBuffer, 0,
                                              ProtocolConstants.AcknowledgedBuffer.Length, ae.End(), null);
                        }
                        catch (Exception exception)
                        {
                            logger.WarnFormat("Failed to write acknowledgement to reciever {0} because {1}",
                                              Destination, exception);
                            Failure(exception);
                            yield break;
                        }

                        yield return(1);

                        try
                        {
                            stream.EndWrite(ae.DequeueAsyncResult());
                        }
                        catch (Exception exception)
                        {
                            logger.WarnFormat("Failed to write acknowledgement to reciever {0} because {1}",
                                              Destination, exception);
                            Failure(exception);
                            yield break;
                        }

                        var bookmarks = Success();

                        buffer = new byte[ProtocolConstants.RevertBuffer.Length];
                        var  readRevertMessage    = new AsyncEnumerator(ae.ToString());
                        bool startingToReadFailed = false;
                        try
                        {
                            readRevertMessage.BeginExecute(
                                StreamUtil.ReadBytes(buffer, stream, readRevertMessage, "revert", true), ae.End());
                        }
                        catch (Exception)
                        {
                            //more or less expected
                            startingToReadFailed = true;
                        }
                        if (startingToReadFailed)
                        {
                            Commit();
                            yield break;
                        }
                        yield return(1);

                        try
                        {
                            readRevertMessage.EndExecute(ae.DequeueAsyncResult());
                            var revert = Encoding.Unicode.GetString(buffer);
                            if (revert == ProtocolConstants.Revert)
                            {
                                logger.Warn("Got back revert message from receiver, reverting send");
                                Revert(bookmarks);
                            }
                            else
                            {
                                Commit();
                            }
                        }
                        catch (Exception)
                        {
                            // expected, there is nothing to do here, the
                            // reciever didn't report anything for us
                            Commit();
                        }
                    }
                }
            }
            finally
            {
                var completed = SendCompleted;
                if (completed != null)
                {
                    completed();
                }
            }
        }