Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file net.h |
| 3 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 4 | #ifndef POLARSSL_NET_H |
| 5 | #define POLARSSL_NET_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 7 | #define POLARSSL_ERR_NET_UNKNOWN_HOST -0x0F00 |
| 8 | #define POLARSSL_ERR_NET_SOCKET_FAILED -0x0F10 |
| 9 | #define POLARSSL_ERR_NET_CONNECT_FAILED -0x0F20 |
| 10 | #define POLARSSL_ERR_NET_BIND_FAILED -0x0F30 |
| 11 | #define POLARSSL_ERR_NET_LISTEN_FAILED -0x0F40 |
| 12 | #define POLARSSL_ERR_NET_ACCEPT_FAILED -0x0F50 |
| 13 | #define POLARSSL_ERR_NET_RECV_FAILED -0x0F60 |
| 14 | #define POLARSSL_ERR_NET_SEND_FAILED -0x0F70 |
| 15 | #define POLARSSL_ERR_NET_CONN_RESET -0x0F80 |
| 16 | #define POLARSSL_ERR_NET_TRY_AGAIN -0x0F90 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
| 22 | /** |
| 23 | * \brief Initiate a TCP connection with host:port |
| 24 | * |
| 25 | * \return 0 if successful, or one of: |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 26 | * POLARSSL_ERR_NET_SOCKET_FAILED, |
| 27 | * POLARSSL_ERR_NET_UNKNOWN_HOST, |
| 28 | * POLARSSL_ERR_NET_CONNECT_FAILED |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | */ |
| 30 | int net_connect( int *fd, char *host, int port ); |
| 31 | |
| 32 | /** |
| 33 | * \brief Create a listening socket on bind_ip:port. |
| 34 | * If bind_ip == NULL, all interfaces are binded. |
| 35 | * |
| 36 | * \return 0 if successful, or one of: |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 37 | * POLARSSL_ERR_NET_SOCKET_FAILED, |
| 38 | * POLARSSL_ERR_NET_BIND_FAILED, |
| 39 | * POLARSSL_ERR_NET_LISTEN_FAILED |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 40 | */ |
| 41 | int net_bind( int *fd, char *bind_ip, int port ); |
| 42 | |
| 43 | /** |
| 44 | * \brief Accept a connection from a remote client |
| 45 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 46 | * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or |
| 47 | * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | * non-blocking and accept() is blocking. |
| 49 | */ |
| 50 | int net_accept( int bind_fd, int *client_fd, void *client_ip ); |
| 51 | |
| 52 | /** |
| 53 | * \brief Set the socket blocking |
| 54 | * |
| 55 | * \return 0 if successful, or a non-zero error code |
| 56 | */ |
| 57 | int net_set_block( int fd ); |
| 58 | |
| 59 | /** |
| 60 | * \brief Set the socket non-blocking |
| 61 | * |
| 62 | * \return 0 if successful, or a non-zero error code |
| 63 | */ |
| 64 | int net_set_nonblock( int fd ); |
| 65 | |
| 66 | /** |
| 67 | * \brief Portable usleep helper |
| 68 | * |
| 69 | * \note Real amount of time slept will not be less than |
| 70 | * select()'s timeout granularity (typically, 10ms). |
| 71 | */ |
| 72 | void net_usleep( unsigned long usec ); |
| 73 | |
| 74 | /** |
| 75 | * \brief Read at most 'len' characters. len is updated to |
| 76 | * reflect the actual number of characters read. |
| 77 | * |
| 78 | * \return This function returns the number of bytes received, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 79 | * or a negative error code; POLARSSL_ERR_NET_TRY_AGAIN |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | * indicates read() is blocking. |
| 81 | */ |
| 82 | int net_recv( void *ctx, unsigned char *buf, int len ); |
| 83 | |
| 84 | /** |
| 85 | * \brief Write at most 'len' characters. len is updated to |
| 86 | * reflect the number of characters _not_ written. |
| 87 | * |
| 88 | * \return This function returns the number of bytes sent, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 89 | * or a negative error code; POLARSSL_ERR_NET_TRY_AGAIN |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | * indicates write() is blocking. |
| 91 | */ |
| 92 | int net_send( void *ctx, unsigned char *buf, int len ); |
| 93 | |
| 94 | /** |
| 95 | * \brief Gracefully shutdown the connection |
| 96 | */ |
| 97 | void net_close( int fd ); |
| 98 | |
| 99 | #ifdef __cplusplus |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | #endif /* net.h */ |