Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file net.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
| 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
Paul Bakker | 785a9ee | 2009-01-25 14:15:10 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 22 | #ifndef POLARSSL_NET_H |
| 23 | #define POLARSSL_NET_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 25 | #define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0F00 |
| 26 | #define POLARSSL_ERR_NET_SOCKET_FAILED -0x0F10 |
| 27 | #define POLARSSL_ERR_NET_CONNECT_FAILED -0x0F20 |
| 28 | #define POLARSSL_ERR_NET_BIND_FAILED -0x0F30 |
| 29 | #define POLARSSL_ERR_NET_LISTEN_FAILED -0x0F40 |
| 30 | #define POLARSSL_ERR_NET_ACCEPT_FAILED -0x0F50 |
| 31 | #define POLARSSL_ERR_NET_RECV_FAILED -0x0F60 |
| 32 | #define POLARSSL_ERR_NET_SEND_FAILED -0x0F70 |
| 33 | #define POLARSSL_ERR_NET_CONN_RESET -0x0F80 |
| 34 | #define POLARSSL_ERR_NET_TRY_AGAIN -0x0F90 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
| 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | /** |
| 41 | * \brief Initiate a TCP connection with host:port |
| 42 | * |
| 43 | * \return 0 if successful, or one of: |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 44 | * POLARSSL_ERR_NET_SOCKET_FAILED, |
| 45 | * POLARSSL_ERR_NET_UNKNOWN_HOST, |
| 46 | * POLARSSL_ERR_NET_CONNECT_FAILED |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | */ |
| 48 | int net_connect( int *fd, char *host, int port ); |
| 49 | |
| 50 | /** |
| 51 | * \brief Create a listening socket on bind_ip:port. |
| 52 | * If bind_ip == NULL, all interfaces are binded. |
| 53 | * |
| 54 | * \return 0 if successful, or one of: |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 55 | * POLARSSL_ERR_NET_SOCKET_FAILED, |
| 56 | * POLARSSL_ERR_NET_BIND_FAILED, |
| 57 | * POLARSSL_ERR_NET_LISTEN_FAILED |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | */ |
| 59 | int net_bind( int *fd, char *bind_ip, int port ); |
| 60 | |
| 61 | /** |
| 62 | * \brief Accept a connection from a remote client |
| 63 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 64 | * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or |
| 65 | * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | * non-blocking and accept() is blocking. |
| 67 | */ |
| 68 | int net_accept( int bind_fd, int *client_fd, void *client_ip ); |
| 69 | |
| 70 | /** |
| 71 | * \brief Set the socket blocking |
| 72 | * |
| 73 | * \return 0 if successful, or a non-zero error code |
| 74 | */ |
| 75 | int net_set_block( int fd ); |
| 76 | |
| 77 | /** |
| 78 | * \brief Set the socket non-blocking |
| 79 | * |
| 80 | * \return 0 if successful, or a non-zero error code |
| 81 | */ |
| 82 | int net_set_nonblock( int fd ); |
| 83 | |
| 84 | /** |
| 85 | * \brief Portable usleep helper |
| 86 | * |
| 87 | * \note Real amount of time slept will not be less than |
| 88 | * select()'s timeout granularity (typically, 10ms). |
| 89 | */ |
| 90 | void net_usleep( unsigned long usec ); |
| 91 | |
| 92 | /** |
| 93 | * \brief Read at most 'len' characters. len is updated to |
| 94 | * reflect the actual number of characters read. |
| 95 | * |
| 96 | * \return This function returns the number of bytes received, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 97 | * or a negative error code; POLARSSL_ERR_NET_TRY_AGAIN |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | * indicates read() is blocking. |
| 99 | */ |
| 100 | int net_recv( void *ctx, unsigned char *buf, int len ); |
| 101 | |
| 102 | /** |
| 103 | * \brief Write at most 'len' characters. len is updated to |
| 104 | * reflect the number of characters _not_ written. |
| 105 | * |
| 106 | * \return This function returns the number of bytes sent, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 107 | * or a negative error code; POLARSSL_ERR_NET_TRY_AGAIN |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | * indicates write() is blocking. |
| 109 | */ |
| 110 | int net_send( void *ctx, unsigned char *buf, int len ); |
| 111 | |
| 112 | /** |
| 113 | * \brief Gracefully shutdown the connection |
| 114 | */ |
| 115 | void net_close( int fd ); |
| 116 | |
| 117 | #ifdef __cplusplus |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | #endif /* net.h */ |