blob: 1af6eb8486e5cbd9a1b3ffd6e5f3859c78d60105 [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.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
Paul Bakker40e46942009-01-03 21:51:57 +000025#ifndef POLARSSL_NET_H
26#define POLARSSL_NET_H
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker3391b122009-07-28 20:11:54 +000028#define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0F00
29#define POLARSSL_ERR_NET_SOCKET_FAILED -0x0F10
30#define POLARSSL_ERR_NET_CONNECT_FAILED -0x0F20
31#define POLARSSL_ERR_NET_BIND_FAILED -0x0F30
32#define POLARSSL_ERR_NET_LISTEN_FAILED -0x0F40
33#define POLARSSL_ERR_NET_ACCEPT_FAILED -0x0F50
34#define POLARSSL_ERR_NET_RECV_FAILED -0x0F60
35#define POLARSSL_ERR_NET_SEND_FAILED -0x0F70
36#define POLARSSL_ERR_NET_CONN_RESET -0x0F80
37#define POLARSSL_ERR_NET_TRY_AGAIN -0x0F90
Paul Bakker5121ce52009-01-03 21:22:43 +000038
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/**
44 * \brief Initiate a TCP connection with host:port
45 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000046 * \param fd Socket to use
47 * \param host Host to connect to
48 * \param port Port to connect to
49 *
Paul Bakker5121ce52009-01-03 21:22:43 +000050 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000051 * POLARSSL_ERR_NET_SOCKET_FAILED,
52 * POLARSSL_ERR_NET_UNKNOWN_HOST,
53 * POLARSSL_ERR_NET_CONNECT_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000054 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000055int net_connect( int *fd, const char *host, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000056
57/**
58 * \brief Create a listening socket on bind_ip:port.
59 * If bind_ip == NULL, all interfaces are binded.
60 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000061 * \param fd Socket to use
62 * \param bind_ip IP to bind to, can be NULL
63 * \param port Port number to use
64 *
Paul Bakker5121ce52009-01-03 21:22:43 +000065 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000066 * POLARSSL_ERR_NET_SOCKET_FAILED,
67 * POLARSSL_ERR_NET_BIND_FAILED,
68 * POLARSSL_ERR_NET_LISTEN_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000069 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000070int net_bind( int *fd, const char *bind_ip, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000071
72/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +000073 * \brief Accept a connection from a remote client
Paul Bakker5121ce52009-01-03 21:22:43 +000074 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000075 * \param bind_fd Relevant socket
76 * \param client_fd Will contain the connected client socket
77 * \param client_ip Will contain the client IP address
78 *
79 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
80 * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
81 * non-blocking and accept() is blocking.
Paul Bakker5121ce52009-01-03 21:22:43 +000082 */
83int net_accept( int bind_fd, int *client_fd, void *client_ip );
84
85/**
86 * \brief Set the socket blocking
87 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000088 * \param fd Socket to set
89 *
Paul Bakker5121ce52009-01-03 21:22:43 +000090 * \return 0 if successful, or a non-zero error code
91 */
92int net_set_block( int fd );
93
94/**
95 * \brief Set the socket non-blocking
96 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000097 * \param fd Socket to set
98 *
Paul Bakker5121ce52009-01-03 21:22:43 +000099 * \return 0 if successful, or a non-zero error code
100 */
101int net_set_nonblock( int fd );
102
103/**
104 * \brief Portable usleep helper
105 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000106 * \param usec Amount of microseconds to sleep
107 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 * \note Real amount of time slept will not be less than
109 * select()'s timeout granularity (typically, 10ms).
110 */
111void net_usleep( unsigned long usec );
112
113/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000114 * \brief Read at most 'len' characters. If no error occurs,
115 * the actual amount read is returned.
116 *
117 * \param ctx Socket
118 * \param buf The buffer to write to
119 * \param len Maximum length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 *
121 * \return This function returns the number of bytes received,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000122 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 * indicates read() is blocking.
124 */
125int net_recv( void *ctx, unsigned char *buf, int len );
126
127/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000128 * \brief Write at most 'len' characters. If no error occurs,
129 * the actual amount read is returned.
130 *
131 * \param ctx Socket
Paul Bakkerff60ee62010-03-16 21:09:09 +0000132 * \param buf The buffer to read from
133 * \param len The length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 *
135 * \return This function returns the number of bytes sent,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000136 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 * indicates write() is blocking.
138 */
139int net_send( void *ctx, unsigned char *buf, int len );
140
141/**
142 * \brief Gracefully shutdown the connection
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000143 *
144 * \param fd The socket to close
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
146void net_close( int fd );
147
148#ifdef __cplusplus
149}
150#endif
151
152#endif /* net.h */