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)
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 ) == SOCKET_ERROR )
127 signal( SIGPIPE, SIG_IGN );
130 if( ( server_host = gethostbyname( host ) ) == NULL )
133 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
136 memcpy( (
void *) &server_addr.sin_addr,
137 (
void *) server_host->h_addr,
138 server_host->h_length );
140 server_addr.sin_family = AF_INET;
141 server_addr.sin_port = net_htons( port );
143 if( connect( *fd, (
struct sockaddr *) &server_addr,
144 sizeof( server_addr ) ) < 0 )
156 int net_bind(
int *fd,
const char *bind_ip,
int port )
159 struct sockaddr_in server_addr;
161 #if defined(_WIN32) || defined(_WIN32_WCE)
164 if( wsa_init_done == 0 )
166 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
172 signal( SIGPIPE, SIG_IGN );
175 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
179 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
180 (
const char *) &n,
sizeof( n ) );
182 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
183 server_addr.sin_family = AF_INET;
184 server_addr.sin_port = net_htons( port );
186 if( bind_ip != NULL )
188 memset( c, 0,
sizeof( c ) );
189 sscanf( bind_ip,
"%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
191 for( n = 0; n < 4; n++ )
192 if( c[n] < 0 || c[n] > 255 )
196 server_addr.sin_addr.s_addr = net_htonl(
197 ( (uint32_t) c[0] << 24 ) |
198 ( (uint32_t) c[1] << 16 ) |
199 ( (uint32_t) c[2] << 8 ) |
200 ( (uint32_t) c[3] ) );
203 if( bind( *fd, (
struct sockaddr *) &server_addr,
204 sizeof( server_addr ) ) < 0 )
222 static int net_is_blocking(
void )
224 #if defined(_WIN32) || defined(_WIN32_WCE)
225 return( WSAGetLastError() == WSAEWOULDBLOCK );
232 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
244 int net_accept(
int bind_fd,
int *client_fd,
void *client_ip )
246 struct sockaddr_in client_addr;
248 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
249 defined(_SOCKLEN_T_DECLARED)
250 socklen_t n = (socklen_t)
sizeof( client_addr );
252 int n = (int)
sizeof( client_addr );
255 *client_fd = accept( bind_fd, (
struct sockaddr *)
260 if( net_is_blocking() != 0 )
266 if( client_ip != NULL )
267 memcpy( client_ip, &client_addr.sin_addr.s_addr,
268 sizeof( client_addr.sin_addr.s_addr ) );
278 #if defined(_WIN32) || defined(_WIN32_WCE)
280 return( ioctlsocket( fd, FIONBIO, &n ) );
282 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
288 #if defined(_WIN32) || defined(_WIN32_WCE)
290 return( ioctlsocket( fd, FIONBIO, &n ) );
292 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
304 select( 0, NULL, NULL, NULL, &tv );
310 int net_recv(
void *ctx,
unsigned char *buf,
size_t len )
312 int ret = read( *((
int *) ctx), buf, len );
316 if( net_is_blocking() != 0 )
319 #if defined(_WIN32) || defined(_WIN32_WCE)
320 if( WSAGetLastError() == WSAECONNRESET )
323 if( errno == EPIPE || errno == ECONNRESET )
339 int net_send(
void *ctx,
const unsigned char *buf,
size_t len )
341 int ret = write( *((
int *) ctx), buf, len );
345 if( net_is_blocking() != 0 )
348 #if defined(_WIN32) || defined(_WIN32_WCE)
349 if( WSAGetLastError() == WSAECONNRESET )
352 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.