blob: 378f7982dbe6a791f065b4745198fe861321bb25 [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.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakkerff60ee62010-03-16 21:09:09 +000032#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
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
Paul Bakker23986e52011-04-24 08:57:21 +000043#define read(fd,buf,len) recv(fd,buf,(int) len,0)
44#define write(fd,buf,len) send(fd,buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000045#define close(fd) closesocket(fd)
46
47static 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>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000061
Paul Bakker854963c2009-07-19 20:50:11 +000062#if defined(__FreeBSD__)
63#include <sys/endian.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000064#elif defined(__APPLE__)
65#include <machine/endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000066#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000067#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70#endif
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072#include <stdlib.h>
73#include <stdio.h>
74#include <time.h>
75
76/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000077 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +000080 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000081#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000082#define POLARSSL_HTONS(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +000083#else
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000084#define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
Paul Bakker1d4f30c2009-04-19 18:55:16 +000085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Paul Bakker1d4f30c2009-04-19 18:55:16 +000087unsigned short net_htons(unsigned short n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000088#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90/*
91 * Initiate a TCP connection with host:port
92 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000093int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +000094{
95 struct sockaddr_in server_addr;
96 struct hostent *server_host;
97
Paul Bakkerff60ee62010-03-16 21:09:09 +000098#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000099 WSADATA wsaData;
100
101 if( wsa_init_done == 0 )
102 {
103 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000104 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
106 wsa_init_done = 1;
107 }
108#else
109 signal( SIGPIPE, SIG_IGN );
110#endif
111
112 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000113 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000116 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
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 );
Paul Bakker40e46942009-01-03 21:51:57 +0000129 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 }
131
132 return( 0 );
133}
134
135/*
136 * Create a listening socket on bind_ip:port
137 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139{
140 int n, c[4];
141 struct sockaddr_in server_addr;
142
Paul Bakkerff60ee62010-03-16 21:09:09 +0000143#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 WSADATA wsaData;
145
146 if( wsa_init_done == 0 )
147 {
148 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000149 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
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 )
Paul Bakker40e46942009-01-03 21:51:57 +0000158 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
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 );
Paul Bakker40e46942009-01-03 21:51:57 +0000189 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 }
191
192 if( listen( *fd, 10 ) != 0 )
193 {
194 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000195 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 }
197
198 return( 0 );
199}
200
201/*
202 * Check if the current operation is blocking
203 */
204static int net_is_blocking( void )
205{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000206#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 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 */
226int net_accept( int bind_fd, int *client_fd, void *client_ip )
227{
228 struct sockaddr_in client_addr;
229
Paul Bakker4ed999c2010-03-16 21:16:16 +0000230#if defined(__socklen_t_defined) || defined(_SOCKLEN_T)
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 socklen_t n = (socklen_t) sizeof( client_addr );
232#else
233 int n = (int) sizeof( client_addr );
234#endif
235
236 *client_fd = accept( bind_fd, (struct sockaddr *)
237 &client_addr, &n );
238
239 if( *client_fd < 0 )
240 {
241 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000242 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Paul Bakker40e46942009-01-03 21:51:57 +0000244 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 }
246
247 if( client_ip != NULL )
248 memcpy( client_ip, &client_addr.sin_addr.s_addr,
249 sizeof( client_addr.sin_addr.s_addr ) );
250
251 return( 0 );
252}
253
254/*
255 * Set the socket blocking or non-blocking
256 */
257int net_set_block( int fd )
258{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000259#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 long n = 0;
261 return( ioctlsocket( fd, FIONBIO, &n ) );
262#else
263 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
264#endif
265}
266
267int net_set_nonblock( int fd )
268{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000269#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 long n = 1;
271 return( ioctlsocket( fd, FIONBIO, &n ) );
272#else
273 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
274#endif
275}
276
277/*
278 * Portable usleep helper
279 */
280void net_usleep( unsigned long usec )
281{
282 struct timeval tv;
283 tv.tv_sec = 0;
284 tv.tv_usec = usec;
285 select( 0, NULL, NULL, NULL, &tv );
286}
287
288/*
289 * Read at most 'len' characters
290 */
Paul Bakker23986e52011-04-24 08:57:21 +0000291int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292{
293 int ret = read( *((int *) ctx), buf, len );
294
295 if( len > 0 && ret == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000296 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298 if( ret < 0 )
299 {
300 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000301 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Paul Bakkerff60ee62010-03-16 21:09:09 +0000303#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000305 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306#else
307 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000308 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
310 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000311 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312#endif
313
Paul Bakker40e46942009-01-03 21:51:57 +0000314 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 }
316
317 return( ret );
318}
319
320/*
321 * Write at most 'len' characters
322 */
Paul Bakker23986e52011-04-24 08:57:21 +0000323int net_send( void *ctx, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000324{
325 int ret = write( *((int *) ctx), buf, len );
326
327 if( ret < 0 )
328 {
329 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000330 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Paul Bakkerff60ee62010-03-16 21:09:09 +0000332#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000334 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335#else
336 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000337 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
339 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000340 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341#endif
342
Paul Bakker40e46942009-01-03 21:51:57 +0000343 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 }
345
346 return( ret );
347}
348
349/*
350 * Gracefully close the connection
351 */
352void net_close( int fd )
353{
354 shutdown( fd, 2 );
355 close( fd );
356}
357
358#endif