Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | f4acfe1 | 2014-09-17 10:56:54 +0200 | [diff] [blame] | 2 | * TCP/IP or UDP/IP networking functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_NET_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/net.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 35 | #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \ |
| 36 | !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | 3b6269a | 2014-03-21 10:31:12 +0100 | [diff] [blame] | 38 | #ifdef _WIN32_WINNT |
| 39 | #undef _WIN32_WINNT |
| 40 | #endif |
| 41 | /* Enables getaddrinfo() & Co */ |
Manuel Pégourié-Gonnard | 1321135 | 2013-12-17 17:38:55 +0100 | [diff] [blame] | 42 | #define _WIN32_WINNT 0x0501 |
Manuel Pégourié-Gonnard | 6a398d4 | 2013-12-17 16:10:58 +0100 | [diff] [blame] | 43 | #include <ws2tcpip.h> |
Manuel Pégourié-Gonnard | 6a398d4 | 2013-12-17 16:10:58 +0100 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 1321135 | 2013-12-17 17:38:55 +0100 | [diff] [blame] | 45 | #include <winsock2.h> |
| 46 | #include <windows.h> |
| 47 | |
Paul Bakker | f0fc2a2 | 2013-12-30 15:42:43 +0100 | [diff] [blame] | 48 | #if defined(_MSC_VER) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 | #if defined(_WIN32_WCE) |
| 50 | #pragma comment( lib, "ws2.lib" ) |
| 51 | #else |
| 52 | #pragma comment( lib, "ws2_32.lib" ) |
| 53 | #endif |
Paul Bakker | f0fc2a2 | 2013-12-30 15:42:43 +0100 | [diff] [blame] | 54 | #endif /* _MSC_VER */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | |
Paul Bakker | f4f6968 | 2011-04-24 16:08:12 +0000 | [diff] [blame] | 56 | #define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0) |
| 57 | #define write(fd,buf,len) send(fd,(char*)buf,(int) len,0) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | #define close(fd) closesocket(fd) |
| 59 | |
| 60 | static int wsa_init_done = 0; |
| 61 | |
Paul Bakker | db20c10 | 2014-06-17 14:34:44 +0200 | [diff] [blame] | 62 | #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | |
| 64 | #include <sys/types.h> |
| 65 | #include <sys/socket.h> |
| 66 | #include <netinet/in.h> |
| 67 | #include <arpa/inet.h> |
| 68 | #include <sys/time.h> |
| 69 | #include <unistd.h> |
| 70 | #include <signal.h> |
| 71 | #include <fcntl.h> |
| 72 | #include <netdb.h> |
| 73 | #include <errno.h> |
Paul Bakker | b3bb6c0 | 2009-07-27 21:09:47 +0000 | [diff] [blame] | 74 | |
Paul Bakker | db20c10 | 2014-06-17 14:34:44 +0200 | [diff] [blame] | 75 | #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | #include <stdlib.h> |
| 78 | #include <stdio.h> |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 79 | |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 80 | #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \ |
| 81 | !defined(EFI32) |
| 82 | #define snprintf _snprintf |
| 83 | #endif |
| 84 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | #include <time.h> |
| 86 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 87 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 88 | #include <basetsd.h> |
| 89 | typedef UINT32 uint32_t; |
| 90 | #else |
| 91 | #include <inttypes.h> |
| 92 | #endif |
| 93 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 95 | #include "mbedtls/platform.h" |
Rich Evans | a18b11f | 2015-01-30 10:58:35 +0000 | [diff] [blame] | 96 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | #define mbedtls_snprintf snprintf |
Rich Evans | a18b11f | 2015-01-30 10:58:35 +0000 | [diff] [blame] | 98 | #endif |
| 99 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | /* |
Manuel Pégourié-Gonnard | 2e5c316 | 2013-12-13 11:55:32 +0100 | [diff] [blame] | 101 | * Prepare for using the sockets interface |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | */ |
Manuel Pégourié-Gonnard | 173402b | 2013-12-17 15:57:05 +0100 | [diff] [blame] | 103 | static int net_prepare( void ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | { |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 105 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
| 106 | !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | WSADATA wsaData; |
| 108 | |
| 109 | if( wsa_init_done == 0 ) |
| 110 | { |
Peter Vaskovic | 7015de7 | 2014-05-15 02:54:37 +0200 | [diff] [blame] | 111 | if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | return( MBEDTLS_ERR_NET_SOCKET_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | |
| 114 | wsa_init_done = 1; |
| 115 | } |
| 116 | #else |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 117 | #if !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | signal( SIGPIPE, SIG_IGN ); |
| 119 | #endif |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 120 | #endif |
Manuel Pégourié-Gonnard | ee5db1d | 2013-12-17 16:46:19 +0100 | [diff] [blame] | 121 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e5c316 | 2013-12-13 11:55:32 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 125 | * Initiate a TCP connection with host:port and the given protocol |
Manuel Pégourié-Gonnard | 2e5c316 | 2013-12-13 11:55:32 +0100 | [diff] [blame] | 126 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | int mbedtls_net_connect( int *fd, const char *host, int port, int proto ) |
Manuel Pégourié-Gonnard | 2e5c316 | 2013-12-13 11:55:32 +0100 | [diff] [blame] | 128 | { |
Manuel Pégourié-Gonnard | 173402b | 2013-12-17 15:57:05 +0100 | [diff] [blame] | 129 | int ret; |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 130 | struct addrinfo hints, *addr_list, *cur; |
| 131 | char port_str[6]; |
| 132 | |
Manuel Pégourié-Gonnard | 173402b | 2013-12-17 15:57:05 +0100 | [diff] [blame] | 133 | if( ( ret = net_prepare() ) != 0 ) |
| 134 | return( ret ); |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 135 | |
| 136 | /* getaddrinfo expects port as a string */ |
| 137 | memset( port_str, 0, sizeof( port_str ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | mbedtls_snprintf( port_str, sizeof( port_str ), "%d", port ); |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 139 | |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 140 | /* Do name resolution with both IPv6 and IPv4 */ |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 141 | memset( &hints, 0, sizeof( hints ) ); |
| 142 | hints.ai_family = AF_UNSPEC; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; |
| 144 | hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP; |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 145 | |
| 146 | if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | return( MBEDTLS_ERR_NET_UNKNOWN_HOST ); |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 148 | |
| 149 | /* Try the sockaddrs until a connection succeeds */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | ret = MBEDTLS_ERR_NET_UNKNOWN_HOST; |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 151 | for( cur = addr_list; cur != NULL; cur = cur->ai_next ) |
| 152 | { |
Paul Bakker | 00f5c52 | 2013-12-31 10:45:16 +0100 | [diff] [blame] | 153 | *fd = (int) socket( cur->ai_family, cur->ai_socktype, |
| 154 | cur->ai_protocol ); |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 155 | if( *fd < 0 ) |
| 156 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | ret = MBEDTLS_ERR_NET_SOCKET_FAILED; |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 158 | continue; |
| 159 | } |
| 160 | |
| 161 | if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 ) |
| 162 | { |
| 163 | ret = 0; |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | close( *fd ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | ret = MBEDTLS_ERR_NET_CONNECT_FAILED; |
Manuel Pégourié-Gonnard | 10934de | 2013-12-13 12:54:09 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | freeaddrinfo( addr_list ); |
| 172 | |
| 173 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Create a listening socket on bind_ip:port |
| 178 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | int mbedtls_net_bind( int *fd, const char *bind_ip, int port, int proto ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 180 | { |
Manuel Pégourié-Gonnard | 173402b | 2013-12-17 15:57:05 +0100 | [diff] [blame] | 181 | int n, ret; |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 182 | struct addrinfo hints, *addr_list, *cur; |
| 183 | char port_str[6]; |
| 184 | |
Manuel Pégourié-Gonnard | 173402b | 2013-12-17 15:57:05 +0100 | [diff] [blame] | 185 | if( ( ret = net_prepare() ) != 0 ) |
| 186 | return( ret ); |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 187 | |
| 188 | /* getaddrinfo expects port as a string */ |
| 189 | memset( port_str, 0, sizeof( port_str ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | mbedtls_snprintf( port_str, sizeof( port_str ), "%d", port ); |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 191 | |
| 192 | /* Bind to IPv6 and/or IPv4, but only in TCP */ |
| 193 | memset( &hints, 0, sizeof( hints ) ); |
| 194 | hints.ai_family = AF_UNSPEC; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; |
| 196 | hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP; |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 197 | if( bind_ip == NULL ) |
| 198 | hints.ai_flags = AI_PASSIVE; |
| 199 | |
| 200 | if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | return( MBEDTLS_ERR_NET_UNKNOWN_HOST ); |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 202 | |
| 203 | /* Try the sockaddrs until a binding succeeds */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | ret = MBEDTLS_ERR_NET_UNKNOWN_HOST; |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 205 | for( cur = addr_list; cur != NULL; cur = cur->ai_next ) |
| 206 | { |
Paul Bakker | 00f5c52 | 2013-12-31 10:45:16 +0100 | [diff] [blame] | 207 | *fd = (int) socket( cur->ai_family, cur->ai_socktype, |
| 208 | cur->ai_protocol ); |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 209 | if( *fd < 0 ) |
| 210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | ret = MBEDTLS_ERR_NET_SOCKET_FAILED; |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 212 | continue; |
| 213 | } |
| 214 | |
Manuel Pégourié-Gonnard | fd6b4cc | 2013-12-17 13:59:01 +0100 | [diff] [blame] | 215 | n = 1; |
Paul Bakker | 874bd64 | 2014-04-17 12:43:05 +0200 | [diff] [blame] | 216 | if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR, |
| 217 | (const char *) &n, sizeof( n ) ) != 0 ) |
| 218 | { |
| 219 | close( *fd ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | ret = MBEDTLS_ERR_NET_SOCKET_FAILED; |
Paul Bakker | 874bd64 | 2014-04-17 12:43:05 +0200 | [diff] [blame] | 221 | continue; |
| 222 | } |
Manuel Pégourié-Gonnard | fd6b4cc | 2013-12-17 13:59:01 +0100 | [diff] [blame] | 223 | |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 224 | if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 ) |
| 225 | { |
| 226 | close( *fd ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | ret = MBEDTLS_ERR_NET_BIND_FAILED; |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 228 | continue; |
| 229 | } |
| 230 | |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 231 | /* Listen only makes sense for TCP */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | if( proto == MBEDTLS_NET_PROTO_TCP ) |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 233 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | if( listen( *fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 ) |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 235 | { |
| 236 | close( *fd ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | ret = MBEDTLS_ERR_NET_LISTEN_FAILED; |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 238 | continue; |
| 239 | } |
Manuel Pégourié-Gonnard | 389ce63 | 2013-12-13 14:00:51 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* I we ever get there, it's a success */ |
| 243 | ret = 0; |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | freeaddrinfo( addr_list ); |
| 248 | |
| 249 | return( ret ); |
| 250 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 253 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
| 254 | !defined(EFI32) |
Paul Bakker | 8002541 | 2014-01-23 20:59:49 +0100 | [diff] [blame] | 255 | /* |
| 256 | * Check if the requested operation would be blocking on a non-blocking socket |
| 257 | * and thus 'failed' with a negative return value. |
| 258 | */ |
| 259 | static int net_would_block( int fd ) |
| 260 | { |
Manuel Pégourié-Gonnard | 3b6269a | 2014-03-21 10:31:12 +0100 | [diff] [blame] | 261 | ((void) fd); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 262 | return( WSAGetLastError() == WSAEWOULDBLOCK ); |
Paul Bakker | 8002541 | 2014-01-23 20:59:49 +0100 | [diff] [blame] | 263 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 264 | #else |
Paul Bakker | 8002541 | 2014-01-23 20:59:49 +0100 | [diff] [blame] | 265 | /* |
| 266 | * Check if the requested operation would be blocking on a non-blocking socket |
| 267 | * and thus 'failed' with a negative return value. |
| 268 | * |
| 269 | * Note: on a blocking socket this function always returns 0! |
| 270 | */ |
| 271 | static int net_would_block( int fd ) |
| 272 | { |
| 273 | /* |
| 274 | * Never return 'WOULD BLOCK' on a non-blocking socket |
| 275 | */ |
| 276 | if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK ) |
| 277 | return( 0 ); |
| 278 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | switch( errno ) |
| 280 | { |
| 281 | #if defined EAGAIN |
| 282 | case EAGAIN: |
| 283 | #endif |
| 284 | #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN |
| 285 | case EWOULDBLOCK: |
| 286 | #endif |
| 287 | return( 1 ); |
| 288 | } |
| 289 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | } |
Paul Bakker | db20c10 | 2014-06-17 14:34:44 +0200 | [diff] [blame] | 291 | #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * Accept a connection from a remote client |
| 295 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | int mbedtls_net_accept( int bind_fd, int *client_fd, void *client_ip ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 297 | { |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 298 | int ret; |
| 299 | int type; |
| 300 | |
Manuel Pégourié-Gonnard | 6e315a9 | 2013-12-13 16:21:25 +0100 | [diff] [blame] | 301 | struct sockaddr_storage client_addr; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 302 | |
Paul Bakker | 394c56f | 2011-12-20 12:19:03 +0000 | [diff] [blame] | 303 | #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \ |
| 304 | defined(_SOCKLEN_T_DECLARED) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 305 | socklen_t n = (socklen_t) sizeof( client_addr ); |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 306 | socklen_t type_len = (socklen_t) sizeof( type ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 307 | #else |
| 308 | int n = (int) sizeof( client_addr ); |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 309 | int type_len = (int) sizeof( type ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 310 | #endif |
| 311 | |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 312 | /* Is this a TCP or UDP socket? */ |
Manuel Pégourié-Gonnard | 9325b26 | 2015-03-25 16:57:52 +0100 | [diff] [blame] | 313 | if( getsockopt( bind_fd, SOL_SOCKET, SO_TYPE, (void *) &type, &type_len ) != 0 || |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 314 | ( type != SOCK_STREAM && type != SOCK_DGRAM ) ) |
| 315 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | return( MBEDTLS_ERR_NET_ACCEPT_FAILED ); |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 317 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 318 | |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 319 | if( type == SOCK_STREAM ) |
| 320 | { |
| 321 | /* TCP: actual accept() */ |
| 322 | ret = *client_fd = (int) accept( bind_fd, |
| 323 | (struct sockaddr *) &client_addr, &n ); |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | /* UDP: wait for a message, but keep it in the queue */ |
| 328 | char buf[1] = { 0 }; |
| 329 | |
Manuel Pégourié-Gonnard | f3c500f | 2015-01-12 19:02:15 +0100 | [diff] [blame] | 330 | ret = recvfrom( bind_fd, buf, sizeof( buf ), MSG_PEEK, |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 331 | (struct sockaddr *) &client_addr, &n ); |
| 332 | } |
| 333 | |
| 334 | if( ret < 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 335 | { |
Manuel Pégourié-Gonnard | 9a6b442 | 2014-07-21 13:42:54 +0200 | [diff] [blame] | 336 | if( net_would_block( bind_fd ) != 0 ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 337 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | return( MBEDTLS_ERR_NET_ACCEPT_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 342 | /* UDP: hijack the listening socket for communicating with the client */ |
| 343 | if( type != SOCK_STREAM ) |
| 344 | { |
| 345 | if( connect( bind_fd, (struct sockaddr *) &client_addr, n ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | return( MBEDTLS_ERR_NET_ACCEPT_FAILED ); |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 347 | |
| 348 | *client_fd = bind_fd; |
| 349 | } |
| 350 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 351 | if( client_ip != NULL ) |
Manuel Pégourié-Gonnard | 6e315a9 | 2013-12-13 16:21:25 +0100 | [diff] [blame] | 352 | { |
Manuel Pégourié-Gonnard | 6e315a9 | 2013-12-13 16:21:25 +0100 | [diff] [blame] | 353 | if( client_addr.ss_family == AF_INET ) |
| 354 | { |
| 355 | struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr; |
| 356 | memcpy( client_ip, &addr4->sin_addr.s_addr, |
| 357 | sizeof( addr4->sin_addr.s_addr ) ); |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr; |
| 362 | memcpy( client_ip, &addr6->sin6_addr.s6_addr, |
| 363 | sizeof( addr6->sin6_addr.s6_addr ) ); |
| 364 | } |
Manuel Pégourié-Gonnard | 6e315a9 | 2013-12-13 16:21:25 +0100 | [diff] [blame] | 365 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 366 | |
| 367 | return( 0 ); |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Set the socket blocking or non-blocking |
| 372 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 373 | int mbedtls_net_set_block( int fd ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 374 | { |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 375 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
| 376 | !defined(EFI32) |
Paul Bakker | f4f6968 | 2011-04-24 16:08:12 +0000 | [diff] [blame] | 377 | u_long n = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 378 | return( ioctlsocket( fd, FIONBIO, &n ) ); |
| 379 | #else |
| 380 | return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) ); |
| 381 | #endif |
| 382 | } |
| 383 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 384 | int mbedtls_net_set_nonblock( int fd ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 385 | { |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 386 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
| 387 | !defined(EFI32) |
Paul Bakker | f4f6968 | 2011-04-24 16:08:12 +0000 | [diff] [blame] | 388 | u_long n = 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 389 | return( ioctlsocket( fd, FIONBIO, &n ) ); |
| 390 | #else |
| 391 | return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) ); |
| 392 | #endif |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Portable usleep helper |
| 397 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | void mbedtls_net_usleep( unsigned long usec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 399 | { |
| 400 | struct timeval tv; |
Manuel Pégourié-Gonnard | e423246 | 2014-11-21 09:52:23 +0100 | [diff] [blame] | 401 | tv.tv_sec = usec / 1000000; |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 402 | #if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \ |
| 403 | ( defined(__APPLE__) && defined(__MACH__) ) ) |
Manuel Pégourié-Gonnard | e423246 | 2014-11-21 09:52:23 +0100 | [diff] [blame] | 404 | tv.tv_usec = (suseconds_t) usec % 1000000; |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 405 | #else |
Manuel Pégourié-Gonnard | e423246 | 2014-11-21 09:52:23 +0100 | [diff] [blame] | 406 | tv.tv_usec = usec % 1000000; |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 407 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 408 | select( 0, NULL, NULL, NULL, &tv ); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Read at most 'len' characters |
| 413 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 414 | int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len ) |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 415 | { |
Paul Bakker | 8002541 | 2014-01-23 20:59:49 +0100 | [diff] [blame] | 416 | int fd = *((int *) ctx); |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 417 | int ret = (int) read( fd, buf, len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 418 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 419 | if( ret < 0 ) |
| 420 | { |
Paul Bakker | 8002541 | 2014-01-23 20:59:49 +0100 | [diff] [blame] | 421 | if( net_would_block( fd ) != 0 ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 422 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 423 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 424 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
| 425 | !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 426 | if( WSAGetLastError() == WSAECONNRESET ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 427 | return( MBEDTLS_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 428 | #else |
| 429 | if( errno == EPIPE || errno == ECONNRESET ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 430 | return( MBEDTLS_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 431 | |
| 432 | if( errno == EINTR ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 433 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 434 | #endif |
| 435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 436 | return( MBEDTLS_ERR_NET_RECV_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | return( ret ); |
| 440 | } |
| 441 | |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 442 | /* |
Manuel Pégourié-Gonnard | c8d8e97 | 2014-10-01 15:01:39 +0200 | [diff] [blame] | 443 | * Read at most 'len' characters, blocking for at most 'timeout' ms |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 444 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 445 | int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, |
Manuel Pégourié-Gonnard | c8d8e97 | 2014-10-01 15:01:39 +0200 | [diff] [blame] | 446 | uint32_t timeout ) |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 447 | { |
| 448 | int ret; |
| 449 | struct timeval tv; |
| 450 | fd_set read_fds; |
| 451 | int fd = *((int *) ctx); |
| 452 | |
| 453 | FD_ZERO( &read_fds ); |
| 454 | FD_SET( fd, &read_fds ); |
| 455 | |
Manuel Pégourié-Gonnard | c8d8e97 | 2014-10-01 15:01:39 +0200 | [diff] [blame] | 456 | tv.tv_sec = timeout / 1000; |
| 457 | tv.tv_usec = ( timeout % 1000 ) * 1000; |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 458 | |
| 459 | ret = select( fd + 1, &read_fds, NULL, NULL, &tv ); |
| 460 | |
| 461 | /* Zero fds ready means we timed out */ |
| 462 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 463 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 464 | |
| 465 | if( ret < 0 ) |
| 466 | { |
| 467 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
| 468 | !defined(EFI32) |
| 469 | if( WSAGetLastError() == WSAEINTR ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 470 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 471 | #else |
| 472 | if( errno == EINTR ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 473 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 474 | #endif |
| 475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | return( MBEDTLS_ERR_NET_RECV_FAILED ); |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* This call will not block */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | return( mbedtls_net_recv( ctx, buf, len ) ); |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 481 | } |
Manuel Pégourié-Gonnard | 9d9b003 | 2014-09-18 11:22:45 +0200 | [diff] [blame] | 482 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 483 | /* |
| 484 | * Write at most 'len' characters |
| 485 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 486 | int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 487 | { |
Paul Bakker | 8002541 | 2014-01-23 20:59:49 +0100 | [diff] [blame] | 488 | int fd = *((int *) ctx); |
Sander Niemeijer | ef5087d | 2014-08-16 12:45:52 +0200 | [diff] [blame] | 489 | int ret = (int) write( fd, buf, len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 490 | |
| 491 | if( ret < 0 ) |
| 492 | { |
Paul Bakker | 8002541 | 2014-01-23 20:59:49 +0100 | [diff] [blame] | 493 | if( net_would_block( fd ) != 0 ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 494 | return( MBEDTLS_ERR_SSL_WANT_WRITE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 495 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 496 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
| 497 | !defined(EFI32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 498 | if( WSAGetLastError() == WSAECONNRESET ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | return( MBEDTLS_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 500 | #else |
| 501 | if( errno == EPIPE || errno == ECONNRESET ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 502 | return( MBEDTLS_ERR_NET_CONN_RESET ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 503 | |
| 504 | if( errno == EINTR ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 505 | return( MBEDTLS_ERR_SSL_WANT_WRITE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 506 | #endif |
| 507 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 508 | return( MBEDTLS_ERR_NET_SEND_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | return( ret ); |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Gracefully close the connection |
| 516 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 517 | void mbedtls_net_close( int fd ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 518 | { |
| 519 | shutdown( fd, 2 ); |
| 520 | close( fd ); |
| 521 | } |
| 522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 523 | #endif /* MBEDTLS_NET_C */ |