blob: b213f2444eb8a9396698f88ab286c758e88c4339 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00004 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakker5121ce52009-01-03 21:22:43 +00007 *
8 * 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
29#if defined(WIN32) || defined(_WIN32_WCE)
30
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 Bakker854963c2009-07-19 20:50:11 +000058#if defined(__FreeBSD__)
59#include <sys/endian.h>
60#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000061#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000062#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64#endif
65
66#include <string.h>
67#include <stdlib.h>
68#include <stdio.h>
69#include <time.h>
70
71/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000072 * htons() is not always available.
73 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
74 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000075 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000076#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
77#define HTONS(n) (n)
78#else
79#define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
80#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Paul Bakker1d4f30c2009-04-19 18:55:16 +000082unsigned short net_htons(unsigned short n);
83#define net_htons(n) HTONS(n)
Paul Bakker5121ce52009-01-03 21:22:43 +000084
85/*
86 * Initiate a TCP connection with host:port
87 */
88int net_connect( int *fd, char *host, int port )
89{
90 struct sockaddr_in server_addr;
91 struct hostent *server_host;
92
93#if defined(WIN32) || defined(_WIN32_WCE)
94 WSADATA wsaData;
95
96 if( wsa_init_done == 0 )
97 {
98 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +000099 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101 wsa_init_done = 1;
102 }
103#else
104 signal( SIGPIPE, SIG_IGN );
105#endif
106
107 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000108 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000111 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113 memcpy( (void *) &server_addr.sin_addr,
114 (void *) server_host->h_addr,
115 server_host->h_length );
116
117 server_addr.sin_family = AF_INET;
118 server_addr.sin_port = net_htons( port );
119
120 if( connect( *fd, (struct sockaddr *) &server_addr,
121 sizeof( server_addr ) ) < 0 )
122 {
123 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000124 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 }
126
127 return( 0 );
128}
129
130/*
131 * Create a listening socket on bind_ip:port
132 */
133int net_bind( int *fd, char *bind_ip, int port )
134{
135 int n, c[4];
136 struct sockaddr_in server_addr;
137
138#if defined(WIN32) || defined(_WIN32_WCE)
139 WSADATA wsaData;
140
141 if( wsa_init_done == 0 )
142 {
143 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000144 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 wsa_init_done = 1;
147 }
148#else
149 signal( SIGPIPE, SIG_IGN );
150#endif
151
152 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000153 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155 n = 1;
156 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
157 (const char *) &n, sizeof( n ) );
158
159 server_addr.sin_addr.s_addr = INADDR_ANY;
160 server_addr.sin_family = AF_INET;
161 server_addr.sin_port = net_htons( port );
162
163 if( bind_ip != NULL )
164 {
165 memset( c, 0, sizeof( c ) );
166 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
167
168 for( n = 0; n < 4; n++ )
169 if( c[n] < 0 || c[n] > 255 )
170 break;
171
172 if( n == 4 )
173 server_addr.sin_addr.s_addr =
174 ( (unsigned long) c[0] << 24 ) |
175 ( (unsigned long) c[1] << 16 ) |
176 ( (unsigned long) c[2] << 8 ) |
177 ( (unsigned long) c[3] );
178 }
179
180 if( bind( *fd, (struct sockaddr *) &server_addr,
181 sizeof( server_addr ) ) < 0 )
182 {
183 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000184 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 }
186
187 if( listen( *fd, 10 ) != 0 )
188 {
189 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000190 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 }
192
193 return( 0 );
194}
195
196/*
197 * Check if the current operation is blocking
198 */
199static int net_is_blocking( void )
200{
201#if defined(WIN32) || defined(_WIN32_WCE)
202 return( WSAGetLastError() == WSAEWOULDBLOCK );
203#else
204 switch( errno )
205 {
206#if defined EAGAIN
207 case EAGAIN:
208#endif
209#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
210 case EWOULDBLOCK:
211#endif
212 return( 1 );
213 }
214 return( 0 );
215#endif
216}
217
218/*
219 * Accept a connection from a remote client
220 */
221int net_accept( int bind_fd, int *client_fd, void *client_ip )
222{
223 struct sockaddr_in client_addr;
224
225#if defined(__socklen_t_defined)
226 socklen_t n = (socklen_t) sizeof( client_addr );
227#else
228 int n = (int) sizeof( client_addr );
229#endif
230
231 *client_fd = accept( bind_fd, (struct sockaddr *)
232 &client_addr, &n );
233
234 if( *client_fd < 0 )
235 {
236 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000237 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
Paul Bakker40e46942009-01-03 21:51:57 +0000239 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 }
241
242 if( client_ip != NULL )
243 memcpy( client_ip, &client_addr.sin_addr.s_addr,
244 sizeof( client_addr.sin_addr.s_addr ) );
245
246 return( 0 );
247}
248
249/*
250 * Set the socket blocking or non-blocking
251 */
252int net_set_block( int fd )
253{
254#if defined(WIN32) || defined(_WIN32_WCE)
255 long n = 0;
256 return( ioctlsocket( fd, FIONBIO, &n ) );
257#else
258 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
259#endif
260}
261
262int net_set_nonblock( int fd )
263{
264#if defined(WIN32) || defined(_WIN32_WCE)
265 long n = 1;
266 return( ioctlsocket( fd, FIONBIO, &n ) );
267#else
268 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
269#endif
270}
271
272/*
273 * Portable usleep helper
274 */
275void net_usleep( unsigned long usec )
276{
277 struct timeval tv;
278 tv.tv_sec = 0;
279 tv.tv_usec = usec;
280 select( 0, NULL, NULL, NULL, &tv );
281}
282
283/*
284 * Read at most 'len' characters
285 */
286int net_recv( void *ctx, unsigned char *buf, int len )
287{
288 int ret = read( *((int *) ctx), buf, len );
289
290 if( len > 0 && ret == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000291 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293 if( ret < 0 )
294 {
295 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000296 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298#if defined(WIN32) || defined(_WIN32_WCE)
299 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000300 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301#else
302 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000303 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000306 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307#endif
308
Paul Bakker40e46942009-01-03 21:51:57 +0000309 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 }
311
312 return( ret );
313}
314
315/*
316 * Write at most 'len' characters
317 */
318int net_send( void *ctx, unsigned char *buf, int len )
319{
320 int ret = write( *((int *) ctx), buf, len );
321
322 if( ret < 0 )
323 {
324 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000325 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327#if defined(WIN32) || defined(_WIN32_WCE)
328 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000329 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330#else
331 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000332 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
334 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000335 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336#endif
337
Paul Bakker40e46942009-01-03 21:51:57 +0000338 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 }
340
341 return( ret );
342}
343
344/*
345 * Gracefully close the connection
346 */
347void net_close( int fd )
348{
349 shutdown( fd, 2 );
350 close( fd );
351}
352
353#endif