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