blob: 9724c07d7f2657c34d725234c0061054d7294915 [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 Bakkerf4f69682011-04-24 16:08:12 +000043#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)
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 Bakker1fad5bf2011-07-01 09:07:24 +000062#if defined(__FreeBSD__) || defined(__OpenBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000063#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
Paul Bakker192381a2011-05-20 12:31:31 +0000192 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 {
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 Bakker394c56f2011-12-20 12:19:03 +0000230#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
231 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 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 )
Paul Bakker831a7552011-05-18 13:32:51 +0000243 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
Paul Bakker40e46942009-01-03 21:51:57 +0000245 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 }
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 */
258int net_set_block( int fd )
259{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000260#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000261 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 return( ioctlsocket( fd, FIONBIO, &n ) );
263#else
264 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
265#endif
266}
267
268int net_set_nonblock( int fd )
269{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000270#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000271 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 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 */
281void 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 */
Paul Bakker23986e52011-04-24 08:57:21 +0000292int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000293{
294 int ret = read( *((int *) ctx), buf, len );
295
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 if( ret < 0 )
297 {
298 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000299 return( POLARSSL_ERR_NET_WANT_READ );
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 Bakker831a7552011-05-18 13:32:51 +0000309 return( POLARSSL_ERR_NET_WANT_READ );
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 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000321int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000322{
323 int ret = write( *((int *) ctx), buf, len );
324
325 if( ret < 0 )
326 {
327 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000328 return( POLARSSL_ERR_NET_WANT_WRITE );
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 Bakker831a7552011-05-18 13:32:51 +0000338 return( POLARSSL_ERR_NET_WANT_WRITE );
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