blob: 41bb2d93f76c32fabfb598078d680b57bf3dec2c [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 Bakker1d4f30c2009-04-19 18:55:16 +000058#include <endian.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60#endif
61
62#include <string.h>
63#include <stdlib.h>
64#include <stdio.h>
65#include <time.h>
66
67/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000068 * htons() is not always available.
69 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
70 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000072#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
73#define HTONS(n) (n)
74#else
75#define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
76#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Paul Bakker1d4f30c2009-04-19 18:55:16 +000078unsigned short net_htons(unsigned short n);
79#define net_htons(n) HTONS(n)
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81/*
82 * Initiate a TCP connection with host:port
83 */
84int net_connect( int *fd, char *host, int port )
85{
86 struct sockaddr_in server_addr;
87 struct hostent *server_host;
88
89#if defined(WIN32) || defined(_WIN32_WCE)
90 WSADATA wsaData;
91
92 if( wsa_init_done == 0 )
93 {
94 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +000095 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +000096
97 wsa_init_done = 1;
98 }
99#else
100 signal( SIGPIPE, SIG_IGN );
101#endif
102
103 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000104 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
106 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000107 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109 memcpy( (void *) &server_addr.sin_addr,
110 (void *) server_host->h_addr,
111 server_host->h_length );
112
113 server_addr.sin_family = AF_INET;
114 server_addr.sin_port = net_htons( port );
115
116 if( connect( *fd, (struct sockaddr *) &server_addr,
117 sizeof( server_addr ) ) < 0 )
118 {
119 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000120 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 }
122
123 return( 0 );
124}
125
126/*
127 * Create a listening socket on bind_ip:port
128 */
129int net_bind( int *fd, char *bind_ip, int port )
130{
131 int n, c[4];
132 struct sockaddr_in server_addr;
133
134#if defined(WIN32) || defined(_WIN32_WCE)
135 WSADATA wsaData;
136
137 if( wsa_init_done == 0 )
138 {
139 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000140 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142 wsa_init_done = 1;
143 }
144#else
145 signal( SIGPIPE, SIG_IGN );
146#endif
147
148 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000149 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 n = 1;
152 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
153 (const char *) &n, sizeof( n ) );
154
155 server_addr.sin_addr.s_addr = INADDR_ANY;
156 server_addr.sin_family = AF_INET;
157 server_addr.sin_port = net_htons( port );
158
159 if( bind_ip != NULL )
160 {
161 memset( c, 0, sizeof( c ) );
162 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
163
164 for( n = 0; n < 4; n++ )
165 if( c[n] < 0 || c[n] > 255 )
166 break;
167
168 if( n == 4 )
169 server_addr.sin_addr.s_addr =
170 ( (unsigned long) c[0] << 24 ) |
171 ( (unsigned long) c[1] << 16 ) |
172 ( (unsigned long) c[2] << 8 ) |
173 ( (unsigned long) c[3] );
174 }
175
176 if( bind( *fd, (struct sockaddr *) &server_addr,
177 sizeof( server_addr ) ) < 0 )
178 {
179 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000180 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 }
182
183 if( listen( *fd, 10 ) != 0 )
184 {
185 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000186 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 }
188
189 return( 0 );
190}
191
192/*
193 * Check if the current operation is blocking
194 */
195static int net_is_blocking( void )
196{
197#if defined(WIN32) || defined(_WIN32_WCE)
198 return( WSAGetLastError() == WSAEWOULDBLOCK );
199#else
200 switch( errno )
201 {
202#if defined EAGAIN
203 case EAGAIN:
204#endif
205#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
206 case EWOULDBLOCK:
207#endif
208 return( 1 );
209 }
210 return( 0 );
211#endif
212}
213
214/*
215 * Accept a connection from a remote client
216 */
217int net_accept( int bind_fd, int *client_fd, void *client_ip )
218{
219 struct sockaddr_in client_addr;
220
221#if defined(__socklen_t_defined)
222 socklen_t n = (socklen_t) sizeof( client_addr );
223#else
224 int n = (int) sizeof( client_addr );
225#endif
226
227 *client_fd = accept( bind_fd, (struct sockaddr *)
228 &client_addr, &n );
229
230 if( *client_fd < 0 )
231 {
232 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000233 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Paul Bakker40e46942009-01-03 21:51:57 +0000235 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 }
237
238 if( client_ip != NULL )
239 memcpy( client_ip, &client_addr.sin_addr.s_addr,
240 sizeof( client_addr.sin_addr.s_addr ) );
241
242 return( 0 );
243}
244
245/*
246 * Set the socket blocking or non-blocking
247 */
248int net_set_block( int fd )
249{
250#if defined(WIN32) || defined(_WIN32_WCE)
251 long n = 0;
252 return( ioctlsocket( fd, FIONBIO, &n ) );
253#else
254 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
255#endif
256}
257
258int net_set_nonblock( int fd )
259{
260#if defined(WIN32) || defined(_WIN32_WCE)
261 long n = 1;
262 return( ioctlsocket( fd, FIONBIO, &n ) );
263#else
264 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
265#endif
266}
267
268/*
269 * Portable usleep helper
270 */
271void net_usleep( unsigned long usec )
272{
273 struct timeval tv;
274 tv.tv_sec = 0;
275 tv.tv_usec = usec;
276 select( 0, NULL, NULL, NULL, &tv );
277}
278
279/*
280 * Read at most 'len' characters
281 */
282int net_recv( void *ctx, unsigned char *buf, int len )
283{
284 int ret = read( *((int *) ctx), buf, len );
285
286 if( len > 0 && ret == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000287 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
289 if( ret < 0 )
290 {
291 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000292 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294#if defined(WIN32) || defined(_WIN32_WCE)
295 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000296 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297#else
298 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000299 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
301 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000302 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303#endif
304
Paul Bakker40e46942009-01-03 21:51:57 +0000305 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 }
307
308 return( ret );
309}
310
311/*
312 * Write at most 'len' characters
313 */
314int net_send( void *ctx, unsigned char *buf, int len )
315{
316 int ret = write( *((int *) ctx), buf, len );
317
318 if( ret < 0 )
319 {
320 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000321 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323#if defined(WIN32) || defined(_WIN32_WCE)
324 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000325 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326#else
327 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000328 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000331 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332#endif
333
Paul Bakker40e46942009-01-03 21:51:57 +0000334 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 }
336
337 return( ret );
338}
339
340/*
341 * Gracefully close the connection
342 */
343void net_close( int fd )
344{
345 shutdown( fd, 2 );
346 close( fd );
347}
348
349#endif