PolarSSL v1.3.8
net.c
Go to the documentation of this file.
1 /*
2  * TCP networking functions
3  *
4  * Copyright (C) 2006-2014, 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 #if !defined(POLARSSL_CONFIG_FILE)
27 #include "polarssl/config.h"
28 #else
29 #include POLARSSL_CONFIG_FILE
30 #endif
31 
32 #if defined(POLARSSL_NET_C)
33 
34 #include "polarssl/net.h"
35 
36 #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
37  !defined(EFI32)
38 
39 #if defined(POLARSSL_HAVE_IPV6)
40 #ifdef _WIN32_WINNT
41 #undef _WIN32_WINNT
42 #endif
43 /* Enables getaddrinfo() & Co */
44 #define _WIN32_WINNT 0x0501
45 #include <ws2tcpip.h>
46 #endif
47 
48 #include <winsock2.h>
49 #include <windows.h>
50 
51 #if defined(_MSC_VER)
52 #if defined(_WIN32_WCE)
53 #pragma comment( lib, "ws2.lib" )
54 #else
55 #pragma comment( lib, "ws2_32.lib" )
56 #endif
57 #endif /* _MSC_VER */
58 
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)
62 
63 static int wsa_init_done = 0;
64 
65 #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
66 
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)
72 #include <sys/time.h>
73 #endif
74 #include <unistd.h>
75 #include <signal.h>
76 #include <fcntl.h>
77 #include <netdb.h>
78 #include <errno.h>
79 
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>
86 #elif defined(sun)
87 #include <sys/isa_defs.h>
88 #elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
89 #include <arpa/nameser_compat.h>
90 #else
91 #include <endian.h>
92 #endif
93 
94 #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
95 
96 #include <stdlib.h>
97 #include <stdio.h>
98 
99 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
100  !defined(EFI32)
101 #define snprintf _snprintf
102 #endif
103 
104 #if defined(POLARSSL_HAVE_TIME)
105 #include <time.h>
106 #endif
107 
108 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
109 #include <basetsd.h>
110 typedef UINT32 uint32_t;
111 #else
112 #include <inttypes.h>
113 #endif
114 
115 /*
116  * htons() is not always available.
117  * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and
118  * __BIG_ENDIAN to help determine endianness.
119  */
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)
124 #else
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))
131 #endif
132 
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)
137 
138 /*
139  * Prepare for using the sockets interface
140  */
141 static int net_prepare( void )
142 {
143 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
144  !defined(EFI32)
145  WSADATA wsaData;
146 
147  if( wsa_init_done == 0 )
148  {
149  if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
151 
152  wsa_init_done = 1;
153  }
154 #else
155 #if !defined(EFIX64) && !defined(EFI32)
156  signal( SIGPIPE, SIG_IGN );
157 #endif
158 #endif
159  return( 0 );
160 }
161 
162 /*
163  * Initiate a TCP connection with host:port
164  */
165 int net_connect( int *fd, const char *host, int port )
166 {
167 #if defined(POLARSSL_HAVE_IPV6)
168  int ret;
169  struct addrinfo hints, *addr_list, *cur;
170  char port_str[6];
171 
172  if( ( ret = net_prepare() ) != 0 )
173  return( ret );
174 
175  /* getaddrinfo expects port as a string */
176  memset( port_str, 0, sizeof( port_str ) );
177  snprintf( port_str, sizeof( port_str ), "%d", port );
178 
179  /* Do name resolution with both IPv6 and IPv4, but only TCP */
180  memset( &hints, 0, sizeof( hints ) );
181  hints.ai_family = AF_UNSPEC;
182  hints.ai_socktype = SOCK_STREAM;
183  hints.ai_protocol = IPPROTO_TCP;
184 
185  if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
187 
188  /* Try the sockaddrs until a connection succeeds */
190  for( cur = addr_list; cur != NULL; cur = cur->ai_next )
191  {
192  *fd = (int) socket( cur->ai_family, cur->ai_socktype,
193  cur->ai_protocol );
194  if( *fd < 0 )
195  {
197  continue;
198  }
199 
200  if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
201  {
202  ret = 0;
203  break;
204  }
205 
206  close( *fd );
208  }
209 
210  freeaddrinfo( addr_list );
211 
212  return( ret );
213 
214 #else
215  /* Legacy IPv4-only version */
216 
217  int ret;
218  struct sockaddr_in server_addr;
219  struct hostent *server_host;
220 
221  if( ( ret = net_prepare() ) != 0 )
222  return( ret );
223 
224  if( ( server_host = gethostbyname( host ) ) == NULL )
226 
227  if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
229 
230  memcpy( (void *) &server_addr.sin_addr,
231  (void *) server_host->h_addr,
232  server_host->h_length );
233 
234  server_addr.sin_family = AF_INET;
235  server_addr.sin_port = net_htons( port );
236 
237  if( connect( *fd, (struct sockaddr *) &server_addr,
238  sizeof( server_addr ) ) < 0 )
239  {
240  close( *fd );
242  }
243 
244  return( 0 );
245 #endif /* POLARSSL_HAVE_IPV6 */
246 }
247 
248 /*
249  * Create a listening socket on bind_ip:port
250  */
251 int net_bind( int *fd, const char *bind_ip, int port )
252 {
253 #if defined(POLARSSL_HAVE_IPV6)
254  int n, ret;
255  struct addrinfo hints, *addr_list, *cur;
256  char port_str[6];
257 
258  if( ( ret = net_prepare() ) != 0 )
259  return( ret );
260 
261  /* getaddrinfo expects port as a string */
262  memset( port_str, 0, sizeof( port_str ) );
263  snprintf( port_str, sizeof( port_str ), "%d", port );
264 
265  /* Bind to IPv6 and/or IPv4, but only in TCP */
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;
272 
273  if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
275 
276  /* Try the sockaddrs until a binding succeeds */
278  for( cur = addr_list; cur != NULL; cur = cur->ai_next )
279  {
280  *fd = (int) socket( cur->ai_family, cur->ai_socktype,
281  cur->ai_protocol );
282  if( *fd < 0 )
283  {
285  continue;
286  }
287 
288  n = 1;
289  if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
290  (const char *) &n, sizeof( n ) ) != 0 )
291  {
292  close( *fd );
294  continue;
295  }
296 
297  if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
298  {
299  close( *fd );
301  continue;
302  }
303 
304  if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
305  {
306  close( *fd );
308  continue;
309  }
310 
311  /* I we ever get there, it's a success */
312  ret = 0;
313  break;
314  }
315 
316  freeaddrinfo( addr_list );
317 
318  return( ret );
319 
320 #else
321  /* Legacy IPv4-only version */
322 
323  int ret, n, c[4];
324  struct sockaddr_in server_addr;
325 
326  if( ( ret = net_prepare() ) != 0 )
327  return( ret );
328 
329  if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
331 
332  n = 1;
333  setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
334  (const char *) &n, sizeof( n ) );
335 
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 );
339 
340  if( bind_ip != NULL )
341  {
342  memset( c, 0, sizeof( c ) );
343  sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
344 
345  for( n = 0; n < 4; n++ )
346  if( c[n] < 0 || c[n] > 255 )
347  break;
348 
349  if( n == 4 )
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] ) );
355  }
356 
357  if( bind( *fd, (struct sockaddr *) &server_addr,
358  sizeof( server_addr ) ) < 0 )
359  {
360  close( *fd );
362  }
363 
364  if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
365  {
366  close( *fd );
368  }
369 
370  return( 0 );
371 #endif /* POLARSSL_HAVE_IPV6 */
372 }
373 
374 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
375  !defined(EFI32)
376 /*
377  * Check if the requested operation would be blocking on a non-blocking socket
378  * and thus 'failed' with a negative return value.
379  */
380 static int net_would_block( int fd )
381 {
382  ((void) fd);
383  return( WSAGetLastError() == WSAEWOULDBLOCK );
384 }
385 #else
386 /*
387  * Check if the requested operation would be blocking on a non-blocking socket
388  * and thus 'failed' with a negative return value.
389  *
390  * Note: on a blocking socket this function always returns 0!
391  */
392 static int net_would_block( int fd )
393 {
394  /*
395  * Never return 'WOULD BLOCK' on a non-blocking socket
396  */
397  if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
398  return( 0 );
399 
400  switch( errno )
401  {
402 #if defined EAGAIN
403  case EAGAIN:
404 #endif
405 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
406  case EWOULDBLOCK:
407 #endif
408  return( 1 );
409  }
410  return( 0 );
411 }
412 #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
413 
414 /*
415  * Accept a connection from a remote client
416  */
417 int net_accept( int bind_fd, int *client_fd, void *client_ip )
418 {
419 #if defined(POLARSSL_HAVE_IPV6)
420  struct sockaddr_storage client_addr;
421 #else
422  struct sockaddr_in client_addr;
423 #endif
424 
425 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
426  defined(_SOCKLEN_T_DECLARED)
427  socklen_t n = (socklen_t) sizeof( client_addr );
428 #else
429  int n = (int) sizeof( client_addr );
430 #endif
431 
432  *client_fd = (int) accept( bind_fd, (struct sockaddr *)
433  &client_addr, &n );
434 
435  if( *client_fd < 0 )
436  {
437  if( net_would_block( *client_fd ) != 0 )
438  return( POLARSSL_ERR_NET_WANT_READ );
439 
441  }
442 
443  if( client_ip != NULL )
444  {
445 #if defined(POLARSSL_HAVE_IPV6)
446  if( client_addr.ss_family == AF_INET )
447  {
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 ) );
451  }
452  else
453  {
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 ) );
457  }
458 #else
459  memcpy( client_ip, &client_addr.sin_addr.s_addr,
460  sizeof( client_addr.sin_addr.s_addr ) );
461 #endif /* POLARSSL_HAVE_IPV6 */
462  }
463 
464  return( 0 );
465 }
466 
467 /*
468  * Set the socket blocking or non-blocking
469  */
470 int net_set_block( int fd )
471 {
472 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
473  !defined(EFI32)
474  u_long n = 0;
475  return( ioctlsocket( fd, FIONBIO, &n ) );
476 #else
477  return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
478 #endif
479 }
480 
481 int net_set_nonblock( int fd )
482 {
483 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
484  !defined(EFI32)
485  u_long n = 1;
486  return( ioctlsocket( fd, FIONBIO, &n ) );
487 #else
488  return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
489 #endif
490 }
491 
492 #if defined(POLARSSL_HAVE_TIME)
493 /*
494  * Portable usleep helper
495  */
496 void net_usleep( unsigned long usec )
497 {
498  struct timeval tv;
499  tv.tv_sec = 0;
500  tv.tv_usec = usec;
501  select( 0, NULL, NULL, NULL, &tv );
502 }
503 #endif /* POLARSSL_HAVE_TIME */
504 
505 /*
506  * Read at most 'len' characters
507  */
508 int net_recv( void *ctx, unsigned char *buf, size_t len )
509 {
510  int fd = *((int *) ctx);
511  int ret = read( fd, buf, len );
512 
513  if( ret < 0 )
514  {
515  if( net_would_block( fd ) != 0 )
516  return( POLARSSL_ERR_NET_WANT_READ );
517 
518 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
519  !defined(EFI32)
520  if( WSAGetLastError() == WSAECONNRESET )
521  return( POLARSSL_ERR_NET_CONN_RESET );
522 #else
523  if( errno == EPIPE || errno == ECONNRESET )
524  return( POLARSSL_ERR_NET_CONN_RESET );
525 
526  if( errno == EINTR )
527  return( POLARSSL_ERR_NET_WANT_READ );
528 #endif
529 
531  }
532 
533  return( ret );
534 }
535 
536 /*
537  * Write at most 'len' characters
538  */
539 int net_send( void *ctx, const unsigned char *buf, size_t len )
540 {
541  int fd = *((int *) ctx);
542  int ret = write( fd, buf, len );
543 
544  if( ret < 0 )
545  {
546  if( net_would_block( fd ) != 0 )
547  return( POLARSSL_ERR_NET_WANT_WRITE );
548 
549 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
550  !defined(EFI32)
551  if( WSAGetLastError() == WSAECONNRESET )
552  return( POLARSSL_ERR_NET_CONN_RESET );
553 #else
554  if( errno == EPIPE || errno == ECONNRESET )
555  return( POLARSSL_ERR_NET_CONN_RESET );
556 
557  if( errno == EINTR )
558  return( POLARSSL_ERR_NET_WANT_WRITE );
559 #endif
560 
562  }
563 
564  return( ret );
565 }
566 
567 /*
568  * Gracefully close the connection
569  */
570 void net_close( int fd )
571 {
572  shutdown( fd, 2 );
573  close( fd );
574 }
575 
576 #endif /* POLARSSL_NET_C */
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 '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.
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 'len' characters.
#define POLARSSL_ERR_NET_UNKNOWN_HOST
Failed to get an IP address for the given hostname.
Definition: net.h:32