blob: ae7f69fb5d5cd6ab5a10eaf7ee827ad6aabe3b80 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file net.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * \brief Network communication functions
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00008 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_NET_H
25#define POLARSSL_NET_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker23986e52011-04-24 08:57:21 +000027#include <string.h>
28
Paul Bakkerbdb912d2012-02-13 23:11:30 +000029#define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0056 /**< Failed to get an IP address for the given hostname. */
Paul Bakker9d781402011-05-09 16:17:09 +000030#define POLARSSL_ERR_NET_SOCKET_FAILED -0x0042 /**< Failed to open a socket. */
31#define POLARSSL_ERR_NET_CONNECT_FAILED -0x0044 /**< The connection to the given server / port failed. */
32#define POLARSSL_ERR_NET_BIND_FAILED -0x0046 /**< Binding of the socket failed. */
33#define POLARSSL_ERR_NET_LISTEN_FAILED -0x0048 /**< Could not listen on the socket. */
34#define POLARSSL_ERR_NET_ACCEPT_FAILED -0x004A /**< Could not accept the incoming connection. */
35#define POLARSSL_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */
36#define POLARSSL_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */
37#define POLARSSL_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */
Paul Bakker831a7552011-05-18 13:32:51 +000038#define POLARSSL_ERR_NET_WANT_READ -0x0052 /**< Connection requires a read call. */
39#define POLARSSL_ERR_NET_WANT_WRITE -0x0054 /**< Connection requires a write call. */
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker192381a2011-05-20 12:31:31 +000041#define POLARSSL_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043#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
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +010082 * Must be at least 4 bytes, or 16 if IPv6 is supported
Paul Bakker13e2dfe2009-07-28 07:18:38 +000083 *
84 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +010085 * POLARSSL_ERR_NET_WANT_READ is bind_fd was set to
Paul Bakker13e2dfe2009-07-28 07:18:38 +000086 * non-blocking and accept() is blocking.
Paul Bakker5121ce52009-01-03 21:22:43 +000087 */
88int net_accept( int bind_fd, int *client_fd, void *client_ip );
89
90/**
91 * \brief Set the socket 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_block( int fd );
98
99/**
100 * \brief Set the socket non-blocking
101 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000102 * \param fd Socket to set
103 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 * \return 0 if successful, or a non-zero error code
105 */
106int net_set_nonblock( int fd );
107
108/**
109 * \brief Portable usleep helper
110 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000111 * \param usec Amount of microseconds to sleep
112 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 * \note Real amount of time slept will not be less than
114 * select()'s timeout granularity (typically, 10ms).
115 */
116void net_usleep( unsigned long usec );
117
118/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000119 * \brief Read at most 'len' characters. If no error occurs,
120 * the actual amount read is returned.
121 *
122 * \param ctx Socket
123 * \param buf The buffer to write to
124 * \param len Maximum length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 *
126 * \return This function returns the number of bytes received,
Paul Bakker831a7552011-05-18 13:32:51 +0000127 * or a non-zero error code; POLARSSL_ERR_NET_WANT_READ
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 * indicates read() is blocking.
129 */
Paul Bakker23986e52011-04-24 08:57:21 +0000130int net_recv( void *ctx, unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000133 * \brief Write at most 'len' characters. If no error occurs,
134 * the actual amount read is returned.
135 *
136 * \param ctx Socket
Paul Bakkerff60ee62010-03-16 21:09:09 +0000137 * \param buf The buffer to read from
138 * \param len The length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 *
140 * \return This function returns the number of bytes sent,
Paul Bakker831a7552011-05-18 13:32:51 +0000141 * or a non-zero error code; POLARSSL_ERR_NET_WANT_WRITE
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 * indicates write() is blocking.
143 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000144int net_send( void *ctx, const unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
147 * \brief Gracefully shutdown the connection
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000148 *
149 * \param fd The socket to close
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 */
151void net_close( int fd );
152
153#ifdef __cplusplus
154}
155#endif
156
157#endif /* net.h */