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__)
63 #include <sys/endian.h>
64 #elif defined(__APPLE__)
65 #include <machine/endian.h>
81 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
82 #define POLARSSL_HTONS(n) (n)
84 #define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
87 unsigned short net_htons(
unsigned short n);
88 #define net_htons(n) POLARSSL_HTONS(n)
93 int net_connect(
int *fd,
const char *host,
int port )
95 struct sockaddr_in server_addr;
96 struct hostent *server_host;
98 #if defined(_WIN32) || defined(_WIN32_WCE)
101 if( wsa_init_done == 0 )
103 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
109 signal( SIGPIPE, SIG_IGN );
112 if( ( server_host = gethostbyname( host ) ) == NULL )
115 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
118 memcpy( (
void *) &server_addr.sin_addr,
119 (
void *) server_host->h_addr,
120 server_host->h_length );
122 server_addr.sin_family = AF_INET;
123 server_addr.sin_port = net_htons( port );
125 if( connect( *fd, (
struct sockaddr *) &server_addr,
126 sizeof( server_addr ) ) < 0 )
138 int net_bind(
int *fd,
const char *bind_ip,
int port )
141 struct sockaddr_in server_addr;
143 #if defined(_WIN32) || defined(_WIN32_WCE)
146 if( wsa_init_done == 0 )
148 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
154 signal( SIGPIPE, SIG_IGN );
157 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
161 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
162 (
const char *) &n,
sizeof( n ) );
164 server_addr.sin_addr.s_addr = INADDR_ANY;
165 server_addr.sin_family = AF_INET;
166 server_addr.sin_port = net_htons( port );
168 if( bind_ip != NULL )
170 memset( c, 0,
sizeof( c ) );
171 sscanf( bind_ip,
"%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
173 for( n = 0; n < 4; n++ )
174 if( c[n] < 0 || c[n] > 255 )
178 server_addr.sin_addr.s_addr =
179 ( (
unsigned long) c[0] << 24 ) |
180 ( (
unsigned long) c[1] << 16 ) |
181 ( (
unsigned long) c[2] << 8 ) |
182 ( (
unsigned long) c[3] );
185 if( bind( *fd, (
struct sockaddr *) &server_addr,
186 sizeof( server_addr ) ) < 0 )
204 static int net_is_blocking(
void )
206 #if defined(_WIN32) || defined(_WIN32_WCE)
207 return( WSAGetLastError() == WSAEWOULDBLOCK );
214 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
226 int net_accept(
int bind_fd,
int *client_fd,
void *client_ip )
228 struct sockaddr_in client_addr;
230 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
231 defined(_SOCKLEN_T_DECLARED)
232 socklen_t n = (socklen_t)
sizeof( client_addr );
234 int n = (int)
sizeof( client_addr );
237 *client_fd = accept( bind_fd, (
struct sockaddr *)
242 if( net_is_blocking() != 0 )
248 if( client_ip != NULL )
249 memcpy( client_ip, &client_addr.sin_addr.s_addr,
250 sizeof( client_addr.sin_addr.s_addr ) );
260 #if defined(_WIN32) || defined(_WIN32_WCE)
262 return( ioctlsocket( fd, FIONBIO, &n ) );
264 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
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 ) );
286 select( 0, NULL, NULL, NULL, &tv );
292 int net_recv(
void *ctx,
unsigned char *buf,
size_t len )
294 int ret = read( *((
int *) ctx), buf, len );
298 if( net_is_blocking() != 0 )
301 #if defined(_WIN32) || defined(_WIN32_WCE)
302 if( WSAGetLastError() == WSAECONNRESET )
305 if( errno == EPIPE || errno == ECONNRESET )
321 int net_send(
void *ctx,
const unsigned char *buf,
size_t len )
323 int ret = write( *((
int *) ctx), buf, len );
327 if( net_is_blocking() != 0 )
330 #if defined(_WIN32) || defined(_WIN32_WCE)
331 if( WSAGetLastError() == WSAECONNRESET )
334 if( errno == EPIPE || errno == ECONNRESET )