Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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