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