]> git.meshlink.io Git - meshlink-tiny/blob - src/utcp.h
Remove logging when compiled with -DNDEBUG.
[meshlink-tiny] / src / utcp.h
1 /*
2     utcp.h -- Userspace TCP
3     Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org>
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 2 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 along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef UTCP_H
21 #define UTCP_H
22
23 #include <unistd.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 // TODO: Windows
27 #include <sys/time.h>
28
29 #ifndef UTCP_INTERNAL
30 struct utcp {
31         void *priv;
32 };
33
34 struct utcp_connection {
35         void *priv;
36         struct utcp *const utcp;
37         const uint32_t flags;
38 };
39 #else
40 struct utcp;
41 struct utcp_connection;
42 #endif
43
44 #define UTCP_SHUT_RD 0
45 #define UTCP_SHUT_WR 1
46 #define UTCP_SHUT_RDWR 2
47
48 //#define UTCP_ORDERED 1
49 //#define UTCP_RELIABLE 2
50 //#define UTCP_FRAMED 4
51 //#define UTCP_DROP_LATE 8
52 //#define UTCP_NO_PARTIAL 16
53
54 //#define UTCP_TCP 3
55 #define UTCP_UDP 0
56 #define UTCP_CHANGEABLE_FLAGS 0x0U
57
58 typedef bool (*utcp_listen_t)(struct utcp *utcp, uint16_t port);
59 typedef void (*utcp_accept_t)(struct utcp_connection *utcp_connection, uint16_t port);
60
61 typedef ssize_t (*utcp_send_t)(struct utcp *utcp, const void *data, size_t len);
62 typedef ssize_t (*utcp_recv_t)(struct utcp_connection *connection, const void *data, size_t len);
63
64
65 struct utcp *utcp_init(utcp_accept_t accept, utcp_listen_t listen, utcp_send_t send, void *priv);
66 void utcp_exit(struct utcp *utcp);
67
68 struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t port, utcp_recv_t recv, void *priv, uint32_t flags);
69 void utcp_accept(struct utcp_connection *utcp, utcp_recv_t recv, void *priv);
70 ssize_t utcp_send(struct utcp_connection *connection, const void *data, size_t len);
71 ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len);
72 int utcp_close(struct utcp_connection *connection);
73 int utcp_abort(struct utcp_connection *connection);
74 int utcp_shutdown(struct utcp_connection *connection, int how);
75 struct timespec utcp_timeout(struct utcp *utcp);
76 void utcp_set_recv_cb(struct utcp_connection *connection, utcp_recv_t recv);
77 void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_listen_t listen);
78 bool utcp_is_active(struct utcp *utcp);
79 void utcp_reset_all_connections(struct utcp *utcp);
80
81 // Global socket options
82
83 int utcp_get_user_timeout(struct utcp *utcp);
84 void utcp_set_user_timeout(struct utcp *utcp, int seconds);
85
86 uint16_t utcp_get_mtu(struct utcp *utcp);
87 uint16_t utcp_get_mss(struct utcp *utcp);
88 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu);
89
90 void utcp_reset_timers(struct utcp *utcp);
91
92 void utcp_offline(struct utcp *utcp, bool offline);
93
94 // Per-socket options
95
96 bool utcp_get_nodelay(struct utcp_connection *connection);
97 void utcp_set_nodelay(struct utcp_connection *connection, bool nodelay);
98
99 bool utcp_get_keepalive(struct utcp_connection *connection);
100 void utcp_set_keepalive(struct utcp_connection *connection, bool keepalive);
101
102 void utcp_expect_data(struct utcp_connection *connection, bool expect);
103
104 void utcp_set_flags(struct utcp_connection *connection, uint32_t flags);
105
106 // Completely global options
107
108 void utcp_set_clock_granularity(long granularity);
109
110 #endif