From 0be12e3546e3620b8c6d74d102e0f933c9a30bde Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Tue, 15 Dec 2009 13:39:01 +0100 Subject: [PATCH] Added htoun16/32 and untoh16/32 to read/write unaligned network order integers --- src/libstrongswan/utils.h | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/libstrongswan/utils.h b/src/libstrongswan/utils.h index f54aa16de..3f8ffc6d4 100644 --- a/src/libstrongswan/utils.h +++ b/src/libstrongswan/utils.h @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include @@ -320,6 +322,58 @@ bool return_true(); */ bool return_false(); +/** + * Write a 16-bit host order value in network order to an unaligned address. + * + * @param host host order 16-bit value + * @param network unaligned address to write network order value to + */ +static inline void htoun16(void *network, u_int16_t host) +{ + host = htons(host); + memcpy(network, &host, sizeof(host)); +} + +/** + * Write a 32-bit host order value in network order to an unaligned address. + * + * @param host host order 32-bit value + * @param network unaligned address to write network order value to + */ +static inline void htoun32(void *network, u_int32_t host) +{ + host = htonl(host); + memcpy(network, &host, sizeof(host)); +} + +/** + * Read a 16-bit value in network order from an unaligned address to host order. + * + * @param network unaligned address to read network order value from + * @return host order value + */ +static inline u_int16_t untoh16(void *network) +{ + u_int16_t tmp; + + memcpy(&tmp, network, sizeof(tmp)); + return ntohs(tmp); +} + +/** + * Read a 32-bit value in network order from an unaligned address to host order. + * + * @param network unaligned address to read network order value from + * @return host order value + */ +static inline u_int32_t untoh32(void *network) +{ + u_int32_t tmp; + + memcpy(&tmp, network, sizeof(tmp)); + return ntohs(tmp); +} + /** * Special type to count references */