From 37286a573bd36b7d80fd101b54bd25bb04979b4e Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Wed, 6 Mar 2013 16:55:11 +0100 Subject: [PATCH] Fixed net_bind() for specified IP addresses on little endian systems --- ChangeLog | 1 + library/net.c | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index fa5412094..f5d6b4a3e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,7 @@ Bugfix * Corrected GCM counter incrementation to use only 32-bits instead of 128-bits (found by Yawning Angel) * Fixes for 64-bit compilation with MS Visual Studio + * Fixed net_bind() for specified IP addresses on little endian systems Changes * Internally split up rsa_pkcs1_encrypt(), rsa_pkcs1_decrypt(), diff --git a/library/net.c b/library/net.c index fedde16fe..7a1818df0 100644 --- a/library/net.c +++ b/library/net.c @@ -90,12 +90,20 @@ typedef UINT32 uint32_t; */ #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN #define POLARSSL_HTONS(n) (n) +#define POLARSSL_HTONL(n) (n) #else -#define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) +#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \ + (((unsigned short)(n) & 0xFF00 ) >> 8 )) +#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \ + (((unsigned long )(n) & 0xFF00 ) << 8 ) | \ + (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \ + (((unsigned long )(n) & 0xFF000000) >> 24)) #endif unsigned short net_htons(unsigned short n); +unsigned long net_htonl(unsigned long n); #define net_htons(n) POLARSSL_HTONS(n) +#define net_htonl(n) POLARSSL_HTONL(n) /* * Initiate a TCP connection with host:port @@ -171,7 +179,7 @@ int net_bind( int *fd, const char *bind_ip, int port ) setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &n, sizeof( n ) ); - server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY ); server_addr.sin_family = AF_INET; server_addr.sin_port = net_htons( port ); @@ -185,11 +193,11 @@ int net_bind( int *fd, const char *bind_ip, int port ) break; if( n == 4 ) - server_addr.sin_addr.s_addr = + server_addr.sin_addr.s_addr = net_htonl( ( (uint32_t) c[0] << 24 ) | ( (uint32_t) c[1] << 16 ) | ( (uint32_t) c[2] << 8 ) | - ( (uint32_t) c[3] ); + ( (uint32_t) c[3] ) ); } if( bind( *fd, (struct sockaddr *) &server_addr,