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(__DragonFly__)
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)
93 #define POLARSSL_HTONL(n) (n)
95 #define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
96 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
97 #define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
98 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
99 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
100 (((unsigned long )(n) & 0xFF000000) >> 24))
103 unsigned short net_htons(
unsigned short n);
104 unsigned long net_htonl(
unsigned long n);
105 #define net_htons(n) POLARSSL_HTONS(n)
106 #define net_htonl(n) POLARSSL_HTONL(n)
111 int net_connect(
int *fd,
const char *host,
int port )
113 struct sockaddr_in server_addr;
114 struct hostent *server_host;
116 #if defined(_WIN32) || defined(_WIN32_WCE)
119 if( wsa_init_done == 0 )
121 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
127 signal( SIGPIPE, SIG_IGN );
130 memset( &server_addr, 0,
sizeof( server_addr ) );
132 if( ( server_host = gethostbyname( host ) ) == NULL )
135 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
138 memcpy( (
void *) &server_addr.sin_addr,
139 (
void *) server_host->h_addr,
140 server_host->h_length );
142 server_addr.sin_family = AF_INET;
143 server_addr.sin_port = net_htons( port );
145 if( connect( *fd, (
struct sockaddr *) &server_addr,
146 sizeof( server_addr ) ) < 0 )
158 int net_bind(
int *fd,
const char *bind_ip,
int port )
161 struct sockaddr_in server_addr;
163 #if defined(_WIN32) || defined(_WIN32_WCE)
166 if( wsa_init_done == 0 )
168 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
174 signal( SIGPIPE, SIG_IGN );
177 memset( &server_addr, 0,
sizeof( server_addr ) );
179 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
183 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
184 (
const char *) &n,
sizeof( n ) ) != 0 )
190 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
191 server_addr.sin_family = AF_INET;
192 server_addr.sin_port = net_htons( port );
194 if( bind_ip != NULL )
196 memset( c, 0,
sizeof( c ) );
197 sscanf( bind_ip,
"%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
199 for( n = 0; n < 4; n++ )
200 if( c[n] < 0 || c[n] > 255 )
204 server_addr.sin_addr.s_addr = net_htonl(
205 ( (uint32_t) c[0] << 24 ) |
206 ( (uint32_t) c[1] << 16 ) |
207 ( (uint32_t) c[2] << 8 ) |
208 ( (uint32_t) c[3] ) );
211 if( bind( *fd, (
struct sockaddr *) &server_addr,
212 sizeof( server_addr ) ) < 0 )
227 #if ( defined(_WIN32) || defined(_WIN32_WCE) )
232 static int net_would_block(
int fd )
235 return( WSAGetLastError() == WSAEWOULDBLOCK );
244 static int net_would_block(
int fd )
249 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
257 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
269 int net_accept(
int bind_fd,
int *client_fd,
void *client_ip )
271 struct sockaddr_in client_addr;
273 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
274 defined(_SOCKLEN_T_DECLARED)
275 socklen_t n = (socklen_t)
sizeof( client_addr );
277 int n = (int)
sizeof( client_addr );
280 *client_fd = (int) accept( bind_fd, (
struct sockaddr *)
285 if( net_would_block( *client_fd ) != 0 )
291 if( client_ip != NULL )
292 memcpy( client_ip, &client_addr.sin_addr.s_addr,
293 sizeof( client_addr.sin_addr.s_addr ) );
303 #if defined(_WIN32) || defined(_WIN32_WCE)
305 return( ioctlsocket( fd, FIONBIO, &n ) );
307 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
313 #if defined(_WIN32) || defined(_WIN32_WCE)
315 return( ioctlsocket( fd, FIONBIO, &n ) );
317 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
329 select( 0, NULL, NULL, NULL, &tv );
335 int net_recv(
void *ctx,
unsigned char *buf,
size_t len )
337 int fd = *((
int *) ctx);
338 int ret = read( fd, buf, len );
342 if( net_would_block( fd ) != 0 )
345 #if defined(_WIN32) || defined(_WIN32_WCE)
346 if( WSAGetLastError() == WSAECONNRESET )
349 if( errno == EPIPE || errno == ECONNRESET )
365 int net_send(
void *ctx,
const unsigned char *buf,
size_t len )
367 int fd = *((
int *) ctx);
368 int ret = write( fd, buf, len );
372 if( net_would_block( fd ) != 0 )
375 #if defined(_WIN32) || defined(_WIN32_WCE)
376 if( WSAGetLastError() == WSAECONNRESET )
379 if( errno == EPIPE || errno == ECONNRESET )
void net_usleep(unsigned long usec)
Portable usleep helper.
int net_set_nonblock(int fd)
Set the socket non-blocking.
#define POLARSSL_ERR_NET_BIND_FAILED
Binding of the socket failed.
#define POLARSSL_ERR_NET_RECV_FAILED
Reading information from the socket failed.
#define POLARSSL_ERR_NET_WANT_WRITE
Connection requires a write call.
Network communication functions.
int net_send(void *ctx, const unsigned char *buf, size_t len)
Write at most 'len' characters.
Configuration options (set of defines)
void net_close(int fd)
Gracefully shutdown the connection.
#define POLARSSL_ERR_NET_CONN_RESET
Connection was reset by peer.
int net_bind(int *fd, const char *bind_ip, int port)
Create a listening socket on bind_ip:port.
int net_accept(int bind_fd, int *client_fd, void *client_ip)
Accept a connection from a remote client.
#define POLARSSL_ERR_NET_CONNECT_FAILED
The connection to the given server / port failed.
#define POLARSSL_NET_LISTEN_BACKLOG
The backlog that listen() should use.
#define POLARSSL_ERR_NET_SEND_FAILED
Sending information through the socket failed.
#define POLARSSL_ERR_NET_WANT_READ
Connection requires a read call.
#define POLARSSL_ERR_NET_ACCEPT_FAILED
Could not accept the incoming connection.
int net_connect(int *fd, const char *host, int port)
Initiate a TCP connection with host:port.
int net_set_block(int fd)
Set the socket blocking.
#define POLARSSL_ERR_NET_SOCKET_FAILED
Failed to open a socket.
#define POLARSSL_ERR_NET_LISTEN_FAILED
Could not listen on the socket.
int net_recv(void *ctx, unsigned char *buf, size_t len)
Read at most 'len' characters.
#define POLARSSL_ERR_NET_UNKNOWN_HOST
Failed to get an IP address for the given hostname.