blob: 80ee8bf0678ca5273ea23f5829084bbdb2fdcffa [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file net.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerfc8c4362010-03-21 17:37:16 +00004 * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00005 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Paul Bakker40e46942009-01-03 21:51:57 +000021#ifndef POLARSSL_NET_H
22#define POLARSSL_NET_H
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Paul Bakker3391b122009-07-28 20:11:54 +000024#define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0F00
25#define POLARSSL_ERR_NET_SOCKET_FAILED -0x0F10
26#define POLARSSL_ERR_NET_CONNECT_FAILED -0x0F20
27#define POLARSSL_ERR_NET_BIND_FAILED -0x0F30
28#define POLARSSL_ERR_NET_LISTEN_FAILED -0x0F40
29#define POLARSSL_ERR_NET_ACCEPT_FAILED -0x0F50
30#define POLARSSL_ERR_NET_RECV_FAILED -0x0F60
31#define POLARSSL_ERR_NET_SEND_FAILED -0x0F70
32#define POLARSSL_ERR_NET_CONN_RESET -0x0F80
33#define POLARSSL_ERR_NET_TRY_AGAIN -0x0F90
Paul Bakker5121ce52009-01-03 21:22:43 +000034
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/**
40 * \brief Initiate a TCP connection with host:port
41 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000042 * \param fd Socket to use
43 * \param host Host to connect to
44 * \param port Port to connect to
45 *
Paul Bakker5121ce52009-01-03 21:22:43 +000046 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000047 * POLARSSL_ERR_NET_SOCKET_FAILED,
48 * POLARSSL_ERR_NET_UNKNOWN_HOST,
49 * POLARSSL_ERR_NET_CONNECT_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000050 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000051int net_connect( int *fd, const char *host, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53/**
54 * \brief Create a listening socket on bind_ip:port.
55 * If bind_ip == NULL, all interfaces are binded.
56 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000057 * \param fd Socket to use
58 * \param bind_ip IP to bind to, can be NULL
59 * \param port Port number to use
60 *
Paul Bakker5121ce52009-01-03 21:22:43 +000061 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000062 * POLARSSL_ERR_NET_SOCKET_FAILED,
63 * POLARSSL_ERR_NET_BIND_FAILED,
64 * POLARSSL_ERR_NET_LISTEN_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000065 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000066int net_bind( int *fd, const char *bind_ip, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000067
68/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +000069 * \brief Accept a connection from a remote client
Paul Bakker5121ce52009-01-03 21:22:43 +000070 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000071 * \param bind_fd Relevant socket
72 * \param client_fd Will contain the connected client socket
73 * \param client_ip Will contain the client IP address
74 *
75 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
76 * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
77 * non-blocking and accept() is blocking.
Paul Bakker5121ce52009-01-03 21:22:43 +000078 */
79int net_accept( int bind_fd, int *client_fd, void *client_ip );
80
81/**
82 * \brief Set the socket blocking
83 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000084 * \param fd Socket to set
85 *
Paul Bakker5121ce52009-01-03 21:22:43 +000086 * \return 0 if successful, or a non-zero error code
87 */
88int net_set_block( int fd );
89
90/**
91 * \brief Set the socket non-blocking
92 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000093 * \param fd Socket to set
94 *
Paul Bakker5121ce52009-01-03 21:22:43 +000095 * \return 0 if successful, or a non-zero error code
96 */
97int net_set_nonblock( int fd );
98
99/**
100 * \brief Portable usleep helper
101 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000102 * \param usec Amount of microseconds to sleep
103 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 * \note Real amount of time slept will not be less than
105 * select()'s timeout granularity (typically, 10ms).
106 */
107void net_usleep( unsigned long usec );
108
109/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000110 * \brief Read at most 'len' characters. If no error occurs,
111 * the actual amount read is returned.
112 *
113 * \param ctx Socket
114 * \param buf The buffer to write to
115 * \param len Maximum length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 *
117 * \return This function returns the number of bytes received,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000118 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 * indicates read() is blocking.
120 */
121int net_recv( void *ctx, unsigned char *buf, int len );
122
123/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000124 * \brief Write at most 'len' characters. If no error occurs,
125 * the actual amount read is returned.
126 *
127 * \param ctx Socket
Paul Bakkerff60ee62010-03-16 21:09:09 +0000128 * \param buf The buffer to read from
129 * \param len The length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 *
131 * \return This function returns the number of bytes sent,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000132 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 * indicates write() is blocking.
134 */
135int net_send( void *ctx, unsigned char *buf, int len );
136
137/**
138 * \brief Gracefully shutdown the connection
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000139 *
140 * \param fd The socket to close
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
142void net_close( int fd );
143
144#ifdef __cplusplus
145}
146#endif
147
148#endif /* net.h */