4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef TROPICSSL_NET_H
36 #define TROPICSSL_NET_H
38 #define TROPICSSL_ERR_NET_UNKNOWN_HOST -0x0F00
39 #define TROPICSSL_ERR_NET_SOCKET_FAILED -0x0F10
40 #define TROPICSSL_ERR_NET_CONNECT_FAILED -0x0F20
41 #define TROPICSSL_ERR_NET_BIND_FAILED -0x0F30
42 #define TROPICSSL_ERR_NET_LISTEN_FAILED -0x0F40
43 #define TROPICSSL_ERR_NET_ACCEPT_FAILED -0x0F50
44 #define TROPICSSL_ERR_NET_RECV_FAILED -0x0F60
45 #define TROPICSSL_ERR_NET_SEND_FAILED -0x0F70
46 #define TROPICSSL_ERR_NET_CONN_RESET -0x0F80
47 #define TROPICSSL_ERR_NET_TRY_AGAIN -0x0F90
54 * \brief Initiate a TCP connection with host:port
56 * \return 0 if successful, or one of:
57 * TROPICSSL_ERR_NET_SOCKET_FAILED,
58 * TROPICSSL_ERR_NET_UNKNOWN_HOST,
59 * TROPICSSL_ERR_NET_CONNECT_FAILED
61 int net_connect(int *fd
, const char *host
, int port
);
64 * \brief Create a listening socket on bind_ip:port.
65 * If bind_ip == NULL, all interfaces are binded.
67 * \return 0 if successful, or one of:
68 * TROPICSSL_ERR_NET_SOCKET_FAILED,
69 * TROPICSSL_ERR_NET_BIND_FAILED,
70 * TROPICSSL_ERR_NET_LISTEN_FAILED
72 int net_bind(int *fd
, const char *bind_ip
, int port
);
75 * \brief Accept a connection from a remote client
77 * \return 0 if successful, TROPICSSL_ERR_NET_ACCEPT_FAILED, or
78 * TROPICSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
79 * non-blocking and accept() is blocking.
81 int net_accept(int bind_fd
, int *client_fd
, void *client_ip
);
84 * \brief Set the socket blocking
86 * \return 0 if successful, or a non-zero error code
88 int net_set_block(int fd
);
91 * \brief Set the socket non-blocking
93 * \return 0 if successful, or a non-zero error code
95 int net_set_nonblock(int fd
);
98 * \brief Portable usleep helper
100 * \note Real amount of time slept will not be less than
101 * select()'s timeout granularity (typically, 10ms).
103 void net_usleep(unsigned long usec
);
106 * \brief Read at most 'len' characters. len is updated to
107 * reflect the actual number of characters read.
109 * \return This function returns the number of bytes received,
110 * or a negative error code; TROPICSSL_ERR_NET_TRY_AGAIN
111 * indicates read() is blocking.
113 int net_recv(void *ctx
, unsigned char *buf
, int len
);
116 * \brief Write at most 'len' characters. len is updated to
117 * reflect the number of characters _not_ written.
119 * \return This function returns the number of bytes sent,
120 * or a negative error code; TROPICSSL_ERR_NET_TRY_AGAIN
121 * indicates write() is blocking.
123 int net_send(void *ctx
, const unsigned char *buf
, int len
);
126 * \brief Gracefully shutdown the connection
128 void net_close(int fd
);