PolarSSL v1.3.1
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)
33 
34 #include <winsock2.h>
35 #include <windows.h>
36 
37 #if defined(_WIN32_WCE)
38 #pragma comment( lib, "ws2.lib" )
39 #else
40 #pragma comment( lib, "ws2_32.lib" )
41 #endif
42 
43 #define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
44 #define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
45 #define close(fd) closesocket(fd)
46 
47 static int wsa_init_done = 0;
48 
49 #else
50 
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #if defined(POLARSSL_HAVE_TIME)
56 #include <sys/time.h>
57 #endif
58 #include <unistd.h>
59 #include <signal.h>
60 #include <fcntl.h>
61 #include <netdb.h>
62 #include <errno.h>
63 
64 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
65  defined(__DragonflyBSD__)
66 #include <sys/endian.h>
67 #elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H)
68 #include <machine/endian.h>
69 #elif defined(sun)
70 #include <sys/isa_defs.h>
71 #elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
72 #include <arpa/nameser_compat.h>
73 #else
74 #include <endian.h>
75 #endif
76 
77 #endif
78 
79 #include <stdlib.h>
80 #include <stdio.h>
81 
82 #if defined(POLARSSL_HAVE_TIME)
83 #include <time.h>
84 #endif
85 
86 #ifdef _MSC_VER
87 #include <basetsd.h>
88 typedef UINT32 uint32_t;
89 #else
90 #include <inttypes.h>
91 #endif
92 
93 /*
94  * htons() is not always available.
95  * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
96  * to help determine endianess.
97  */
98 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
99 #define POLARSSL_HTONS(n) (n)
100 #define POLARSSL_HTONL(n) (n)
101 #else
102 #define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
103  (((unsigned short)(n) & 0xFF00 ) >> 8 ))
104 #define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
105  (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
106  (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
107  (((unsigned long )(n) & 0xFF000000) >> 24))
108 #endif
109 
110 unsigned short net_htons(unsigned short n);
111 unsigned long net_htonl(unsigned long n);
112 #define net_htons(n) POLARSSL_HTONS(n)
113 #define net_htonl(n) POLARSSL_HTONL(n)
114 
115 /*
116  * Initiate a TCP connection with host:port
117  */
118 int net_connect( int *fd, const char *host, int port )
119 {
120  struct sockaddr_in server_addr;
121  struct hostent *server_host;
122 
123 #if defined(_WIN32) || defined(_WIN32_WCE)
124  WSADATA wsaData;
125 
126  if( wsa_init_done == 0 )
127  {
128  if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
130 
131  wsa_init_done = 1;
132  }
133 #else
134  signal( SIGPIPE, SIG_IGN );
135 #endif
136 
137  if( ( server_host = gethostbyname( host ) ) == NULL )
139 
140  if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
142 
143  memcpy( (void *) &server_addr.sin_addr,
144  (void *) server_host->h_addr,
145  server_host->h_length );
146 
147  server_addr.sin_family = AF_INET;
148  server_addr.sin_port = net_htons( port );
149 
150  if( connect( *fd, (struct sockaddr *) &server_addr,
151  sizeof( server_addr ) ) < 0 )
152  {
153  close( *fd );
155  }
156 
157  return( 0 );
158 }
159 
160 /*
161  * Create a listening socket on bind_ip:port
162  */
163 int net_bind( int *fd, const char *bind_ip, int port )
164 {
165  int n, c[4];
166  struct sockaddr_in server_addr;
167 
168 #if defined(_WIN32) || defined(_WIN32_WCE)
169  WSADATA wsaData;
170 
171  if( wsa_init_done == 0 )
172  {
173  if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
175 
176  wsa_init_done = 1;
177  }
178 #else
179  signal( SIGPIPE, SIG_IGN );
180 #endif
181 
182  if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
184 
185  n = 1;
186  setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
187  (const char *) &n, sizeof( n ) );
188 
189  server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
190  server_addr.sin_family = AF_INET;
191  server_addr.sin_port = net_htons( port );
192 
193  if( bind_ip != NULL )
194  {
195  memset( c, 0, sizeof( c ) );
196  sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
197 
198  for( n = 0; n < 4; n++ )
199  if( c[n] < 0 || c[n] > 255 )
200  break;
201 
202  if( n == 4 )
203  server_addr.sin_addr.s_addr = net_htonl(
204  ( (uint32_t) c[0] << 24 ) |
205  ( (uint32_t) c[1] << 16 ) |
206  ( (uint32_t) c[2] << 8 ) |
207  ( (uint32_t) c[3] ) );
208  }
209 
210  if( bind( *fd, (struct sockaddr *) &server_addr,
211  sizeof( server_addr ) ) < 0 )
212  {
213  close( *fd );
215  }
216 
217  if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
218  {
219  close( *fd );
221  }
222 
223  return( 0 );
224 }
225 
226 /*
227  * Check if the current operation is blocking
228  */
229 static int net_is_blocking( void )
230 {
231 #if defined(_WIN32) || defined(_WIN32_WCE)
232  return( WSAGetLastError() == WSAEWOULDBLOCK );
233 #else
234  switch( errno )
235  {
236 #if defined EAGAIN
237  case EAGAIN:
238 #endif
239 #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
240  case EWOULDBLOCK:
241 #endif
242  return( 1 );
243  }
244  return( 0 );
245 #endif
246 }
247 
248 /*
249  * Accept a connection from a remote client
250  */
251 int net_accept( int bind_fd, int *client_fd, void *client_ip )
252 {
253  struct sockaddr_in client_addr;
254 
255 #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
256  defined(_SOCKLEN_T_DECLARED)
257  socklen_t n = (socklen_t) sizeof( client_addr );
258 #else
259  int n = (int) sizeof( client_addr );
260 #endif
261 
262  *client_fd = (int) accept( bind_fd, (struct sockaddr *)
263  &client_addr, &n );
264 
265  if( *client_fd < 0 )
266  {
267  if( net_is_blocking() != 0 )
268  return( POLARSSL_ERR_NET_WANT_READ );
269 
271  }
272 
273  if( client_ip != NULL )
274  memcpy( client_ip, &client_addr.sin_addr.s_addr,
275  sizeof( client_addr.sin_addr.s_addr ) );
276 
277  return( 0 );
278 }
279 
280 /*
281  * Set the socket blocking or non-blocking
282  */
283 int net_set_block( int fd )
284 {
285 #if defined(_WIN32) || defined(_WIN32_WCE)
286  u_long n = 0;
287  return( ioctlsocket( fd, FIONBIO, &n ) );
288 #else
289  return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
290 #endif
291 }
292 
293 int net_set_nonblock( int fd )
294 {
295 #if defined(_WIN32) || defined(_WIN32_WCE)
296  u_long n = 1;
297  return( ioctlsocket( fd, FIONBIO, &n ) );
298 #else
299  return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
300 #endif
301 }
302 
303 #if defined(POLARSSL_HAVE_TIME)
304 /*
305  * Portable usleep helper
306  */
307 void net_usleep( unsigned long usec )
308 {
309  struct timeval tv;
310  tv.tv_sec = 0;
311  tv.tv_usec = usec;
312  select( 0, NULL, NULL, NULL, &tv );
313 }
314 #endif /* POLARSSL_HAVE_TIME */
315 
316 /*
317  * Read at most 'len' characters
318  */
319 int net_recv( void *ctx, unsigned char *buf, size_t len )
320 {
321  int ret = read( *((int *) ctx), buf, len );
322 
323  if( ret < 0 )
324  {
325  if( net_is_blocking() != 0 )
326  return( POLARSSL_ERR_NET_WANT_READ );
327 
328 #if defined(_WIN32) || defined(_WIN32_WCE)
329  if( WSAGetLastError() == WSAECONNRESET )
330  return( POLARSSL_ERR_NET_CONN_RESET );
331 #else
332  if( errno == EPIPE || errno == ECONNRESET )
333  return( POLARSSL_ERR_NET_CONN_RESET );
334 
335  if( errno == EINTR )
336  return( POLARSSL_ERR_NET_WANT_READ );
337 #endif
338 
340  }
341 
342  return( ret );
343 }
344 
345 /*
346  * Write at most 'len' characters
347  */
348 int net_send( void *ctx, const unsigned char *buf, size_t len )
349 {
350  int ret = write( *((int *) ctx), buf, len );
351 
352  if( ret < 0 )
353  {
354  if( net_is_blocking() != 0 )
355  return( POLARSSL_ERR_NET_WANT_WRITE );
356 
357 #if defined(_WIN32) || defined(_WIN32_WCE)
358  if( WSAGetLastError() == WSAECONNRESET )
359  return( POLARSSL_ERR_NET_CONN_RESET );
360 #else
361  if( errno == EPIPE || errno == ECONNRESET )
362  return( POLARSSL_ERR_NET_CONN_RESET );
363 
364  if( errno == EINTR )
365  return( POLARSSL_ERR_NET_WANT_WRITE );
366 #endif
367 
369  }
370 
371  return( ret );
372 }
373 
374 /*
375  * Gracefully close the connection
376  */
377 void net_close( int fd )
378 {
379  shutdown( fd, 2 );
380  close( fd );
381 }
382 
383 #endif