简体   繁体   English

确定 TLS 数据包 Java/Android 的数据包大小

[英]Determine packet size of TLS packet Java/Android

I have read all around the Stack, but I cannot find a working solution to determine the packet size and set the array of bytes to match that length to receive a correct amount of bytes for one message at once.我已经阅读了堆栈的所有内容,但是我找不到确定数据包大小并设置字节数组以匹配该长度以一次接收一条消息的正确字节数的有效解决方案。

DataInputStream in = new DataInputStream(socket.getInputStream());
while(true) {
    short myShortStreamSize = in.readShort();
    byte[] payload = new byte[myShortStreamSize];
    in.readFully(payload);
    Log.i("Foo", new String(payload));
}

That doesn't work for me.对我不起作用。

My TLS server is made with Node.js.我的 TLS 服务器是用 Node.js 制作的。 Normally Node.js TLS module handles the packets in the way if I use the same module to connect to the server it's enough to use socket.write("message") and the Node.js client understands the sizes already so you don't need to define it yourself.通常,Node.js TLS 模块以这种方式处理数据包,如果我使用相同的模块连接到服务器,则使用socket.write("message")就足够了,并且 Node.js 客户端已经了解大小,因此您不会需要自己定义。 It will write that message to the console without issues.它将将该消息写入控制台而不会出现问题。 As I mentioned that my server is TLS.正如我提到的,我的服务器是 TLS。 I'm not sure if these have something to do with my issue, but would be awesome to make it work.我不确定这些是否与我的问题有关,但让它发挥作用会很棒。

My goal is to receive one message at once without cutting it into pieces.我的目标是一次接收一条消息而不将其分割成碎片。 If I send "Good morning, today is a sunny day!"如果我发送“早上好,今天是晴天!” I want my Java/Android client to read it from start to end without mixing it up with other packets.我希望我的 Java/Android 客户端从头到尾读取它,而不会将它与其他数据包混淆。

... size of TLS packet ... determine the packet size ... receive one message at once ... TLS 包的大小 ... 确定包的大小 ... 一次接收一条消息

There is no "TLS packet".没有“TLS 数据包”。 There is a TLS record which has the length in the first 2 bytes.有一个 TLS 记录,其长度在前 2 个字节中。 This TLS record might be split into multiple TCP fragments or TCP fragments might also contain multiple TLS records in full, in half or whatever.此 TLS 记录可能被拆分为多个 TCP 片段,或者 TCP 片段也可能包含多个完整、一半或其他形式的 TLS 记录。 These TCP fragments then might even be cut into multiple IP packets although TCP tries hard to avoid this.这些 TCP 片段甚至可能被切割成多个 IP 数据包,尽管 TCP 努力避免这种情况。

And your plain text "message" itself might end up being spread over multiple TLS records since the size of such a record might be smaller than the message.并且您的纯文本“消息”本身最终可能会分布在多个 TLS 记录上,因为此类记录的大小可能小于消息。 Depending on your application and TLS stack it might also be mixed together with other "messages".根据您的应用程序和 TLS 堆栈,它也可能与其他“消息”混合在一起。

In other words: what you want is impossible.换句话说:你想要的是不可能的。 Unless you somehow know already the size of your message before reading it you cannot rely on reading the full message and also only this message.除非你在阅读之前以某种方式知道你的消息的大小,否则你不能依赖阅读完整的消息,也只能阅读这条消息。 Neither TCP nor TLS preserve any message "boundaries" you might have imagined when crafting your writes but which have no actual representation in the payload. TCP 和 TLS 都不会保留您在编写写入时可能想象的但在有效负载中没有实际表示的任何消息“边界”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM