blob: cda1662a2a2129e1ee8431b454e11d4401c5f46f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Paul Bakker40e46942009-01-03 21:51:57 +000023#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Paul Bakker40e46942009-01-03 21:51:57 +000025#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakkerff60ee62010-03-16 21:09:09 +000029#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
31#include <winsock2.h>
32#include <windows.h>
33
34#if defined(_WIN32_WCE)
35#pragma comment( lib, "ws2.lib" )
36#else
37#pragma comment( lib, "ws2_32.lib" )
38#endif
39
40#define read(fd,buf,len) recv(fd,buf,len,0)
41#define write(fd,buf,len) send(fd,buf,len,0)
42#define close(fd) closesocket(fd)
43
44static int wsa_init_done = 0;
45
46#else
47
48#include <sys/types.h>
49#include <sys/socket.h>
50#include <netinet/in.h>
51#include <arpa/inet.h>
52#include <sys/time.h>
53#include <unistd.h>
54#include <signal.h>
55#include <fcntl.h>
56#include <netdb.h>
57#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000058
Paul Bakker854963c2009-07-19 20:50:11 +000059#if defined(__FreeBSD__)
60#include <sys/endian.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000061#elif defined(__APPLE__)
62#include <machine/endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000063#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000064#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000065#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67#endif
68
69#include <string.h>
70#include <stdlib.h>
71#include <stdio.h>
72#include <time.h>
73
74/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000075 * htons() is not always available.
76 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
77 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000078 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000079#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000080#define POLARSSL_HTONS(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +000081#else
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000082#define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
Paul Bakker1d4f30c2009-04-19 18:55:16 +000083#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Paul Bakker1d4f30c2009-04-19 18:55:16 +000085unsigned short net_htons(unsigned short n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000086#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88/*
89 * Initiate a TCP connection with host:port
90 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000091int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +000092{
93 struct sockaddr_in server_addr;
94 struct hostent *server_host;
95
Paul Bakkerff60ee62010-03-16 21:09:09 +000096#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000097 WSADATA wsaData;
98
99 if( wsa_init_done == 0 )
100 {
101 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000102 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 wsa_init_done = 1;
105 }
106#else
107 signal( SIGPIPE, SIG_IGN );
108#endif
109
110 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000111 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000114 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 memcpy( (void *) &server_addr.sin_addr,
117 (void *) server_host->h_addr,
118 server_host->h_length );
119
120 server_addr.sin_family = AF_INET;
121 server_addr.sin_port = net_htons( port );
122
123 if( connect( *fd, (struct sockaddr *) &server_addr,
124 sizeof( server_addr ) ) < 0 )
125 {
126 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000127 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 }
129
130 return( 0 );
131}
132
133/*
134 * Create a listening socket on bind_ip:port
135 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000136int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000137{
138 int n, c[4];
139 struct sockaddr_in server_addr;
140
Paul Bakkerff60ee62010-03-16 21:09:09 +0000141#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 WSADATA wsaData;
143
144 if( wsa_init_done == 0 )
145 {
146 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000147 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149 wsa_init_done = 1;
150 }
151#else
152 signal( SIGPIPE, SIG_IGN );
153#endif
154
155 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000156 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158 n = 1;
159 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
160 (const char *) &n, sizeof( n ) );
161
162 server_addr.sin_addr.s_addr = INADDR_ANY;
163 server_addr.sin_family = AF_INET;
164 server_addr.sin_port = net_htons( port );
165
166 if( bind_ip != NULL )
167 {
168 memset( c, 0, sizeof( c ) );
169 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
170
171 for( n = 0; n < 4; n++ )
172 if( c[n] < 0 || c[n] > 255 )
173 break;
174
175 if( n == 4 )
176 server_addr.sin_addr.s_addr =
177 ( (unsigned long) c[0] << 24 ) |
178 ( (unsigned long) c[1] << 16 ) |
179 ( (unsigned long) c[2] << 8 ) |
180 ( (unsigned long) c[3] );
181 }
182
183 if( bind( *fd, (struct sockaddr *) &server_addr,
184 sizeof( server_addr ) ) < 0 )
185 {
186 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000187 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 }
189
190 if( listen( *fd, 10 ) != 0 )
191 {
192 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000193 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 }
195
196 return( 0 );
197}
198
199/*
200 * Check if the current operation is blocking
201 */
202static int net_is_blocking( void )
203{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000204#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 return( WSAGetLastError() == WSAEWOULDBLOCK );
206#else
207 switch( errno )
208 {
209#if defined EAGAIN
210 case EAGAIN:
211#endif
212#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
213 case EWOULDBLOCK:
214#endif
215 return( 1 );
216 }
217 return( 0 );
218#endif
219}
220
221/*
222 * Accept a connection from a remote client
223 */
224int net_accept( int bind_fd, int *client_fd, void *client_ip )
225{
226 struct sockaddr_in client_addr;
227
Paul Bakker4ed999c2010-03-16 21:16:16 +0000228#if defined(__socklen_t_defined) || defined(_SOCKLEN_T)
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 socklen_t n = (socklen_t) sizeof( client_addr );
230#else
231 int n = (int) sizeof( client_addr );
232#endif
233
234 *client_fd = accept( bind_fd, (struct sockaddr *)
235 &client_addr, &n );
236
237 if( *client_fd < 0 )
238 {
239 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000240 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Paul Bakker40e46942009-01-03 21:51:57 +0000242 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 }
244
245 if( client_ip != NULL )
246 memcpy( client_ip, &client_addr.sin_addr.s_addr,
247 sizeof( client_addr.sin_addr.s_addr ) );
248
249 return( 0 );
250}
251
252/*
253 * Set the socket blocking or non-blocking
254 */
255int net_set_block( int fd )
256{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000257#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 long n = 0;
259 return( ioctlsocket( fd, FIONBIO, &n ) );
260#else
261 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
262#endif
263}
264
265int net_set_nonblock( int fd )
266{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000267#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 long n = 1;
269 return( ioctlsocket( fd, FIONBIO, &n ) );
270#else
271 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
272#endif
273}
274
275/*
276 * Portable usleep helper
277 */
278void net_usleep( unsigned long usec )
279{
280 struct timeval tv;
281 tv.tv_sec = 0;
282 tv.tv_usec = usec;
283 select( 0, NULL, NULL, NULL, &tv );
284}
285
286/*
287 * Read at most 'len' characters
288 */
289int net_recv( void *ctx, unsigned char *buf, int len )
290{
291 int ret = read( *((int *) ctx), buf, len );
292
293 if( len > 0 && ret == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000294 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296 if( ret < 0 )
297 {
298 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000299 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
Paul Bakkerff60ee62010-03-16 21:09:09 +0000301#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000303 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304#else
305 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000306 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000309 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310#endif
311
Paul Bakker40e46942009-01-03 21:51:57 +0000312 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 }
314
315 return( ret );
316}
317
318/*
319 * Write at most 'len' characters
320 */
321int net_send( void *ctx, unsigned char *buf, int len )
322{
323 int ret = write( *((int *) ctx), buf, len );
324
325 if( ret < 0 )
326 {
327 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000328 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
Paul Bakkerff60ee62010-03-16 21:09:09 +0000330#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000332 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333#else
334 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000335 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000338 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339#endif
340
Paul Bakker40e46942009-01-03 21:51:57 +0000341 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 }
343
344 return( ret );
345}
346
347/*
348 * Gracefully close the connection
349 */
350void net_close( int fd )
351{
352 shutdown( fd, 2 );
353 close( fd );
354}
355
356#endif