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