steffengy/schannel-rs

Can read up to 5 bytes after every complete message

Opened this issue · 1 comments

needs_read: 1,

The TLS chunks always start with a 5 byte header (1 byte content type, 2 bytes protocol version, 2 bytes length). At that point the length of the whole packet is known and SEC_E_INCOMPLETE_MESSAGE will be returned, usually providing the whole size in SECBUFFER_MISSING then (sometimes not, and byte by byte has to be read).

By starting with 5 bytes, performance should be improved a little bit.

I think this bug is outdated an can be closed. Currently in read_in() at least 1024 bytes are allocated for the read even if needs_read = 1.

let min_len = cmp::max(cmp::max(1024, 2 * existing_len), self.needs_read);

self.stream.read(buf) will then happily read more than needs_read bytes. The actual amount of bytes read nread is then saturating_subtracted from needs_read since we might have nread > needs_read.

self.needs_read = self.needs_read.saturating_sub(nread);

And the loop continues to make sure that at least needs_read bytes are read. In practice the first read(buf) will often receive the whole message in one go or at least fill the initial 1024 bytes of the buffer.