blob: f135f911eceb8c8b72f0dcafc8eb5216ede54b82 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file net.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
Paul Bakker40e46942009-01-03 21:51:57 +000022#ifndef POLARSSL_NET_H
23#define POLARSSL_NET_H
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Paul Bakker40e46942009-01-03 21:51:57 +000025#define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0F00
26#define POLARSSL_ERR_NET_SOCKET_FAILED -0x0F10
27#define POLARSSL_ERR_NET_CONNECT_FAILED -0x0F20
28#define POLARSSL_ERR_NET_BIND_FAILED -0x0F30
29#define POLARSSL_ERR_NET_LISTEN_FAILED -0x0F40
30#define POLARSSL_ERR_NET_ACCEPT_FAILED -0x0F50
31#define POLARSSL_ERR_NET_RECV_FAILED -0x0F60
32#define POLARSSL_ERR_NET_SEND_FAILED -0x0F70
33#define POLARSSL_ERR_NET_CONN_RESET -0x0F80
34#define POLARSSL_ERR_NET_TRY_AGAIN -0x0F90
Paul Bakker5121ce52009-01-03 21:22:43 +000035
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/**
41 * \brief Initiate a TCP connection with host:port
42 *
43 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000044 * POLARSSL_ERR_NET_SOCKET_FAILED,
45 * POLARSSL_ERR_NET_UNKNOWN_HOST,
46 * POLARSSL_ERR_NET_CONNECT_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48int net_connect( int *fd, char *host, int port );
49
50/**
51 * \brief Create a listening socket on bind_ip:port.
52 * If bind_ip == NULL, all interfaces are binded.
53 *
54 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000055 * POLARSSL_ERR_NET_SOCKET_FAILED,
56 * POLARSSL_ERR_NET_BIND_FAILED,
57 * POLARSSL_ERR_NET_LISTEN_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000058 */
59int net_bind( int *fd, char *bind_ip, int port );
60
61/**
62 * \brief Accept a connection from a remote client
63 *
Paul Bakker40e46942009-01-03 21:51:57 +000064 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
65 * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
Paul Bakker5121ce52009-01-03 21:22:43 +000066 * non-blocking and accept() is blocking.
67 */
68int net_accept( int bind_fd, int *client_fd, void *client_ip );
69
70/**
71 * \brief Set the socket blocking
72 *
73 * \return 0 if successful, or a non-zero error code
74 */
75int net_set_block( int fd );
76
77/**
78 * \brief Set the socket non-blocking
79 *
80 * \return 0 if successful, or a non-zero error code
81 */
82int net_set_nonblock( int fd );
83
84/**
85 * \brief Portable usleep helper
86 *
87 * \note Real amount of time slept will not be less than
88 * select()'s timeout granularity (typically, 10ms).
89 */
90void net_usleep( unsigned long usec );
91
92/**
93 * \brief Read at most 'len' characters. len is updated to
94 * reflect the actual number of characters read.
95 *
96 * \return This function returns the number of bytes received,
Paul Bakker40e46942009-01-03 21:51:57 +000097 * or a negative error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +000098 * indicates read() is blocking.
99 */
100int net_recv( void *ctx, unsigned char *buf, int len );
101
102/**
103 * \brief Write at most 'len' characters. len is updated to
104 * reflect the number of characters _not_ written.
105 *
106 * \return This function returns the number of bytes sent,
Paul Bakker40e46942009-01-03 21:51:57 +0000107 * or a negative error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 * indicates write() is blocking.
109 */
110int net_send( void *ctx, unsigned char *buf, int len );
111
112/**
113 * \brief Gracefully shutdown the connection
114 */
115void net_close( int fd );
116
117#ifdef __cplusplus
118}
119#endif
120
121#endif /* net.h */