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