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>
62 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
63 defined(__DragonflyBSD__)
64 #include <sys/endian.h>
65 #elif defined(__APPLE__)
66 #include <machine/endian.h>
68 #include <sys/isa_defs.h>
81 typedef UINT32 uint32_t;
91 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
92 #define POLARSSL_HTONS(n) (n)
94 #define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
97 unsigned short net_htons(
unsigned short n);
98 #define net_htons(n) POLARSSL_HTONS(n)
103 int net_connect(
int *fd,
const char *host,
int port )
105 struct sockaddr_in server_addr;
106 struct hostent *server_host;
108 #if defined(_WIN32) || defined(_WIN32_WCE)
111 if( wsa_init_done == 0 )
113 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
119 signal( SIGPIPE, SIG_IGN );
122 if( ( server_host = gethostbyname( host ) ) == NULL )
125 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
128 memcpy( (
void *) &server_addr.sin_addr,
129 (
void *) server_host->h_addr,
130 server_host->h_length );
132 server_addr.sin_family = AF_INET;
133 server_addr.sin_port = net_htons( port );
135 if( connect( *fd, (
struct sockaddr *) &server_addr,
136 sizeof( server_addr ) ) < 0 )
148 int net_bind(
int *fd,
const char *bind_ip,
int port )
151 struct sockaddr_in server_addr;
153 #if defined(_WIN32) || defined(_WIN32_WCE)
156 if( wsa_init_done == 0 )
158 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
164 signal( SIGPIPE, SIG_IGN );
167 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
171 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
172 (
const char *) &n,
sizeof( n ) );
174 server_addr.sin_addr.s_addr = INADDR_ANY;
175 server_addr.sin_family = AF_INET;
176 server_addr.sin_port = net_htons( port );
178 if( bind_ip != NULL )
180 memset( c, 0,
sizeof( c ) );
181 sscanf( bind_ip,
"%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
183 for( n = 0; n < 4; n++ )
184 if( c[n] < 0 || c[n] > 255 )
188 server_addr.sin_addr.s_addr =
189 ( (uint32_t) c[0] << 24 ) |
190 ( (uint32_t) c[1] << 16 ) |
191 ( (uint32_t) c[2] << 8 ) |
195 if( bind( *fd, (
struct sockaddr *) &server_addr,
196 sizeof( server_addr ) ) < 0 )
214 static int net_is_blocking(
void )
216 #if defined(_WIN32) || defined(_WIN32_WCE)
217 return( WSAGetLastError() == WSAEWOULDBLOCK );
224 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
236 int net_accept(
int bind_fd,
int *client_fd,
void *client_ip )
238 struct sockaddr_in client_addr;
240 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
241 defined(_SOCKLEN_T_DECLARED)
242 socklen_t n = (socklen_t)
sizeof( client_addr );
244 int n = (int)
sizeof( client_addr );
247 *client_fd = accept( bind_fd, (
struct sockaddr *)
252 if( net_is_blocking() != 0 )
258 if( client_ip != NULL )
259 memcpy( client_ip, &client_addr.sin_addr.s_addr,
260 sizeof( client_addr.sin_addr.s_addr ) );
270 #if defined(_WIN32) || defined(_WIN32_WCE)
272 return( ioctlsocket( fd, FIONBIO, &n ) );
274 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
280 #if defined(_WIN32) || defined(_WIN32_WCE)
282 return( ioctlsocket( fd, FIONBIO, &n ) );
284 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
296 select( 0, NULL, NULL, NULL, &tv );
302 int net_recv(
void *ctx,
unsigned char *buf,
size_t len )
304 int ret = read( *((
int *) ctx), buf, len );
308 if( net_is_blocking() != 0 )
311 #if defined(_WIN32) || defined(_WIN32_WCE)
312 if( WSAGetLastError() == WSAECONNRESET )
315 if( errno == EPIPE || errno == ECONNRESET )
331 int net_send(
void *ctx,
const unsigned char *buf,
size_t len )
333 int ret = write( *((
int *) ctx), buf, len );
337 if( net_is_blocking() != 0 )
340 #if defined(_WIN32) || defined(_WIN32_WCE)
341 if( WSAGetLastError() == WSAECONNRESET )
344 if( errno == EPIPE || errno == ECONNRESET )