Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:32

0001 /**
0002  * \file net_sockets.h
0003  *
0004  * \brief   Network sockets abstraction layer to integrate Mbed TLS into a
0005  *          BSD-style sockets API.
0006  *
0007  *          The network sockets module provides an example integration of the
0008  *          Mbed TLS library into a BSD sockets implementation. The module is
0009  *          intended to be an example of how Mbed TLS can be integrated into a
0010  *          networking stack, as well as to be Mbed TLS's network integration
0011  *          for its supported platforms.
0012  *
0013  *          The module is intended only to be used with the Mbed TLS library and
0014  *          is not intended to be used by third party application software
0015  *          directly.
0016  *
0017  *          The supported platforms are as follows:
0018  *              * Microsoft Windows and Windows CE
0019  *              * POSIX/Unix platforms including Linux, OS X
0020  *
0021  */
0022 /*
0023  *  Copyright The Mbed TLS Contributors
0024  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0025  */
0026 #ifndef MBEDTLS_NET_SOCKETS_H
0027 #define MBEDTLS_NET_SOCKETS_H
0028 #include "mbedtls/private_access.h"
0029 
0030 #include "mbedtls/build_info.h"
0031 
0032 #include "mbedtls/ssl.h"
0033 
0034 #include <stddef.h>
0035 #include <stdint.h>
0036 
0037 /** Failed to open a socket. */
0038 #define MBEDTLS_ERR_NET_SOCKET_FAILED                     -0x0042
0039 /** The connection to the given server / port failed. */
0040 #define MBEDTLS_ERR_NET_CONNECT_FAILED                    -0x0044
0041 /** Binding of the socket failed. */
0042 #define MBEDTLS_ERR_NET_BIND_FAILED                       -0x0046
0043 /** Could not listen on the socket. */
0044 #define MBEDTLS_ERR_NET_LISTEN_FAILED                     -0x0048
0045 /** Could not accept the incoming connection. */
0046 #define MBEDTLS_ERR_NET_ACCEPT_FAILED                     -0x004A
0047 /** Reading information from the socket failed. */
0048 #define MBEDTLS_ERR_NET_RECV_FAILED                       -0x004C
0049 /** Sending information through the socket failed. */
0050 #define MBEDTLS_ERR_NET_SEND_FAILED                       -0x004E
0051 /** Connection was reset by peer. */
0052 #define MBEDTLS_ERR_NET_CONN_RESET                        -0x0050
0053 /** Failed to get an IP address for the given hostname. */
0054 #define MBEDTLS_ERR_NET_UNKNOWN_HOST                      -0x0052
0055 /** Buffer is too small to hold the data. */
0056 #define MBEDTLS_ERR_NET_BUFFER_TOO_SMALL                  -0x0043
0057 /** The context is invalid, eg because it was free()ed. */
0058 #define MBEDTLS_ERR_NET_INVALID_CONTEXT                   -0x0045
0059 /** Polling the net context failed. */
0060 #define MBEDTLS_ERR_NET_POLL_FAILED                       -0x0047
0061 /** Input invalid. */
0062 #define MBEDTLS_ERR_NET_BAD_INPUT_DATA                    -0x0049
0063 
0064 #define MBEDTLS_NET_LISTEN_BACKLOG         10 /**< The backlog that listen() should use. */
0065 
0066 #define MBEDTLS_NET_PROTO_TCP 0 /**< The TCP transport protocol */
0067 #define MBEDTLS_NET_PROTO_UDP 1 /**< The UDP transport protocol */
0068 
0069 #define MBEDTLS_NET_POLL_READ  1 /**< Used in \c mbedtls_net_poll to check for pending data  */
0070 #define MBEDTLS_NET_POLL_WRITE 2 /**< Used in \c mbedtls_net_poll to check if write possible */
0071 
0072 #ifdef __cplusplus
0073 extern "C" {
0074 #endif
0075 
0076 /**
0077  * Wrapper type for sockets.
0078  *
0079  * Currently backed by just a file descriptor, but might be more in the future
0080  * (eg two file descriptors for combined IPv4 + IPv6 support, or additional
0081  * structures for hand-made UDP demultiplexing).
0082  */
0083 typedef struct mbedtls_net_context {
0084     /** The underlying file descriptor.
0085      *
0086      * This field is only guaranteed to be present on POSIX/Unix-like platforms.
0087      * On other platforms, it may have a different type, have a different
0088      * meaning, or be absent altogether.
0089      */
0090     int fd;
0091 }
0092 mbedtls_net_context;
0093 
0094 /**
0095  * \brief          Initialize a context
0096  *                 Just makes the context ready to be used or freed safely.
0097  *
0098  * \param ctx      Context to initialize
0099  */
0100 void mbedtls_net_init(mbedtls_net_context *ctx);
0101 
0102 /**
0103  * \brief          Initiate a connection with host:port in the given protocol
0104  *
0105  * \param ctx      Socket to use
0106  * \param host     Host to connect to
0107  * \param port     Port to connect to
0108  * \param proto    Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
0109  *
0110  * \return         0 if successful, or one of:
0111  *                      MBEDTLS_ERR_NET_SOCKET_FAILED,
0112  *                      MBEDTLS_ERR_NET_UNKNOWN_HOST,
0113  *                      MBEDTLS_ERR_NET_CONNECT_FAILED
0114  *
0115  * \note           Sets the socket in connected mode even with UDP.
0116  */
0117 int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto);
0118 
0119 /**
0120  * \brief          Create a receiving socket on bind_ip:port in the chosen
0121  *                 protocol. If bind_ip == NULL, all interfaces are bound.
0122  *
0123  * \param ctx      Socket to use
0124  * \param bind_ip  IP to bind to, can be NULL
0125  * \param port     Port number to use
0126  * \param proto    Protocol: MBEDTLS_NET_PROTO_TCP or MBEDTLS_NET_PROTO_UDP
0127  *
0128  * \return         0 if successful, or one of:
0129  *                      MBEDTLS_ERR_NET_SOCKET_FAILED,
0130  *                      MBEDTLS_ERR_NET_UNKNOWN_HOST,
0131  *                      MBEDTLS_ERR_NET_BIND_FAILED,
0132  *                      MBEDTLS_ERR_NET_LISTEN_FAILED
0133  *
0134  * \note           Regardless of the protocol, opens the sockets and binds it.
0135  *                 In addition, make the socket listening if protocol is TCP.
0136  */
0137 int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto);
0138 
0139 /**
0140  * \brief           Accept a connection from a remote client
0141  *
0142  * \param bind_ctx  Relevant socket
0143  * \param client_ctx Will contain the connected client socket
0144  * \param client_ip Will contain the client IP address, can be NULL
0145  * \param buf_size  Size of the client_ip buffer
0146  * \param cip_len   Will receive the size of the client IP written,
0147  *                  can be NULL if client_ip is null
0148  *
0149  * \return          0 if successful, or
0150  *                  MBEDTLS_ERR_NET_SOCKET_FAILED,
0151  *                  MBEDTLS_ERR_NET_BIND_FAILED,
0152  *                  MBEDTLS_ERR_NET_ACCEPT_FAILED, or
0153  *                  MBEDTLS_ERR_NET_BUFFER_TOO_SMALL if buf_size is too small,
0154  *                  MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to
0155  *                  non-blocking and accept() would block.
0156  */
0157 int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
0158                        mbedtls_net_context *client_ctx,
0159                        void *client_ip, size_t buf_size, size_t *cip_len);
0160 
0161 /**
0162  * \brief          Check and wait for the context to be ready for read/write
0163  *
0164  * \note           The current implementation of this function uses
0165  *                 select() and returns an error if the file descriptor
0166  *                 is \c FD_SETSIZE or greater.
0167  *
0168  * \param ctx      Socket to check
0169  * \param rw       Bitflag composed of MBEDTLS_NET_POLL_READ and
0170  *                 MBEDTLS_NET_POLL_WRITE specifying the events
0171  *                 to wait for:
0172  *                 - If MBEDTLS_NET_POLL_READ is set, the function
0173  *                   will return as soon as the net context is available
0174  *                   for reading.
0175  *                 - If MBEDTLS_NET_POLL_WRITE is set, the function
0176  *                   will return as soon as the net context is available
0177  *                   for writing.
0178  * \param timeout  Maximal amount of time to wait before returning,
0179  *                 in milliseconds. If \c timeout is zero, the
0180  *                 function returns immediately. If \c timeout is
0181  *                 -1u, the function blocks potentially indefinitely.
0182  *
0183  * \return         Bitmask composed of MBEDTLS_NET_POLL_READ/WRITE
0184  *                 on success or timeout, or a negative return code otherwise.
0185  */
0186 int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout);
0187 
0188 /**
0189  * \brief          Set the socket blocking
0190  *
0191  * \param ctx      Socket to set
0192  *
0193  * \return         0 if successful, or a non-zero error code
0194  */
0195 int mbedtls_net_set_block(mbedtls_net_context *ctx);
0196 
0197 /**
0198  * \brief          Set the socket non-blocking
0199  *
0200  * \param ctx      Socket to set
0201  *
0202  * \return         0 if successful, or a non-zero error code
0203  */
0204 int mbedtls_net_set_nonblock(mbedtls_net_context *ctx);
0205 
0206 /**
0207  * \brief          Portable usleep helper
0208  *
0209  * \param usec     Amount of microseconds to sleep
0210  *
0211  * \note           Real amount of time slept will not be less than
0212  *                 select()'s timeout granularity (typically, 10ms).
0213  */
0214 void mbedtls_net_usleep(unsigned long usec);
0215 
0216 /**
0217  * \brief          Read at most 'len' characters. If no error occurs,
0218  *                 the actual amount read is returned.
0219  *
0220  * \param ctx      Socket
0221  * \param buf      The buffer to write to
0222  * \param len      Maximum length of the buffer
0223  *
0224  * \return         the number of bytes received,
0225  *                 or a non-zero error code; with a non-blocking socket,
0226  *                 MBEDTLS_ERR_SSL_WANT_READ indicates read() would block.
0227  */
0228 int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len);
0229 
0230 /**
0231  * \brief          Write at most 'len' characters. If no error occurs,
0232  *                 the actual amount read is returned.
0233  *
0234  * \param ctx      Socket
0235  * \param buf      The buffer to read from
0236  * \param len      The length of the buffer
0237  *
0238  * \return         the number of bytes sent,
0239  *                 or a non-zero error code; with a non-blocking socket,
0240  *                 MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block.
0241  */
0242 int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len);
0243 
0244 /**
0245  * \brief          Read at most 'len' characters, blocking for at most
0246  *                 'timeout' seconds. If no error occurs, the actual amount
0247  *                 read is returned.
0248  *
0249  * \note           The current implementation of this function uses
0250  *                 select() and returns an error if the file descriptor
0251  *                 is \c FD_SETSIZE or greater.
0252  *
0253  * \param ctx      Socket
0254  * \param buf      The buffer to write to
0255  * \param len      Maximum length of the buffer
0256  * \param timeout  Maximum number of milliseconds to wait for data
0257  *                 0 means no timeout (wait forever)
0258  *
0259  * \return         The number of bytes received if successful.
0260  *                 MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out.
0261  *                 MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
0262  *                 Another negative error code (MBEDTLS_ERR_NET_xxx)
0263  *                 for other failures.
0264  *
0265  * \note           This function will block (until data becomes available or
0266  *                 timeout is reached) even if the socket is set to
0267  *                 non-blocking. Handling timeouts with non-blocking reads
0268  *                 requires a different strategy.
0269  */
0270 int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len,
0271                              uint32_t timeout);
0272 
0273 /**
0274  * \brief          Closes down the connection and free associated data
0275  *
0276  * \param ctx      The context to close
0277  *
0278  * \note           This function frees and clears data associated with the
0279  *                 context but does not free the memory pointed to by \p ctx.
0280  *                 This memory is the responsibility of the caller.
0281  */
0282 void mbedtls_net_close(mbedtls_net_context *ctx);
0283 
0284 /**
0285  * \brief          Gracefully shutdown the connection and free associated data
0286  *
0287  * \param ctx      The context to free
0288  *
0289  * \note           This function frees and clears data associated with the
0290  *                 context but does not free the memory pointed to by \p ctx.
0291  *                 This memory is the responsibility of the caller.
0292  */
0293 void mbedtls_net_free(mbedtls_net_context *ctx);
0294 
0295 #ifdef __cplusplus
0296 }
0297 #endif
0298 
0299 #endif /* net_sockets.h */