Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers net_sockets.h Source File

net_sockets.h

Go to the documentation of this file.
00001 /**
00002  * \file net_sockets.h
00003  *
00004  * \brief Network communication functions
00005  */
00006 /*
00007  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00008  *  SPDX-License-Identifier: Apache-2.0
00009  *
00010  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00011  *  not use this file except in compliance with the License.
00012  *  You may obtain a copy of the License at
00013  *
00014  *  http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  *  Unless required by applicable law or agreed to in writing, software
00017  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00018  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  *  See the License for the specific language governing permissions and
00020  *  limitations under the License.
00021  *
00022  *  This file is part of mbed TLS (https://tls.mbed.org)
00023  */
00024 #ifndef MBEDTLS_NET_SOCKETS_H
00025 #define MBEDTLS_NET_SOCKETS_H
00026 
00027 #if !defined(MBEDTLS_CONFIG_FILE)
00028 #include "config.h"
00029 #else
00030 #include MBEDTLS_CONFIG_FILE
00031 #endif
00032 
00033 #include "ssl.h"
00034 
00035 #include <stddef.h>
00036 #include <stdint.h>
00037 
00038 #define MBEDTLS_ERR_NET_SOCKET_FAILED                     -0x0042  /**< Failed to open a socket. */
00039 #define MBEDTLS_ERR_NET_CONNECT_FAILED                    -0x0044  /**< The connection to the given server / port failed. */
00040 #define MBEDTLS_ERR_NET_BIND_FAILED                       -0x0046  /**< Binding of the socket failed. */
00041 #define MBEDTLS_ERR_NET_LISTEN_FAILED                     -0x0048  /**< Could not listen on the socket. */
00042 #define MBEDTLS_ERR_NET_ACCEPT_FAILED                     -0x004A  /**< Could not accept the incoming connection. */
00043 #define MBEDTLS_ERR_NET_RECV_FAILED                       -0x004C  /**< Reading information from the socket failed. */
00044 #define MBEDTLS_ERR_NET_SEND_FAILED                       -0x004E  /**< Sending information through the socket failed. */
00045 #define MBEDTLS_ERR_NET_CONN_RESET                        -0x0050  /**< Connection was reset by peer. */
00046 #define MBEDTLS_ERR_NET_UNKNOWN_HOST                      -0x0052  /**< Failed to get an IP address for the given hostname. */
00047 #define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL                  -0x0043  /**< Buffer is too small to hold the data. */
00048 #define MBEDTLS_ERR_NET_INVALID_CONTEXT                   -0x0045  /**< The context is invalid, eg because it was free()ed. */
00049 #define MBEDTLS_ERR_NET_POLL_FAILED                       -0x0047  /**< Polling the net context failed. */
00050 #define MBEDTLS_ERR_NET_BAD_INPUT_DATA                    -0x0049  /**< Input invalid. */
00051 
00052 #define MBEDTLS_NET_LISTEN_BACKLOG         10 /**< The backlog that listen() should use. */
00053 
00054 #define MBEDTLS_NET_PROTO_TCP 0 /**< The TCP transport protocol */
00055 #define MBEDTLS_NET_PROTO_UDP 1 /**< The UDP transport protocol */
00056 
00057 #define MBEDTLS_NET_POLL_READ  1 /**< Used in \c mbedtls_net_poll to check for pending data  */
00058 #define MBEDTLS_NET_POLL_WRITE 2 /**< Used in \c mbedtls_net_poll to check if write possible */
00059 
00060 #ifdef __cplusplus
00061 extern "C" {
00062 #endif
00063 
00064 /**
00065  * Wrapper type for sockets.
00066  *
00067  * Currently backed by just a file descriptor, but might be more in the future
00068  * (eg two file descriptors for combined IPv4 + IPv6 support, or additional
00069  * structures for hand-made UDP demultiplexing).
00070  */
00071 typedef struct
00072 {
00073     int fd;             /**< The underlying file descriptor                 */
00074 }
00075 mbedtls_net_context;
00076 
00077 /**
00078  * \brief          Initialize a context
00079  *                 Just makes the context ready to be used or freed safely.
00080  *
00081  * \param ctx      Context to initialize
00082  */
00083 void mbedtls_net_init( mbedtls_net_context *ctx );
00084 
00085 /**
00086  * \brief          Initiate a connection with host:port in the given protocol
00087  *
00088  * \param ctx      Socket to use
00089  * \param host     Host to connect to
00090  * \param port     Port to connect to
00091  * \param proto    Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
00092  *
00093  * \return         0 if successful, or one of:
00094  *                      MBEDTLS_ERR_NET_SOCKET_FAILED,
00095  *                      MBEDTLS_ERR_NET_UNKNOWN_HOST,
00096  *                      MBEDTLS_ERR_NET_CONNECT_FAILED
00097  *
00098  * \note           Sets the socket in connected mode even with UDP.
00099  */
00100 int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto );
00101 
00102 /**
00103  * \brief          Create a receiving socket on bind_ip:port in the chosen
00104  *                 protocol. If bind_ip == NULL, all interfaces are bound.
00105  *
00106  * \param ctx      Socket to use
00107  * \param bind_ip  IP to bind to, can be NULL
00108  * \param port     Port number to use
00109  * \param proto    Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
00110  *
00111  * \return         0 if successful, or one of:
00112  *                      MBEDTLS_ERR_NET_SOCKET_FAILED,
00113  *                      MBEDTLS_ERR_NET_BIND_FAILED,
00114  *                      MBEDTLS_ERR_NET_LISTEN_FAILED
00115  *
00116  * \note           Regardless of the protocol, opens the sockets and binds it.
00117  *                 In addition, make the socket listening if protocol is TCP.
00118  */
00119 int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto );
00120 
00121 /**
00122  * \brief           Accept a connection from a remote client
00123  *
00124  * \param bind_ctx  Relevant socket
00125  * \param client_ctx Will contain the connected client socket
00126  * \param client_ip Will contain the client IP address, can be NULL
00127  * \param buf_size  Size of the client_ip buffer
00128  * \param ip_len    Will receive the size of the client IP written,
00129  *                  can be NULL if client_ip is null
00130  *
00131  * \return          0 if successful, or
00132  *                  MBEDTLS_ERR_NET_ACCEPT_FAILED, or
00133  *                  MBEDTLS_ERR_NET_BUFFER_TOO_SMALL if buf_size is too small,
00134  *                  MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to
00135  *                  non-blocking and accept() would block.
00136  */
00137 int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
00138                         mbedtls_net_context *client_ctx,
00139                         void *client_ip, size_t buf_size, size_t *ip_len );
00140 
00141 /**
00142  * \brief          Check and wait for the context to be ready for read/write
00143  *
00144  * \param ctx      Socket to check
00145  * \param rw       Bitflag composed of MBEDTLS_NET_POLL_READ and
00146  *                 MBEDTLS_NET_POLL_WRITE specifying the events
00147  *                 to wait for:
00148  *                 - If MBEDTLS_NET_POLL_READ is set, the function
00149  *                   will return as soon as the net context is available
00150  *                   for reading.
00151  *                 - If MBEDTLS_NET_POLL_WRITE is set, the function
00152  *                   will return as soon as the net context is available
00153  *                   for writing.
00154  * \param timeout  Maximal amount of time to wait before returning,
00155  *                 in milliseconds. If \c timeout is zero, the
00156  *                 function returns immediately. If \c timeout is
00157  *                 -1u, the function blocks potentially indefinitely.
00158  *
00159  * \return         Bitmask composed of MBEDTLS_NET_POLL_READ/WRITE
00160  *                 on success or timeout, or a negative return code otherwise.
00161  */
00162 int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout );
00163 
00164 /**
00165  * \brief          Set the socket blocking
00166  *
00167  * \param ctx      Socket to set
00168  *
00169  * \return         0 if successful, or a non-zero error code
00170  */
00171 int mbedtls_net_set_block( mbedtls_net_context *ctx );
00172 
00173 /**
00174  * \brief          Set the socket non-blocking
00175  *
00176  * \param ctx      Socket to set
00177  *
00178  * \return         0 if successful, or a non-zero error code
00179  */
00180 int mbedtls_net_set_nonblock( mbedtls_net_context *ctx );
00181 
00182 /**
00183  * \brief          Portable usleep helper
00184  *
00185  * \param usec     Amount of microseconds to sleep
00186  *
00187  * \note           Real amount of time slept will not be less than
00188  *                 select()'s timeout granularity (typically, 10ms).
00189  */
00190 void mbedtls_net_usleep( unsigned long usec );
00191 
00192 /**
00193  * \brief          Read at most 'len' characters. If no error occurs,
00194  *                 the actual amount read is returned.
00195  *
00196  * \param ctx      Socket
00197  * \param buf      The buffer to write to
00198  * \param len      Maximum length of the buffer
00199  *
00200  * \return         the number of bytes received,
00201  *                 or a non-zero error code; with a non-blocking socket,
00202  *                 MBEDTLS_ERR_SSL_WANT_READ indicates read() would block.
00203  */
00204 int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len );
00205 
00206 /**
00207  * \brief          Write at most 'len' characters. If no error occurs,
00208  *                 the actual amount read is returned.
00209  *
00210  * \param ctx      Socket
00211  * \param buf      The buffer to read from
00212  * \param len      The length of the buffer
00213  *
00214  * \return         the number of bytes sent,
00215  *                 or a non-zero error code; with a non-blocking socket,
00216  *                 MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block.
00217  */
00218 int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len );
00219 
00220 /**
00221  * \brief          Read at most 'len' characters, blocking for at most
00222  *                 'timeout' seconds. If no error occurs, the actual amount
00223  *                 read is returned.
00224  *
00225  * \param ctx      Socket
00226  * \param buf      The buffer to write to
00227  * \param len      Maximum length of the buffer
00228  * \param timeout  Maximum number of milliseconds to wait for data
00229  *                 0 means no timeout (wait forever)
00230  *
00231  * \return         the number of bytes received,
00232  *                 or a non-zero error code:
00233  *                 MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out,
00234  *                 MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
00235  *
00236  * \note           This function will block (until data becomes available or
00237  *                 timeout is reached) even if the socket is set to
00238  *                 non-blocking. Handling timeouts with non-blocking reads
00239  *                 requires a different strategy.
00240  */
00241 int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
00242                       uint32_t timeout );
00243 
00244 /**
00245  * \brief          Gracefully shutdown the connection and free associated data
00246  *
00247  * \param ctx      The context to free
00248  */
00249 void mbedtls_net_free( mbedtls_net_context *ctx );
00250 
00251 #ifdef __cplusplus
00252 }
00253 #endif
00254 
00255 #endif /* net_sockets.h */