- Made net_htons() endian-clean for big endian.
diff --git a/ChangeLog b/ChangeLog
index d20689e..32d01f8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,6 +14,8 @@
Olsson).
* Centralized file opening and reading for x509 files into
load_file()
+ * Made definition of net_htons() endian-clean for big endian
+ systems (Found by Gernot).
= Version 0.10.0 released on 2009-01-12
* Migrated XySSL to PolarSSL
diff --git a/library/net.c b/library/net.c
index d243fb3..41bb2d9 100644
--- a/library/net.c
+++ b/library/net.c
@@ -55,6 +55,7 @@
#include <fcntl.h>
#include <netdb.h>
#include <errno.h>
+#include <endian.h>
#endif
@@ -64,18 +65,18 @@
#include <time.h>
/*
- * htons() is not always available
+ * htons() is not always available.
+ * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
+ * to help determine endianess.
*/
-static unsigned short net_htons( int port )
-{
- unsigned char buf[4];
+#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
+#define HTONS(n) (n)
+#else
+#define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
+#endif
- buf[0] = (unsigned char)( port >> 8 );
- buf[1] = (unsigned char)( port );
- buf[2] = buf[3] = 0;
-
- return( *(unsigned short *) buf );
-}
+unsigned short net_htons(unsigned short n);
+#define net_htons(n) HTONS(n)
/*
* Initiate a TCP connection with host:port