|
@@ -12,7 +12,6 @@ pub const PADDING_BLOCK_SIZE: u32 = 10 * 128 / 8;
|
|
// from https://github.com/signalapp/Signal-Android/blob/36a8c4d8ba9fdb62905ecb9a20e3eeba4d2f9022/app/src/main/java/org/thoughtcrime/securesms/mms/PushMediaConstraints.java
|
|
// from https://github.com/signalapp/Signal-Android/blob/36a8c4d8ba9fdb62905ecb9a20e3eeba4d2f9022/app/src/main/java/org/thoughtcrime/securesms/mms/PushMediaConstraints.java
|
|
pub const MAX_BLOCKS_IN_BODY: u32 = (100 * 1024 * 1024) / PADDING_BLOCK_SIZE;
|
|
pub const MAX_BLOCKS_IN_BODY: u32 = (100 * 1024 * 1024) / PADDING_BLOCK_SIZE;
|
|
/// The maxmimum number of bytes that can be sent inline; larger values use the HTTP server.
|
|
/// The maxmimum number of bytes that can be sent inline; larger values use the HTTP server.
|
|
-// FIXME: should only apply to client-server, not p2p
|
|
|
|
// In actuality, this is 2000 for Signal:
|
|
// In actuality, this is 2000 for Signal:
|
|
// https://github.com/signalapp/Signal-Android/blob/244902ecfc30e21287a35bb1680e2dbe6366975b/app/src/main/java/org/thoughtcrime/securesms/util/PushCharacterCalculator.java#L23
|
|
// https://github.com/signalapp/Signal-Android/blob/244902ecfc30e21287a35bb1680e2dbe6366975b/app/src/main/java/org/thoughtcrime/securesms/util/PushCharacterCalculator.java#L23
|
|
// but we align to a close block count since in practice we sample from block counts
|
|
// but we align to a close block count since in practice we sample from block counts
|
|
@@ -75,12 +74,12 @@ impl MessageBody {
|
|
}
|
|
}
|
|
|
|
|
|
/// Size on the wire of the message's body, exluding bytes fetched via http
|
|
/// Size on the wire of the message's body, exluding bytes fetched via http
|
|
- fn inline_size(&self) -> usize {
|
|
|
|
|
|
+ fn inline_size<const P2P: bool>(&self) -> usize {
|
|
match self {
|
|
match self {
|
|
MessageBody::Receipt => PADDING_BLOCK_SIZE as usize,
|
|
MessageBody::Receipt => PADDING_BLOCK_SIZE as usize,
|
|
MessageBody::Size(size) => {
|
|
MessageBody::Size(size) => {
|
|
let size = size.get();
|
|
let size = size.get();
|
|
- if size <= INLINE_MAX_SIZE {
|
|
|
|
|
|
+ if P2P || size <= INLINE_MAX_SIZE {
|
|
size as usize
|
|
size as usize
|
|
} else {
|
|
} else {
|
|
INLINE_MAX_SIZE as usize
|
|
INLINE_MAX_SIZE as usize
|
|
@@ -228,24 +227,24 @@ pub async fn parse_identifier<T: AsyncReadExt + std::marker::Unpin>(
|
|
}
|
|
}
|
|
|
|
|
|
/// Gets a message from the stream, returning the raw byte buffer
|
|
/// Gets a message from the stream, returning the raw byte buffer
|
|
-pub async fn get_message_bytes<T: AsyncReadExt + std::marker::Unpin>(
|
|
|
|
|
|
+pub async fn get_message_bytes<const P2P: bool, T: AsyncReadExt + std::marker::Unpin>(
|
|
stream: &mut T,
|
|
stream: &mut T,
|
|
) -> Result<Vec<u8>, Error> {
|
|
) -> Result<Vec<u8>, Error> {
|
|
let mut header_size_bytes = [0u8; 4];
|
|
let mut header_size_bytes = [0u8; 4];
|
|
stream.read_exact(&mut header_size_bytes).await?;
|
|
stream.read_exact(&mut header_size_bytes).await?;
|
|
- get_message_with_header_size(stream, header_size_bytes).await
|
|
|
|
|
|
+ get_message_with_header_size::<P2P, _>(stream, header_size_bytes).await
|
|
}
|
|
}
|
|
|
|
|
|
/// Gets a message from the stream and constructs a MessageHeader object
|
|
/// Gets a message from the stream and constructs a MessageHeader object
|
|
-pub async fn get_message<T: AsyncReadExt + std::marker::Unpin>(
|
|
|
|
|
|
+pub async fn get_message<const P2P: bool, T: AsyncReadExt + std::marker::Unpin>(
|
|
stream: &mut T,
|
|
stream: &mut T,
|
|
) -> Result<MessageHeader, Error> {
|
|
) -> Result<MessageHeader, Error> {
|
|
- let buf = get_message_bytes(stream).await?;
|
|
|
|
|
|
+ let buf = get_message_bytes::<P2P, _>(stream).await?;
|
|
let msg = MessageHeader::deserialize(&buf[4..])?;
|
|
let msg = MessageHeader::deserialize(&buf[4..])?;
|
|
Ok(msg)
|
|
Ok(msg)
|
|
}
|
|
}
|
|
|
|
|
|
-async fn get_message_with_header_size<T: AsyncReadExt + std::marker::Unpin>(
|
|
|
|
|
|
+async fn get_message_with_header_size<const P2P: bool, T: AsyncReadExt + std::marker::Unpin>(
|
|
stream: &mut T,
|
|
stream: &mut T,
|
|
header_size_bytes: [u8; 4],
|
|
header_size_bytes: [u8; 4],
|
|
) -> Result<Vec<u8>, Error> {
|
|
) -> Result<Vec<u8>, Error> {
|
|
@@ -256,7 +255,7 @@ async fn get_message_with_header_size<T: AsyncReadExt + std::marker::Unpin>(
|
|
let header_size_buf = &mut header_buf[..4];
|
|
let header_size_buf = &mut header_buf[..4];
|
|
header_size_buf.copy_from_slice(&header_size_bytes);
|
|
header_size_buf.copy_from_slice(&header_size_bytes);
|
|
copy(
|
|
copy(
|
|
- &mut stream.take(header.body.inline_size() as u64),
|
|
|
|
|
|
+ &mut stream.take(header.body.inline_size::<P2P>() as u64),
|
|
&mut sink(),
|
|
&mut sink(),
|
|
)
|
|
)
|
|
.await?;
|
|
.await?;
|
|
@@ -304,11 +303,11 @@ pub struct SerializedMessage {
|
|
}
|
|
}
|
|
|
|
|
|
impl SerializedMessage {
|
|
impl SerializedMessage {
|
|
- pub async fn write_all_to<T: AsyncWriteExt + std::marker::Unpin>(
|
|
|
|
|
|
+ pub async fn write_all_to<const P2P: bool, T: AsyncWriteExt + std::marker::Unpin>(
|
|
&self,
|
|
&self,
|
|
writer: &mut T,
|
|
writer: &mut T,
|
|
) -> std::io::Result<()> {
|
|
) -> std::io::Result<()> {
|
|
- let body_buf = vec![0; self.body.inline_size()];
|
|
|
|
|
|
+ let body_buf = vec![0; self.body.inline_size::<P2P>()];
|
|
|
|
|
|
// write_all_vectored is not yet stable x_x
|
|
// write_all_vectored is not yet stable x_x
|
|
// https://github.com/rust-lang/rust/issues/70436
|
|
// https://github.com/rust-lang/rust/issues/70436
|