blob: 1860b3d588ff9c67c641517fa6cd4fa430f3b5d6 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file net.h
3 */
4#ifndef XYSSL_NET_H
5#define XYSSL_NET_H
6
7#define XYSSL_ERR_NET_UNKNOWN_HOST -0x0F00
8#define XYSSL_ERR_NET_SOCKET_FAILED -0x0F10
9#define XYSSL_ERR_NET_CONNECT_FAILED -0x0F20
10#define XYSSL_ERR_NET_BIND_FAILED -0x0F30
11#define XYSSL_ERR_NET_LISTEN_FAILED -0x0F40
12#define XYSSL_ERR_NET_ACCEPT_FAILED -0x0F50
13#define XYSSL_ERR_NET_RECV_FAILED -0x0F60
14#define XYSSL_ERR_NET_SEND_FAILED -0x0F70
15#define XYSSL_ERR_NET_CONN_RESET -0x0F80
16#define XYSSL_ERR_NET_TRY_AGAIN -0x0F90
17
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:
26 * XYSSL_ERR_NET_SOCKET_FAILED,
27 * XYSSL_ERR_NET_UNKNOWN_HOST,
28 * XYSSL_ERR_NET_CONNECT_FAILED
29 */
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:
37 * XYSSL_ERR_NET_SOCKET_FAILED,
38 * XYSSL_ERR_NET_BIND_FAILED,
39 * XYSSL_ERR_NET_LISTEN_FAILED
40 */
41int net_bind( int *fd, char *bind_ip, int port );
42
43/**
44 * \brief Accept a connection from a remote client
45 *
46 * \return 0 if successful, XYSSL_ERR_NET_ACCEPT_FAILED, or
47 * XYSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to
48 * 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,
79 * or a negative error code; XYSSL_ERR_NET_TRY_AGAIN
80 * 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,
89 * or a negative error code; XYSSL_ERR_NET_TRY_AGAIN
90 * 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 */