blob: d243fb3b33a2cf24e4bb650a19db31e7646dc656 [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 Bakker27db1f52009-01-25 15:27:00 +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>
58
59#endif
60
61#include <string.h>
62#include <stdlib.h>
63#include <stdio.h>
64#include <time.h>
65
66/*
67 * htons() is not always available
68 */
69static unsigned short net_htons( int port )
70{
71 unsigned char buf[4];
72
73 buf[0] = (unsigned char)( port >> 8 );
74 buf[1] = (unsigned char)( port );
75 buf[2] = buf[3] = 0;
76
77 return( *(unsigned short *) buf );
78}
79
80/*
81 * Initiate a TCP connection with host:port
82 */
83int net_connect( int *fd, char *host, int port )
84{
85 struct sockaddr_in server_addr;
86 struct hostent *server_host;
87
88#if defined(WIN32) || defined(_WIN32_WCE)
89 WSADATA wsaData;
90
91 if( wsa_init_done == 0 )
92 {
93 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +000094 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 wsa_init_done = 1;
97 }
98#else
99 signal( SIGPIPE, SIG_IGN );
100#endif
101
102 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000103 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000106 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 memcpy( (void *) &server_addr.sin_addr,
109 (void *) server_host->h_addr,
110 server_host->h_length );
111
112 server_addr.sin_family = AF_INET;
113 server_addr.sin_port = net_htons( port );
114
115 if( connect( *fd, (struct sockaddr *) &server_addr,
116 sizeof( server_addr ) ) < 0 )
117 {
118 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000119 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 }
121
122 return( 0 );
123}
124
125/*
126 * Create a listening socket on bind_ip:port
127 */
128int net_bind( int *fd, char *bind_ip, int port )
129{
130 int n, c[4];
131 struct sockaddr_in server_addr;
132
133#if defined(WIN32) || defined(_WIN32_WCE)
134 WSADATA wsaData;
135
136 if( wsa_init_done == 0 )
137 {
138 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000139 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 wsa_init_done = 1;
142 }
143#else
144 signal( SIGPIPE, SIG_IGN );
145#endif
146
147 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000148 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150 n = 1;
151 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
152 (const char *) &n, sizeof( n ) );
153
154 server_addr.sin_addr.s_addr = INADDR_ANY;
155 server_addr.sin_family = AF_INET;
156 server_addr.sin_port = net_htons( port );
157
158 if( bind_ip != NULL )
159 {
160 memset( c, 0, sizeof( c ) );
161 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
162
163 for( n = 0; n < 4; n++ )
164 if( c[n] < 0 || c[n] > 255 )
165 break;
166
167 if( n == 4 )
168 server_addr.sin_addr.s_addr =
169 ( (unsigned long) c[0] << 24 ) |
170 ( (unsigned long) c[1] << 16 ) |
171 ( (unsigned long) c[2] << 8 ) |
172 ( (unsigned long) c[3] );
173 }
174
175 if( bind( *fd, (struct sockaddr *) &server_addr,
176 sizeof( server_addr ) ) < 0 )
177 {
178 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000179 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 }
181
182 if( listen( *fd, 10 ) != 0 )
183 {
184 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000185 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 }
187
188 return( 0 );
189}
190
191/*
192 * Check if the current operation is blocking
193 */
194static int net_is_blocking( void )
195{
196#if defined(WIN32) || defined(_WIN32_WCE)
197 return( WSAGetLastError() == WSAEWOULDBLOCK );
198#else
199 switch( errno )
200 {
201#if defined EAGAIN
202 case EAGAIN:
203#endif
204#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
205 case EWOULDBLOCK:
206#endif
207 return( 1 );
208 }
209 return( 0 );
210#endif
211}
212
213/*
214 * Accept a connection from a remote client
215 */
216int net_accept( int bind_fd, int *client_fd, void *client_ip )
217{
218 struct sockaddr_in client_addr;
219
220#if defined(__socklen_t_defined)
221 socklen_t n = (socklen_t) sizeof( client_addr );
222#else
223 int n = (int) sizeof( client_addr );
224#endif
225
226 *client_fd = accept( bind_fd, (struct sockaddr *)
227 &client_addr, &n );
228
229 if( *client_fd < 0 )
230 {
231 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000232 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
Paul Bakker40e46942009-01-03 21:51:57 +0000234 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 }
236
237 if( client_ip != NULL )
238 memcpy( client_ip, &client_addr.sin_addr.s_addr,
239 sizeof( client_addr.sin_addr.s_addr ) );
240
241 return( 0 );
242}
243
244/*
245 * Set the socket blocking or non-blocking
246 */
247int net_set_block( int fd )
248{
249#if defined(WIN32) || defined(_WIN32_WCE)
250 long n = 0;
251 return( ioctlsocket( fd, FIONBIO, &n ) );
252#else
253 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
254#endif
255}
256
257int net_set_nonblock( int fd )
258{
259#if defined(WIN32) || defined(_WIN32_WCE)
260 long n = 1;
261 return( ioctlsocket( fd, FIONBIO, &n ) );
262#else
263 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
264#endif
265}
266
267/*
268 * Portable usleep helper
269 */
270void net_usleep( unsigned long usec )
271{
272 struct timeval tv;
273 tv.tv_sec = 0;
274 tv.tv_usec = usec;
275 select( 0, NULL, NULL, NULL, &tv );
276}
277
278/*
279 * Read at most 'len' characters
280 */
281int net_recv( void *ctx, unsigned char *buf, int len )
282{
283 int ret = read( *((int *) ctx), buf, len );
284
285 if( len > 0 && ret == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000286 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 if( ret < 0 )
289 {
290 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000291 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293#if defined(WIN32) || defined(_WIN32_WCE)
294 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000295 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296#else
297 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000298 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000301 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000302#endif
303
Paul Bakker40e46942009-01-03 21:51:57 +0000304 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 }
306
307 return( ret );
308}
309
310/*
311 * Write at most 'len' characters
312 */
313int net_send( void *ctx, unsigned char *buf, int len )
314{
315 int ret = write( *((int *) ctx), buf, len );
316
317 if( ret < 0 )
318 {
319 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000320 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322#if defined(WIN32) || defined(_WIN32_WCE)
323 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000324 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325#else
326 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000327 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000330 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331#endif
332
Paul Bakker40e46942009-01-03 21:51:57 +0000333 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 }
335
336 return( ret );
337}
338
339/*
340 * Gracefully close the connection
341 */
342void net_close( int fd )
343{
344 shutdown( fd, 2 );
345 close( fd );
346}
347
348#endif