]> git.meshlink.io Git - meshlink-tiny/blob - src/utcp_priv.h
602140acb65e87e2d5c861c17edd79ca5661bae4
[meshlink-tiny] / src / utcp_priv.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_PRIV_H
21 #define UTCP_PRIV_H
22
23 #define UTCP_INTERNAL
24 #include "utcp.h"
25
26 #define PREP(l) char pkt[(l) + sizeof struct hdr]; struct hdr *hdr = &pkt;
27
28 #define SYN 1
29 #define ACK 2
30 #define FIN 4
31 #define RST 8
32 #define MF 16
33
34 #define AUX_INIT 1
35 #define AUX_FRAME 2
36 #define AUX_SAK 3
37 #define AUX_TIMESTAMP 4
38
39 #define DEFAULT_SNDBUFSIZE 0
40 #define DEFAULT_MAXSNDBUFSIZE 131072
41 #define DEFAULT_RCVBUFSIZE 0
42 #define DEFAULT_MAXRCVBUFSIZE 131072
43
44 #define MAX_UNRELIABLE_SIZE 16777215
45 #define DEFAULT_MTU 1000
46
47 #define USEC_PER_SEC 1000000L
48 #define NSEC_PER_SEC 1000000000L
49 #define DEFAULT_USER_TIMEOUT 60
50 #define START_RTO (1 * USEC_PER_SEC)
51 #define MAX_RTO (3 * USEC_PER_SEC)
52
53 struct hdr {
54         uint16_t src; // Source port
55         uint16_t dst; // Destination port
56         uint32_t seq; // Sequence number
57         uint32_t ack; // Acknowledgement number
58         uint32_t wnd; // Window size
59         uint16_t ctl; // Flags (SYN, ACK, FIN, RST)
60         uint16_t aux; // other stuff
61 };
62
63 enum state {
64         CLOSED,
65         LISTEN,
66         SYN_SENT,
67         SYN_RECEIVED,
68         ESTABLISHED,
69         FIN_WAIT_1,
70         FIN_WAIT_2,
71         CLOSE_WAIT,
72         CLOSING,
73         LAST_ACK,
74         TIME_WAIT
75 };
76
77 static const char *strstate[] __attribute__((unused)) = {
78         [CLOSED] = "CLOSED",
79         [LISTEN] = "LISTEN",
80         [SYN_SENT] = "SYN_SENT",
81         [SYN_RECEIVED] = "SYN_RECEIVED",
82         [ESTABLISHED] = "ESTABLISHED",
83         [FIN_WAIT_1] = "FIN_WAIT_1",
84         [FIN_WAIT_2] = "FIN_WAIT_2",
85         [CLOSE_WAIT] = "CLOSE_WAIT",
86         [CLOSING] = "CLOSING",
87         [LAST_ACK] = "LAST_ACK",
88         [TIME_WAIT] = "TIME_WAIT"
89 };
90
91 struct buffer {
92         char *data;
93         uint32_t offset;
94         uint32_t used;
95         uint32_t size;
96         uint32_t maxsize;
97         bool external;
98 };
99
100 struct utcp_connection {
101         void *priv;
102         struct utcp *utcp;
103         uint32_t flags;
104
105         bool reapable;
106
107         // Callbacks
108
109         utcp_recv_t recv;
110
111         // TCP State
112
113         uint16_t src;
114         uint16_t dst;
115         enum state state;
116
117         struct {
118                 uint32_t una;
119                 uint32_t nxt;
120                 uint32_t wnd;
121                 uint32_t iss;
122
123                 uint32_t last;
124                 uint32_t cwnd;
125                 uint32_t ssthresh;
126         } snd;
127
128         struct {
129                 uint32_t nxt;
130                 uint32_t irs;
131         } rcv;
132
133         int dupack;
134
135         // Timers
136
137         struct timespec conn_timeout;
138         struct timespec rtrx_timeout;
139         struct timespec rtt_start;
140         uint32_t rtt_seq;
141
142         // RTT variables
143
144         uint32_t srtt; // usec
145         uint32_t rttvar; // usec
146         uint32_t rto; // usec
147
148         // Buffers
149
150         uint32_t prev_free;
151         struct buffer sndbuf;
152         struct buffer rcvbuf;
153
154         // Per-socket options
155
156         bool nodelay;
157         bool keepalive;
158         bool shut_wr;
159
160         // Congestion avoidance state
161
162         struct timespec tlast;
163         uint64_t bandwidth;
164 };
165
166 struct utcp {
167         void *priv;
168
169         // Callbacks
170
171         utcp_accept_t accept;
172         utcp_listen_t listen;
173         utcp_send_t send;
174
175         // Packet buffer
176
177         void *pkt;
178
179         // Global socket options
180
181         uint16_t mtu; // The maximum size of a UTCP packet, including headers.
182         uint16_t mss; // The maximum size of the payload of a UTCP packet.
183         int timeout; // sec
184
185         // Connection management
186
187         struct utcp_connection **connections;
188         int nconnections;
189         int nallocated;
190 };
191
192 #endif