Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * TCP networking functions |
| 3 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org> |
| 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 7 | * Joined copyright on original XySSL code with: Christophe Devine |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | */ |
| 23 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 26 | #if defined(POLARSSL_NET_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 28 | #include "polarssl/net.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
| 30 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 31 | |
| 32 | #include <winsock2.h> |
| 33 | #include <windows.h> |
| 34 | |
| 35 | #if defined(_WIN32_WCE) |
| 36 | #pragma comment( lib, "ws2.lib" ) |
| 37 | #else |
| 38 | #pragma comment( lib, "ws2_32.lib" ) |
| 39 | #endif |
| 40 | |
| 41 | #define read(fd,buf,len) recv(fd,buf,len,0) |
| 42 | #define write(fd,buf,len) send(fd,buf,len,0) |
| 43 | #define close(fd) closesocket(fd) |
| 44 | |
| 45 | static int wsa_init_done = 0; |
| 46 | |
| 47 | #else |
| 48 | |
| 49 | #include <sys/types.h> |
| 50 | #include <sys/socket.h> |
| 51 | #include <netinet/in.h> |
| 52 | #include <arpa/inet.h> |
| 53 | #include <sys/time.h> |
| 54 | #include <unistd.h> |
| 55 | #include <signal.h> |
| 56 | #include <fcntl.h> |
| 57 | #include <netdb.h> |
| 58 | #include <errno.h> |
Paul Bakker | b3bb6c0 | 2009-07-27 21:09:47 +0000 | [diff] [blame] | 59 | |
Paul Bakker | 854963c | 2009-07-19 20:50:11 +0000 | [diff] [blame] | 60 | #if defined(__FreeBSD__) |
| 61 | #include <sys/endian.h> |
Paul Bakker | b3bb6c0 | 2009-07-27 21:09:47 +0000 | [diff] [blame] | 62 | #elif defined(__APPLE__) |
| 63 | #include <machine/endian.h> |
Paul Bakker | 854963c | 2009-07-19 20:50:11 +0000 | [diff] [blame] | 64 | #else |
Paul Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame] | 65 | #include <endian.h> |
Paul Bakker | 854963c | 2009-07-19 20:50:11 +0000 | [diff] [blame] | 66 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | |
| 68 | #endif |
| 69 | |
| 70 | #include <string.h> |
| 71 | #include <stdlib.h> |
| 72 | #include <stdio.h> |
| 73 | #include <time.h> |
| 74 | |
| 75 | /* |
Paul Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame] | 76 | * htons() is not always available. |
| 77 | * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN |
| 78 | * to help determine endianess. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | */ |
Paul Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame] | 80 | #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN |
Paul Bakker | b3bb6c0 | 2009-07-27 21:09:47 +0000 | [diff] [blame] | 81 | #define POLARSSL_HTONS(n) (n) |
Paul Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame] | 82 | #else |
Paul Bakker | b3bb6c0 | 2009-07-27 21:09:47 +0000 | [diff] [blame] | 83 | #define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8)) |
Paul Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame] | 84 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | |
Paul Bakker | 1d4f30c | 2009-04-19 18:55:16 +0000 | [diff] [blame] | 86 | unsigned short net_htons(unsigned short n); |
Paul Bakker | b3bb6c0 | 2009-07-27 21:09:47 +0000 | [diff] [blame] | 87 | #define net_htons(n) POLARSSL_HTONS(n) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * Initiate a TCP connection with host:port |
| 91 | */ |
| 92 | int net_connect( int *fd, char *host, int port ) |
| 93 | { |
| 94 | struct sockaddr_in server_addr; |
| 95 | struct hostent *server_host; |
| 96 | |
| 97 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 98 | WSADATA wsaData; |
| 99 | |
| 100 | if( wsa_init_done == 0 ) |
| 101 | { |
| 102 | if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 103 | return( POLARSSL_ERR_NET_SOCKET_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | |
| 105 | wsa_init_done = 1; |
| 106 | } |
| 107 | #else |
| 108 | signal( SIGPIPE, SIG_IGN ); |
| 109 | #endif |
| 110 | |
| 111 | if( ( server_host = gethostbyname( host ) ) == NULL ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 112 | return( POLARSSL_ERR_NET_UNKNOWN_HOST ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | |
| 114 | if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 115 | return( POLARSSL_ERR_NET_SOCKET_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | |
| 117 | memcpy( (void *) &server_addr.sin_addr, |
| 118 | (void *) server_host->h_addr, |
| 119 | server_host->h_length ); |
| 120 | |
| 121 | server_addr.sin_family = AF_INET; |
| 122 | server_addr.sin_port = net_htons( port ); |
| 123 | |
| 124 | if( connect( *fd, (struct sockaddr *) &server_addr, |
| 125 | sizeof( server_addr ) ) < 0 ) |
| 126 | { |
| 127 | close( *fd ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 128 | return( POLARSSL_ERR_NET_CONNECT_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return( 0 ); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Create a listening socket on bind_ip:port |
| 136 | */ |
| 137 | int net_bind( int *fd, char *bind_ip, int port ) |
| 138 | { |
| 139 | int n, c[4]; |
| 140 | struct sockaddr_in server_addr; |
| 141 | |
| 142 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 143 | WSADATA wsaData; |
| 144 | |
| 145 | if( wsa_init_done == 0 ) |
| 146 | { |
| 147 | if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 148 | return( POLARSSL_ERR_NET_SOCKET_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | |
| 150 | wsa_init_done = 1; |
| 151 | } |
| 152 | #else |
| 153 | signal( SIGPIPE, SIG_IGN ); |
| 154 | #endif |
| 155 | |
| 156 | if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 157 | return( POLARSSL_ERR_NET_SOCKET_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | |
| 159 | n = 1; |
| 160 | setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR, |
| 161 | (const char *) &n, sizeof( n ) ); |
| 162 | |
| 163 | server_addr.sin_addr.s_addr = INADDR_ANY; |
| 164 | server_addr.sin_family = AF_INET; |
| 165 | server_addr.sin_port = net_htons( port ); |
| 166 | |
| 167 | if( bind_ip != NULL ) |
| 168 | { |
| 169 | memset( c, 0, sizeof( c ) ); |
| 170 | sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] ); |
| 171 | |
| 172 | for( n = 0; n < 4; n++ ) |
| 173 | if( c[n] < 0 || c[n] > 255 ) |
| 174 | break; |
| 175 | |
| 176 | if( n == 4 ) |
| 177 | server_addr.sin_addr.s_addr = |
| 178 | ( (unsigned long) c[0] << 24 ) | |
| 179 | ( (unsigned long) c[1] << 16 ) | |
| 180 | ( (unsigned long) c[2] << 8 ) | |
| 181 | ( (unsigned long) c[3] ); |
| 182 | } |
| 183 | |
| 184 | if( bind( *fd, (struct sockaddr *) &server_addr, |
| 185 | sizeof( server_addr ) ) < 0 ) |
| 186 | { |
| 187 | close( *fd ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 188 | return( POLARSSL_ERR_NET_BIND_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if( listen( *fd, 10 ) != 0 ) |
| 192 | { |
| 193 | close( *fd ); |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 194 | return( POLARSSL_ERR_NET_LISTEN_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return( 0 ); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Check if the current operation is blocking |
| 202 | */ |
| 203 | static int net_is_blocking( void ) |
| 204 | { |
| 205 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 206 | return( WSAGetLastError() == WSAEWOULDBLOCK ); |
| 207 | #else |
| 208 | switch( errno ) |
| 209 | { |
| 210 | #if defined EAGAIN |
| 211 | case EAGAIN: |
| 212 | #endif |
| 213 | #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN |
| 214 | case EWOULDBLOCK: |
| 215 | #endif |
| 216 | return( 1 ); |
| 217 | } |
| 218 | return( 0 ); |
| 219 | #endif |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Accept a connection from a remote client |
| 224 | */ |
| 225 | int net_accept( int bind_fd, int *client_fd, void *client_ip ) |
| 226 | { |
| 227 | struct sockaddr_in client_addr; |
| 228 | |
| 229 | #if defined(__socklen_t_defined) |
| 230 | socklen_t n = (socklen_t) sizeof( client_addr ); |
| 231 | #else |
| 232 | int n = (int) sizeof( client_addr ); |
| 233 | #endif |
| 234 | |
| 235 | *client_fd = accept( bind_fd, (struct sockaddr *) |
| 236 | &client_addr, &n ); |
| 237 | |
| 238 | if( *client_fd < 0 ) |
| 239 | { |
| 240 | if( net_is_blocking() != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 241 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 243 | return( POLARSSL_ERR_NET_ACCEPT_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | if( client_ip != NULL ) |
| 247 | memcpy( client_ip, &client_addr.sin_addr.s_addr, |
| 248 | sizeof( client_addr.sin_addr.s_addr ) ); |
| 249 | |
| 250 | return( 0 ); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Set the socket blocking or non-blocking |
| 255 | */ |
| 256 | int net_set_block( int fd ) |
| 257 | { |
| 258 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 259 | long n = 0; |
| 260 | return( ioctlsocket( fd, FIONBIO, &n ) ); |
| 261 | #else |
| 262 | return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) ); |
| 263 | #endif |
| 264 | } |
| 265 | |
| 266 | int net_set_nonblock( int fd ) |
| 267 | { |
| 268 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 269 | long n = 1; |
| 270 | return( ioctlsocket( fd, FIONBIO, &n ) ); |
| 271 | #else |
| 272 | return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) ); |
| 273 | #endif |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Portable usleep helper |
| 278 | */ |
| 279 | void net_usleep( unsigned long usec ) |
| 280 | { |
| 281 | struct timeval tv; |
| 282 | tv.tv_sec = 0; |
| 283 | tv.tv_usec = usec; |
| 284 | select( 0, NULL, NULL, NULL, &tv ); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Read at most 'len' characters |
| 289 | */ |
| 290 | int net_recv( void *ctx, unsigned char *buf, int len ) |
| 291 | { |
| 292 | int ret = read( *((int *) ctx), buf, len ); |
| 293 | |
| 294 | if( len > 0 && ret == 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 295 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 296 | |
| 297 | if( ret < 0 ) |
| 298 | { |
| 299 | if( net_is_blocking() != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 300 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 301 | |
| 302 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 303 | if( WSAGetLastError() == WSAECONNRESET ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 304 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 305 | #else |
| 306 | if( errno == EPIPE || errno == ECONNRESET ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 307 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 308 | |
| 309 | if( errno == EINTR ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 310 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 311 | #endif |
| 312 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 313 | return( POLARSSL_ERR_NET_RECV_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | return( ret ); |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Write at most 'len' characters |
| 321 | */ |
| 322 | int net_send( void *ctx, unsigned char *buf, int len ) |
| 323 | { |
| 324 | int ret = write( *((int *) ctx), buf, len ); |
| 325 | |
| 326 | if( ret < 0 ) |
| 327 | { |
| 328 | if( net_is_blocking() != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 329 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | |
| 331 | #if defined(WIN32) || defined(_WIN32_WCE) |
| 332 | if( WSAGetLastError() == WSAECONNRESET ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 333 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 334 | #else |
| 335 | if( errno == EPIPE || errno == ECONNRESET ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 336 | return( POLARSSL_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 337 | |
| 338 | if( errno == EINTR ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 339 | return( POLARSSL_ERR_NET_TRY_AGAIN ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 340 | #endif |
| 341 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 342 | return( POLARSSL_ERR_NET_SEND_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | return( ret ); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Gracefully close the connection |
| 350 | */ |
| 351 | void net_close( int fd ) |
| 352 | { |
| 353 | shutdown( fd, 2 ); |
| 354 | close( fd ); |
| 355 | } |
| 356 | |
| 357 | #endif |