blob: 9e7c5ddbc96061272f6787a0c6aa420525d491d6 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakker5121ce52009-01-03 21:22:43 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Paul Bakker40e46942009-01-03 21:51:57 +000026#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000029
30#if defined(WIN32) || defined(_WIN32_WCE)
31
32#include <winsock2.h>
33#include <windows.h>
34
35#if defined(_WIN32_WCE)
36#pragma comment( lib, "ws2.lib" )
37#else
38#pragma comment( lib, "ws2_32.lib" )
39#endif
40
41#define read(fd,buf,len) recv(fd,buf,len,0)
42#define write(fd,buf,len) send(fd,buf,len,0)
43#define close(fd) closesocket(fd)
44
45static int wsa_init_done = 0;
46
47#else
48
49#include <sys/types.h>
50#include <sys/socket.h>
51#include <netinet/in.h>
52#include <arpa/inet.h>
53#include <sys/time.h>
54#include <unistd.h>
55#include <signal.h>
56#include <fcntl.h>
57#include <netdb.h>
58#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000059
Paul Bakker854963c2009-07-19 20:50:11 +000060#if defined(__FreeBSD__)
61#include <sys/endian.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000062#elif defined(__APPLE__)
63#include <machine/endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000064#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000065#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
68#endif
69
70#include <string.h>
71#include <stdlib.h>
72#include <stdio.h>
73#include <time.h>
74
75/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000076 * htons() is not always available.
77 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
78 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000079 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000080#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000081#define POLARSSL_HTONS(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +000082#else
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000083#define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
Paul Bakker1d4f30c2009-04-19 18:55:16 +000084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Paul Bakker1d4f30c2009-04-19 18:55:16 +000086unsigned short net_htons(unsigned short n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000087#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89/*
90 * Initiate a TCP connection with host:port
91 */
92int net_connect( int *fd, char *host, int port )
93{
94 struct sockaddr_in server_addr;
95 struct hostent *server_host;
96
97#if defined(WIN32) || defined(_WIN32_WCE)
98 WSADATA wsaData;
99
100 if( wsa_init_done == 0 )
101 {
102 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000103 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105 wsa_init_done = 1;
106 }
107#else
108 signal( SIGPIPE, SIG_IGN );
109#endif
110
111 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000112 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000115 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117 memcpy( (void *) &server_addr.sin_addr,
118 (void *) server_host->h_addr,
119 server_host->h_length );
120
121 server_addr.sin_family = AF_INET;
122 server_addr.sin_port = net_htons( port );
123
124 if( connect( *fd, (struct sockaddr *) &server_addr,
125 sizeof( server_addr ) ) < 0 )
126 {
127 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000128 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 }
130
131 return( 0 );
132}
133
134/*
135 * Create a listening socket on bind_ip:port
136 */
137int net_bind( int *fd, char *bind_ip, int port )
138{
139 int n, c[4];
140 struct sockaddr_in server_addr;
141
142#if defined(WIN32) || defined(_WIN32_WCE)
143 WSADATA wsaData;
144
145 if( wsa_init_done == 0 )
146 {
147 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000148 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150 wsa_init_done = 1;
151 }
152#else
153 signal( SIGPIPE, SIG_IGN );
154#endif
155
156 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000157 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 n = 1;
160 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
161 (const char *) &n, sizeof( n ) );
162
163 server_addr.sin_addr.s_addr = INADDR_ANY;
164 server_addr.sin_family = AF_INET;
165 server_addr.sin_port = net_htons( port );
166
167 if( bind_ip != NULL )
168 {
169 memset( c, 0, sizeof( c ) );
170 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
171
172 for( n = 0; n < 4; n++ )
173 if( c[n] < 0 || c[n] > 255 )
174 break;
175
176 if( n == 4 )
177 server_addr.sin_addr.s_addr =
178 ( (unsigned long) c[0] << 24 ) |
179 ( (unsigned long) c[1] << 16 ) |
180 ( (unsigned long) c[2] << 8 ) |
181 ( (unsigned long) c[3] );
182 }
183
184 if( bind( *fd, (struct sockaddr *) &server_addr,
185 sizeof( server_addr ) ) < 0 )
186 {
187 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000188 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 }
190
191 if( listen( *fd, 10 ) != 0 )
192 {
193 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000194 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 }
196
197 return( 0 );
198}
199
200/*
201 * Check if the current operation is blocking
202 */
203static int net_is_blocking( void )
204{
205#if defined(WIN32) || defined(_WIN32_WCE)
206 return( WSAGetLastError() == WSAEWOULDBLOCK );
207#else
208 switch( errno )
209 {
210#if defined EAGAIN
211 case EAGAIN:
212#endif
213#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
214 case EWOULDBLOCK:
215#endif
216 return( 1 );
217 }
218 return( 0 );
219#endif
220}
221
222/*
223 * Accept a connection from a remote client
224 */
225int net_accept( int bind_fd, int *client_fd, void *client_ip )
226{
227 struct sockaddr_in client_addr;
228
229#if defined(__socklen_t_defined)
230 socklen_t n = (socklen_t) sizeof( client_addr );
231#else
232 int n = (int) sizeof( client_addr );
233#endif
234
235 *client_fd = accept( bind_fd, (struct sockaddr *)
236 &client_addr, &n );
237
238 if( *client_fd < 0 )
239 {
240 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000241 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
Paul Bakker40e46942009-01-03 21:51:57 +0000243 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 }
245
246 if( client_ip != NULL )
247 memcpy( client_ip, &client_addr.sin_addr.s_addr,
248 sizeof( client_addr.sin_addr.s_addr ) );
249
250 return( 0 );
251}
252
253/*
254 * Set the socket blocking or non-blocking
255 */
256int net_set_block( int fd )
257{
258#if defined(WIN32) || defined(_WIN32_WCE)
259 long n = 0;
260 return( ioctlsocket( fd, FIONBIO, &n ) );
261#else
262 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
263#endif
264}
265
266int net_set_nonblock( int fd )
267{
268#if defined(WIN32) || defined(_WIN32_WCE)
269 long n = 1;
270 return( ioctlsocket( fd, FIONBIO, &n ) );
271#else
272 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
273#endif
274}
275
276/*
277 * Portable usleep helper
278 */
279void net_usleep( unsigned long usec )
280{
281 struct timeval tv;
282 tv.tv_sec = 0;
283 tv.tv_usec = usec;
284 select( 0, NULL, NULL, NULL, &tv );
285}
286
287/*
288 * Read at most 'len' characters
289 */
290int net_recv( void *ctx, unsigned char *buf, int len )
291{
292 int ret = read( *((int *) ctx), buf, len );
293
294 if( len > 0 && ret == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000295 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
297 if( ret < 0 )
298 {
299 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000300 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302#if defined(WIN32) || defined(_WIN32_WCE)
303 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000304 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305#else
306 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000307 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000310 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311#endif
312
Paul Bakker40e46942009-01-03 21:51:57 +0000313 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 }
315
316 return( ret );
317}
318
319/*
320 * Write at most 'len' characters
321 */
322int net_send( void *ctx, unsigned char *buf, int len )
323{
324 int ret = write( *((int *) ctx), buf, len );
325
326 if( ret < 0 )
327 {
328 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000329 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331#if defined(WIN32) || defined(_WIN32_WCE)
332 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000333 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334#else
335 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000336 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
338 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000339 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340#endif
341
Paul Bakker40e46942009-01-03 21:51:57 +0000342 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 }
344
345 return( ret );
346}
347
348/*
349 * Gracefully close the connection
350 */
351void net_close( int fd )
352{
353 shutdown( fd, 2 );
354 close( fd );
355}
356
357#endif