mbed TLS upgraded to 2.6.0
Fork of mbedtls by
library/net_sockets.c@2:bbdeda018a3c, 2017-09-29 (annotated)
- Committer:
- Jasper Wallace
- Date:
- Fri Sep 29 19:50:30 2017 +0100
- Revision:
- 2:bbdeda018a3c
- Parent:
- 1:9ebc941037d5
Update to mbedtls 2.6.0, many changes.
Changes to mbedtls sources made:
in include/mbedtls/config.h comment out:
#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C
uncomment:
#define MBEDTLS_NO_PLATFORM_ENTROPY
remove the following directorys:
programs
yotta
visualc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /* |
markrad | 0:cdf462088d13 | 2 | * TCP/IP or UDP/IP networking functions |
markrad | 0:cdf462088d13 | 3 | * |
markrad | 0:cdf462088d13 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
markrad | 0:cdf462088d13 | 5 | * SPDX-License-Identifier: Apache-2.0 |
markrad | 0:cdf462088d13 | 6 | * |
markrad | 0:cdf462088d13 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
markrad | 0:cdf462088d13 | 8 | * not use this file except in compliance with the License. |
markrad | 0:cdf462088d13 | 9 | * You may obtain a copy of the License at |
markrad | 0:cdf462088d13 | 10 | * |
markrad | 0:cdf462088d13 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
markrad | 0:cdf462088d13 | 12 | * |
markrad | 0:cdf462088d13 | 13 | * Unless required by applicable law or agreed to in writing, software |
markrad | 0:cdf462088d13 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
markrad | 0:cdf462088d13 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
markrad | 0:cdf462088d13 | 16 | * See the License for the specific language governing permissions and |
markrad | 0:cdf462088d13 | 17 | * limitations under the License. |
markrad | 0:cdf462088d13 | 18 | * |
markrad | 0:cdf462088d13 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
markrad | 0:cdf462088d13 | 20 | */ |
markrad | 0:cdf462088d13 | 21 | |
markrad | 0:cdf462088d13 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
markrad | 0:cdf462088d13 | 23 | #include "mbedtls/config.h" |
markrad | 0:cdf462088d13 | 24 | #else |
markrad | 0:cdf462088d13 | 25 | #include MBEDTLS_CONFIG_FILE |
markrad | 0:cdf462088d13 | 26 | #endif |
markrad | 0:cdf462088d13 | 27 | |
markrad | 0:cdf462088d13 | 28 | #if defined(MBEDTLS_NET_C) |
markrad | 0:cdf462088d13 | 29 | |
markrad | 0:cdf462088d13 | 30 | #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ |
markrad | 0:cdf462088d13 | 31 | !defined(__APPLE__) && !defined(_WIN32) |
markrad | 0:cdf462088d13 | 32 | #error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h" |
markrad | 0:cdf462088d13 | 33 | #endif |
markrad | 0:cdf462088d13 | 34 | |
markrad | 0:cdf462088d13 | 35 | #if defined(MBEDTLS_PLATFORM_C) |
markrad | 0:cdf462088d13 | 36 | #include "mbedtls/platform.h" |
markrad | 0:cdf462088d13 | 37 | #else |
markrad | 0:cdf462088d13 | 38 | #include <stdlib.h> |
markrad | 0:cdf462088d13 | 39 | #endif |
markrad | 0:cdf462088d13 | 40 | |
markrad | 0:cdf462088d13 | 41 | #include "mbedtls/net_sockets.h" |
markrad | 0:cdf462088d13 | 42 | |
markrad | 0:cdf462088d13 | 43 | #include <string.h> |
markrad | 0:cdf462088d13 | 44 | |
markrad | 0:cdf462088d13 | 45 | #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \ |
markrad | 0:cdf462088d13 | 46 | !defined(EFI32) |
markrad | 0:cdf462088d13 | 47 | |
markrad | 0:cdf462088d13 | 48 | #ifdef _WIN32_WINNT |
markrad | 0:cdf462088d13 | 49 | #undef _WIN32_WINNT |
markrad | 0:cdf462088d13 | 50 | #endif |
markrad | 0:cdf462088d13 | 51 | /* Enables getaddrinfo() & Co */ |
markrad | 0:cdf462088d13 | 52 | #define _WIN32_WINNT 0x0501 |
markrad | 0:cdf462088d13 | 53 | #include <ws2tcpip.h> |
markrad | 0:cdf462088d13 | 54 | |
markrad | 0:cdf462088d13 | 55 | #include <winsock2.h> |
markrad | 0:cdf462088d13 | 56 | #include <windows.h> |
markrad | 0:cdf462088d13 | 57 | |
markrad | 0:cdf462088d13 | 58 | #if defined(_MSC_VER) |
markrad | 0:cdf462088d13 | 59 | #if defined(_WIN32_WCE) |
markrad | 0:cdf462088d13 | 60 | #pragma comment( lib, "ws2.lib" ) |
markrad | 0:cdf462088d13 | 61 | #else |
markrad | 0:cdf462088d13 | 62 | #pragma comment( lib, "ws2_32.lib" ) |
markrad | 0:cdf462088d13 | 63 | #endif |
markrad | 0:cdf462088d13 | 64 | #endif /* _MSC_VER */ |
markrad | 0:cdf462088d13 | 65 | |
markrad | 0:cdf462088d13 | 66 | #define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0) |
markrad | 0:cdf462088d13 | 67 | #define write(fd,buf,len) send(fd,(char*)buf,(int) len,0) |
markrad | 0:cdf462088d13 | 68 | #define close(fd) closesocket(fd) |
markrad | 0:cdf462088d13 | 69 | |
markrad | 0:cdf462088d13 | 70 | static int wsa_init_done = 0; |
markrad | 0:cdf462088d13 | 71 | |
markrad | 0:cdf462088d13 | 72 | #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
markrad | 0:cdf462088d13 | 73 | |
markrad | 0:cdf462088d13 | 74 | #include <sys/types.h> |
markrad | 0:cdf462088d13 | 75 | #include <sys/socket.h> |
markrad | 0:cdf462088d13 | 76 | #include <netinet/in.h> |
markrad | 0:cdf462088d13 | 77 | #include <arpa/inet.h> |
markrad | 0:cdf462088d13 | 78 | #include <sys/time.h> |
markrad | 0:cdf462088d13 | 79 | #include <unistd.h> |
markrad | 0:cdf462088d13 | 80 | #include <signal.h> |
markrad | 0:cdf462088d13 | 81 | #include <fcntl.h> |
markrad | 0:cdf462088d13 | 82 | #include <netdb.h> |
markrad | 0:cdf462088d13 | 83 | #include <errno.h> |
markrad | 0:cdf462088d13 | 84 | |
markrad | 0:cdf462088d13 | 85 | #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
markrad | 0:cdf462088d13 | 86 | |
markrad | 0:cdf462088d13 | 87 | /* Some MS functions want int and MSVC warns if we pass size_t, |
markrad | 0:cdf462088d13 | 88 | * but the standard fucntions use socklen_t, so cast only for MSVC */ |
markrad | 0:cdf462088d13 | 89 | #if defined(_MSC_VER) |
markrad | 0:cdf462088d13 | 90 | #define MSVC_INT_CAST (int) |
markrad | 0:cdf462088d13 | 91 | #else |
markrad | 0:cdf462088d13 | 92 | #define MSVC_INT_CAST |
markrad | 0:cdf462088d13 | 93 | #endif |
markrad | 0:cdf462088d13 | 94 | |
markrad | 0:cdf462088d13 | 95 | #include <stdio.h> |
markrad | 0:cdf462088d13 | 96 | |
markrad | 0:cdf462088d13 | 97 | #include <time.h> |
markrad | 0:cdf462088d13 | 98 | |
markrad | 0:cdf462088d13 | 99 | #include <stdint.h> |
markrad | 0:cdf462088d13 | 100 | |
markrad | 0:cdf462088d13 | 101 | /* |
markrad | 0:cdf462088d13 | 102 | * Prepare for using the sockets interface |
markrad | 0:cdf462088d13 | 103 | */ |
markrad | 0:cdf462088d13 | 104 | static int net_prepare( void ) |
markrad | 0:cdf462088d13 | 105 | { |
markrad | 0:cdf462088d13 | 106 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
markrad | 0:cdf462088d13 | 107 | !defined(EFI32) |
markrad | 0:cdf462088d13 | 108 | WSADATA wsaData; |
markrad | 0:cdf462088d13 | 109 | |
markrad | 0:cdf462088d13 | 110 | if( wsa_init_done == 0 ) |
markrad | 0:cdf462088d13 | 111 | { |
markrad | 0:cdf462088d13 | 112 | if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 ) |
markrad | 0:cdf462088d13 | 113 | return( MBEDTLS_ERR_NET_SOCKET_FAILED ); |
markrad | 0:cdf462088d13 | 114 | |
markrad | 0:cdf462088d13 | 115 | wsa_init_done = 1; |
markrad | 0:cdf462088d13 | 116 | } |
markrad | 0:cdf462088d13 | 117 | #else |
markrad | 0:cdf462088d13 | 118 | #if !defined(EFIX64) && !defined(EFI32) |
markrad | 0:cdf462088d13 | 119 | signal( SIGPIPE, SIG_IGN ); |
markrad | 0:cdf462088d13 | 120 | #endif |
markrad | 0:cdf462088d13 | 121 | #endif |
markrad | 0:cdf462088d13 | 122 | return( 0 ); |
markrad | 0:cdf462088d13 | 123 | } |
markrad | 0:cdf462088d13 | 124 | |
markrad | 0:cdf462088d13 | 125 | /* |
markrad | 0:cdf462088d13 | 126 | * Initialize a context |
markrad | 0:cdf462088d13 | 127 | */ |
markrad | 0:cdf462088d13 | 128 | void mbedtls_net_init( mbedtls_net_context *ctx ) |
markrad | 0:cdf462088d13 | 129 | { |
markrad | 0:cdf462088d13 | 130 | ctx->fd = -1; |
markrad | 0:cdf462088d13 | 131 | } |
markrad | 0:cdf462088d13 | 132 | |
markrad | 0:cdf462088d13 | 133 | /* |
markrad | 0:cdf462088d13 | 134 | * Initiate a TCP connection with host:port and the given protocol |
markrad | 0:cdf462088d13 | 135 | */ |
Jasper Wallace |
1:9ebc941037d5 | 136 | int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, |
Jasper Wallace |
1:9ebc941037d5 | 137 | const char *port, int proto ) |
markrad | 0:cdf462088d13 | 138 | { |
markrad | 0:cdf462088d13 | 139 | int ret; |
markrad | 0:cdf462088d13 | 140 | struct addrinfo hints, *addr_list, *cur; |
markrad | 0:cdf462088d13 | 141 | |
markrad | 0:cdf462088d13 | 142 | if( ( ret = net_prepare() ) != 0 ) |
markrad | 0:cdf462088d13 | 143 | return( ret ); |
markrad | 0:cdf462088d13 | 144 | |
markrad | 0:cdf462088d13 | 145 | /* Do name resolution with both IPv6 and IPv4 */ |
markrad | 0:cdf462088d13 | 146 | memset( &hints, 0, sizeof( hints ) ); |
markrad | 0:cdf462088d13 | 147 | hints.ai_family = AF_UNSPEC; |
markrad | 0:cdf462088d13 | 148 | hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; |
markrad | 0:cdf462088d13 | 149 | hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP; |
markrad | 0:cdf462088d13 | 150 | |
markrad | 0:cdf462088d13 | 151 | if( getaddrinfo( host, port, &hints, &addr_list ) != 0 ) |
markrad | 0:cdf462088d13 | 152 | return( MBEDTLS_ERR_NET_UNKNOWN_HOST ); |
markrad | 0:cdf462088d13 | 153 | |
markrad | 0:cdf462088d13 | 154 | /* Try the sockaddrs until a connection succeeds */ |
markrad | 0:cdf462088d13 | 155 | ret = MBEDTLS_ERR_NET_UNKNOWN_HOST; |
markrad | 0:cdf462088d13 | 156 | for( cur = addr_list; cur != NULL; cur = cur->ai_next ) |
markrad | 0:cdf462088d13 | 157 | { |
markrad | 0:cdf462088d13 | 158 | ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype, |
markrad | 0:cdf462088d13 | 159 | cur->ai_protocol ); |
markrad | 0:cdf462088d13 | 160 | if( ctx->fd < 0 ) |
markrad | 0:cdf462088d13 | 161 | { |
markrad | 0:cdf462088d13 | 162 | ret = MBEDTLS_ERR_NET_SOCKET_FAILED; |
markrad | 0:cdf462088d13 | 163 | continue; |
markrad | 0:cdf462088d13 | 164 | } |
markrad | 0:cdf462088d13 | 165 | |
markrad | 0:cdf462088d13 | 166 | if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 ) |
markrad | 0:cdf462088d13 | 167 | { |
markrad | 0:cdf462088d13 | 168 | ret = 0; |
markrad | 0:cdf462088d13 | 169 | break; |
markrad | 0:cdf462088d13 | 170 | } |
markrad | 0:cdf462088d13 | 171 | |
markrad | 0:cdf462088d13 | 172 | close( ctx->fd ); |
markrad | 0:cdf462088d13 | 173 | ret = MBEDTLS_ERR_NET_CONNECT_FAILED; |
markrad | 0:cdf462088d13 | 174 | } |
markrad | 0:cdf462088d13 | 175 | |
markrad | 0:cdf462088d13 | 176 | freeaddrinfo( addr_list ); |
markrad | 0:cdf462088d13 | 177 | |
markrad | 0:cdf462088d13 | 178 | return( ret ); |
markrad | 0:cdf462088d13 | 179 | } |
markrad | 0:cdf462088d13 | 180 | |
markrad | 0:cdf462088d13 | 181 | /* |
markrad | 0:cdf462088d13 | 182 | * Create a listening socket on bind_ip:port |
markrad | 0:cdf462088d13 | 183 | */ |
markrad | 0:cdf462088d13 | 184 | int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto ) |
markrad | 0:cdf462088d13 | 185 | { |
markrad | 0:cdf462088d13 | 186 | int n, ret; |
markrad | 0:cdf462088d13 | 187 | struct addrinfo hints, *addr_list, *cur; |
markrad | 0:cdf462088d13 | 188 | |
markrad | 0:cdf462088d13 | 189 | if( ( ret = net_prepare() ) != 0 ) |
markrad | 0:cdf462088d13 | 190 | return( ret ); |
markrad | 0:cdf462088d13 | 191 | |
markrad | 0:cdf462088d13 | 192 | /* Bind to IPv6 and/or IPv4, but only in the desired protocol */ |
markrad | 0:cdf462088d13 | 193 | memset( &hints, 0, sizeof( hints ) ); |
markrad | 0:cdf462088d13 | 194 | hints.ai_family = AF_UNSPEC; |
markrad | 0:cdf462088d13 | 195 | hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; |
markrad | 0:cdf462088d13 | 196 | hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP; |
markrad | 0:cdf462088d13 | 197 | if( bind_ip == NULL ) |
markrad | 0:cdf462088d13 | 198 | hints.ai_flags = AI_PASSIVE; |
markrad | 0:cdf462088d13 | 199 | |
markrad | 0:cdf462088d13 | 200 | if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 ) |
markrad | 0:cdf462088d13 | 201 | return( MBEDTLS_ERR_NET_UNKNOWN_HOST ); |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | /* Try the sockaddrs until a binding succeeds */ |
markrad | 0:cdf462088d13 | 204 | ret = MBEDTLS_ERR_NET_UNKNOWN_HOST; |
markrad | 0:cdf462088d13 | 205 | for( cur = addr_list; cur != NULL; cur = cur->ai_next ) |
markrad | 0:cdf462088d13 | 206 | { |
markrad | 0:cdf462088d13 | 207 | ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype, |
markrad | 0:cdf462088d13 | 208 | cur->ai_protocol ); |
markrad | 0:cdf462088d13 | 209 | if( ctx->fd < 0 ) |
markrad | 0:cdf462088d13 | 210 | { |
markrad | 0:cdf462088d13 | 211 | ret = MBEDTLS_ERR_NET_SOCKET_FAILED; |
markrad | 0:cdf462088d13 | 212 | continue; |
markrad | 0:cdf462088d13 | 213 | } |
markrad | 0:cdf462088d13 | 214 | |
markrad | 0:cdf462088d13 | 215 | n = 1; |
markrad | 0:cdf462088d13 | 216 | if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR, |
markrad | 0:cdf462088d13 | 217 | (const char *) &n, sizeof( n ) ) != 0 ) |
markrad | 0:cdf462088d13 | 218 | { |
markrad | 0:cdf462088d13 | 219 | close( ctx->fd ); |
markrad | 0:cdf462088d13 | 220 | ret = MBEDTLS_ERR_NET_SOCKET_FAILED; |
markrad | 0:cdf462088d13 | 221 | continue; |
markrad | 0:cdf462088d13 | 222 | } |
markrad | 0:cdf462088d13 | 223 | |
markrad | 0:cdf462088d13 | 224 | if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 ) |
markrad | 0:cdf462088d13 | 225 | { |
markrad | 0:cdf462088d13 | 226 | close( ctx->fd ); |
markrad | 0:cdf462088d13 | 227 | ret = MBEDTLS_ERR_NET_BIND_FAILED; |
markrad | 0:cdf462088d13 | 228 | continue; |
markrad | 0:cdf462088d13 | 229 | } |
markrad | 0:cdf462088d13 | 230 | |
markrad | 0:cdf462088d13 | 231 | /* Listen only makes sense for TCP */ |
markrad | 0:cdf462088d13 | 232 | if( proto == MBEDTLS_NET_PROTO_TCP ) |
markrad | 0:cdf462088d13 | 233 | { |
markrad | 0:cdf462088d13 | 234 | if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 ) |
markrad | 0:cdf462088d13 | 235 | { |
markrad | 0:cdf462088d13 | 236 | close( ctx->fd ); |
markrad | 0:cdf462088d13 | 237 | ret = MBEDTLS_ERR_NET_LISTEN_FAILED; |
markrad | 0:cdf462088d13 | 238 | continue; |
markrad | 0:cdf462088d13 | 239 | } |
markrad | 0:cdf462088d13 | 240 | } |
markrad | 0:cdf462088d13 | 241 | |
markrad | 0:cdf462088d13 | 242 | /* Bind was successful */ |
markrad | 0:cdf462088d13 | 243 | ret = 0; |
markrad | 0:cdf462088d13 | 244 | break; |
markrad | 0:cdf462088d13 | 245 | } |
markrad | 0:cdf462088d13 | 246 | |
markrad | 0:cdf462088d13 | 247 | freeaddrinfo( addr_list ); |
markrad | 0:cdf462088d13 | 248 | |
markrad | 0:cdf462088d13 | 249 | return( ret ); |
markrad | 0:cdf462088d13 | 250 | |
markrad | 0:cdf462088d13 | 251 | } |
markrad | 0:cdf462088d13 | 252 | |
markrad | 0:cdf462088d13 | 253 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
markrad | 0:cdf462088d13 | 254 | !defined(EFI32) |
markrad | 0:cdf462088d13 | 255 | /* |
markrad | 0:cdf462088d13 | 256 | * Check if the requested operation would be blocking on a non-blocking socket |
markrad | 0:cdf462088d13 | 257 | * and thus 'failed' with a negative return value. |
markrad | 0:cdf462088d13 | 258 | */ |
markrad | 0:cdf462088d13 | 259 | static int net_would_block( const mbedtls_net_context *ctx ) |
markrad | 0:cdf462088d13 | 260 | { |
markrad | 0:cdf462088d13 | 261 | ((void) ctx); |
markrad | 0:cdf462088d13 | 262 | return( WSAGetLastError() == WSAEWOULDBLOCK ); |
markrad | 0:cdf462088d13 | 263 | } |
markrad | 0:cdf462088d13 | 264 | #else |
markrad | 0:cdf462088d13 | 265 | /* |
markrad | 0:cdf462088d13 | 266 | * Check if the requested operation would be blocking on a non-blocking socket |
markrad | 0:cdf462088d13 | 267 | * and thus 'failed' with a negative return value. |
markrad | 0:cdf462088d13 | 268 | * |
markrad | 0:cdf462088d13 | 269 | * Note: on a blocking socket this function always returns 0! |
markrad | 0:cdf462088d13 | 270 | */ |
markrad | 0:cdf462088d13 | 271 | static int net_would_block( const mbedtls_net_context *ctx ) |
markrad | 0:cdf462088d13 | 272 | { |
markrad | 0:cdf462088d13 | 273 | /* |
markrad | 0:cdf462088d13 | 274 | * Never return 'WOULD BLOCK' on a non-blocking socket |
markrad | 0:cdf462088d13 | 275 | */ |
markrad | 0:cdf462088d13 | 276 | if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK ) |
markrad | 0:cdf462088d13 | 277 | return( 0 ); |
markrad | 0:cdf462088d13 | 278 | |
markrad | 0:cdf462088d13 | 279 | switch( errno ) |
markrad | 0:cdf462088d13 | 280 | { |
markrad | 0:cdf462088d13 | 281 | #if defined EAGAIN |
markrad | 0:cdf462088d13 | 282 | case EAGAIN: |
markrad | 0:cdf462088d13 | 283 | #endif |
markrad | 0:cdf462088d13 | 284 | #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN |
markrad | 0:cdf462088d13 | 285 | case EWOULDBLOCK: |
markrad | 0:cdf462088d13 | 286 | #endif |
markrad | 0:cdf462088d13 | 287 | return( 1 ); |
markrad | 0:cdf462088d13 | 288 | } |
markrad | 0:cdf462088d13 | 289 | return( 0 ); |
markrad | 0:cdf462088d13 | 290 | } |
markrad | 0:cdf462088d13 | 291 | #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
markrad | 0:cdf462088d13 | 292 | |
markrad | 0:cdf462088d13 | 293 | /* |
markrad | 0:cdf462088d13 | 294 | * Accept a connection from a remote client |
markrad | 0:cdf462088d13 | 295 | */ |
markrad | 0:cdf462088d13 | 296 | int mbedtls_net_accept( mbedtls_net_context *bind_ctx, |
markrad | 0:cdf462088d13 | 297 | mbedtls_net_context *client_ctx, |
markrad | 0:cdf462088d13 | 298 | void *client_ip, size_t buf_size, size_t *ip_len ) |
markrad | 0:cdf462088d13 | 299 | { |
markrad | 0:cdf462088d13 | 300 | int ret; |
markrad | 0:cdf462088d13 | 301 | int type; |
markrad | 0:cdf462088d13 | 302 | |
markrad | 0:cdf462088d13 | 303 | struct sockaddr_storage client_addr; |
markrad | 0:cdf462088d13 | 304 | |
markrad | 0:cdf462088d13 | 305 | #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \ |
markrad | 0:cdf462088d13 | 306 | defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) |
markrad | 0:cdf462088d13 | 307 | socklen_t n = (socklen_t) sizeof( client_addr ); |
markrad | 0:cdf462088d13 | 308 | socklen_t type_len = (socklen_t) sizeof( type ); |
markrad | 0:cdf462088d13 | 309 | #else |
markrad | 0:cdf462088d13 | 310 | int n = (int) sizeof( client_addr ); |
markrad | 0:cdf462088d13 | 311 | int type_len = (int) sizeof( type ); |
markrad | 0:cdf462088d13 | 312 | #endif |
markrad | 0:cdf462088d13 | 313 | |
markrad | 0:cdf462088d13 | 314 | /* Is this a TCP or UDP socket? */ |
markrad | 0:cdf462088d13 | 315 | if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE, |
markrad | 0:cdf462088d13 | 316 | (void *) &type, &type_len ) != 0 || |
markrad | 0:cdf462088d13 | 317 | ( type != SOCK_STREAM && type != SOCK_DGRAM ) ) |
markrad | 0:cdf462088d13 | 318 | { |
markrad | 0:cdf462088d13 | 319 | return( MBEDTLS_ERR_NET_ACCEPT_FAILED ); |
markrad | 0:cdf462088d13 | 320 | } |
markrad | 0:cdf462088d13 | 321 | |
markrad | 0:cdf462088d13 | 322 | if( type == SOCK_STREAM ) |
markrad | 0:cdf462088d13 | 323 | { |
markrad | 0:cdf462088d13 | 324 | /* TCP: actual accept() */ |
markrad | 0:cdf462088d13 | 325 | ret = client_ctx->fd = (int) accept( bind_ctx->fd, |
Jasper Wallace |
1:9ebc941037d5 | 326 | (struct sockaddr *) &client_addr, &n ); |
markrad | 0:cdf462088d13 | 327 | } |
markrad | 0:cdf462088d13 | 328 | else |
markrad | 0:cdf462088d13 | 329 | { |
markrad | 0:cdf462088d13 | 330 | /* UDP: wait for a message, but keep it in the queue */ |
markrad | 0:cdf462088d13 | 331 | char buf[1] = { 0 }; |
markrad | 0:cdf462088d13 | 332 | |
markrad | 0:cdf462088d13 | 333 | ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK, |
markrad | 0:cdf462088d13 | 334 | (struct sockaddr *) &client_addr, &n ); |
markrad | 0:cdf462088d13 | 335 | |
markrad | 0:cdf462088d13 | 336 | #if defined(_WIN32) |
markrad | 0:cdf462088d13 | 337 | if( ret == SOCKET_ERROR && |
markrad | 0:cdf462088d13 | 338 | WSAGetLastError() == WSAEMSGSIZE ) |
markrad | 0:cdf462088d13 | 339 | { |
markrad | 0:cdf462088d13 | 340 | /* We know buf is too small, thanks, just peeking here */ |
markrad | 0:cdf462088d13 | 341 | ret = 0; |
markrad | 0:cdf462088d13 | 342 | } |
markrad | 0:cdf462088d13 | 343 | #endif |
markrad | 0:cdf462088d13 | 344 | } |
markrad | 0:cdf462088d13 | 345 | |
markrad | 0:cdf462088d13 | 346 | if( ret < 0 ) |
markrad | 0:cdf462088d13 | 347 | { |
markrad | 0:cdf462088d13 | 348 | if( net_would_block( bind_ctx ) != 0 ) |
markrad | 0:cdf462088d13 | 349 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
markrad | 0:cdf462088d13 | 350 | |
markrad | 0:cdf462088d13 | 351 | return( MBEDTLS_ERR_NET_ACCEPT_FAILED ); |
markrad | 0:cdf462088d13 | 352 | } |
markrad | 0:cdf462088d13 | 353 | |
markrad | 0:cdf462088d13 | 354 | /* UDP: hijack the listening socket to communicate with the client, |
markrad | 0:cdf462088d13 | 355 | * then bind a new socket to accept new connections */ |
markrad | 0:cdf462088d13 | 356 | if( type != SOCK_STREAM ) |
markrad | 0:cdf462088d13 | 357 | { |
markrad | 0:cdf462088d13 | 358 | struct sockaddr_storage local_addr; |
markrad | 0:cdf462088d13 | 359 | int one = 1; |
markrad | 0:cdf462088d13 | 360 | |
markrad | 0:cdf462088d13 | 361 | if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 ) |
markrad | 0:cdf462088d13 | 362 | return( MBEDTLS_ERR_NET_ACCEPT_FAILED ); |
markrad | 0:cdf462088d13 | 363 | |
markrad | 0:cdf462088d13 | 364 | client_ctx->fd = bind_ctx->fd; |
markrad | 0:cdf462088d13 | 365 | bind_ctx->fd = -1; /* In case we exit early */ |
markrad | 0:cdf462088d13 | 366 | |
markrad | 0:cdf462088d13 | 367 | n = sizeof( struct sockaddr_storage ); |
markrad | 0:cdf462088d13 | 368 | if( getsockname( client_ctx->fd, |
markrad | 0:cdf462088d13 | 369 | (struct sockaddr *) &local_addr, &n ) != 0 || |
markrad | 0:cdf462088d13 | 370 | ( bind_ctx->fd = (int) socket( local_addr.ss_family, |
markrad | 0:cdf462088d13 | 371 | SOCK_DGRAM, IPPROTO_UDP ) ) < 0 || |
markrad | 0:cdf462088d13 | 372 | setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR, |
markrad | 0:cdf462088d13 | 373 | (const char *) &one, sizeof( one ) ) != 0 ) |
markrad | 0:cdf462088d13 | 374 | { |
markrad | 0:cdf462088d13 | 375 | return( MBEDTLS_ERR_NET_SOCKET_FAILED ); |
markrad | 0:cdf462088d13 | 376 | } |
markrad | 0:cdf462088d13 | 377 | |
markrad | 0:cdf462088d13 | 378 | if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 ) |
markrad | 0:cdf462088d13 | 379 | { |
markrad | 0:cdf462088d13 | 380 | return( MBEDTLS_ERR_NET_BIND_FAILED ); |
markrad | 0:cdf462088d13 | 381 | } |
markrad | 0:cdf462088d13 | 382 | } |
markrad | 0:cdf462088d13 | 383 | |
markrad | 0:cdf462088d13 | 384 | if( client_ip != NULL ) |
markrad | 0:cdf462088d13 | 385 | { |
markrad | 0:cdf462088d13 | 386 | if( client_addr.ss_family == AF_INET ) |
markrad | 0:cdf462088d13 | 387 | { |
markrad | 0:cdf462088d13 | 388 | struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr; |
markrad | 0:cdf462088d13 | 389 | *ip_len = sizeof( addr4->sin_addr.s_addr ); |
markrad | 0:cdf462088d13 | 390 | |
markrad | 0:cdf462088d13 | 391 | if( buf_size < *ip_len ) |
markrad | 0:cdf462088d13 | 392 | return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL ); |
markrad | 0:cdf462088d13 | 393 | |
markrad | 0:cdf462088d13 | 394 | memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len ); |
markrad | 0:cdf462088d13 | 395 | } |
markrad | 0:cdf462088d13 | 396 | else |
markrad | 0:cdf462088d13 | 397 | { |
markrad | 0:cdf462088d13 | 398 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr; |
markrad | 0:cdf462088d13 | 399 | *ip_len = sizeof( addr6->sin6_addr.s6_addr ); |
markrad | 0:cdf462088d13 | 400 | |
markrad | 0:cdf462088d13 | 401 | if( buf_size < *ip_len ) |
markrad | 0:cdf462088d13 | 402 | return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL ); |
markrad | 0:cdf462088d13 | 403 | |
markrad | 0:cdf462088d13 | 404 | memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len); |
markrad | 0:cdf462088d13 | 405 | } |
markrad | 0:cdf462088d13 | 406 | } |
markrad | 0:cdf462088d13 | 407 | |
markrad | 0:cdf462088d13 | 408 | return( 0 ); |
markrad | 0:cdf462088d13 | 409 | } |
markrad | 0:cdf462088d13 | 410 | |
markrad | 0:cdf462088d13 | 411 | /* |
markrad | 0:cdf462088d13 | 412 | * Set the socket blocking or non-blocking |
markrad | 0:cdf462088d13 | 413 | */ |
markrad | 0:cdf462088d13 | 414 | int mbedtls_net_set_block( mbedtls_net_context *ctx ) |
markrad | 0:cdf462088d13 | 415 | { |
markrad | 0:cdf462088d13 | 416 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
markrad | 0:cdf462088d13 | 417 | !defined(EFI32) |
markrad | 0:cdf462088d13 | 418 | u_long n = 0; |
markrad | 0:cdf462088d13 | 419 | return( ioctlsocket( ctx->fd, FIONBIO, &n ) ); |
markrad | 0:cdf462088d13 | 420 | #else |
markrad | 0:cdf462088d13 | 421 | return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) ); |
markrad | 0:cdf462088d13 | 422 | #endif |
markrad | 0:cdf462088d13 | 423 | } |
markrad | 0:cdf462088d13 | 424 | |
markrad | 0:cdf462088d13 | 425 | int mbedtls_net_set_nonblock( mbedtls_net_context *ctx ) |
markrad | 0:cdf462088d13 | 426 | { |
markrad | 0:cdf462088d13 | 427 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
markrad | 0:cdf462088d13 | 428 | !defined(EFI32) |
markrad | 0:cdf462088d13 | 429 | u_long n = 1; |
markrad | 0:cdf462088d13 | 430 | return( ioctlsocket( ctx->fd, FIONBIO, &n ) ); |
markrad | 0:cdf462088d13 | 431 | #else |
markrad | 0:cdf462088d13 | 432 | return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) ); |
markrad | 0:cdf462088d13 | 433 | #endif |
markrad | 0:cdf462088d13 | 434 | } |
markrad | 0:cdf462088d13 | 435 | |
markrad | 0:cdf462088d13 | 436 | /* |
markrad | 0:cdf462088d13 | 437 | * Portable usleep helper |
markrad | 0:cdf462088d13 | 438 | */ |
markrad | 0:cdf462088d13 | 439 | void mbedtls_net_usleep( unsigned long usec ) |
markrad | 0:cdf462088d13 | 440 | { |
markrad | 0:cdf462088d13 | 441 | #if defined(_WIN32) |
markrad | 0:cdf462088d13 | 442 | Sleep( ( usec + 999 ) / 1000 ); |
markrad | 0:cdf462088d13 | 443 | #else |
markrad | 0:cdf462088d13 | 444 | struct timeval tv; |
markrad | 0:cdf462088d13 | 445 | tv.tv_sec = usec / 1000000; |
markrad | 0:cdf462088d13 | 446 | #if defined(__unix__) || defined(__unix) || \ |
markrad | 0:cdf462088d13 | 447 | ( defined(__APPLE__) && defined(__MACH__) ) |
markrad | 0:cdf462088d13 | 448 | tv.tv_usec = (suseconds_t) usec % 1000000; |
markrad | 0:cdf462088d13 | 449 | #else |
markrad | 0:cdf462088d13 | 450 | tv.tv_usec = usec % 1000000; |
markrad | 0:cdf462088d13 | 451 | #endif |
markrad | 0:cdf462088d13 | 452 | select( 0, NULL, NULL, NULL, &tv ); |
markrad | 0:cdf462088d13 | 453 | #endif |
markrad | 0:cdf462088d13 | 454 | } |
markrad | 0:cdf462088d13 | 455 | |
markrad | 0:cdf462088d13 | 456 | /* |
markrad | 0:cdf462088d13 | 457 | * Read at most 'len' characters |
markrad | 0:cdf462088d13 | 458 | */ |
markrad | 0:cdf462088d13 | 459 | int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len ) |
markrad | 0:cdf462088d13 | 460 | { |
markrad | 0:cdf462088d13 | 461 | int ret; |
markrad | 0:cdf462088d13 | 462 | int fd = ((mbedtls_net_context *) ctx)->fd; |
markrad | 0:cdf462088d13 | 463 | |
markrad | 0:cdf462088d13 | 464 | if( fd < 0 ) |
markrad | 0:cdf462088d13 | 465 | return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); |
markrad | 0:cdf462088d13 | 466 | |
markrad | 0:cdf462088d13 | 467 | ret = (int) read( fd, buf, len ); |
markrad | 0:cdf462088d13 | 468 | |
markrad | 0:cdf462088d13 | 469 | if( ret < 0 ) |
markrad | 0:cdf462088d13 | 470 | { |
markrad | 0:cdf462088d13 | 471 | if( net_would_block( ctx ) != 0 ) |
markrad | 0:cdf462088d13 | 472 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
markrad | 0:cdf462088d13 | 473 | |
markrad | 0:cdf462088d13 | 474 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
markrad | 0:cdf462088d13 | 475 | !defined(EFI32) |
markrad | 0:cdf462088d13 | 476 | if( WSAGetLastError() == WSAECONNRESET ) |
markrad | 0:cdf462088d13 | 477 | return( MBEDTLS_ERR_NET_CONN_RESET ); |
markrad | 0:cdf462088d13 | 478 | #else |
markrad | 0:cdf462088d13 | 479 | if( errno == EPIPE || errno == ECONNRESET ) |
markrad | 0:cdf462088d13 | 480 | return( MBEDTLS_ERR_NET_CONN_RESET ); |
markrad | 0:cdf462088d13 | 481 | |
markrad | 0:cdf462088d13 | 482 | if( errno == EINTR ) |
markrad | 0:cdf462088d13 | 483 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
markrad | 0:cdf462088d13 | 484 | #endif |
markrad | 0:cdf462088d13 | 485 | |
markrad | 0:cdf462088d13 | 486 | return( MBEDTLS_ERR_NET_RECV_FAILED ); |
markrad | 0:cdf462088d13 | 487 | } |
markrad | 0:cdf462088d13 | 488 | |
markrad | 0:cdf462088d13 | 489 | return( ret ); |
markrad | 0:cdf462088d13 | 490 | } |
markrad | 0:cdf462088d13 | 491 | |
markrad | 0:cdf462088d13 | 492 | /* |
markrad | 0:cdf462088d13 | 493 | * Read at most 'len' characters, blocking for at most 'timeout' ms |
markrad | 0:cdf462088d13 | 494 | */ |
markrad | 0:cdf462088d13 | 495 | int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, |
markrad | 0:cdf462088d13 | 496 | uint32_t timeout ) |
markrad | 0:cdf462088d13 | 497 | { |
markrad | 0:cdf462088d13 | 498 | int ret; |
markrad | 0:cdf462088d13 | 499 | struct timeval tv; |
markrad | 0:cdf462088d13 | 500 | fd_set read_fds; |
markrad | 0:cdf462088d13 | 501 | int fd = ((mbedtls_net_context *) ctx)->fd; |
markrad | 0:cdf462088d13 | 502 | |
markrad | 0:cdf462088d13 | 503 | if( fd < 0 ) |
markrad | 0:cdf462088d13 | 504 | return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); |
markrad | 0:cdf462088d13 | 505 | |
markrad | 0:cdf462088d13 | 506 | FD_ZERO( &read_fds ); |
markrad | 0:cdf462088d13 | 507 | FD_SET( fd, &read_fds ); |
markrad | 0:cdf462088d13 | 508 | |
markrad | 0:cdf462088d13 | 509 | tv.tv_sec = timeout / 1000; |
markrad | 0:cdf462088d13 | 510 | tv.tv_usec = ( timeout % 1000 ) * 1000; |
markrad | 0:cdf462088d13 | 511 | |
markrad | 0:cdf462088d13 | 512 | ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv ); |
markrad | 0:cdf462088d13 | 513 | |
markrad | 0:cdf462088d13 | 514 | /* Zero fds ready means we timed out */ |
markrad | 0:cdf462088d13 | 515 | if( ret == 0 ) |
markrad | 0:cdf462088d13 | 516 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
markrad | 0:cdf462088d13 | 517 | |
markrad | 0:cdf462088d13 | 518 | if( ret < 0 ) |
markrad | 0:cdf462088d13 | 519 | { |
markrad | 0:cdf462088d13 | 520 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
markrad | 0:cdf462088d13 | 521 | !defined(EFI32) |
markrad | 0:cdf462088d13 | 522 | if( WSAGetLastError() == WSAEINTR ) |
markrad | 0:cdf462088d13 | 523 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
markrad | 0:cdf462088d13 | 524 | #else |
markrad | 0:cdf462088d13 | 525 | if( errno == EINTR ) |
markrad | 0:cdf462088d13 | 526 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
markrad | 0:cdf462088d13 | 527 | #endif |
markrad | 0:cdf462088d13 | 528 | |
markrad | 0:cdf462088d13 | 529 | return( MBEDTLS_ERR_NET_RECV_FAILED ); |
markrad | 0:cdf462088d13 | 530 | } |
markrad | 0:cdf462088d13 | 531 | |
markrad | 0:cdf462088d13 | 532 | /* This call will not block */ |
markrad | 0:cdf462088d13 | 533 | return( mbedtls_net_recv( ctx, buf, len ) ); |
markrad | 0:cdf462088d13 | 534 | } |
markrad | 0:cdf462088d13 | 535 | |
markrad | 0:cdf462088d13 | 536 | /* |
markrad | 0:cdf462088d13 | 537 | * Write at most 'len' characters |
markrad | 0:cdf462088d13 | 538 | */ |
markrad | 0:cdf462088d13 | 539 | int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len ) |
markrad | 0:cdf462088d13 | 540 | { |
markrad | 0:cdf462088d13 | 541 | int ret; |
markrad | 0:cdf462088d13 | 542 | int fd = ((mbedtls_net_context *) ctx)->fd; |
markrad | 0:cdf462088d13 | 543 | |
markrad | 0:cdf462088d13 | 544 | if( fd < 0 ) |
markrad | 0:cdf462088d13 | 545 | return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); |
markrad | 0:cdf462088d13 | 546 | |
markrad | 0:cdf462088d13 | 547 | ret = (int) write( fd, buf, len ); |
markrad | 0:cdf462088d13 | 548 | |
markrad | 0:cdf462088d13 | 549 | if( ret < 0 ) |
markrad | 0:cdf462088d13 | 550 | { |
markrad | 0:cdf462088d13 | 551 | if( net_would_block( ctx ) != 0 ) |
markrad | 0:cdf462088d13 | 552 | return( MBEDTLS_ERR_SSL_WANT_WRITE ); |
markrad | 0:cdf462088d13 | 553 | |
markrad | 0:cdf462088d13 | 554 | #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ |
markrad | 0:cdf462088d13 | 555 | !defined(EFI32) |
markrad | 0:cdf462088d13 | 556 | if( WSAGetLastError() == WSAECONNRESET ) |
markrad | 0:cdf462088d13 | 557 | return( MBEDTLS_ERR_NET_CONN_RESET ); |
markrad | 0:cdf462088d13 | 558 | #else |
markrad | 0:cdf462088d13 | 559 | if( errno == EPIPE || errno == ECONNRESET ) |
markrad | 0:cdf462088d13 | 560 | return( MBEDTLS_ERR_NET_CONN_RESET ); |
markrad | 0:cdf462088d13 | 561 | |
markrad | 0:cdf462088d13 | 562 | if( errno == EINTR ) |
markrad | 0:cdf462088d13 | 563 | return( MBEDTLS_ERR_SSL_WANT_WRITE ); |
markrad | 0:cdf462088d13 | 564 | #endif |
markrad | 0:cdf462088d13 | 565 | |
markrad | 0:cdf462088d13 | 566 | return( MBEDTLS_ERR_NET_SEND_FAILED ); |
markrad | 0:cdf462088d13 | 567 | } |
markrad | 0:cdf462088d13 | 568 | |
markrad | 0:cdf462088d13 | 569 | return( ret ); |
markrad | 0:cdf462088d13 | 570 | } |
markrad | 0:cdf462088d13 | 571 | |
markrad | 0:cdf462088d13 | 572 | /* |
markrad | 0:cdf462088d13 | 573 | * Gracefully close the connection |
markrad | 0:cdf462088d13 | 574 | */ |
markrad | 0:cdf462088d13 | 575 | void mbedtls_net_free( mbedtls_net_context *ctx ) |
markrad | 0:cdf462088d13 | 576 | { |
markrad | 0:cdf462088d13 | 577 | if( ctx->fd == -1 ) |
markrad | 0:cdf462088d13 | 578 | return; |
markrad | 0:cdf462088d13 | 579 | |
markrad | 0:cdf462088d13 | 580 | shutdown( ctx->fd, 2 ); |
markrad | 0:cdf462088d13 | 581 | close( ctx->fd ); |
markrad | 0:cdf462088d13 | 582 | |
markrad | 0:cdf462088d13 | 583 | ctx->fd = -1; |
markrad | 0:cdf462088d13 | 584 | } |
markrad | 0:cdf462088d13 | 585 | |
markrad | 0:cdf462088d13 | 586 | #endif /* MBEDTLS_NET_C */ |