Trivial: make sptps_receive_data_datagram() a little more readable.
The new code updates variables as stuff is being consumed, so that the reader doesn't have to do that in his head.
This commit is contained in:
parent
30e839b0a1
commit
aa52300b2b
1 changed files with 11 additions and 9 deletions
20
src/sptps.c
20
src/sptps.c
|
@ -447,6 +447,7 @@ static bool sptps_receive_data_datagram(sptps_t *s, const char *data, size_t len
|
||||||
uint32_t seqno;
|
uint32_t seqno;
|
||||||
memcpy(&seqno, data, 4);
|
memcpy(&seqno, data, 4);
|
||||||
seqno = ntohl(seqno);
|
seqno = ntohl(seqno);
|
||||||
|
data += 4; len -= 4;
|
||||||
|
|
||||||
if(!s->instate) {
|
if(!s->instate) {
|
||||||
if(seqno != s->inseqno)
|
if(seqno != s->inseqno)
|
||||||
|
@ -454,38 +455,39 @@ static bool sptps_receive_data_datagram(sptps_t *s, const char *data, size_t len
|
||||||
|
|
||||||
s->inseqno = seqno + 1;
|
s->inseqno = seqno + 1;
|
||||||
|
|
||||||
uint8_t type = data[4];
|
uint8_t type = *(data++); len--;
|
||||||
|
|
||||||
if(type != SPTPS_HANDSHAKE)
|
if(type != SPTPS_HANDSHAKE)
|
||||||
return error(s, EIO, "Application record received before handshake finished");
|
return error(s, EIO, "Application record received before handshake finished");
|
||||||
|
|
||||||
return receive_handshake(s, data + 5, len - 5);
|
return receive_handshake(s, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrypt
|
// Decrypt
|
||||||
|
|
||||||
char buffer[len];
|
char buffer[len];
|
||||||
|
|
||||||
size_t outlen;
|
size_t outlen;
|
||||||
|
if(!chacha_poly1305_decrypt(s->incipher, seqno, data, len, buffer, &outlen))
|
||||||
if(!chacha_poly1305_decrypt(s->incipher, seqno, data + 4, len - 4, buffer, &outlen))
|
|
||||||
return error(s, EIO, "Failed to decrypt and verify packet");
|
return error(s, EIO, "Failed to decrypt and verify packet");
|
||||||
|
|
||||||
if(!sptps_check_seqno(s, seqno, true))
|
if(!sptps_check_seqno(s, seqno, true))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Append a NULL byte for safety.
|
// Append a NULL byte for safety.
|
||||||
buffer[len - 20] = 0;
|
buffer[outlen] = 0;
|
||||||
|
|
||||||
uint8_t type = buffer[0];
|
data = buffer;
|
||||||
|
len = outlen;
|
||||||
|
|
||||||
|
uint8_t type = *(data++); len--;
|
||||||
|
|
||||||
if(type < SPTPS_HANDSHAKE) {
|
if(type < SPTPS_HANDSHAKE) {
|
||||||
if(!s->instate)
|
if(!s->instate)
|
||||||
return error(s, EIO, "Application record received before handshake finished");
|
return error(s, EIO, "Application record received before handshake finished");
|
||||||
if(!s->receive_record(s->handle, type, buffer + 1, len - 21))
|
if(!s->receive_record(s->handle, type, data, len))
|
||||||
return false;
|
return false;
|
||||||
} else if(type == SPTPS_HANDSHAKE) {
|
} else if(type == SPTPS_HANDSHAKE) {
|
||||||
if(!receive_handshake(s, buffer + 1, len - 21))
|
if(!receive_handshake(s, data, len))
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
return error(s, EIO, "Invalid record type %d", type);
|
return error(s, EIO, "Invalid record type %d", type);
|
||||||
|
|
Loading…
Reference in a new issue