blob: 188432e9adcc126f302ea7de071bb2319c4ce024 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file net.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief MD5 message digest algorithm (hash function)
5 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_NET_H
28#define POLARSSL_NET_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker3391b122009-07-28 20:11:54 +000030#define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0F00
31#define POLARSSL_ERR_NET_SOCKET_FAILED -0x0F10
32#define POLARSSL_ERR_NET_CONNECT_FAILED -0x0F20
33#define POLARSSL_ERR_NET_BIND_FAILED -0x0F30
34#define POLARSSL_ERR_NET_LISTEN_FAILED -0x0F40
35#define POLARSSL_ERR_NET_ACCEPT_FAILED -0x0F50
36#define POLARSSL_ERR_NET_RECV_FAILED -0x0F60
37#define POLARSSL_ERR_NET_SEND_FAILED -0x0F70
38#define POLARSSL_ERR_NET_CONN_RESET -0x0F80
39#define POLARSSL_ERR_NET_TRY_AGAIN -0x0F90
Paul Bakker5121ce52009-01-03 21:22:43 +000040
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/**
46 * \brief Initiate a TCP connection with host:port
47 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000048 * \param fd Socket to use
49 * \param host Host to connect to
50 * \param port Port to connect to
51 *
Paul Bakker5121ce52009-01-03 21:22:43 +000052 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000053 * POLARSSL_ERR_NET_SOCKET_FAILED,
54 * POLARSSL_ERR_NET_UNKNOWN_HOST,
55 * POLARSSL_ERR_NET_CONNECT_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000056 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000057int net_connect( int *fd, const char *host, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000058
59/**
60 * \brief Create a listening socket on bind_ip:port.
61 * If bind_ip == NULL, all interfaces are binded.
62 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000063 * \param fd Socket to use
64 * \param bind_ip IP to bind to, can be NULL
65 * \param port Port number to use
66 *
Paul Bakker5121ce52009-01-03 21:22:43 +000067 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000068 * POLARSSL_ERR_NET_SOCKET_FAILED,
69 * POLARSSL_ERR_NET_BIND_FAILED,
70 * POLARSSL_ERR_NET_LISTEN_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000071 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000072int net_bind( int *fd, const char *bind_ip, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +000075 * \brief Accept a connection from a remote client
Paul Bakker5121ce52009-01-03 21:22:43 +000076 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000077 * \param bind_fd Relevant socket
78 * \param client_fd Will contain the connected client socket
79 * \param client_ip Will contain the client IP address
80 *
81 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
82 * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
83 * non-blocking and accept() is blocking.
Paul Bakker5121ce52009-01-03 21:22:43 +000084 */
85int net_accept( int bind_fd, int *client_fd, void *client_ip );
86
87/**
88 * \brief Set the socket blocking
89 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000090 * \param fd Socket to set
91 *
Paul Bakker5121ce52009-01-03 21:22:43 +000092 * \return 0 if successful, or a non-zero error code
93 */
94int net_set_block( int fd );
95
96/**
97 * \brief Set the socket non-blocking
98 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000099 * \param fd Socket to set
100 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 * \return 0 if successful, or a non-zero error code
102 */
103int net_set_nonblock( int fd );
104
105/**
106 * \brief Portable usleep helper
107 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000108 * \param usec Amount of microseconds to sleep
109 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 * \note Real amount of time slept will not be less than
111 * select()'s timeout granularity (typically, 10ms).
112 */
113void net_usleep( unsigned long usec );
114
115/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000116 * \brief Read at most 'len' characters. If no error occurs,
117 * the actual amount read is returned.
118 *
119 * \param ctx Socket
120 * \param buf The buffer to write to
121 * \param len Maximum length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 *
123 * \return This function returns the number of bytes received,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000124 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 * indicates read() is blocking.
126 */
127int net_recv( void *ctx, unsigned char *buf, int len );
128
129/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000130 * \brief Write at most 'len' characters. If no error occurs,
131 * the actual amount read is returned.
132 *
133 * \param ctx Socket
Paul Bakkerff60ee62010-03-16 21:09:09 +0000134 * \param buf The buffer to read from
135 * \param len The length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 *
137 * \return This function returns the number of bytes sent,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000138 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 * indicates write() is blocking.
140 */
141int net_send( void *ctx, unsigned char *buf, int len );
142
143/**
144 * \brief Gracefully shutdown the connection
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000145 *
146 * \param fd The socket to close
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 */
148void net_close( int fd );
149
150#ifdef __cplusplus
151}
152#endif
153
154#endif /* net.h */