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