PolarSSL v1.3.2
net.c
Go to the documentation of this file.
1 /*
2  * TCP networking functions
3  *
4  * Copyright (C) 2006-2013, Brainspark B.V.
5  *
6  * This file is part of PolarSSL (http://www.polarssl.org)
7  * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8  *
9  * All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
26 #include "polarssl/config.h"
27 
28 #if defined(POLARSSL_NET_C)
29 
30 #include "polarssl/net.h"
31 
32 #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
33  !defined(EFI32)
34 
35 #include <winsock2.h>
36 #include <windows.h>
37 
38 #if defined(_WIN32_WCE)
39 #pragma comment( lib, "ws2.lib" )
40 #else
41 #pragma comment( lib, "ws2_32.lib" )
42 #endif
43 
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)
47 
48 static int wsa_init_done = 0;
49 
50 #else
51 
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)
57 #include <sys/time.h>
58 #endif
59 #include <unistd.h>
60 #include <signal.h>
61 #include <fcntl.h>
62 #include <netdb.h>
63 #include <errno.h>
64 
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>
71 #elif defined(sun)
72 #include <sys/isa_defs.h>
73 #elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
74 #include <arpa/nameser_compat.h>
75 #else
76 #include <endian.h>
77 #endif
78 
79 #endif
80 
81 #include <stdlib.h>
82 #include <stdio.h>
83 
84 #if defined(POLARSSL_HAVE_TIME)
85 #include <time.h>
86 #endif
87 
88 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
89 #include <basetsd.h>
90 typedef UINT32 uint32_t;
91 #else
92 #include <inttypes.h>
93 #endif
94 
95 /*
96  * htons() is not always available.
97  * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
98  * to help determine endianness.
99  */
100 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
101 #define POLARSSL_HTONS(n) (n)
102 #define POLARSSL_HTONL(n) (n)
103 #else
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))
110 #endif
111 
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)
116 
117 /*
118  * Initiate a TCP connection with host:port
119  */
120 int net_connect( int *fd, const char *host, int port )
121 {
122  struct sockaddr_in server_addr;
123  struct hostent *server_host;
124 
125 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
126  !defined(EFI32)
127 
128  WSADATA wsaData;
129 
130  if( wsa_init_done == 0 )
131  {
132  if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
134 
135  wsa_init_done = 1;
136  }
137 #else
138 #if !defined(EFIX64) && !defined(EFI32)
139  signal( SIGPIPE, SIG_IGN );
140 #endif
141 #endif
142 
143  if( ( server_host = gethostbyname( host ) ) == NULL )
145 
146  if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
148 
149  memcpy( (void *) &server_addr.sin_addr,
150  (void *) server_host->h_addr,
151  server_host->h_length );
152 
153  server_addr.sin_family = AF_INET;
154  server_addr.sin_port = net_htons( port );
155 
156  if( connect( *fd, (struct sockaddr *) &server_addr,
157  sizeof( server_addr ) ) < 0 )
158  {
159  close( *fd );
161  }
162 
163  return( 0 );
164 }
165 
166 /*
167  * Create a listening socket on bind_ip:port
168  */
169 int net_bind( int *fd, const char *bind_ip, int port )
170 {
171  int n, c[4];
172  struct sockaddr_in server_addr;
173 
174 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
175  !defined(EFI32)
176  WSADATA wsaData;
177 
178  if( wsa_init_done == 0 )
179  {
180  if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
182 
183  wsa_init_done = 1;
184  }
185 #else
186 #if !defined(EFIX64) && !defined(EFI32)
187  signal( SIGPIPE, SIG_IGN );
188 #endif
189 #endif
190 
191  if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
193 
194  n = 1;
195  setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
196  (const char *) &n, sizeof( n ) );
197 
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 );
201 
202  if( bind_ip != NULL )
203  {
204  memset( c, 0, sizeof( c ) );
205  sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
206 
207  for( n = 0; n < 4; n++ )
208  if( c[n] < 0 || c[n] > 255 )
209  break;
210 
211  if( n == 4 )
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] ) );
217  }
218 
219  if( bind( *fd, (struct sockaddr *) &server_addr,
220  sizeof( server_addr ) ) < 0 )
221  {
222  close( *fd );
224  }
225 
226  if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
227  {
228  close( *fd );
230  }
231 
232  return( 0 );
233 }
234 
235 /*
236  * Check if the current operation is blocking
237  */
238 static int net_is_blocking( void )
239 {
240 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
241  !defined(EFI32)
242  return( WSAGetLastError() == WSAEWOULDBLOCK );
243 #else
244  switch( errno )
245  {
246 #if defined EAGAIN
247  case EAGAIN:
248 #endif
249 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
250  case EWOULDBLOCK:
251 #endif
252  return( 1 );
253  }
254  return( 0 );
255 #endif
256 }
257 
258 /*
259  * Accept a connection from a remote client
260  */
261 int net_accept( int bind_fd, int *client_fd, void *client_ip )
262 {
263  struct sockaddr_in client_addr;
264 
265 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
266  defined(_SOCKLEN_T_DECLARED)
267  socklen_t n = (socklen_t) sizeof( client_addr );
268 #else
269  int n = (int) sizeof( client_addr );
270 #endif
271 
272  *client_fd = (int) accept( bind_fd, (struct sockaddr *)
273  &client_addr, &n );
274 
275  if( *client_fd < 0 )
276  {
277  if( net_is_blocking() != 0 )
278  return( POLARSSL_ERR_NET_WANT_READ );
279 
281  }
282 
283  if( client_ip != NULL )
284  memcpy( client_ip, &client_addr.sin_addr.s_addr,
285  sizeof( client_addr.sin_addr.s_addr ) );
286 
287  return( 0 );
288 }
289 
290 /*
291  * Set the socket blocking or non-blocking
292  */
293 int net_set_block( int fd )
294 {
295 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
296  !defined(EFI32)
297  u_long n = 0;
298  return( ioctlsocket( fd, FIONBIO, &n ) );
299 #else
300  return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
301 #endif
302 }
303 
304 int net_set_nonblock( int fd )
305 {
306 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
307  !defined(EFI32)
308  u_long n = 1;
309  return( ioctlsocket( fd, FIONBIO, &n ) );
310 #else
311  return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
312 #endif
313 }
314 
315 #if defined(POLARSSL_HAVE_TIME)
316 /*
317  * Portable usleep helper
318  */
319 void net_usleep( unsigned long usec )
320 {
321  struct timeval tv;
322  tv.tv_sec = 0;
323  tv.tv_usec = usec;
324  select( 0, NULL, NULL, NULL, &tv );
325 }
326 #endif /* POLARSSL_HAVE_TIME */
327 
328 /*
329  * Read at most 'len' characters
330  */
331 int net_recv( void *ctx, unsigned char *buf, size_t len )
332 {
333  int ret = read( *((int *) ctx), buf, len );
334 
335  if( ret < 0 )
336  {
337  if( net_is_blocking() != 0 )
338  return( POLARSSL_ERR_NET_WANT_READ );
339 
340 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
341  !defined(EFI32)
342  if( WSAGetLastError() == WSAECONNRESET )
343  return( POLARSSL_ERR_NET_CONN_RESET );
344 #else
345  if( errno == EPIPE || errno == ECONNRESET )
346  return( POLARSSL_ERR_NET_CONN_RESET );
347 
348  if( errno == EINTR )
349  return( POLARSSL_ERR_NET_WANT_READ );
350 #endif
351 
353  }
354 
355  return( ret );
356 }
357 
358 /*
359  * Write at most 'len' characters
360  */
361 int net_send( void *ctx, const unsigned char *buf, size_t len )
362 {
363  int ret = write( *((int *) ctx), buf, len );
364 
365  if( ret < 0 )
366  {
367  if( net_is_blocking() != 0 )
368  return( POLARSSL_ERR_NET_WANT_WRITE );
369 
370 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
371  !defined(EFI32)
372  if( WSAGetLastError() == WSAECONNRESET )
373  return( POLARSSL_ERR_NET_CONN_RESET );
374 #else
375  if( errno == EPIPE || errno == ECONNRESET )
376  return( POLARSSL_ERR_NET_CONN_RESET );
377 
378  if( errno == EINTR )
379  return( POLARSSL_ERR_NET_WANT_WRITE );
380 #endif
381 
383  }
384 
385  return( ret );
386 }
387 
388 /*
389  * Gracefully close the connection
390  */
391 void net_close( int fd )
392 {
393  shutdown( fd, 2 );
394  close( fd );
395 }
396 
397 #endif
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.
Definition: net.h:35
#define POLARSSL_ERR_NET_RECV_FAILED
Reading information from the socket failed.
Definition: net.h:38
#define POLARSSL_ERR_NET_WANT_WRITE
Connection requires a write call.
Definition: net.h:42
Network communication functions.
int net_send(void *ctx, const unsigned char *buf, size_t len)
Write at most &#39;len&#39; 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.
Definition: net.h:40
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.
Definition: net.h:34
#define POLARSSL_NET_LISTEN_BACKLOG
The backlog that listen() should use.
Definition: net.h:44
#define POLARSSL_ERR_NET_SEND_FAILED
Sending information through the socket failed.
Definition: net.h:39
#define POLARSSL_ERR_NET_WANT_READ
Connection requires a read call.
Definition: net.h:41
#define POLARSSL_ERR_NET_ACCEPT_FAILED
Could not accept the incoming connection.
Definition: net.h:37
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.
Definition: net.h:33
#define POLARSSL_ERR_NET_LISTEN_FAILED
Could not listen on the socket.
Definition: net.h:36
int net_recv(void *ctx, unsigned char *buf, size_t len)
Read at most &#39;len&#39; characters.
#define POLARSSL_ERR_NET_UNKNOWN_HOST
Failed to get an IP address for the given hostname.
Definition: net.h:32