blob: df79bf3a7695700b82897a729f94813648103097 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
4 * Copyright (C) 2006-2007 Christophe Devine
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
Paul Bakker40e46942009-01-03 21:51:57 +000021#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000022
Paul Bakker40e46942009-01-03 21:51:57 +000023#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Paul Bakker40e46942009-01-03 21:51:57 +000025#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
27#if defined(WIN32) || defined(_WIN32_WCE)
28
29#include <winsock2.h>
30#include <windows.h>
31
32#if defined(_WIN32_WCE)
33#pragma comment( lib, "ws2.lib" )
34#else
35#pragma comment( lib, "ws2_32.lib" )
36#endif
37
38#define read(fd,buf,len) recv(fd,buf,len,0)
39#define write(fd,buf,len) send(fd,buf,len,0)
40#define close(fd) closesocket(fd)
41
42static int wsa_init_done = 0;
43
44#else
45
46#include <sys/types.h>
47#include <sys/socket.h>
48#include <netinet/in.h>
49#include <arpa/inet.h>
50#include <sys/time.h>
51#include <unistd.h>
52#include <signal.h>
53#include <fcntl.h>
54#include <netdb.h>
55#include <errno.h>
56
57#endif
58
59#include <string.h>
60#include <stdlib.h>
61#include <stdio.h>
62#include <time.h>
63
64/*
65 * htons() is not always available
66 */
67static unsigned short net_htons( int port )
68{
69 unsigned char buf[4];
70
71 buf[0] = (unsigned char)( port >> 8 );
72 buf[1] = (unsigned char)( port );
73 buf[2] = buf[3] = 0;
74
75 return( *(unsigned short *) buf );
76}
77
78/*
79 * Initiate a TCP connection with host:port
80 */
81int net_connect( int *fd, char *host, int port )
82{
83 struct sockaddr_in server_addr;
84 struct hostent *server_host;
85
86#if defined(WIN32) || defined(_WIN32_WCE)
87 WSADATA wsaData;
88
89 if( wsa_init_done == 0 )
90 {
91 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +000092 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94 wsa_init_done = 1;
95 }
96#else
97 signal( SIGPIPE, SIG_IGN );
98#endif
99
100 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000101 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
103 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000104 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
106 memcpy( (void *) &server_addr.sin_addr,
107 (void *) server_host->h_addr,
108 server_host->h_length );
109
110 server_addr.sin_family = AF_INET;
111 server_addr.sin_port = net_htons( port );
112
113 if( connect( *fd, (struct sockaddr *) &server_addr,
114 sizeof( server_addr ) ) < 0 )
115 {
116 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000117 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 }
119
120 return( 0 );
121}
122
123/*
124 * Create a listening socket on bind_ip:port
125 */
126int net_bind( int *fd, char *bind_ip, int port )
127{
128 int n, c[4];
129 struct sockaddr_in server_addr;
130
131#if defined(WIN32) || defined(_WIN32_WCE)
132 WSADATA wsaData;
133
134 if( wsa_init_done == 0 )
135 {
136 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000137 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139 wsa_init_done = 1;
140 }
141#else
142 signal( SIGPIPE, SIG_IGN );
143#endif
144
145 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000146 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148 n = 1;
149 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
150 (const char *) &n, sizeof( n ) );
151
152 server_addr.sin_addr.s_addr = INADDR_ANY;
153 server_addr.sin_family = AF_INET;
154 server_addr.sin_port = net_htons( port );
155
156 if( bind_ip != NULL )
157 {
158 memset( c, 0, sizeof( c ) );
159 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
160
161 for( n = 0; n < 4; n++ )
162 if( c[n] < 0 || c[n] > 255 )
163 break;
164
165 if( n == 4 )
166 server_addr.sin_addr.s_addr =
167 ( (unsigned long) c[0] << 24 ) |
168 ( (unsigned long) c[1] << 16 ) |
169 ( (unsigned long) c[2] << 8 ) |
170 ( (unsigned long) c[3] );
171 }
172
173 if( bind( *fd, (struct sockaddr *) &server_addr,
174 sizeof( server_addr ) ) < 0 )
175 {
176 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000177 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 }
179
180 if( listen( *fd, 10 ) != 0 )
181 {
182 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000183 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 }
185
186 return( 0 );
187}
188
189/*
190 * Check if the current operation is blocking
191 */
192static int net_is_blocking( void )
193{
194#if defined(WIN32) || defined(_WIN32_WCE)
195 return( WSAGetLastError() == WSAEWOULDBLOCK );
196#else
197 switch( errno )
198 {
199#if defined EAGAIN
200 case EAGAIN:
201#endif
202#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
203 case EWOULDBLOCK:
204#endif
205 return( 1 );
206 }
207 return( 0 );
208#endif
209}
210
211/*
212 * Accept a connection from a remote client
213 */
214int net_accept( int bind_fd, int *client_fd, void *client_ip )
215{
216 struct sockaddr_in client_addr;
217
218#if defined(__socklen_t_defined)
219 socklen_t n = (socklen_t) sizeof( client_addr );
220#else
221 int n = (int) sizeof( client_addr );
222#endif
223
224 *client_fd = accept( bind_fd, (struct sockaddr *)
225 &client_addr, &n );
226
227 if( *client_fd < 0 )
228 {
229 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000230 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakker40e46942009-01-03 21:51:57 +0000232 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 }
234
235 if( client_ip != NULL )
236 memcpy( client_ip, &client_addr.sin_addr.s_addr,
237 sizeof( client_addr.sin_addr.s_addr ) );
238
239 return( 0 );
240}
241
242/*
243 * Set the socket blocking or non-blocking
244 */
245int net_set_block( int fd )
246{
247#if defined(WIN32) || defined(_WIN32_WCE)
248 long n = 0;
249 return( ioctlsocket( fd, FIONBIO, &n ) );
250#else
251 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
252#endif
253}
254
255int net_set_nonblock( int fd )
256{
257#if defined(WIN32) || defined(_WIN32_WCE)
258 long n = 1;
259 return( ioctlsocket( fd, FIONBIO, &n ) );
260#else
261 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
262#endif
263}
264
265/*
266 * Portable usleep helper
267 */
268void net_usleep( unsigned long usec )
269{
270 struct timeval tv;
271 tv.tv_sec = 0;
272 tv.tv_usec = usec;
273 select( 0, NULL, NULL, NULL, &tv );
274}
275
276/*
277 * Read at most 'len' characters
278 */
279int net_recv( void *ctx, unsigned char *buf, int len )
280{
281 int ret = read( *((int *) ctx), buf, len );
282
283 if( len > 0 && ret == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000284 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286 if( ret < 0 )
287 {
288 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000289 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
291#if defined(WIN32) || defined(_WIN32_WCE)
292 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000293 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294#else
295 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000296 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000299 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300#endif
301
Paul Bakker40e46942009-01-03 21:51:57 +0000302 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 }
304
305 return( ret );
306}
307
308/*
309 * Write at most 'len' characters
310 */
311int net_send( void *ctx, unsigned char *buf, int len )
312{
313 int ret = write( *((int *) ctx), buf, len );
314
315 if( ret < 0 )
316 {
317 if( net_is_blocking() != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000318 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320#if defined(WIN32) || defined(_WIN32_WCE)
321 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000322 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323#else
324 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000325 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327 if( errno == EINTR )
Paul Bakker40e46942009-01-03 21:51:57 +0000328 return( POLARSSL_ERR_NET_TRY_AGAIN );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329#endif
330
Paul Bakker40e46942009-01-03 21:51:57 +0000331 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 }
333
334 return( ret );
335}
336
337/*
338 * Gracefully close the connection
339 */
340void net_close( int fd )
341{
342 shutdown( fd, 2 );
343 close( fd );
344}
345
346#endif