blob: a8fcdca1583fbfc55ed16796e2d309b92f45aca9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file net.h
3 */
Paul Bakker40e46942009-01-03 21:51:57 +00004#ifndef POLARSSL_NET_H
5#define POLARSSL_NET_H
Paul Bakker5121ce52009-01-03 21:22:43 +00006
Paul Bakker40e46942009-01-03 21:51:57 +00007#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 Bakker5121ce52009-01-03 21:22:43 +000017
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/**
23 * \brief Initiate a TCP connection with host:port
24 *
25 * \return 0 if successful, or one of:
Paul Bakker40e46942009-01-03 21:51:57 +000026 * POLARSSL_ERR_NET_SOCKET_FAILED,
27 * POLARSSL_ERR_NET_UNKNOWN_HOST,
28 * POLARSSL_ERR_NET_CONNECT_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000029 */
30int 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 Bakker40e46942009-01-03 21:51:57 +000037 * POLARSSL_ERR_NET_SOCKET_FAILED,
38 * POLARSSL_ERR_NET_BIND_FAILED,
39 * POLARSSL_ERR_NET_LISTEN_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +000040 */
41int net_bind( int *fd, char *bind_ip, int port );
42
43/**
44 * \brief Accept a connection from a remote client
45 *
Paul Bakker40e46942009-01-03 21:51:57 +000046 * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or
47 * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
Paul Bakker5121ce52009-01-03 21:22:43 +000048 * non-blocking and accept() is blocking.
49 */
50int 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 */
57int 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 */
64int 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 */
72void 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 Bakker40e46942009-01-03 21:51:57 +000079 * or a negative error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +000080 * indicates read() is blocking.
81 */
82int 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 Bakker40e46942009-01-03 21:51:57 +000089 * or a negative error code; POLARSSL_ERR_NET_TRY_AGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +000090 * indicates write() is blocking.
91 */
92int net_send( void *ctx, unsigned char *buf, int len );
93
94/**
95 * \brief Gracefully shutdown the connection
96 */
97void net_close( int fd );
98
99#ifdef __cplusplus
100}
101#endif
102
103#endif /* net.h */