blob: d828a3e958b29d0ffeed92491554b7c1446f9e3b [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 Bakker23986e52011-04-24 08:57:21 +000030#include <string.h>
31
Paul Bakker9d781402011-05-09 16:17:09 +000032#define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0040 /**< Failed to get an IP address for the given hostname. */
33#define POLARSSL_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */
34#define POLARSSL_ERR_NET_CONNECT_FAILED -0x0044 /**< The connection to the given server / port failed. */
35#define POLARSSL_ERR_NET_BIND_FAILED -0x0046 /**< Binding of the socket failed. */
36#define POLARSSL_ERR_NET_LISTEN_FAILED -0x0048 /**< Could not listen on the socket. */
37#define POLARSSL_ERR_NET_ACCEPT_FAILED -0x004A /**< Could not accept the incoming connection. */
38#define POLARSSL_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */
39#define POLARSSL_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */
40#define POLARSSL_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */
41#define POLARSSL_ERR_NET_TRY_AGAIN -0x0052 /**< Connection was busy, try again. */
Paul Bakker5121ce52009-01-03 21:22:43 +000042
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47/**
48 * \brief Initiate a TCP connection with host:port
49 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000050 * \param fd Socket to use
51 * \param host Host to connect to
52 * \param port Port to connect to
53 *
Paul Bakker5121ce52009-01-03 21:22:43 +000054 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000055 * POLARSSL_ERR_NET_SOCKET_FAILED,
56 * POLARSSL_ERR_NET_UNKNOWN_HOST,
57 * POLARSSL_ERR_NET_CONNECT_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000058 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000059int net_connect( int *fd, const char *host, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61/**
62 * \brief Create a listening socket on bind_ip:port.
63 * If bind_ip == NULL, all interfaces are binded.
64 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000065 * \param fd Socket to use
66 * \param bind_ip IP to bind to, can be NULL
67 * \param port Port number to use
68 *
Paul Bakker5121ce52009-01-03 21:22:43 +000069 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000070 * POLARSSL_ERR_NET_SOCKET_FAILED,
71 * POLARSSL_ERR_NET_BIND_FAILED,
72 * POLARSSL_ERR_NET_LISTEN_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000073 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000074int net_bind( int *fd, const char *bind_ip, int port );
Paul Bakker5121ce52009-01-03 21:22:43 +000075
76/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +000077 * \brief Accept a connection from a remote client
Paul Bakker5121ce52009-01-03 21:22:43 +000078 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000079 * \param bind_fd Relevant socket
80 * \param client_fd Will contain the connected client socket
81 * \param client_ip Will contain the client IP address
82 *
83 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
84 * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
85 * non-blocking and accept() is blocking.
Paul Bakker5121ce52009-01-03 21:22:43 +000086 */
87int net_accept( int bind_fd, int *client_fd, void *client_ip );
88
89/**
90 * \brief Set the socket blocking
91 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000092 * \param fd Socket to set
93 *
Paul Bakker5121ce52009-01-03 21:22:43 +000094 * \return 0 if successful, or a non-zero error code
95 */
96int net_set_block( int fd );
97
98/**
99 * \brief Set the socket non-blocking
100 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000101 * \param fd Socket to set
102 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 * \return 0 if successful, or a non-zero error code
104 */
105int net_set_nonblock( int fd );
106
107/**
108 * \brief Portable usleep helper
109 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000110 * \param usec Amount of microseconds to sleep
111 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 * \note Real amount of time slept will not be less than
113 * select()'s timeout granularity (typically, 10ms).
114 */
115void net_usleep( unsigned long usec );
116
117/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000118 * \brief Read at most 'len' characters. If no error occurs,
119 * the actual amount read is returned.
120 *
121 * \param ctx Socket
122 * \param buf The buffer to write to
123 * \param len Maximum length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 *
125 * \return This function returns the number of bytes received,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000126 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 * indicates read() is blocking.
128 */
Paul Bakker23986e52011-04-24 08:57:21 +0000129int net_recv( void *ctx, unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000132 * \brief Write at most 'len' characters. If no error occurs,
133 * the actual amount read is returned.
134 *
135 * \param ctx Socket
Paul Bakkerff60ee62010-03-16 21:09:09 +0000136 * \param buf The buffer to read from
137 * \param len The length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 *
139 * \return This function returns the number of bytes sent,
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000140 * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 * indicates write() is blocking.
142 */
Paul Bakker23986e52011-04-24 08:57:21 +0000143int net_send( void *ctx, unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145/**
146 * \brief Gracefully shutdown the connection
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000147 *
148 * \param fd The socket to close
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 */
150void net_close( int fd );
151
152#ifdef __cplusplus
153}
154#endif
155
156#endif /* net.h */