Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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