Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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