blob: eff53cb5b77fc94c8e1b7fde84e21003bee0eda7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file net.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Paul Bakker40e46942009-01-03 21:51:57 +000023#ifndef POLARSSL_NET_H
24#define POLARSSL_NET_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Paul Bakker3391b122009-07-28 20:11:54 +000026#define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0F00
27#define POLARSSL_ERR_NET_SOCKET_FAILED -0x0F10
28#define POLARSSL_ERR_NET_CONNECT_FAILED -0x0F20
29#define POLARSSL_ERR_NET_BIND_FAILED -0x0F30
30#define POLARSSL_ERR_NET_LISTEN_FAILED -0x0F40
31#define POLARSSL_ERR_NET_ACCEPT_FAILED -0x0F50
32#define POLARSSL_ERR_NET_RECV_FAILED -0x0F60
33#define POLARSSL_ERR_NET_SEND_FAILED -0x0F70
34#define POLARSSL_ERR_NET_CONN_RESET -0x0F80
35#define POLARSSL_ERR_NET_TRY_AGAIN -0x0F90
Paul Bakker5121ce52009-01-03 21:22:43 +000036
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42 * \brief Initiate a TCP connection with host:port
43 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000044 * \param fd Socket to use
45 * \param host Host to connect to
46 * \param port Port to connect to
47 *
Paul Bakker5121ce52009-01-03 21:22:43 +000048 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000049 * POLARSSL_ERR_NET_SOCKET_FAILED,
50 * POLARSSL_ERR_NET_UNKNOWN_HOST,
51 * POLARSSL_ERR_NET_CONNECT_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000052 */
53int net_connect( int *fd, char *host, int port );
54
55/**
56 * \brief Create a listening socket on bind_ip:port.
57 * If bind_ip == NULL, all interfaces are binded.
58 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000059 * \param fd Socket to use
60 * \param bind_ip IP to bind to, can be NULL
61 * \param port Port number to use
62 *
Paul Bakker5121ce52009-01-03 21:22:43 +000063 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000064 * POLARSSL_ERR_NET_SOCKET_FAILED,
65 * POLARSSL_ERR_NET_BIND_FAILED,
66 * POLARSSL_ERR_NET_LISTEN_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000067 */
68int net_bind( int *fd, char *bind_ip, int port );
69
70/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +000071 * \brief Accept a connection from a remote client
Paul Bakker5121ce52009-01-03 21:22:43 +000072 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000073 * \param bind_fd Relevant socket
74 * \param client_fd Will contain the connected client socket
75 * \param client_ip Will contain the client IP address
76 *
77 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
78 * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
79 * non-blocking and accept() is blocking.
Paul Bakker5121ce52009-01-03 21:22:43 +000080 */
81int net_accept( int bind_fd, int *client_fd, void *client_ip );
82
83/**
84 * \brief Set the socket blocking
85 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000086 * \param fd Socket to set
87 *
Paul Bakker5121ce52009-01-03 21:22:43 +000088 * \return 0 if successful, or a non-zero error code
89 */
90int net_set_block( int fd );
91
92/**
93 * \brief Set the socket non-blocking
94 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000095 * \param fd Socket to set
96 *
Paul Bakker5121ce52009-01-03 21:22:43 +000097 * \return 0 if successful, or a non-zero error code
98 */
99int net_set_nonblock( int fd );
100
101/**
102 * \brief Portable usleep helper
103 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000104 * \param usec Amount of microseconds to sleep
105 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 * \note Real amount of time slept will not be less than
107 * select()'s timeout granularity (typically, 10ms).
108 */
109void net_usleep( unsigned long usec );
110
111/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000112 * \brief Read at most 'len' characters. If no error occurs,
113 * the actual amount read is returned.
114 *
115 * \param ctx Socket
116 * \param buf The buffer to write to
117 * \param len Maximum length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 *
119 * \return This function returns the number of bytes received,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000120 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 * indicates read() is blocking.
122 */
123int net_recv( void *ctx, unsigned char *buf, int len );
124
125/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000126 * \brief Write at most 'len' characters. If no error occurs,
127 * the actual amount read is returned.
128 *
129 * \param ctx Socket
130 * \param buf The buffer to write to
131 * \param len Maximum length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 *
133 * \return This function returns the number of bytes sent,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000134 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * indicates write() is blocking.
136 */
137int net_send( void *ctx, unsigned char *buf, int len );
138
139/**
140 * \brief Gracefully shutdown the connection
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000141 *
142 * \param fd The socket to close
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 */
144void net_close( int fd );
145
146#ifdef __cplusplus
147}
148#endif
149
150#endif /* net.h */