28 #if defined(POLARSSL_NET_C)
32 #if defined(_WIN32) || defined(_WIN32_WCE)
37 #if defined(_WIN32_WCE)
38 #pragma comment( lib, "ws2.lib" )
40 #pragma comment( lib, "ws2_32.lib" )
43 #define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
44 #define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
45 #define close(fd) closesocket(fd)
47 static int wsa_init_done = 0;
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #if defined(POLARSSL_HAVE_TIME)
64 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
65 defined(__DragonflyBSD__)
66 #include <sys/endian.h>
67 #elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H)
68 #include <machine/endian.h>
70 #include <sys/isa_defs.h>
71 #elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
72 #include <arpa/nameser_compat.h>
82 #if defined(POLARSSL_HAVE_TIME)
88 typedef UINT32 uint32_t;
98 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
99 #define POLARSSL_HTONS(n) (n)
100 #define POLARSSL_HTONL(n) (n)
102 #define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
103 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
104 #define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
105 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
106 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
107 (((unsigned long )(n) & 0xFF000000) >> 24))
110 unsigned short net_htons(
unsigned short n);
111 unsigned long net_htonl(
unsigned long n);
112 #define net_htons(n) POLARSSL_HTONS(n)
113 #define net_htonl(n) POLARSSL_HTONL(n)
118 int net_connect(
int *fd,
const char *host,
int port )
120 struct sockaddr_in server_addr;
121 struct hostent *server_host;
123 #if defined(_WIN32) || defined(_WIN32_WCE)
126 if( wsa_init_done == 0 )
128 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
134 signal( SIGPIPE, SIG_IGN );
137 if( ( server_host = gethostbyname( host ) ) == NULL )
140 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
143 memcpy( (
void *) &server_addr.sin_addr,
144 (
void *) server_host->h_addr,
145 server_host->h_length );
147 server_addr.sin_family = AF_INET;
148 server_addr.sin_port = net_htons( port );
150 if( connect( *fd, (
struct sockaddr *) &server_addr,
151 sizeof( server_addr ) ) < 0 )
163 int net_bind(
int *fd,
const char *bind_ip,
int port )
166 struct sockaddr_in server_addr;
168 #if defined(_WIN32) || defined(_WIN32_WCE)
171 if( wsa_init_done == 0 )
173 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
179 signal( SIGPIPE, SIG_IGN );
182 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
186 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
187 (
const char *) &n,
sizeof( n ) );
189 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
190 server_addr.sin_family = AF_INET;
191 server_addr.sin_port = net_htons( port );
193 if( bind_ip != NULL )
195 memset( c, 0,
sizeof( c ) );
196 sscanf( bind_ip,
"%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
198 for( n = 0; n < 4; n++ )
199 if( c[n] < 0 || c[n] > 255 )
203 server_addr.sin_addr.s_addr = net_htonl(
204 ( (uint32_t) c[0] << 24 ) |
205 ( (uint32_t) c[1] << 16 ) |
206 ( (uint32_t) c[2] << 8 ) |
207 ( (uint32_t) c[3] ) );
210 if( bind( *fd, (
struct sockaddr *) &server_addr,
211 sizeof( server_addr ) ) < 0 )
229 static int net_is_blocking(
void )
231 #if defined(_WIN32) || defined(_WIN32_WCE)
232 return( WSAGetLastError() == WSAEWOULDBLOCK );
239 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
251 int net_accept(
int bind_fd,
int *client_fd,
void *client_ip )
253 struct sockaddr_in client_addr;
255 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
256 defined(_SOCKLEN_T_DECLARED)
257 socklen_t n = (socklen_t)
sizeof( client_addr );
259 int n = (int)
sizeof( client_addr );
262 *client_fd = (int) accept( bind_fd, (
struct sockaddr *)
267 if( net_is_blocking() != 0 )
273 if( client_ip != NULL )
274 memcpy( client_ip, &client_addr.sin_addr.s_addr,
275 sizeof( client_addr.sin_addr.s_addr ) );
285 #if defined(_WIN32) || defined(_WIN32_WCE)
287 return( ioctlsocket( fd, FIONBIO, &n ) );
289 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
295 #if defined(_WIN32) || defined(_WIN32_WCE)
297 return( ioctlsocket( fd, FIONBIO, &n ) );
299 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
303 #if defined(POLARSSL_HAVE_TIME)
312 select( 0, NULL, NULL, NULL, &tv );
319 int net_recv(
void *ctx,
unsigned char *buf,
size_t len )
321 int ret = read( *((
int *) ctx), buf, len );
325 if( net_is_blocking() != 0 )
328 #if defined(_WIN32) || defined(_WIN32_WCE)
329 if( WSAGetLastError() == WSAECONNRESET )
332 if( errno == EPIPE || errno == ECONNRESET )
348 int net_send(
void *ctx,
const unsigned char *buf,
size_t len )
350 int ret = write( *((
int *) ctx), buf, len );
354 if( net_is_blocking() != 0 )
357 #if defined(_WIN32) || defined(_WIN32_WCE)
358 if( WSAGetLastError() == WSAECONNRESET )
361 if( errno == EPIPE || errno == ECONNRESET )