PolarSSL v1.3.3
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 /*
361  * Check if the current operation is blocking
362  */
363 static int net_is_blocking( void )
364 {
365 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
366  !defined(EFI32)
367  return( WSAGetLastError() == WSAEWOULDBLOCK );
368 #else
369  switch( errno )
370  {
371 #if defined EAGAIN
372  case EAGAIN:
373 #endif
374 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
375  case EWOULDBLOCK:
376 #endif
377  return( 1 );
378  }
379  return( 0 );
380 #endif
381 }
382 
383 /*
384  * Accept a connection from a remote client
385  */
386 int net_accept( int bind_fd, int *client_fd, void *client_ip )
387 {
388 #if defined(POLARSSL_HAVE_IPV6)
389  struct sockaddr_storage client_addr;
390 #else
391  struct sockaddr_in client_addr;
392 #endif
393 
394 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
395  defined(_SOCKLEN_T_DECLARED)
396  socklen_t n = (socklen_t) sizeof( client_addr );
397 #else
398  int n = (int) sizeof( client_addr );
399 #endif
400 
401  *client_fd = (int) accept( bind_fd, (struct sockaddr *)
402  &client_addr, &n );
403 
404  if( *client_fd < 0 )
405  {
406  if( net_is_blocking() != 0 )
407  return( POLARSSL_ERR_NET_WANT_READ );
408 
410  }
411 
412  if( client_ip != NULL )
413  {
414 #if defined(POLARSSL_HAVE_IPV6)
415  if( client_addr.ss_family == AF_INET )
416  {
417  struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
418  memcpy( client_ip, &addr4->sin_addr.s_addr,
419  sizeof( addr4->sin_addr.s_addr ) );
420  }
421  else
422  {
423  struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
424  memcpy( client_ip, &addr6->sin6_addr.s6_addr,
425  sizeof( addr6->sin6_addr.s6_addr ) );
426  }
427 #else
428  memcpy( client_ip, &client_addr.sin_addr.s_addr,
429  sizeof( client_addr.sin_addr.s_addr ) );
430 #endif /* POLARSSL_HAVE_IPV6 */
431  }
432 
433  return( 0 );
434 }
435 
436 /*
437  * Set the socket blocking or non-blocking
438  */
439 int net_set_block( int fd )
440 {
441 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
442  !defined(EFI32)
443  u_long n = 0;
444  return( ioctlsocket( fd, FIONBIO, &n ) );
445 #else
446  return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
447 #endif
448 }
449 
450 int net_set_nonblock( int fd )
451 {
452 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
453  !defined(EFI32)
454  u_long n = 1;
455  return( ioctlsocket( fd, FIONBIO, &n ) );
456 #else
457  return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
458 #endif
459 }
460 
461 #if defined(POLARSSL_HAVE_TIME)
462 /*
463  * Portable usleep helper
464  */
465 void net_usleep( unsigned long usec )
466 {
467  struct timeval tv;
468  tv.tv_sec = 0;
469  tv.tv_usec = usec;
470  select( 0, NULL, NULL, NULL, &tv );
471 }
472 #endif /* POLARSSL_HAVE_TIME */
473 
474 /*
475  * Read at most 'len' characters
476  */
477 int net_recv( void *ctx, unsigned char *buf, size_t len )
478 {
479  int ret = read( *((int *) ctx), buf, len );
480 
481  if( ret < 0 )
482  {
483  if( net_is_blocking() != 0 )
484  return( POLARSSL_ERR_NET_WANT_READ );
485 
486 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
487  !defined(EFI32)
488  if( WSAGetLastError() == WSAECONNRESET )
489  return( POLARSSL_ERR_NET_CONN_RESET );
490 #else
491  if( errno == EPIPE || errno == ECONNRESET )
492  return( POLARSSL_ERR_NET_CONN_RESET );
493 
494  if( errno == EINTR )
495  return( POLARSSL_ERR_NET_WANT_READ );
496 #endif
497 
499  }
500 
501  return( ret );
502 }
503 
504 /*
505  * Write at most 'len' characters
506  */
507 int net_send( void *ctx, const unsigned char *buf, size_t len )
508 {
509  int ret = write( *((int *) ctx), buf, len );
510 
511  if( ret < 0 )
512  {
513  if( net_is_blocking() != 0 )
514  return( POLARSSL_ERR_NET_WANT_WRITE );
515 
516 #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
517  !defined(EFI32)
518  if( WSAGetLastError() == WSAECONNRESET )
519  return( POLARSSL_ERR_NET_CONN_RESET );
520 #else
521  if( errno == EPIPE || errno == ECONNRESET )
522  return( POLARSSL_ERR_NET_CONN_RESET );
523 
524  if( errno == EINTR )
525  return( POLARSSL_ERR_NET_WANT_WRITE );
526 #endif
527 
529  }
530 
531  return( ret );
532 }
533 
534 /*
535  * Gracefully close the connection
536  */
537 void net_close( int fd )
538 {
539  shutdown( fd, 2 );
540  close( fd );
541 }
542 
543 #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