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 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame^] | 4 | * Copyright (C) 2006-2010, Brainspark B.V. |
| 5 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 6 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 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 | 3391b12 | 2009-07-28 20:11:54 +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 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 43 | * \param fd Socket to use |
| 44 | * \param host Host to connect to |
| 45 | * \param port Port to connect to |
| 46 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | * \return 0 if successful, or one of: |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 48 | * POLARSSL_ERR_NET_SOCKET_FAILED, |
| 49 | * POLARSSL_ERR_NET_UNKNOWN_HOST, |
| 50 | * POLARSSL_ERR_NET_CONNECT_FAILED |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 51 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 52 | int net_connect( int *fd, const char *host, int port ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * \brief Create a listening socket on bind_ip:port. |
| 56 | * If bind_ip == NULL, all interfaces are binded. |
| 57 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 58 | * \param fd Socket to use |
| 59 | * \param bind_ip IP to bind to, can be NULL |
| 60 | * \param port Port number to use |
| 61 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | * \return 0 if successful, or one of: |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 63 | * POLARSSL_ERR_NET_SOCKET_FAILED, |
| 64 | * POLARSSL_ERR_NET_BIND_FAILED, |
| 65 | * POLARSSL_ERR_NET_LISTEN_FAILED |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 67 | int net_bind( int *fd, const char *bind_ip, int port ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 70 | * \brief Accept a connection from a remote client |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 72 | * \param bind_fd Relevant socket |
| 73 | * \param client_fd Will contain the connected client socket |
| 74 | * \param client_ip Will contain the client IP address |
| 75 | * |
| 76 | * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or |
| 77 | * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to |
| 78 | * non-blocking and accept() is blocking. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | */ |
| 80 | int net_accept( int bind_fd, int *client_fd, void *client_ip ); |
| 81 | |
| 82 | /** |
| 83 | * \brief Set the socket blocking |
| 84 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 85 | * \param fd Socket to set |
| 86 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 87 | * \return 0 if successful, or a non-zero error code |
| 88 | */ |
| 89 | int net_set_block( int fd ); |
| 90 | |
| 91 | /** |
| 92 | * \brief Set the socket non-blocking |
| 93 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 94 | * \param fd Socket to set |
| 95 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | * \return 0 if successful, or a non-zero error code |
| 97 | */ |
| 98 | int net_set_nonblock( int fd ); |
| 99 | |
| 100 | /** |
| 101 | * \brief Portable usleep helper |
| 102 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 103 | * \param usec Amount of microseconds to sleep |
| 104 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | * \note Real amount of time slept will not be less than |
| 106 | * select()'s timeout granularity (typically, 10ms). |
| 107 | */ |
| 108 | void net_usleep( unsigned long usec ); |
| 109 | |
| 110 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 111 | * \brief Read at most 'len' characters. If no error occurs, |
| 112 | * the actual amount read is returned. |
| 113 | * |
| 114 | * \param ctx Socket |
| 115 | * \param buf The buffer to write to |
| 116 | * \param len Maximum length of the buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | * |
| 118 | * \return This function returns the number of bytes received, |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 119 | * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | * indicates read() is blocking. |
| 121 | */ |
| 122 | int net_recv( void *ctx, unsigned char *buf, int len ); |
| 123 | |
| 124 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 125 | * \brief Write at most 'len' characters. If no error occurs, |
| 126 | * the actual amount read is returned. |
| 127 | * |
| 128 | * \param ctx Socket |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 129 | * \param buf The buffer to read from |
| 130 | * \param len The length of the buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | * |
| 132 | * \return This function returns the number of bytes sent, |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 133 | * or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | * indicates write() is blocking. |
| 135 | */ |
| 136 | int net_send( void *ctx, unsigned char *buf, int len ); |
| 137 | |
| 138 | /** |
| 139 | * \brief Gracefully shutdown the connection |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 140 | * |
| 141 | * \param fd The socket to close |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | */ |
| 143 | void net_close( int fd ); |
| 144 | |
| 145 | #ifdef __cplusplus |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | #endif /* net.h */ |