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