26 #if !defined(POLARSSL_CONFIG_FILE)
29 #include POLARSSL_CONFIG_FILE
32 #if defined(POLARSSL_NET_C)
36 #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
39 #if defined(POLARSSL_HAVE_IPV6)
44 #define _WIN32_WINNT 0x0501
52 #if defined(_WIN32_WCE)
53 #pragma comment( lib, "ws2.lib" )
55 #pragma comment( lib, "ws2_32.lib" )
59 #define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
60 #define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
61 #define close(fd) closesocket(fd)
63 static int wsa_init_done = 0;
67 #include <sys/types.h>
68 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71 #if defined(POLARSSL_HAVE_TIME)
80 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
81 defined(__DragonFly__)
82 #include <sys/endian.h>
83 #elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
84 defined(EFIX64) || defined(EFI32)
85 #include <machine/endian.h>
87 #include <sys/isa_defs.h>
88 #elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
89 #include <arpa/nameser_compat.h>
99 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
101 #define snprintf _snprintf
104 #if defined(POLARSSL_HAVE_TIME)
108 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
110 typedef UINT32 uint32_t;
112 #include <inttypes.h>
120 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
121 __BYTE_ORDER == __BIG_ENDIAN
122 #define POLARSSL_HTONS(n) (n)
123 #define POLARSSL_HTONL(n) (n)
125 #define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
126 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
127 #define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
128 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
129 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
130 (((unsigned long )(n) & 0xFF000000) >> 24))
133 unsigned short net_htons(
unsigned short n);
134 unsigned long net_htonl(
unsigned long n);
135 #define net_htons(n) POLARSSL_HTONS(n)
136 #define net_htonl(n) POLARSSL_HTONL(n)
141 static int net_prepare(
void )
143 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
147 if( wsa_init_done == 0 )
149 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
155 #if !defined(EFIX64) && !defined(EFI32)
156 signal( SIGPIPE, SIG_IGN );
165 int net_connect(
int *fd,
const char *host,
int port )
167 #if defined(POLARSSL_HAVE_IPV6)
169 struct addrinfo hints, *addr_list, *cur;
172 if( ( ret = net_prepare() ) != 0 )
176 memset( port_str, 0,
sizeof( port_str ) );
177 snprintf( port_str,
sizeof( port_str ),
"%d", port );
180 memset( &hints, 0,
sizeof( hints ) );
181 hints.ai_family = AF_UNSPEC;
182 hints.ai_socktype = SOCK_STREAM;
183 hints.ai_protocol = IPPROTO_TCP;
185 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
190 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
192 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
200 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
210 freeaddrinfo( addr_list );
218 struct sockaddr_in server_addr;
219 struct hostent *server_host;
221 if( ( ret = net_prepare() ) != 0 )
224 if( ( server_host = gethostbyname( host ) ) == NULL )
227 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
230 memcpy( (
void *) &server_addr.sin_addr,
231 (
void *) server_host->h_addr,
232 server_host->h_length );
234 server_addr.sin_family = AF_INET;
235 server_addr.sin_port = net_htons( port );
237 if( connect( *fd, (
struct sockaddr *) &server_addr,
238 sizeof( server_addr ) ) < 0 )
251 int net_bind(
int *fd,
const char *bind_ip,
int port )
253 #if defined(POLARSSL_HAVE_IPV6)
255 struct addrinfo hints, *addr_list, *cur;
258 if( ( ret = net_prepare() ) != 0 )
262 memset( port_str, 0,
sizeof( port_str ) );
263 snprintf( port_str,
sizeof( port_str ),
"%d", port );
266 memset( &hints, 0,
sizeof( hints ) );
267 hints.ai_family = AF_UNSPEC;
268 hints.ai_socktype = SOCK_STREAM;
269 hints.ai_protocol = IPPROTO_TCP;
270 if( bind_ip == NULL )
271 hints.ai_flags = AI_PASSIVE;
273 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
278 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
280 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
289 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
290 (
const char *) &n,
sizeof( n ) ) != 0 )
297 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
316 freeaddrinfo( addr_list );
324 struct sockaddr_in server_addr;
326 if( ( ret = net_prepare() ) != 0 )
329 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
333 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
334 (
const char *) &n,
sizeof( n ) );
336 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
337 server_addr.sin_family = AF_INET;
338 server_addr.sin_port = net_htons( port );
340 if( bind_ip != NULL )
342 memset( c, 0,
sizeof( c ) );
343 sscanf( bind_ip,
"%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
345 for( n = 0; n < 4; n++ )
346 if( c[n] < 0 || c[n] > 255 )
350 server_addr.sin_addr.s_addr = net_htonl(
351 ( (uint32_t) c[0] << 24 ) |
352 ( (uint32_t) c[1] << 16 ) |
353 ( (uint32_t) c[2] << 8 ) |
354 ( (uint32_t) c[3] ) );
357 if( bind( *fd, (
struct sockaddr *) &server_addr,
358 sizeof( server_addr ) ) < 0 )
374 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
380 static int net_would_block(
int fd )
383 return( WSAGetLastError() == WSAEWOULDBLOCK );
392 static int net_would_block(
int fd )
397 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
405 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
417 int net_accept(
int bind_fd,
int *client_fd,
void *client_ip )
419 #if defined(POLARSSL_HAVE_IPV6)
420 struct sockaddr_storage client_addr;
422 struct sockaddr_in client_addr;
425 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
426 defined(_SOCKLEN_T_DECLARED)
427 socklen_t n = (socklen_t)
sizeof( client_addr );
429 int n = (int)
sizeof( client_addr );
432 *client_fd = (int) accept( bind_fd, (
struct sockaddr *)
437 if( net_would_block( *client_fd ) != 0 )
443 if( client_ip != NULL )
445 #if defined(POLARSSL_HAVE_IPV6)
446 if( client_addr.ss_family == AF_INET )
448 struct sockaddr_in *addr4 = (
struct sockaddr_in *) &client_addr;
449 memcpy( client_ip, &addr4->sin_addr.s_addr,
450 sizeof( addr4->sin_addr.s_addr ) );
454 struct sockaddr_in6 *addr6 = (
struct sockaddr_in6 *) &client_addr;
455 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
456 sizeof( addr6->sin6_addr.s6_addr ) );
459 memcpy( client_ip, &client_addr.sin_addr.s_addr,
460 sizeof( client_addr.sin_addr.s_addr ) );
472 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
475 return( ioctlsocket( fd, FIONBIO, &n ) );
477 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
483 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
486 return( ioctlsocket( fd, FIONBIO, &n ) );
488 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
492 #if defined(POLARSSL_HAVE_TIME)
501 select( 0, NULL, NULL, NULL, &tv );
508 int net_recv(
void *ctx,
unsigned char *buf,
size_t len )
510 int fd = *((
int *) ctx);
511 int ret = read( fd, buf, len );
515 if( net_would_block( fd ) != 0 )
518 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
520 if( WSAGetLastError() == WSAECONNRESET )
523 if( errno == EPIPE || errno == ECONNRESET )
539 int net_send(
void *ctx,
const unsigned char *buf,
size_t len )
541 int fd = *((
int *) ctx);
542 int ret = write( fd, buf, len );
546 if( net_would_block( fd ) != 0 )
549 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
551 if( WSAGetLastError() == WSAECONNRESET )
554 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.