blob: 4271a7ce443ca131685fa9edf28f8443a6875cb8 [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 *
Paul Bakkercce9d772011-11-18 14:26:47 +00006 * Copyright (C) 2006-2011, 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 Bakkerbdb912d2012-02-13 23:11:30 +000032#define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0056 /**< Failed to get an IP address for the given hostname. */
Paul Bakker9d781402011-05-09 16:17:09 +000033#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. */
Paul Bakker831a7552011-05-18 13:32:51 +000041#define POLARSSL_ERR_NET_WANT_READ -0x0052 /**< Connection requires a read call. */
42#define POLARSSL_ERR_NET_WANT_WRITE -0x0054 /**< Connection requires a write call. */
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Paul Bakker192381a2011-05-20 12:31:31 +000044#define POLARSSL_NET_LISTEN_BACKLOG 10 /**< The backlog that listen() should use. */
45
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +010046#define NET_PROTO_TCP 0 /**< The TCP transport protocol */
47#define NET_PROTO_UDP 1 /**< The UDP transport protocol */
48
Paul Bakker5121ce52009-01-03 21:22:43 +000049#ifdef __cplusplus
50extern "C" {
51#endif
52
53/**
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +010054 * \brief Initiate a connection with host:port in the given protocol
Paul Bakker5121ce52009-01-03 21:22:43 +000055 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000056 * \param fd Socket to use
57 * \param host Host to connect to
58 * \param port Port to connect to
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +010059 * \param proto Protocol: NET_PROTO_TCP or NET_PROTO_UDP
Paul Bakker13e2dfe2009-07-28 07:18:38 +000060 *
Paul Bakker5121ce52009-01-03 21:22:43 +000061 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000062 * POLARSSL_ERR_NET_SOCKET_FAILED,
63 * POLARSSL_ERR_NET_UNKNOWN_HOST,
64 * POLARSSL_ERR_NET_CONNECT_FAILED
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +010065 *
66 * \note Sets the socket in connected mode even with UDP.
Paul Bakker5121ce52009-01-03 21:22:43 +000067 */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +010068int net_connect( int *fd, const char *host, int port, int proto );
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70/**
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +010071 * \brief Create a receiving socket on bind_ip:port in the chosen
72 * protocol. If bind_ip == NULL, all interfaces are bound.
Paul Bakker5121ce52009-01-03 21:22:43 +000073 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000074 * \param fd Socket to use
75 * \param bind_ip IP to bind to, can be NULL
76 * \param port Port number to use
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +010077 * \param proto Protocol: NET_PROTO_TCP or NET_PROTO_UDP
Paul Bakker13e2dfe2009-07-28 07:18:38 +000078 *
Paul Bakker5121ce52009-01-03 21:22:43 +000079 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000080 * POLARSSL_ERR_NET_SOCKET_FAILED,
81 * POLARSSL_ERR_NET_BIND_FAILED,
82 * POLARSSL_ERR_NET_LISTEN_FAILED
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +010083 *
84 * \note Regardless of the protocol, opens the sockets and binds it.
85 * In addition, make the socket listening if protocol is TCP.
Paul Bakker5121ce52009-01-03 21:22:43 +000086 */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +010087int net_bind( int *fd, const char *bind_ip, int port, int proto );
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +000090 * \brief Accept a connection from a remote client
Paul Bakker5121ce52009-01-03 21:22:43 +000091 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000092 * \param bind_fd Relevant socket
93 * \param client_fd Will contain the connected client socket
94 * \param client_ip Will contain the client IP address
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +010095 * Must be at least 4 bytes, or 16 if IPv6 is supported
Paul Bakker13e2dfe2009-07-28 07:18:38 +000096 *
97 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +010098 * POLARSSL_ERR_NET_WANT_READ is bind_fd was set to
Paul Bakker13e2dfe2009-07-28 07:18:38 +000099 * non-blocking and accept() is blocking.
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100100 *
101 * \note With UDP, connects the bind_fd to the client and just copy
102 * its descriptor to client_fd. New clients will not be able
103 * to connect until you close the socket and bind a new one.
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 */
105int net_accept( int bind_fd, int *client_fd, void *client_ip );
106
107/**
108 * \brief Set the socket blocking
109 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000110 * \param fd Socket to set
111 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 * \return 0 if successful, or a non-zero error code
113 */
114int net_set_block( int fd );
115
116/**
117 * \brief Set the socket non-blocking
118 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000119 * \param fd Socket to set
120 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 * \return 0 if successful, or a non-zero error code
122 */
123int net_set_nonblock( int fd );
124
125/**
126 * \brief Portable usleep helper
127 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000128 * \param usec Amount of microseconds to sleep
129 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 * \note Real amount of time slept will not be less than
131 * select()'s timeout granularity (typically, 10ms).
132 */
133void net_usleep( unsigned long usec );
134
135/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000136 * \brief Read at most 'len' characters. If no error occurs,
137 * the actual amount read is returned.
138 *
139 * \param ctx Socket
140 * \param buf The buffer to write to
141 * \param len Maximum length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 *
143 * \return This function returns the number of bytes received,
Paul Bakker831a7552011-05-18 13:32:51 +0000144 * or a non-zero error code; POLARSSL_ERR_NET_WANT_READ
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 * indicates read() is blocking.
146 */
Paul Bakker23986e52011-04-24 08:57:21 +0000147int net_recv( void *ctx, unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000150 * \brief Write at most 'len' characters. If no error occurs,
151 * the actual amount read is returned.
152 *
153 * \param ctx Socket
Paul Bakkerff60ee62010-03-16 21:09:09 +0000154 * \param buf The buffer to read from
155 * \param len The length of the buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 *
157 * \return This function returns the number of bytes sent,
Paul Bakker831a7552011-05-18 13:32:51 +0000158 * or a non-zero error code; POLARSSL_ERR_NET_WANT_WRITE
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 * indicates write() is blocking.
160 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000161int net_send( void *ctx, const unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163/**
164 * \brief Gracefully shutdown the connection
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000165 *
166 * \param fd The socket to close
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 */
168void net_close( int fd );
169
170#ifdef __cplusplus
171}
172#endif
173
174#endif /* net.h */