diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-10-22 02:16:32 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-10-22 02:23:15 +0200 |
commit | 6359772b9cf6e8b9ffee1ca98413675a4357616f (patch) | |
tree | e965b4fd39d21fa4b1f30efe351a020584288835 | |
parent | 50b83be77b44fa173e3d2e59bb845e761e80caaa (diff) | |
download | fastd-6359772b9cf6e8b9ffee1ca98413675a4357616f.tar fastd-6359772b9cf6e8b9ffee1ca98413675a4357616f.zip |
Use big endian for handshake field values by default
At the moment the only multi-byte field is the MTU; it is kept in little endian
to provide backwards compatiblity. Future fields will be big endian.
-rw-r--r-- | src/handshake.c | 23 | ||||
-rw-r--r-- | src/handshake.h | 20 |
2 files changed, 28 insertions, 15 deletions
diff --git a/src/handshake.c b/src/handshake.c index 97c47cb..0a47987 100644 --- a/src/handshake.c +++ b/src/handshake.c @@ -62,19 +62,24 @@ static inline uint8_t as_uint8(const fastd_handshake_record_t *record) { return record->data[0]; } -/** Reads a TLV record as a 16bit integer (little endian) */ +/** Reads a TLV record as a 16bit integer (big endian) */ static inline uint16_t as_uint16(const fastd_handshake_record_t *record) { - return as_uint8(record) | (uint16_t)record->data[1] << 8; + return (uint16_t)as_uint8(record) << 8 | record->data[1]; } -/** Reads a TLV record as a 24bit integer (little endian) */ +/** Reads a TLV record as a 24bit integer (big endian) */ static inline uint32_t as_uint24(const fastd_handshake_record_t *record) { - return as_uint16(record) | (uint32_t)record->data[2] << 16; + return (uint32_t)as_uint16(record) << 8 | record->data[2]; } -/** Reads a TLV record as a 32bit integer (little endian) */ +/** Reads a TLV record as a 32bit integer (big endian) */ static inline uint32_t as_uint32(const fastd_handshake_record_t *record) { - return as_uint24(record) | (uint32_t)record->data[3] << 24; + return as_uint24(record) << 8 | record->data[3]; +} + +/** Reads a TLV record as a 16bit integer (little endian) */ +static inline uint16_t as_uint16_le(const fastd_handshake_record_t *record) { + return as_uint8(record) | (uint16_t)record->data[1] << 8; } /** Reads a TLV record as a variable-length integer (little endian) */ @@ -170,7 +175,7 @@ static fastd_buffer_t new_handshake(uint8_t type, const fastd_method_info_t *met fastd_handshake_add_uint8(&buffer, RECORD_HANDSHAKE_TYPE, type); fastd_handshake_add_uint8(&buffer, RECORD_MODE, conf.mode); - fastd_handshake_add_uint16(&buffer, RECORD_MTU, conf.mtu); + fastd_handshake_add_uint16_le(&buffer, RECORD_MTU, conf.mtu); fastd_handshake_add(&buffer, RECORD_VERSION_NAME, version_len, FASTD_VERSION); fastd_handshake_add(&buffer, RECORD_PROTOCOL_NAME, protocol_len, conf.protocol->name); @@ -330,9 +335,9 @@ static inline bool check_records(fastd_socket_t *sock, const fastd_peer_address_ if (!conf.secure_handshakes || handshake->type > 1) { if (handshake->records[RECORD_MTU].length == 2) { - if (as_uint16(&handshake->records[RECORD_MTU]) != conf.mtu) { + if (as_uint16_le(&handshake->records[RECORD_MTU]) != conf.mtu) { pr_warn("MTU configuration differs with peer %I: local MTU is %u, remote MTU is %u", - remote_addr, conf.mtu, as_uint16(&handshake->records[RECORD_MTU])); + remote_addr, conf.mtu, as_uint16_le(&handshake->records[RECORD_MTU])); } } } diff --git a/src/handshake.h b/src/handshake.h index a77b730..760d21b 100644 --- a/src/handshake.h +++ b/src/handshake.h @@ -156,27 +156,35 @@ static inline void fastd_handshake_add_uint8(fastd_buffer_t *buffer, fastd_hands static inline void fastd_handshake_add_uint16(fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint16_t value) { uint8_t *dst = fastd_handshake_extend(buffer, type, 2); - dst[0] = value; - dst[1] = value >> 8; + dst[0] = value >> 8; + dst[1] = value; } /** Adds an uint24 TLV record of given type and value to a handshake buffer */ static inline void fastd_handshake_add_uint24(fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint32_t value) { uint8_t *dst = fastd_handshake_extend(buffer, type, 3); - dst[0] = value; + dst[0] = value >> 16; dst[1] = value >> 8; - dst[2] = value >> 16; + dst[2] = value; } /** Adds an uint32 TLV record of given type and value to a handshake buffer */ static inline void fastd_handshake_add_uint32(fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint32_t value) { uint8_t *dst = fastd_handshake_extend(buffer, type, 4); + dst[0] = value >> 24; + dst[1] = value >> 16; + dst[2] = value >> 8; + dst[3] = value; +} + +/** Adds an uint16 TLV record of given type and value to a handshake buffer encoded as little endian */ +static inline void fastd_handshake_add_uint16_le(fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint16_t value) { + uint8_t *dst = fastd_handshake_extend(buffer, type, 2); + dst[0] = value; dst[1] = value >> 8; - dst[2] = value >> 16; - dst[3] = value >> 24; } /** Adds an TLV record of given type and value to a handshake buffer, automatically using a 1- to 4-byte value */ |