]> git.meshlink.io Git - utcp/blob - utcp_priv.h
Handle channel closure during a receive callback when the ringbuffer wraps.
[utcp] / 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 4096
41 #define DEFAULT_MAXSNDBUFSIZE 131072
42 #define DEFAULT_RCVBUFSIZE 0
43 #define DEFAULT_MAXRCVBUFSIZE 131072
44
45 #define MAX_UNRELIABLE_SIZE 65536
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 };
99
100 struct sack {
101         uint32_t offset;
102         uint32_t len;
103 };
104
105 struct utcp_connection {
106         void *priv;
107         struct utcp *utcp;
108         uint32_t flags;
109
110         bool reapable;
111         bool do_poll;
112
113         // Callbacks
114
115         utcp_recv_t recv;
116         utcp_poll_t poll;
117
118         // TCP State
119
120         uint16_t src;
121         uint16_t dst;
122         enum state state;
123
124         struct {
125                 uint32_t una;
126                 uint32_t nxt;
127                 uint32_t wnd;
128                 uint32_t iss;
129
130                 uint32_t last;
131                 uint32_t cwnd;
132                 uint32_t ssthresh;
133         } snd;
134
135         struct {
136                 uint32_t nxt;
137                 uint32_t irs;
138         } rcv;
139
140         int dupack;
141
142         // Timers
143
144         struct timespec conn_timeout;
145         struct timespec rtrx_timeout;
146         struct timespec rtt_start;
147         uint32_t rtt_seq;
148
149         // RTT variables
150
151         uint32_t srtt; // usec
152         uint32_t rttvar; // usec
153         uint32_t rto; // usec
154
155         // Buffers
156
157         uint32_t prev_free;
158         struct buffer sndbuf;
159         struct buffer rcvbuf;
160         struct sack sacks[NSACKS];
161
162         // Per-socket options
163
164         bool nodelay;
165         bool keepalive;
166         bool shut_wr;
167
168         // Congestion avoidance state
169
170         struct timespec tlast;
171         uint64_t bandwidth;
172 };
173
174 struct utcp {
175         void *priv;
176
177         // Callbacks
178
179         utcp_accept_t accept;
180         utcp_pre_accept_t pre_accept;
181         utcp_retransmit_t retransmit;
182         utcp_send_t send;
183
184         // Packet buffer
185
186         void *pkt;
187
188         // Global socket options
189
190         uint16_t mtu; // The maximum size of a UTCP packet, including headers.
191         uint16_t mss; // The maximum size of the payload of a UTCP packet.
192         int timeout; // sec
193
194         // Connection management
195
196         struct utcp_connection **connections;
197         int nconnections;
198         int nallocated;
199 };
200
201 #endif