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