28 #if defined(POLARSSL_NET_C)
32 #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
38 #if defined(_WIN32_WCE)
39 #pragma comment( lib, "ws2.lib" )
41 #pragma comment( lib, "ws2_32.lib" )
44 #define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
45 #define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
46 #define close(fd) closesocket(fd)
48 static int wsa_init_done = 0;
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #if defined(POLARSSL_HAVE_TIME)
65 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
66 defined(__DragonflyBSD__)
67 #include <sys/endian.h>
68 #elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
69 defined(EFIX64) || defined(EFI32)
70 #include <machine/endian.h>
72 #include <sys/isa_defs.h>
73 #elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
74 #include <arpa/nameser_compat.h>
84 #if defined(POLARSSL_HAVE_TIME)
88 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
90 typedef UINT32 uint32_t;
100 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
101 #define POLARSSL_HTONS(n) (n)
102 #define POLARSSL_HTONL(n) (n)
104 #define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
105 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
106 #define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
107 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
108 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
109 (((unsigned long )(n) & 0xFF000000) >> 24))
112 unsigned short net_htons(
unsigned short n);
113 unsigned long net_htonl(
unsigned long n);
114 #define net_htons(n) POLARSSL_HTONS(n)
115 #define net_htonl(n) POLARSSL_HTONL(n)
120 int net_connect(
int *fd,
const char *host,
int port )
122 struct sockaddr_in server_addr;
123 struct hostent *server_host;
125 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
130 if( wsa_init_done == 0 )
132 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
138 #if !defined(EFIX64) && !defined(EFI32)
139 signal( SIGPIPE, SIG_IGN );
143 if( ( server_host = gethostbyname( host ) ) == NULL )
146 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
149 memcpy( (
void *) &server_addr.sin_addr,
150 (
void *) server_host->h_addr,
151 server_host->h_length );
153 server_addr.sin_family = AF_INET;
154 server_addr.sin_port = net_htons( port );
156 if( connect( *fd, (
struct sockaddr *) &server_addr,
157 sizeof( server_addr ) ) < 0 )
169 int net_bind(
int *fd,
const char *bind_ip,
int port )
172 struct sockaddr_in server_addr;
174 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
178 if( wsa_init_done == 0 )
180 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
186 #if !defined(EFIX64) && !defined(EFI32)
187 signal( SIGPIPE, SIG_IGN );
191 if( ( *fd = (
int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
195 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
196 (
const char *) &n,
sizeof( n ) );
198 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
199 server_addr.sin_family = AF_INET;
200 server_addr.sin_port = net_htons( port );
202 if( bind_ip != NULL )
204 memset( c, 0,
sizeof( c ) );
205 sscanf( bind_ip,
"%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
207 for( n = 0; n < 4; n++ )
208 if( c[n] < 0 || c[n] > 255 )
212 server_addr.sin_addr.s_addr = net_htonl(
213 ( (uint32_t) c[0] << 24 ) |
214 ( (uint32_t) c[1] << 16 ) |
215 ( (uint32_t) c[2] << 8 ) |
216 ( (uint32_t) c[3] ) );
219 if( bind( *fd, (
struct sockaddr *) &server_addr,
220 sizeof( server_addr ) ) < 0 )
238 static int net_is_blocking(
void )
240 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
242 return( WSAGetLastError() == WSAEWOULDBLOCK );
249 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
261 int net_accept(
int bind_fd,
int *client_fd,
void *client_ip )
263 struct sockaddr_in client_addr;
265 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
266 defined(_SOCKLEN_T_DECLARED)
267 socklen_t n = (socklen_t)
sizeof( client_addr );
269 int n = (int)
sizeof( client_addr );
272 *client_fd = (int) accept( bind_fd, (
struct sockaddr *)
277 if( net_is_blocking() != 0 )
283 if( client_ip != NULL )
284 memcpy( client_ip, &client_addr.sin_addr.s_addr,
285 sizeof( client_addr.sin_addr.s_addr ) );
295 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
298 return( ioctlsocket( fd, FIONBIO, &n ) );
300 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
306 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
309 return( ioctlsocket( fd, FIONBIO, &n ) );
311 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
315 #if defined(POLARSSL_HAVE_TIME)
324 select( 0, NULL, NULL, NULL, &tv );
331 int net_recv(
void *ctx,
unsigned char *buf,
size_t len )
333 int ret = read( *((
int *) ctx), buf, len );
337 if( net_is_blocking() != 0 )
340 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
342 if( WSAGetLastError() == WSAECONNRESET )
345 if( errno == EPIPE || errno == ECONNRESET )
361 int net_send(
void *ctx,
const unsigned char *buf,
size_t len )
363 int ret = write( *((
int *) ctx), buf, len );
367 if( net_is_blocking() != 0 )
370 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
372 if( WSAGetLastError() == WSAECONNRESET )
375 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.