![](/media/cache/profiles/5f55d0baa59f4bc1dc393149183f1492.jpg.50x50_q85.jpg)
Changes to enabled on-line compiler
platform/mbedtls/network.cpp@0:082731ede69f, 2018-05-30 (annotated)
- Committer:
- JMF
- Date:
- Wed May 30 20:59:51 2018 +0000
- Revision:
- 0:082731ede69f
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:082731ede69f | 1 | /* |
JMF | 0:082731ede69f | 2 | * TCP/IP or UDP/IP networking functions |
JMF | 0:082731ede69f | 3 | * |
JMF | 0:082731ede69f | 4 | * This version of net_sockets.c is setup to use ARM easy-connect for network connectivity |
JMF | 0:082731ede69f | 5 | * |
JMF | 0:082731ede69f | 6 | * |
JMF | 0:082731ede69f | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
JMF | 0:082731ede69f | 8 | * not use this file except in compliance with the License. |
JMF | 0:082731ede69f | 9 | * You may obtain a copy of the License at |
JMF | 0:082731ede69f | 10 | * |
JMF | 0:082731ede69f | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:082731ede69f | 12 | * |
JMF | 0:082731ede69f | 13 | * Unless required by applicable law or agreed to in writing, software |
JMF | 0:082731ede69f | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
JMF | 0:082731ede69f | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 0:082731ede69f | 16 | * See the License for the specific language governing permissions and |
JMF | 0:082731ede69f | 17 | * limitations under the License. |
JMF | 0:082731ede69f | 18 | * |
JMF | 0:082731ede69f | 19 | */ |
JMF | 0:082731ede69f | 20 | |
JMF | 0:082731ede69f | 21 | #include "mbed.h" |
JMF | 0:082731ede69f | 22 | |
JMF | 0:082731ede69f | 23 | #include "easy-connect.h" |
JMF | 0:082731ede69f | 24 | |
JMF | 0:082731ede69f | 25 | #define MBEDTLS_FS_IO 1 |
JMF | 0:082731ede69f | 26 | |
JMF | 0:082731ede69f | 27 | #include <stdbool.h> |
JMF | 0:082731ede69f | 28 | #include <string.h> |
JMF | 0:082731ede69f | 29 | #include <timer_platform.h> |
JMF | 0:082731ede69f | 30 | #include <network_interface.h> |
JMF | 0:082731ede69f | 31 | |
JMF | 0:082731ede69f | 32 | #include "mbedtls/platform.h" |
JMF | 0:082731ede69f | 33 | #include "mbedtls/ssl.h" |
JMF | 0:082731ede69f | 34 | #include "mbedtls/entropy.h" |
JMF | 0:082731ede69f | 35 | #include "mbedtls/ctr_drbg.h" |
JMF | 0:082731ede69f | 36 | #include "mbedtls/error.h" |
JMF | 0:082731ede69f | 37 | #include "mbedtls/x509_crt.h" |
JMF | 0:082731ede69f | 38 | #include "mbedtls/pk.h" |
JMF | 0:082731ede69f | 39 | |
JMF | 0:082731ede69f | 40 | #if DEBUG_LEVEL > 0 |
JMF | 0:082731ede69f | 41 | #include "mbedtls/debug.h" |
JMF | 0:082731ede69f | 42 | #endif |
JMF | 0:082731ede69f | 43 | |
JMF | 0:082731ede69f | 44 | #include "aws_iot_error.h" |
JMF | 0:082731ede69f | 45 | #include "aws_iot_log.h" |
JMF | 0:082731ede69f | 46 | #include "network_interface.h" |
JMF | 0:082731ede69f | 47 | #include "network_platform.h" |
JMF | 0:082731ede69f | 48 | |
JMF | 0:082731ede69f | 49 | #include "awscerts.h" |
JMF | 0:082731ede69f | 50 | |
JMF | 0:082731ede69f | 51 | |
JMF | 0:082731ede69f | 52 | NetworkInterface *network = NULL; |
JMF | 0:082731ede69f | 53 | TCPSocket mbedtls_socket; |
JMF | 0:082731ede69f | 54 | bool network_connected = false; |
JMF | 0:082731ede69f | 55 | |
JMF | 0:082731ede69f | 56 | /* |
JMF | 0:082731ede69f | 57 | * Initialize a context |
JMF | 0:082731ede69f | 58 | */ |
JMF | 0:082731ede69f | 59 | void mbedtls_aws_init( mbedtls_net_context *ctx ) |
JMF | 0:082731ede69f | 60 | { |
JMF | 0:082731ede69f | 61 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 62 | |
JMF | 0:082731ede69f | 63 | if( network != NULL ) |
JMF | 0:082731ede69f | 64 | network->disconnect(); //disconnect from the current network |
JMF | 0:082731ede69f | 65 | |
JMF | 0:082731ede69f | 66 | network_connected = false; |
JMF | 0:082731ede69f | 67 | network = easy_connect(true); |
JMF | 0:082731ede69f | 68 | if (!network) { |
JMF | 0:082731ede69f | 69 | IOT_DEBUG("Network Connection Failed!"); |
JMF | 0:082731ede69f | 70 | return; |
JMF | 0:082731ede69f | 71 | } |
JMF | 0:082731ede69f | 72 | IOT_DEBUG("Modem SW Revision: %s", FIRMWARE_REV(network)); |
JMF | 0:082731ede69f | 73 | network_connected = true; |
JMF | 0:082731ede69f | 74 | ctx->fd = 1; |
JMF | 0:082731ede69f | 75 | } |
JMF | 0:082731ede69f | 76 | |
JMF | 0:082731ede69f | 77 | /* |
JMF | 0:082731ede69f | 78 | * Initiate a TCP connection with host:port and the given protocol |
JMF | 0:082731ede69f | 79 | * return 0 if success, otherwise error is returned |
JMF | 0:082731ede69f | 80 | */ |
JMF | 0:082731ede69f | 81 | int mbedtls_aws_connect( mbedtls_net_context *ctx, const char *host, uint16_t port, int proto ) |
JMF | 0:082731ede69f | 82 | { |
JMF | 0:082731ede69f | 83 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 84 | if( !network_connected ) { |
JMF | 0:082731ede69f | 85 | IOT_DEBUG("No network connection"); |
JMF | 0:082731ede69f | 86 | FUNC_EXIT_RC(NETWORK_ERR_NET_CONNECT_FAILED); |
JMF | 0:082731ede69f | 87 | } |
JMF | 0:082731ede69f | 88 | |
JMF | 0:082731ede69f | 89 | int ret = mbedtls_socket.open(network) || mbedtls_socket.connect(host,port); |
JMF | 0:082731ede69f | 90 | if( ret != 0 ){ |
JMF | 0:082731ede69f | 91 | IOT_DEBUG("Socket Open Failed - %d",ret); |
JMF | 0:082731ede69f | 92 | } |
JMF | 0:082731ede69f | 93 | |
JMF | 0:082731ede69f | 94 | FUNC_EXIT_RC(ret); |
JMF | 0:082731ede69f | 95 | } |
JMF | 0:082731ede69f | 96 | |
JMF | 0:082731ede69f | 97 | /* |
JMF | 0:082731ede69f | 98 | * Create a listening socket on bind_ip:port |
JMF | 0:082731ede69f | 99 | */ |
JMF | 0:082731ede69f | 100 | int mbedtls_aws_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto ) |
JMF | 0:082731ede69f | 101 | { |
JMF | 0:082731ede69f | 102 | FUNC_EXIT_RC(MBEDTLS_ERR_NET_BIND_FAILED); |
JMF | 0:082731ede69f | 103 | } |
JMF | 0:082731ede69f | 104 | |
JMF | 0:082731ede69f | 105 | /* |
JMF | 0:082731ede69f | 106 | * Accept a connection from a remote client |
JMF | 0:082731ede69f | 107 | */ |
JMF | 0:082731ede69f | 108 | int mbedtls_aws_accept( mbedtls_net_context *bind_ctx, |
JMF | 0:082731ede69f | 109 | mbedtls_net_context *client_ctx, |
JMF | 0:082731ede69f | 110 | void *client_ip, size_t buf_size, size_t *ip_len ) |
JMF | 0:082731ede69f | 111 | { |
JMF | 0:082731ede69f | 112 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 113 | FUNC_EXIT_RC(MBEDTLS_ERR_NET_ACCEPT_FAILED ); |
JMF | 0:082731ede69f | 114 | } |
JMF | 0:082731ede69f | 115 | |
JMF | 0:082731ede69f | 116 | /* |
JMF | 0:082731ede69f | 117 | * Set the socket blocking or non-blocking |
JMF | 0:082731ede69f | 118 | */ |
JMF | 0:082731ede69f | 119 | int mbedtls_aws_set_block( mbedtls_net_context *ctx ) |
JMF | 0:082731ede69f | 120 | { |
JMF | 0:082731ede69f | 121 | mbedtls_socket.set_blocking(true); |
JMF | 0:082731ede69f | 122 | return 0; |
JMF | 0:082731ede69f | 123 | } |
JMF | 0:082731ede69f | 124 | |
JMF | 0:082731ede69f | 125 | int mbedtls_aws_set_nonblock( mbedtls_net_context *ctx ) |
JMF | 0:082731ede69f | 126 | { |
JMF | 0:082731ede69f | 127 | mbedtls_socket.set_blocking(false); |
JMF | 0:082731ede69f | 128 | return 0; |
JMF | 0:082731ede69f | 129 | } |
JMF | 0:082731ede69f | 130 | |
JMF | 0:082731ede69f | 131 | /* |
JMF | 0:082731ede69f | 132 | * Portable usleep helper |
JMF | 0:082731ede69f | 133 | */ |
JMF | 0:082731ede69f | 134 | void mbedtls_aws_usleep( unsigned long usec ) |
JMF | 0:082731ede69f | 135 | { |
JMF | 0:082731ede69f | 136 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 137 | Timer t; |
JMF | 0:082731ede69f | 138 | t.start(); |
JMF | 0:082731ede69f | 139 | while( t.read_us() < (int)usec ) |
JMF | 0:082731ede69f | 140 | /* wait here */ ; |
JMF | 0:082731ede69f | 141 | } |
JMF | 0:082731ede69f | 142 | |
JMF | 0:082731ede69f | 143 | /* |
JMF | 0:082731ede69f | 144 | * Read at most 'len' characters |
JMF | 0:082731ede69f | 145 | */ |
JMF | 0:082731ede69f | 146 | int mbedtls_aws_recv( void *ctx, unsigned char *buf, size_t len ) |
JMF | 0:082731ede69f | 147 | { |
JMF | 0:082731ede69f | 148 | int ret; |
JMF | 0:082731ede69f | 149 | int fd = ((mbedtls_net_context *) ctx)->fd; |
JMF | 0:082731ede69f | 150 | |
JMF | 0:082731ede69f | 151 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 152 | if( fd < 0 ) |
JMF | 0:082731ede69f | 153 | FUNC_EXIT_RC(MBEDTLS_ERR_NET_INVALID_CONTEXT ); |
JMF | 0:082731ede69f | 154 | |
JMF | 0:082731ede69f | 155 | ret = (int) mbedtls_socket.recv( buf, len ); |
JMF | 0:082731ede69f | 156 | |
JMF | 0:082731ede69f | 157 | if( ret == NSAPI_ERROR_WOULD_BLOCK ) |
JMF | 0:082731ede69f | 158 | ret = MBEDTLS_ERR_SSL_WANT_READ; |
JMF | 0:082731ede69f | 159 | FUNC_EXIT_RC(ret ); |
JMF | 0:082731ede69f | 160 | } |
JMF | 0:082731ede69f | 161 | |
JMF | 0:082731ede69f | 162 | /* |
JMF | 0:082731ede69f | 163 | * Read at most 'len' characters, blocking for at most 'timeout' ms |
JMF | 0:082731ede69f | 164 | */ |
JMF | 0:082731ede69f | 165 | int mbedtls_aws_recv_timeout( void *ctx, unsigned char *buf, size_t len, uint32_t timeout ) |
JMF | 0:082731ede69f | 166 | { |
JMF | 0:082731ede69f | 167 | int ret, ttime; |
JMF | 0:082731ede69f | 168 | Timer t; |
JMF | 0:082731ede69f | 169 | int fd = ((mbedtls_net_context *) ctx)->fd; |
JMF | 0:082731ede69f | 170 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 171 | if( fd < 0 ) |
JMF | 0:082731ede69f | 172 | FUNC_EXIT_RC(MBEDTLS_ERR_NET_INVALID_CONTEXT ); |
JMF | 0:082731ede69f | 173 | |
JMF | 0:082731ede69f | 174 | t.start(); |
JMF | 0:082731ede69f | 175 | do { |
JMF | 0:082731ede69f | 176 | ret = mbedtls_socket.recv( buf, len ); |
JMF | 0:082731ede69f | 177 | ttime = t.read_ms(); |
JMF | 0:082731ede69f | 178 | if( ret == 0 && ttime < (int)timeout ) |
JMF | 0:082731ede69f | 179 | ret = mbedtls_socket.recv( buf, len ); |
JMF | 0:082731ede69f | 180 | } |
JMF | 0:082731ede69f | 181 | while( ttime < (int)timeout && ret == NSAPI_ERROR_WOULD_BLOCK ); |
JMF | 0:082731ede69f | 182 | |
JMF | 0:082731ede69f | 183 | if( ret < 0 && ttime >= (int)timeout ) |
JMF | 0:082731ede69f | 184 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
JMF | 0:082731ede69f | 185 | FUNC_EXIT_RC(ret); |
JMF | 0:082731ede69f | 186 | } |
JMF | 0:082731ede69f | 187 | |
JMF | 0:082731ede69f | 188 | /* |
JMF | 0:082731ede69f | 189 | * Write at most 'len' characters |
JMF | 0:082731ede69f | 190 | */ |
JMF | 0:082731ede69f | 191 | int mbedtls_aws_send( void *ctx, const unsigned char *buf, size_t len ) |
JMF | 0:082731ede69f | 192 | { |
JMF | 0:082731ede69f | 193 | int ret; |
JMF | 0:082731ede69f | 194 | int fd = ((mbedtls_net_context *) ctx)->fd; |
JMF | 0:082731ede69f | 195 | |
JMF | 0:082731ede69f | 196 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 197 | |
JMF | 0:082731ede69f | 198 | if( fd < 0 ) |
JMF | 0:082731ede69f | 199 | FUNC_EXIT_RC(MBEDTLS_ERR_NET_INVALID_CONTEXT ); |
JMF | 0:082731ede69f | 200 | |
JMF | 0:082731ede69f | 201 | while( (ret = mbedtls_socket.send(buf, len)) == NSAPI_ERROR_WOULD_BLOCK ) |
JMF | 0:082731ede69f | 202 | /* keep trying */; |
JMF | 0:082731ede69f | 203 | if( ret < 0 ) |
JMF | 0:082731ede69f | 204 | ret = (ret == NSAPI_ERROR_WOULD_BLOCK )? MBEDTLS_ERR_SSL_WANT_WRITE : MBEDTLS_ERR_NET_SEND_FAILED; |
JMF | 0:082731ede69f | 205 | FUNC_EXIT_RC( ret ); |
JMF | 0:082731ede69f | 206 | } |
JMF | 0:082731ede69f | 207 | |
JMF | 0:082731ede69f | 208 | /* |
JMF | 0:082731ede69f | 209 | * Gracefully close the connection |
JMF | 0:082731ede69f | 210 | */ |
JMF | 0:082731ede69f | 211 | void mbedtls_aws_free( mbedtls_net_context *ctx ) |
JMF | 0:082731ede69f | 212 | { |
JMF | 0:082731ede69f | 213 | FUNC_ENTRY; |
JMF | 0:082731ede69f | 214 | if( !network_connected || ctx->fd < 0 ) { |
JMF | 0:082731ede69f | 215 | FUNC_EXIT; |
JMF | 0:082731ede69f | 216 | } |
JMF | 0:082731ede69f | 217 | |
JMF | 0:082731ede69f | 218 | mbedtls_socket.close(); |
JMF | 0:082731ede69f | 219 | network->disconnect(); //disconnect from the current network |
JMF | 0:082731ede69f | 220 | ctx->fd = -1; |
JMF | 0:082731ede69f | 221 | FUNC_EXIT; |
JMF | 0:082731ede69f | 222 | } |
JMF | 0:082731ede69f | 223 |