50#ifndef _POSIX_C_SOURCE
51#define _POSIX_C_SOURCE 200112L
54#if defined(__NetBSD__)
56#define _XOPEN_SOURCE 600
60#if !defined(MBEDTLS_CONFIG_FILE)
63#include MBEDTLS_CONFIG_FILE
66#if defined(MBEDTLS_NET_C)
68#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
69 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
71#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
74#if defined(MBEDTLS_PLATFORM_C)
84#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
87#define IS_EINTR( ret ) ( ( ret ) == WSAEINTR )
89#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501)
92#define _WIN32_WINNT 0x0501
101#if defined(_WIN32_WCE)
102#pragma comment( lib, "ws2.lib" )
104#pragma comment( lib, "ws2_32.lib" )
108#define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
109#define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
110#define close(fd) closesocket(fd)
112static int wsa_init_done = 0;
118#include <netinet/in.h>
127#define IS_EINTR( ret ) ( ( ret ) == EINTR )
134#define MSVC_INT_CAST (int)
148static int net_prepare(
void )
150#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
154 if( wsa_init_done == 0 )
162#if !defined(EFIX64) && !defined(EFI32)
174static int check_fd(
int fd,
int for_select )
179#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
211 if( (
ret = net_prepare() ) != 0 )
258 if( (
ret = net_prepare() ) != 0 )
266 if( bind_ip ==
NULL )
286 (
const char *) &
n,
sizeof(
n ) ) != 0 )
293 if(
bind(
ctx->fd,
cur->ai_addr, MSVC_INT_CAST
cur->ai_addrlen ) != 0 )
322#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
358#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
372 void *client_ip,
size_t buf_size,
size_t *ip_len )
379#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
380 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
381 ( defined(__NetBSD__) && defined(socklen_t) )
385 int n = (
int)
sizeof( client_addr );
386 int type_len = (
int)
sizeof(
type );
391 (
void *) &
type, &type_len ) != 0 ||
423 if( net_would_block( bind_ctx ) != 0 )
439 client_ctx->
fd = bind_ctx->
fd;
444 (
struct sockaddr *) &local_addr, &
n ) != 0 ||
445 ( bind_ctx->
fd = (
int)
socket( local_addr.ss_family,
448 (
const char *) &
one,
sizeof(
one ) ) != 0 )
459 if( client_ip !=
NULL )
461 if( client_addr.ss_family ==
AF_INET )
464 *ip_len =
sizeof( addr4->
sin_addr.s_addr );
466 if( buf_size < *ip_len )
474 *ip_len =
sizeof( addr6->
sin6_addr.s6_addr );
476 if( buf_size < *ip_len )
491#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
496 return( fcntl(
ctx->fd, F_SETFL, fcntl(
ctx->fd, F_GETFL ) & ~
O_NONBLOCK ) );
502#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
507 return( fcntl(
ctx->fd, F_SETFL, fcntl(
ctx->fd, F_GETFL ) |
O_NONBLOCK ) );
525 ret = check_fd(
fd, 1 );
529#if defined(__has_feature)
530#if __has_feature(memory_sanitizer)
534 memset( &read_fds, 0,
sizeof( read_fds ) );
535 memset( &write_fds, 0,
sizeof( write_fds ) );
542 rw &= ~MBEDTLS_NET_POLL_READ;
549 rw &= ~MBEDTLS_NET_POLL_WRITE;
557 tv.tv_usec = (
timeout % 1000 ) * 1000;
564 while( IS_EINTR(
ret ) );
584 Sleep( ( usec + 999 ) / 1000 );
587 tv.
tv_sec = usec / 1000000;
588#if defined(__unix__) || defined(__unix) || \
589 ( defined(__APPLE__) && defined(__MACH__) )
592 tv.tv_usec = usec % 1000000;
606 ret = check_fd(
fd, 0 );
614 if( net_would_block(
ctx ) != 0 )
617#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
646 ret = check_fd(
fd, 1 );
654 tv.tv_usec = (
timeout % 1000 ) * 1000;
664#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
688 ret = check_fd(
fd, 0 );
696 if( net_would_block(
ctx ) != 0 )
699#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
INT WSAAPI recvfrom(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags, OUT LPSOCKADDR from, IN OUT INT FAR *fromlen)
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
__kernel_suseconds_t suseconds_t
GLuint GLuint GLsizei GLenum type
GLenum GLuint GLenum GLsizei const GLchar * buf
#define memcpy(s1, s2, n)
Network sockets abstraction layer to integrate Mbed TLS into a BSD-style sockets API.
#define MBEDTLS_ERR_NET_ACCEPT_FAILED
int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
Read at most 'len' characters. If no error occurs, the actual amount read is returned.
#define MBEDTLS_ERR_NET_POLL_FAILED
#define MBEDTLS_ERR_NET_RECV_FAILED
#define MBEDTLS_ERR_NET_CONN_RESET
#define MBEDTLS_NET_LISTEN_BACKLOG
int mbedtls_net_set_nonblock(mbedtls_net_context *ctx)
Set the socket non-blocking.
int mbedtls_net_accept(mbedtls_net_context *bind_ctx, mbedtls_net_context *client_ctx, void *client_ip, size_t buf_size, size_t *ip_len)
Accept a connection from a remote client.
#define MBEDTLS_ERR_NET_BAD_INPUT_DATA
int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
Write at most 'len' characters. If no error occurs, the actual amount read is returned.
#define MBEDTLS_ERR_NET_LISTEN_FAILED
#define MBEDTLS_ERR_NET_BIND_FAILED
int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout)
Read at most 'len' characters, blocking for at most 'timeout' seconds. If no error occurs,...
#define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL
void mbedtls_net_usleep(unsigned long usec)
Portable usleep helper.
#define MBEDTLS_NET_POLL_READ
void mbedtls_net_free(mbedtls_net_context *ctx)
Gracefully shutdown the connection and free associated data.
#define MBEDTLS_NET_POLL_WRITE
#define MBEDTLS_NET_PROTO_UDP
#define MBEDTLS_ERR_NET_UNKNOWN_HOST
#define MBEDTLS_ERR_NET_INVALID_CONTEXT
#define MBEDTLS_NET_PROTO_TCP
int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto)
Create a receiving socket on bind_ip:port in the chosen protocol. If bind_ip == NULL,...
#define MBEDTLS_ERR_NET_SOCKET_FAILED
int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout)
Check and wait for the context to be ready for read/write.
int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto)
Initiate a connection with host:port in the given protocol.
#define MBEDTLS_ERR_NET_CONNECT_FAILED
#define MBEDTLS_ERR_NET_SEND_FAILED
void mbedtls_net_init(mbedtls_net_context *ctx)
Initialize a context Just makes the context ready to be used or freed safely.
int mbedtls_net_set_block(mbedtls_net_context *ctx)
Set the socket blocking.
Configuration options (set of defines)
namespace GUID const ADDRINFOEXW * hints
INT WSAAPI getsockname(IN SOCKET s, OUT LPSOCKADDR name, IN OUT INT FAR *namelen)
INT WSAAPI setsockopt(IN SOCKET s, IN INT level, IN INT optname, IN CONST CHAR FAR *optval, IN INT optlen)
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
INT WSAAPI getsockopt(IN SOCKET s, IN INT level, IN INT optname, OUT CHAR FAR *optval, IN OUT INT FAR *optlen)
INT WSAAPI shutdown(IN SOCKET s, IN INT how)
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
SOCKET WSAAPI accept(IN SOCKET s, OUT LPSOCKADDR addr, OUT INT FAR *addrlen)
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
#define MBEDTLS_ERR_SSL_TIMEOUT
#define MBEDTLS_ERR_SSL_WANT_WRITE
#define MBEDTLS_ERR_SSL_WANT_READ
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
#define FD_ISSET(fd, set)
int PASCAL FAR WSAGetLastError(void)