blob: 9e52e57c9062ce92d4a1a19edfe75926b715561a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file net.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 * 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 Bakker3391b122009-07-28 20:11:54 +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 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000043 * \param fd Socket to use
44 * \param host Host to connect to
45 * \param port Port to connect to
46 *
Paul Bakker5121ce52009-01-03 21:22:43 +000047 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000048 * POLARSSL_ERR_NET_SOCKET_FAILED,
49 * POLARSSL_ERR_NET_UNKNOWN_HOST,
50 * POLARSSL_ERR_NET_CONNECT_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000051 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000052int net_connect( int *fd, const char *host, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000053
54/**
55 * \brief Create a listening socket on bind_ip:port.
56 * If bind_ip == NULL, all interfaces are binded.
57 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000058 * \param fd Socket to use
59 * \param bind_ip IP to bind to, can be NULL
60 * \param port Port number to use
61 *
Paul Bakker5121ce52009-01-03 21:22:43 +000062 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000063 * POLARSSL_ERR_NET_SOCKET_FAILED,
64 * POLARSSL_ERR_NET_BIND_FAILED,
65 * POLARSSL_ERR_NET_LISTEN_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000066 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000067int net_bind( int *fd, const char *bind_ip, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000068
69/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +000070 * \brief Accept a connection from a remote client
Paul Bakker5121ce52009-01-03 21:22:43 +000071 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000072 * \param bind_fd Relevant socket
73 * \param client_fd Will contain the connected client socket
74 * \param client_ip Will contain the client IP address
75 *
76 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
77 * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
78 * non-blocking and accept() is blocking.
Paul Bakker5121ce52009-01-03 21:22:43 +000079 */
80int net_accept( int bind_fd, int *client_fd, void *client_ip );
81
82/**
83 * \brief Set the socket blocking
84 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000085 * \param fd Socket to set
86 *
Paul Bakker5121ce52009-01-03 21:22:43 +000087 * \return 0 if successful, or a non-zero error code
88 */
89int net_set_block( int fd );
90
91/**
92 * \brief Set the socket non-blocking
93 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000094 * \param fd Socket to set
95 *
Paul Bakker5121ce52009-01-03 21:22:43 +000096 * \return 0 if successful, or a non-zero error code
97 */
98int net_set_nonblock( int fd );
99
100/**
101 * \brief Portable usleep helper
102 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000103 * \param usec Amount of microseconds to sleep
104 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 * \note Real amount of time slept will not be less than
106 * select()'s timeout granularity (typically, 10ms).
107 */
108void net_usleep( unsigned long usec );
109
110/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000111 * \brief Read at most 'len' characters. If no error occurs,
112 * the actual amount read is returned.
113 *
114 * \param ctx Socket
115 * \param buf The buffer to write to
116 * \param len Maximum length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 *
118 * \return This function returns the number of bytes received,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000119 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 * indicates read() is blocking.
121 */
122int net_recv( void *ctx, unsigned char *buf, int len );
123
124/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000125 * \brief Write at most 'len' characters. If no error occurs,
126 * the actual amount read is returned.
127 *
128 * \param ctx Socket
Paul Bakkerff60ee62010-03-16 21:09:09 +0000129 * \param buf The buffer to read from
130 * \param len The length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 *
132 * \return This function returns the number of bytes sent,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000133 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 * indicates write() is blocking.
135 */
136int net_send( void *ctx, unsigned char *buf, int len );
137
138/**
139 * \brief Gracefully shutdown the connection
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000140 *
141 * \param fd The socket to close
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
143void net_close( int fd );
144
145#ifdef __cplusplus
146}
147#endif
148
149#endif /* net.h */