From 1d4f30ca4d394c0c4bb63478757374e7b40f1686 Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Sun, 19 Apr 2009 18:55:16 +0000
Subject: [PATCH] - Made net_htons() endian-clean for big endian.
---
ChangeLog | 2 ++
library/net.c | 21 +++++++++++----------
2 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index d20689edc..32d01f864 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,6 +14,8 @@ PolarSSL ChangeLog
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 d243fb3b3..41bb2d93f 100644
--- a/library/net.c
+++ b/library/net.c
@@ -55,6 +55,7 @@ static int wsa_init_done = 0;
#include
#include
#include
+#include
#endif
@@ -64,18 +65,18 @@ static int wsa_init_done = 0;
#include
/*
- * 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