28 #if defined(POLARSSL_NET_C)
32 #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
35 #if defined(POLARSSL_HAVE_IPV6)
36 #define _WIN32_WINNT 0x0501
44 #if defined(_WIN32_WCE)
45 #pragma comment( lib, "ws2.lib" )
47 #pragma comment( lib, "ws2_32.lib" )
51 #define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
52 #define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
53 #define close(fd) closesocket(fd)
55 static int wsa_init_done = 0;
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #if defined(POLARSSL_HAVE_TIME)
72 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
73 defined(__DragonflyBSD__)
74 #include <sys/endian.h>
75 #elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
76 defined(EFIX64) || defined(EFI32)
77 #include <machine/endian.h>
79 #include <sys/isa_defs.h>
80 #elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
81 #include <arpa/nameser_compat.h>
91 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
93 #define snprintf _snprintf
96 #if defined(POLARSSL_HAVE_TIME)
100 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
102 typedef UINT32 uint32_t;
104 #include <inttypes.h>
112 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
113 #define POLARSSL_HTONS(n) (n)
114 #define POLARSSL_HTONL(n) (n)
116 #define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
117 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
118 #define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
119 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
120 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
121 (((unsigned long )(n) & 0xFF000000) >> 24))
124 unsigned short net_htons(
unsigned short n);
125 unsigned long net_htonl(
unsigned long n);
126 #define net_htons(n) POLARSSL_HTONS(n)
127 #define net_htonl(n) POLARSSL_HTONL(n)
132 static int net_prepare(
void )
134 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
138 if( wsa_init_done == 0 )
140 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
146 #if !defined(EFIX64) && !defined(EFI32)
147 signal( SIGPIPE, SIG_IGN );
156 int net_connect(
int *fd,
const char *host,
int port )
158 #if defined(POLARSSL_HAVE_IPV6)
160 struct addrinfo hints, *addr_list, *cur;
163 if( ( ret = net_prepare() ) != 0 )
167 memset( port_str, 0,
sizeof( port_str ) );
168 snprintf( port_str,
sizeof( port_str ),
"%d", port );
171 memset( &hints, 0,
sizeof( hints ) );
172 hints.ai_family = AF_UNSPEC;
173 hints.ai_socktype = SOCK_STREAM;
174 hints.ai_protocol = IPPROTO_TCP;
176 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
181 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
183 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
191 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
201 freeaddrinfo( addr_list );
209 struct sockaddr_in server_addr;
210 struct hostent *server_host;
212 if( ( ret = net_prepare() ) != 0 )
215 if( ( server_host = gethostbyname( host ) ) == NULL )
218 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
221 memcpy( (
void *) &server_addr.sin_addr,
222 (
void *) server_host->h_addr,
223 server_host->h_length );
225 server_addr.sin_family = AF_INET;
226 server_addr.sin_port = net_htons( port );
228 if( connect( *fd, (
struct sockaddr *) &server_addr,
229 sizeof( server_addr ) ) < 0 )
242 int net_bind(
int *fd,
const char *bind_ip,
int port )
244 #if defined(POLARSSL_HAVE_IPV6)
246 struct addrinfo hints, *addr_list, *cur;
249 if( ( ret = net_prepare() ) != 0 )
253 memset( port_str, 0,
sizeof( port_str ) );
254 snprintf( port_str,
sizeof( port_str ),
"%d", port );
257 memset( &hints, 0,
sizeof( hints ) );
258 hints.ai_family = AF_UNSPEC;
259 hints.ai_socktype = SOCK_STREAM;
260 hints.ai_protocol = IPPROTO_TCP;
261 if( bind_ip == NULL )
262 hints.ai_flags = AI_PASSIVE;
264 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
269 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
271 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
280 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
281 (
const char *) &n,
sizeof( n ) );
283 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
302 freeaddrinfo( addr_list );
310 struct sockaddr_in server_addr;
312 if( ( ret = net_prepare() ) != 0 )
315 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
319 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
320 (
const char *) &n,
sizeof( n ) );
322 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
323 server_addr.sin_family = AF_INET;
324 server_addr.sin_port = net_htons( port );
326 if( bind_ip != NULL )
328 memset( c, 0,
sizeof( c ) );
329 sscanf( bind_ip,
"%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
331 for( n = 0; n < 4; n++ )
332 if( c[n] < 0 || c[n] > 255 )
336 server_addr.sin_addr.s_addr = net_htonl(
337 ( (uint32_t) c[0] << 24 ) |
338 ( (uint32_t) c[1] << 16 ) |
339 ( (uint32_t) c[2] << 8 ) |
340 ( (uint32_t) c[3] ) );
343 if( bind( *fd, (
struct sockaddr *) &server_addr,
344 sizeof( server_addr ) ) < 0 )
363 static int net_is_blocking(
void )
365 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
367 return( WSAGetLastError() == WSAEWOULDBLOCK );
374 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
386 int net_accept(
int bind_fd,
int *client_fd,
void *client_ip )
388 #if defined(POLARSSL_HAVE_IPV6)
389 struct sockaddr_storage client_addr;
391 struct sockaddr_in client_addr;
394 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
395 defined(_SOCKLEN_T_DECLARED)
396 socklen_t n = (socklen_t)
sizeof( client_addr );
398 int n = (int)
sizeof( client_addr );
401 *client_fd = (int) accept( bind_fd, (
struct sockaddr *)
406 if( net_is_blocking() != 0 )
412 if( client_ip != NULL )
414 #if defined(POLARSSL_HAVE_IPV6)
415 if( client_addr.ss_family == AF_INET )
417 struct sockaddr_in *addr4 = (
struct sockaddr_in *) &client_addr;
418 memcpy( client_ip, &addr4->sin_addr.s_addr,
419 sizeof( addr4->sin_addr.s_addr ) );
423 struct sockaddr_in6 *addr6 = (
struct sockaddr_in6 *) &client_addr;
424 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
425 sizeof( addr6->sin6_addr.s6_addr ) );
428 memcpy( client_ip, &client_addr.sin_addr.s_addr,
429 sizeof( client_addr.sin_addr.s_addr ) );
441 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
444 return( ioctlsocket( fd, FIONBIO, &n ) );
446 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
452 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
455 return( ioctlsocket( fd, FIONBIO, &n ) );
457 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
461 #if defined(POLARSSL_HAVE_TIME)
470 select( 0, NULL, NULL, NULL, &tv );
477 int net_recv(
void *ctx,
unsigned char *buf,
size_t len )
479 int ret = read( *((
int *) ctx), buf, len );
483 if( net_is_blocking() != 0 )
486 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
488 if( WSAGetLastError() == WSAECONNRESET )
491 if( errno == EPIPE || errno == ECONNRESET )
507 int net_send(
void *ctx,
const unsigned char *buf,
size_t len )
509 int ret = write( *((
int *) ctx), buf, len );
513 if( net_is_blocking() != 0 )
516 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
518 if( WSAGetLastError() == WSAECONNRESET )
521 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.