1 /*
2  * uhub - A tiny ADC p2p connection hub
3  * Copyright (C) 2007-2014, Jan Vidar Krey
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef HAVE_UHUB_NETWORK_CONNECTION_H
21 #define HAVE_UHUB_NETWORK_CONNECTION_H
22 
23 #include "uhub.h"
24 #include "network/common.h"
25 #include "network/backend.h"
26 #include "network/tls.h"
27 
28 #define NET_EVENT_TIMEOUT         0x0001
29 #define NET_EVENT_READ            0x0002
30 #define NET_EVENT_WRITE           0x0004
31 #define NET_EVENT_ERROR           0x1000
32 
33 struct net_connection
34 {
35 	NET_CON_STRUCT_COMMON
36 };
37 
38 struct net_connect_handle;
39 
40 enum net_connect_status
41 {
42 	net_connect_status_ok = 0,
43 	net_connect_status_host_not_found = -1,
44 	net_connect_status_no_address = -2,
45 	net_connect_status_dns_error = -3,
46 	net_connect_status_refused = -4,
47 	net_connect_status_unreachable = -5,
48 	net_connect_status_timeout = -6,
49 	net_connect_status_socket_error = -7,
50 };
51 
52 typedef void (*net_connect_cb)(struct net_connect_handle*, enum net_connect_status status, struct net_connection* con, void* ptr);
53 
54 extern int   net_con_get_sd(struct net_connection* con);
55 extern void* net_con_get_ptr(struct net_connection* con);
56 
57 extern struct net_connection* net_con_create();
58 
59 /**
60  * Establish an outbound TCP connection.
61  * This will resolve the IP-addresses, and connect to
62  * either an IPv4 or IPv6 address depending if it is supported,
63  * and using the happy eyeballs algorithm.
64  *
65  * @param address Hostname, IPv4 or IPv6 address
66  * @param port TCP port number
67  * @param callback A callback to be called once the connection is established, or failed.
68  * @returns a handle to the connection establishment job, or NULL if an immediate error.
69  */
70 extern struct net_connect_handle* net_con_connect(const char* address, uint16_t port, net_connect_cb callback, void* ptr);
71 extern void net_connect_destroy(struct net_connect_handle* handle);
72 
73 extern void net_con_destroy(struct net_connection*);
74 extern void net_con_initialize(struct net_connection* con, int sd, net_connection_cb callback, const void* ptr, int events);
75 extern void net_con_reinitialize(struct net_connection* con, net_connection_cb callback, const void* ptr, int events);
76 extern void net_con_update(struct net_connection* con, int events);
77 extern void net_con_callback(struct net_connection* con, int events);
78 
79 /**
80  * Close the connection.
81  * This will ensure a connection is closed properly and will generate a NET_EVENT_DESTROYED event which indicates
82  * that the con can safely be deleted (or set to NULL).
83  */
84 extern void net_con_close(struct net_connection* con);
85 
86 /**
87  * Send data
88  *
89  * @return returns the number of bytes sent.
90  *         0 if no data is sent, and this function should be called again (EWOULDBLOCK/EINTR)
91  *        <0 if an error occured, the negative number contains the error code.
92  */
93 extern ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len);
94 
95 /**
96  * Receive data
97  *
98  * @return returns the number of bytes sent.
99  *         0 if no data is sent, and this function should be called again (EWOULDBLOCK/EINTR)
100  *        <0 if an error occured, the negative number contains the error code.
101  */
102 extern ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len);
103 
104 /**
105  * Receive data without removing them from the recv() buffer.
106  * NOTE: This does not currently work for SSL connections after the SSL handshake has been
107  * performed.
108  */
109 extern ssize_t net_con_peek(struct net_connection* con, void* buf, size_t len);
110 
111 /**
112  * Set timeout for connetion.
113  *
114  * @param seconds the number of seconds into the future.
115  */
116 extern void net_con_set_timeout(struct net_connection* con, int seconds);
117 extern void net_con_clear_timeout(struct net_connection* con);
118 
119 #endif /* HAVE_UHUB_NETWORK_CONNECTION_H */
120 
121