Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * TCP networking functions |
| 3 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
Paul Bakker | 785a9ee | 2009-01-25 14:15:10 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | * |
| 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 23 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 25 | #if defined(POLARSSL_NET_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #include "polarssl/net.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
| 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 | |
| 44 | static 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 Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame^] | 58 | #include <endian.h> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | |
| 60 | #endif |
| 61 | |
| 62 | #include <string.h> |
| 63 | #include <stdlib.h> |
| 64 | #include <stdio.h> |
| 65 | #include <time.h> |
| 66 | |
| 67 | /* |
Paul Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame^] | 68 | * 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | */ |
Paul Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame^] | 72 | #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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | |
Paul Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame^] | 78 | unsigned short net_htons(unsigned short n); |
| 79 | #define net_htons(n) HTONS(n) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * Initiate a TCP connection with host:port |
| 83 | */ |
| 84 | int 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 95 | return( POLARSSL_ERR_NET_SOCKET_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | |
| 97 | wsa_init_done = 1; |
| 98 | } |
| 99 | #else |
| 100 | signal( SIGPIPE, SIG_IGN ); |
| 101 | #endif |
| 102 | |
| 103 | if( ( server_host = gethostbyname( host ) ) == NULL ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 104 | return( POLARSSL_ERR_NET_UNKNOWN_HOST ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | |
| 106 | if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 107 | return( POLARSSL_ERR_NET_SOCKET_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | |
| 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 120 | return( POLARSSL_ERR_NET_CONNECT_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return( 0 ); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Create a listening socket on bind_ip:port |
| 128 | */ |
| 129 | int 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 140 | return( POLARSSL_ERR_NET_SOCKET_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
| 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 149 | return( POLARSSL_ERR_NET_SOCKET_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 | |
| 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 180 | return( POLARSSL_ERR_NET_BIND_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | if( listen( *fd, 10 ) != 0 ) |
| 184 | { |
| 185 | close( *fd ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 186 | return( POLARSSL_ERR_NET_LISTEN_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return( 0 ); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Check if the current operation is blocking |
| 194 | */ |
| 195 | static 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 | */ |
| 217 | int 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 233 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 234 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 235 | return( POLARSSL_ERR_NET_ACCEPT_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 236 | } |
| 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 | */ |
| 248 | int 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 | |
| 258 | int 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 | */ |
| 271 | void 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 | */ |
| 282 | int 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 287 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 288 | |
| 289 | if( ret < 0 ) |
| 290 | { |
| 291 | if( net_is_blocking() != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 292 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 | |
| 294 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 295 | if( WSAGetLastError() == WSAECONNRESET ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 296 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 297 | #else |
| 298 | if( errno == EPIPE || errno == ECONNRESET ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 299 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 300 | |
| 301 | if( errno == EINTR ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 302 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 303 | #endif |
| 304 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 305 | return( POLARSSL_ERR_NET_RECV_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return( ret ); |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Write at most 'len' characters |
| 313 | */ |
| 314 | int 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 321 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 322 | |
| 323 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 324 | if( WSAGetLastError() == WSAECONNRESET ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 325 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 326 | #else |
| 327 | if( errno == EPIPE || errno == ECONNRESET ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 328 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 329 | |
| 330 | if( errno == EINTR ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 331 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 332 | #endif |
| 333 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 334 | return( POLARSSL_ERR_NET_SEND_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | return( ret ); |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Gracefully close the connection |
| 342 | */ |
| 343 | void net_close( int fd ) |
| 344 | { |
| 345 | shutdown( fd, 2 ); |
| 346 | close( fd ); |
| 347 | } |
| 348 | |
| 349 | #endif |