mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
include/polarssl/net.h@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /** |
ansond | 0:137634ff4186 | 2 | * \file net.h |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * \brief Network communication functions |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 9 | * |
ansond | 0:137634ff4186 | 10 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 11 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 12 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 13 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 14 | * |
ansond | 0:137634ff4186 | 15 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 18 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 19 | * |
ansond | 0:137634ff4186 | 20 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 23 | */ |
ansond | 0:137634ff4186 | 24 | #ifndef POLARSSL_NET_H |
ansond | 0:137634ff4186 | 25 | #define POLARSSL_NET_H |
ansond | 0:137634ff4186 | 26 | |
ansond | 0:137634ff4186 | 27 | #include <stddef.h> |
ansond | 0:137634ff4186 | 28 | |
ansond | 0:137634ff4186 | 29 | #define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0056 /**< Failed to get an IP address for the given hostname. */ |
ansond | 0:137634ff4186 | 30 | #define POLARSSL_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */ |
ansond | 0:137634ff4186 | 31 | #define POLARSSL_ERR_NET_CONNECT_FAILED -0x0044 /**< The connection to the given server / port failed. */ |
ansond | 0:137634ff4186 | 32 | #define POLARSSL_ERR_NET_BIND_FAILED -0x0046 /**< Binding of the socket failed. */ |
ansond | 0:137634ff4186 | 33 | #define POLARSSL_ERR_NET_LISTEN_FAILED -0x0048 /**< Could not listen on the socket. */ |
ansond | 0:137634ff4186 | 34 | #define POLARSSL_ERR_NET_ACCEPT_FAILED -0x004A /**< Could not accept the incoming connection. */ |
ansond | 0:137634ff4186 | 35 | #define POLARSSL_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */ |
ansond | 0:137634ff4186 | 36 | #define POLARSSL_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */ |
ansond | 0:137634ff4186 | 37 | #define POLARSSL_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */ |
ansond | 0:137634ff4186 | 38 | #define POLARSSL_ERR_NET_WANT_READ -0x0052 /**< Connection requires a read call. */ |
ansond | 0:137634ff4186 | 39 | #define POLARSSL_ERR_NET_WANT_WRITE -0x0054 /**< Connection requires a write call. */ |
ansond | 0:137634ff4186 | 40 | |
ansond | 0:137634ff4186 | 41 | #define POLARSSL_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */ |
ansond | 0:137634ff4186 | 42 | |
ansond | 0:137634ff4186 | 43 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 44 | extern "C" { |
ansond | 0:137634ff4186 | 45 | #endif |
ansond | 0:137634ff4186 | 46 | |
ansond | 0:137634ff4186 | 47 | /** |
ansond | 0:137634ff4186 | 48 | * \brief Initiate a TCP connection with host:port |
ansond | 0:137634ff4186 | 49 | * |
ansond | 0:137634ff4186 | 50 | * \param fd Socket to use |
ansond | 0:137634ff4186 | 51 | * \param host Host to connect to |
ansond | 0:137634ff4186 | 52 | * \param port Port to connect to |
ansond | 0:137634ff4186 | 53 | * |
ansond | 0:137634ff4186 | 54 | * \return 0 if successful, or one of: |
ansond | 0:137634ff4186 | 55 | * POLARSSL_ERR_NET_SOCKET_FAILED, |
ansond | 0:137634ff4186 | 56 | * POLARSSL_ERR_NET_UNKNOWN_HOST, |
ansond | 0:137634ff4186 | 57 | * POLARSSL_ERR_NET_CONNECT_FAILED |
ansond | 0:137634ff4186 | 58 | */ |
ansond | 0:137634ff4186 | 59 | int net_connect( int *fd, const char *host, int port ); |
ansond | 0:137634ff4186 | 60 | |
ansond | 0:137634ff4186 | 61 | /** |
ansond | 0:137634ff4186 | 62 | * \brief Create a listening socket on bind_ip:port. |
ansond | 0:137634ff4186 | 63 | * If bind_ip == NULL, all interfaces are binded. |
ansond | 0:137634ff4186 | 64 | * |
ansond | 0:137634ff4186 | 65 | * \param fd Socket to use |
ansond | 0:137634ff4186 | 66 | * \param bind_ip IP to bind to, can be NULL |
ansond | 0:137634ff4186 | 67 | * \param port Port number to use |
ansond | 0:137634ff4186 | 68 | * |
ansond | 0:137634ff4186 | 69 | * \return 0 if successful, or one of: |
ansond | 0:137634ff4186 | 70 | * POLARSSL_ERR_NET_SOCKET_FAILED, |
ansond | 0:137634ff4186 | 71 | * POLARSSL_ERR_NET_BIND_FAILED, |
ansond | 0:137634ff4186 | 72 | * POLARSSL_ERR_NET_LISTEN_FAILED |
ansond | 0:137634ff4186 | 73 | */ |
ansond | 0:137634ff4186 | 74 | int net_bind( int *fd, const char *bind_ip, int port ); |
ansond | 0:137634ff4186 | 75 | |
ansond | 0:137634ff4186 | 76 | /** |
ansond | 0:137634ff4186 | 77 | * \brief Accept a connection from a remote client |
ansond | 0:137634ff4186 | 78 | * |
ansond | 0:137634ff4186 | 79 | * \param bind_fd Relevant socket |
ansond | 0:137634ff4186 | 80 | * \param client_fd Will contain the connected client socket |
ansond | 0:137634ff4186 | 81 | * \param client_ip Will contain the client IP address |
ansond | 0:137634ff4186 | 82 | * Must be at least 4 bytes, or 16 if IPv6 is supported |
ansond | 0:137634ff4186 | 83 | * |
ansond | 0:137634ff4186 | 84 | * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or |
ansond | 0:137634ff4186 | 85 | * POLARSSL_ERR_NET_WANT_READ is bind_fd was set to |
ansond | 0:137634ff4186 | 86 | * non-blocking and accept() is blocking. |
ansond | 0:137634ff4186 | 87 | */ |
ansond | 0:137634ff4186 | 88 | int net_accept( int bind_fd, int *client_fd, void *client_ip ); |
ansond | 0:137634ff4186 | 89 | |
ansond | 0:137634ff4186 | 90 | /** |
ansond | 0:137634ff4186 | 91 | * \brief Set the socket blocking |
ansond | 0:137634ff4186 | 92 | * |
ansond | 0:137634ff4186 | 93 | * \param fd Socket to set |
ansond | 0:137634ff4186 | 94 | * |
ansond | 0:137634ff4186 | 95 | * \return 0 if successful, or a non-zero error code |
ansond | 0:137634ff4186 | 96 | */ |
ansond | 0:137634ff4186 | 97 | int net_set_block( int fd ); |
ansond | 0:137634ff4186 | 98 | |
ansond | 0:137634ff4186 | 99 | /** |
ansond | 0:137634ff4186 | 100 | * \brief Set the socket non-blocking |
ansond | 0:137634ff4186 | 101 | * |
ansond | 0:137634ff4186 | 102 | * \param fd Socket to set |
ansond | 0:137634ff4186 | 103 | * |
ansond | 0:137634ff4186 | 104 | * \return 0 if successful, or a non-zero error code |
ansond | 0:137634ff4186 | 105 | */ |
ansond | 0:137634ff4186 | 106 | int net_set_nonblock( int fd ); |
ansond | 0:137634ff4186 | 107 | |
ansond | 0:137634ff4186 | 108 | /** |
ansond | 0:137634ff4186 | 109 | * \brief Portable usleep helper |
ansond | 0:137634ff4186 | 110 | * |
ansond | 0:137634ff4186 | 111 | * \param usec Amount of microseconds to sleep |
ansond | 0:137634ff4186 | 112 | * |
ansond | 0:137634ff4186 | 113 | * \note Real amount of time slept will not be less than |
ansond | 0:137634ff4186 | 114 | * select()'s timeout granularity (typically, 10ms). |
ansond | 0:137634ff4186 | 115 | */ |
ansond | 0:137634ff4186 | 116 | void net_usleep( unsigned long usec ); |
ansond | 0:137634ff4186 | 117 | |
ansond | 0:137634ff4186 | 118 | /** |
ansond | 0:137634ff4186 | 119 | * \brief Read at most 'len' characters. If no error occurs, |
ansond | 0:137634ff4186 | 120 | * the actual amount read is returned. |
ansond | 0:137634ff4186 | 121 | * |
ansond | 0:137634ff4186 | 122 | * \param ctx Socket |
ansond | 0:137634ff4186 | 123 | * \param buf The buffer to write to |
ansond | 0:137634ff4186 | 124 | * \param len Maximum length of the buffer |
ansond | 0:137634ff4186 | 125 | * |
ansond | 0:137634ff4186 | 126 | * \return This function returns the number of bytes received, |
ansond | 0:137634ff4186 | 127 | * or a non-zero error code; POLARSSL_ERR_NET_WANT_READ |
ansond | 0:137634ff4186 | 128 | * indicates read() is blocking. |
ansond | 0:137634ff4186 | 129 | */ |
ansond | 0:137634ff4186 | 130 | int net_recv( void *ctx, unsigned char *buf, size_t len ); |
ansond | 0:137634ff4186 | 131 | |
ansond | 0:137634ff4186 | 132 | /** |
ansond | 0:137634ff4186 | 133 | * \brief Write at most 'len' characters. If no error occurs, |
ansond | 0:137634ff4186 | 134 | * the actual amount read is returned. |
ansond | 0:137634ff4186 | 135 | * |
ansond | 0:137634ff4186 | 136 | * \param ctx Socket |
ansond | 0:137634ff4186 | 137 | * \param buf The buffer to read from |
ansond | 0:137634ff4186 | 138 | * \param len The length of the buffer |
ansond | 0:137634ff4186 | 139 | * |
ansond | 0:137634ff4186 | 140 | * \return This function returns the number of bytes sent, |
ansond | 0:137634ff4186 | 141 | * or a non-zero error code; POLARSSL_ERR_NET_WANT_WRITE |
ansond | 0:137634ff4186 | 142 | * indicates write() is blocking. |
ansond | 0:137634ff4186 | 143 | */ |
ansond | 0:137634ff4186 | 144 | int net_send( void *ctx, const unsigned char *buf, size_t len ); |
ansond | 0:137634ff4186 | 145 | |
ansond | 0:137634ff4186 | 146 | /** |
ansond | 0:137634ff4186 | 147 | * \brief Gracefully shutdown the connection |
ansond | 0:137634ff4186 | 148 | * |
ansond | 0:137634ff4186 | 149 | * \param fd The socket to close |
ansond | 0:137634ff4186 | 150 | */ |
ansond | 0:137634ff4186 | 151 | void net_close( int fd ); |
ansond | 0:137634ff4186 | 152 | |
ansond | 0:137634ff4186 | 153 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 154 | } |
ansond | 0:137634ff4186 | 155 | #endif |
ansond | 0:137634ff4186 | 156 | |
ansond | 0:137634ff4186 | 157 | #endif /* net.h */ |
ansond | 0:137634ff4186 | 158 |