mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 11:cada08fc8a70 1 /**
mbedAustin 11:cada08fc8a70 2 * \file net.h
mbedAustin 11:cada08fc8a70 3 *
mbedAustin 11:cada08fc8a70 4 * \brief Network communication functions
mbedAustin 11:cada08fc8a70 5 *
mbedAustin 11:cada08fc8a70 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
mbedAustin 11:cada08fc8a70 7 * SPDX-License-Identifier: Apache-2.0
mbedAustin 11:cada08fc8a70 8 *
mbedAustin 11:cada08fc8a70 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
mbedAustin 11:cada08fc8a70 10 * not use this file except in compliance with the License.
mbedAustin 11:cada08fc8a70 11 * You may obtain a copy of the License at
mbedAustin 11:cada08fc8a70 12 *
mbedAustin 11:cada08fc8a70 13 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 11:cada08fc8a70 14 *
mbedAustin 11:cada08fc8a70 15 * Unless required by applicable law or agreed to in writing, software
mbedAustin 11:cada08fc8a70 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
mbedAustin 11:cada08fc8a70 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 11:cada08fc8a70 18 * See the License for the specific language governing permissions and
mbedAustin 11:cada08fc8a70 19 * limitations under the License.
mbedAustin 11:cada08fc8a70 20 *
mbedAustin 11:cada08fc8a70 21 * This file is part of mbed TLS (https://tls.mbed.org)
mbedAustin 11:cada08fc8a70 22 */
mbedAustin 11:cada08fc8a70 23 #ifndef MBEDTLS_NET_H
mbedAustin 11:cada08fc8a70 24 #define MBEDTLS_NET_H
mbedAustin 11:cada08fc8a70 25
mbedAustin 11:cada08fc8a70 26 #if !defined(MBEDTLS_CONFIG_FILE)
mbedAustin 11:cada08fc8a70 27 #include "config.h"
mbedAustin 11:cada08fc8a70 28 #else
mbedAustin 11:cada08fc8a70 29 #include MBEDTLS_CONFIG_FILE
mbedAustin 11:cada08fc8a70 30 #endif
mbedAustin 11:cada08fc8a70 31
mbedAustin 11:cada08fc8a70 32 #include "ssl.h"
mbedAustin 11:cada08fc8a70 33
mbedAustin 11:cada08fc8a70 34 #include <stddef.h>
mbedAustin 11:cada08fc8a70 35 #include <stdint.h>
mbedAustin 11:cada08fc8a70 36
mbedAustin 11:cada08fc8a70 37 #define MBEDTLS_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */
mbedAustin 11:cada08fc8a70 38 #define MBEDTLS_ERR_NET_CONNECT_FAILED -0x0044 /**< The connection to the given server / port failed. */
mbedAustin 11:cada08fc8a70 39 #define MBEDTLS_ERR_NET_BIND_FAILED -0x0046 /**< Binding of the socket failed. */
mbedAustin 11:cada08fc8a70 40 #define MBEDTLS_ERR_NET_LISTEN_FAILED -0x0048 /**< Could not listen on the socket. */
mbedAustin 11:cada08fc8a70 41 #define MBEDTLS_ERR_NET_ACCEPT_FAILED -0x004A /**< Could not accept the incoming connection. */
mbedAustin 11:cada08fc8a70 42 #define MBEDTLS_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */
mbedAustin 11:cada08fc8a70 43 #define MBEDTLS_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */
mbedAustin 11:cada08fc8a70 44 #define MBEDTLS_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */
mbedAustin 11:cada08fc8a70 45 #define MBEDTLS_ERR_NET_UNKNOWN_HOST -0x0052 /**< Failed to get an IP address for the given hostname. */
mbedAustin 11:cada08fc8a70 46 #define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL -0x0043 /**< Buffer is too small to hold the data. */
mbedAustin 11:cada08fc8a70 47 #define MBEDTLS_ERR_NET_INVALID_CONTEXT -0x0045 /**< The context is invalid, eg because it was free()ed. */
mbedAustin 11:cada08fc8a70 48
mbedAustin 11:cada08fc8a70 49 #define MBEDTLS_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */
mbedAustin 11:cada08fc8a70 50
mbedAustin 11:cada08fc8a70 51 #define MBEDTLS_NET_PROTO_TCP 0 /**< The TCP transport protocol */
mbedAustin 11:cada08fc8a70 52 #define MBEDTLS_NET_PROTO_UDP 1 /**< The UDP transport protocol */
mbedAustin 11:cada08fc8a70 53
mbedAustin 11:cada08fc8a70 54 #ifdef __cplusplus
mbedAustin 11:cada08fc8a70 55 extern "C" {
mbedAustin 11:cada08fc8a70 56 #endif
mbedAustin 11:cada08fc8a70 57
mbedAustin 11:cada08fc8a70 58 /**
mbedAustin 11:cada08fc8a70 59 * Wrapper type for sockets.
mbedAustin 11:cada08fc8a70 60 *
mbedAustin 11:cada08fc8a70 61 * Currently backed by just a file descriptor, but might be more in the future
mbedAustin 11:cada08fc8a70 62 * (eg two file descriptors for combined IPv4 + IPv6 support, or additional
mbedAustin 11:cada08fc8a70 63 * structures for hand-made UDP demultiplexing).
mbedAustin 11:cada08fc8a70 64 */
mbedAustin 11:cada08fc8a70 65 typedef struct
mbedAustin 11:cada08fc8a70 66 {
mbedAustin 11:cada08fc8a70 67 int fd; /**< The underlying file descriptor */
mbedAustin 11:cada08fc8a70 68 }
mbedAustin 11:cada08fc8a70 69 mbedtls_net_context;
mbedAustin 11:cada08fc8a70 70
mbedAustin 11:cada08fc8a70 71 /**
mbedAustin 11:cada08fc8a70 72 * \brief Initialize a context
mbedAustin 11:cada08fc8a70 73 * Just makes the context ready to be used or freed safely.
mbedAustin 11:cada08fc8a70 74 *
mbedAustin 11:cada08fc8a70 75 * \param ctx Context to initialize
mbedAustin 11:cada08fc8a70 76 */
mbedAustin 11:cada08fc8a70 77 void mbedtls_net_init( mbedtls_net_context *ctx );
mbedAustin 11:cada08fc8a70 78
mbedAustin 11:cada08fc8a70 79 /**
mbedAustin 11:cada08fc8a70 80 * \brief Initiate a connection with host:port in the given protocol
mbedAustin 11:cada08fc8a70 81 *
mbedAustin 11:cada08fc8a70 82 * \param ctx Socket to use
mbedAustin 11:cada08fc8a70 83 * \param host Host to connect to
mbedAustin 11:cada08fc8a70 84 * \param port Port to connect to
mbedAustin 11:cada08fc8a70 85 * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
mbedAustin 11:cada08fc8a70 86 *
mbedAustin 11:cada08fc8a70 87 * \return 0 if successful, or one of:
mbedAustin 11:cada08fc8a70 88 * MBEDTLS_ERR_NET_SOCKET_FAILED,
mbedAustin 11:cada08fc8a70 89 * MBEDTLS_ERR_NET_UNKNOWN_HOST,
mbedAustin 11:cada08fc8a70 90 * MBEDTLS_ERR_NET_CONNECT_FAILED
mbedAustin 11:cada08fc8a70 91 *
mbedAustin 11:cada08fc8a70 92 * \note Sets the socket in connected mode even with UDP.
mbedAustin 11:cada08fc8a70 93 */
mbedAustin 11:cada08fc8a70 94 int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto );
mbedAustin 11:cada08fc8a70 95
mbedAustin 11:cada08fc8a70 96 /**
mbedAustin 11:cada08fc8a70 97 * \brief Create a receiving socket on bind_ip:port in the chosen
mbedAustin 11:cada08fc8a70 98 * protocol. If bind_ip == NULL, all interfaces are bound.
mbedAustin 11:cada08fc8a70 99 *
mbedAustin 11:cada08fc8a70 100 * \param ctx Socket to use
mbedAustin 11:cada08fc8a70 101 * \param bind_ip IP to bind to, can be NULL
mbedAustin 11:cada08fc8a70 102 * \param port Port number to use
mbedAustin 11:cada08fc8a70 103 * \param proto Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
mbedAustin 11:cada08fc8a70 104 *
mbedAustin 11:cada08fc8a70 105 * \return 0 if successful, or one of:
mbedAustin 11:cada08fc8a70 106 * MBEDTLS_ERR_NET_SOCKET_FAILED,
mbedAustin 11:cada08fc8a70 107 * MBEDTLS_ERR_NET_BIND_FAILED,
mbedAustin 11:cada08fc8a70 108 * MBEDTLS_ERR_NET_LISTEN_FAILED
mbedAustin 11:cada08fc8a70 109 *
mbedAustin 11:cada08fc8a70 110 * \note Regardless of the protocol, opens the sockets and binds it.
mbedAustin 11:cada08fc8a70 111 * In addition, make the socket listening if protocol is TCP.
mbedAustin 11:cada08fc8a70 112 */
mbedAustin 11:cada08fc8a70 113 int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto );
mbedAustin 11:cada08fc8a70 114
mbedAustin 11:cada08fc8a70 115 /**
mbedAustin 11:cada08fc8a70 116 * \brief Accept a connection from a remote client
mbedAustin 11:cada08fc8a70 117 *
mbedAustin 11:cada08fc8a70 118 * \param bind_ctx Relevant socket
mbedAustin 11:cada08fc8a70 119 * \param client_ctx Will contain the connected client socket
mbedAustin 11:cada08fc8a70 120 * \param client_ip Will contain the client IP address
mbedAustin 11:cada08fc8a70 121 * \param buf_size Size of the client_ip buffer
mbedAustin 11:cada08fc8a70 122 * \param ip_len Will receive the size of the client IP written
mbedAustin 11:cada08fc8a70 123 *
mbedAustin 11:cada08fc8a70 124 * \return 0 if successful, or
mbedAustin 11:cada08fc8a70 125 * MBEDTLS_ERR_NET_ACCEPT_FAILED, or
mbedAustin 11:cada08fc8a70 126 * MBEDTLS_ERR_NET_BUFFER_TOO_SMALL if buf_size is too small,
mbedAustin 11:cada08fc8a70 127 * MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to
mbedAustin 11:cada08fc8a70 128 * non-blocking and accept() would block.
mbedAustin 11:cada08fc8a70 129 */
mbedAustin 11:cada08fc8a70 130 int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
mbedAustin 11:cada08fc8a70 131 mbedtls_net_context *client_ctx,
mbedAustin 11:cada08fc8a70 132 void *client_ip, size_t buf_size, size_t *ip_len );
mbedAustin 11:cada08fc8a70 133
mbedAustin 11:cada08fc8a70 134 /**
mbedAustin 11:cada08fc8a70 135 * \brief Set the socket blocking
mbedAustin 11:cada08fc8a70 136 *
mbedAustin 11:cada08fc8a70 137 * \param ctx Socket to set
mbedAustin 11:cada08fc8a70 138 *
mbedAustin 11:cada08fc8a70 139 * \return 0 if successful, or a non-zero error code
mbedAustin 11:cada08fc8a70 140 */
mbedAustin 11:cada08fc8a70 141 int mbedtls_net_set_block( mbedtls_net_context *ctx );
mbedAustin 11:cada08fc8a70 142
mbedAustin 11:cada08fc8a70 143 /**
mbedAustin 11:cada08fc8a70 144 * \brief Set the socket non-blocking
mbedAustin 11:cada08fc8a70 145 *
mbedAustin 11:cada08fc8a70 146 * \param ctx Socket to set
mbedAustin 11:cada08fc8a70 147 *
mbedAustin 11:cada08fc8a70 148 * \return 0 if successful, or a non-zero error code
mbedAustin 11:cada08fc8a70 149 */
mbedAustin 11:cada08fc8a70 150 int mbedtls_net_set_nonblock( mbedtls_net_context *ctx );
mbedAustin 11:cada08fc8a70 151
mbedAustin 11:cada08fc8a70 152 /**
mbedAustin 11:cada08fc8a70 153 * \brief Portable usleep helper
mbedAustin 11:cada08fc8a70 154 *
mbedAustin 11:cada08fc8a70 155 * \param usec Amount of microseconds to sleep
mbedAustin 11:cada08fc8a70 156 *
mbedAustin 11:cada08fc8a70 157 * \note Real amount of time slept will not be less than
mbedAustin 11:cada08fc8a70 158 * select()'s timeout granularity (typically, 10ms).
mbedAustin 11:cada08fc8a70 159 */
mbedAustin 11:cada08fc8a70 160 void mbedtls_net_usleep( unsigned long usec );
mbedAustin 11:cada08fc8a70 161
mbedAustin 11:cada08fc8a70 162 /**
mbedAustin 11:cada08fc8a70 163 * \brief Read at most 'len' characters. If no error occurs,
mbedAustin 11:cada08fc8a70 164 * the actual amount read is returned.
mbedAustin 11:cada08fc8a70 165 *
mbedAustin 11:cada08fc8a70 166 * \param ctx Socket
mbedAustin 11:cada08fc8a70 167 * \param buf The buffer to write to
mbedAustin 11:cada08fc8a70 168 * \param len Maximum length of the buffer
mbedAustin 11:cada08fc8a70 169 *
mbedAustin 11:cada08fc8a70 170 * \return the number of bytes received,
mbedAustin 11:cada08fc8a70 171 * or a non-zero error code; with a non-blocking socket,
mbedAustin 11:cada08fc8a70 172 * MBEDTLS_ERR_SSL_WANT_READ indicates read() would block.
mbedAustin 11:cada08fc8a70 173 */
mbedAustin 11:cada08fc8a70 174 int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len );
mbedAustin 11:cada08fc8a70 175
mbedAustin 11:cada08fc8a70 176 /**
mbedAustin 11:cada08fc8a70 177 * \brief Write at most 'len' characters. If no error occurs,
mbedAustin 11:cada08fc8a70 178 * the actual amount read is returned.
mbedAustin 11:cada08fc8a70 179 *
mbedAustin 11:cada08fc8a70 180 * \param ctx Socket
mbedAustin 11:cada08fc8a70 181 * \param buf The buffer to read from
mbedAustin 11:cada08fc8a70 182 * \param len The length of the buffer
mbedAustin 11:cada08fc8a70 183 *
mbedAustin 11:cada08fc8a70 184 * \return the number of bytes sent,
mbedAustin 11:cada08fc8a70 185 * or a non-zero error code; with a non-blocking socket,
mbedAustin 11:cada08fc8a70 186 * MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block.
mbedAustin 11:cada08fc8a70 187 */
mbedAustin 11:cada08fc8a70 188 int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len );
mbedAustin 11:cada08fc8a70 189
mbedAustin 11:cada08fc8a70 190 /**
mbedAustin 11:cada08fc8a70 191 * \brief Read at most 'len' characters, blocking for at most
mbedAustin 11:cada08fc8a70 192 * 'timeout' seconds. If no error occurs, the actual amount
mbedAustin 11:cada08fc8a70 193 * read is returned.
mbedAustin 11:cada08fc8a70 194 *
mbedAustin 11:cada08fc8a70 195 * \param ctx Socket
mbedAustin 11:cada08fc8a70 196 * \param buf The buffer to write to
mbedAustin 11:cada08fc8a70 197 * \param len Maximum length of the buffer
mbedAustin 11:cada08fc8a70 198 * \param timeout Maximum number of milliseconds to wait for data
mbedAustin 11:cada08fc8a70 199 * 0 means no timeout (wait forever)
mbedAustin 11:cada08fc8a70 200 *
mbedAustin 11:cada08fc8a70 201 * \return the number of bytes received,
mbedAustin 11:cada08fc8a70 202 * or a non-zero error code:
mbedAustin 11:cada08fc8a70 203 * MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out,
mbedAustin 11:cada08fc8a70 204 * MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
mbedAustin 11:cada08fc8a70 205 *
mbedAustin 11:cada08fc8a70 206 * \note This function will block (until data becomes available or
mbedAustin 11:cada08fc8a70 207 * timeout is reached) even if the socket is set to
mbedAustin 11:cada08fc8a70 208 * non-blocking. Handling timeouts with non-blocking reads
mbedAustin 11:cada08fc8a70 209 * requires a different strategy.
mbedAustin 11:cada08fc8a70 210 */
mbedAustin 11:cada08fc8a70 211 int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
mbedAustin 11:cada08fc8a70 212 uint32_t timeout );
mbedAustin 11:cada08fc8a70 213
mbedAustin 11:cada08fc8a70 214 /**
mbedAustin 11:cada08fc8a70 215 * \brief Gracefully shutdown the connection and free associated data
mbedAustin 11:cada08fc8a70 216 *
mbedAustin 11:cada08fc8a70 217 * \param ctx The context to free
mbedAustin 11:cada08fc8a70 218 */
mbedAustin 11:cada08fc8a70 219 void mbedtls_net_free( mbedtls_net_context *ctx );
mbedAustin 11:cada08fc8a70 220
mbedAustin 11:cada08fc8a70 221 #ifdef __cplusplus
mbedAustin 11:cada08fc8a70 222 }
mbedAustin 11:cada08fc8a70 223 #endif
mbedAustin 11:cada08fc8a70 224
mbedAustin 11:cada08fc8a70 225 #endif /* net.h */