]> git.meshlink.io Git - utcp/blob - utcp_priv.h
Don't call the poll callback unnecessarily.
[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
33 #define AUX_INIT 1
34 #define AUX_FRAME 2
35 #define AUX_SAK 3
36 #define AUX_TIMESTAMP 4
37
38 #define NSACKS 4
39 #define DEFAULT_SNDBUFSIZE 4096
40 #define DEFAULT_MAXSNDBUFSIZE 131072
41 #define DEFAULT_RCVBUFSIZE 0
42 #define DEFAULT_MAXRCVBUFSIZE 131072
43
44 #define DEFAULT_MTU 1000
45
46 static const long USEC_PER_SEC = 1000000;
47 static const long NSEC_PER_SEC = 1000000000;
48 static const int DEFAULT_USER_TIMEOUT = 60; // sec
49 static const long START_RTO = 1 * USEC_PER_SEC; // usec
50 static const long MAX_RTO  = 3 * USEC_PER_SEC; // usec
51
52 struct hdr {
53         uint16_t src; // Source port
54         uint16_t dst; // Destination port
55         uint32_t seq; // Sequence number
56         uint32_t ack; // Acknowledgement number
57         uint32_t wnd; // Window size
58         uint16_t ctl; // Flags (SYN, ACK, FIN, RST)
59         uint16_t aux; // other stuff
60 };
61
62 enum state {
63         CLOSED,
64         LISTEN,
65         SYN_SENT,
66         SYN_RECEIVED,
67         ESTABLISHED,
68         FIN_WAIT_1,
69         FIN_WAIT_2,
70         CLOSE_WAIT,
71         CLOSING,
72         LAST_ACK,
73         TIME_WAIT
74 };
75
76 static const char *strstate[] __attribute__((unused)) = {
77         [CLOSED] = "CLOSED",
78         [LISTEN] = "LISTEN",
79         [SYN_SENT] = "SYN_SENT",
80         [SYN_RECEIVED] = "SYN_RECEIVED",
81         [ESTABLISHED] = "ESTABLISHED",
82         [FIN_WAIT_1] = "FIN_WAIT_1",
83         [FIN_WAIT_2] = "FIN_WAIT_2",
84         [CLOSE_WAIT] = "CLOSE_WAIT",
85         [CLOSING] = "CLOSING",
86         [LAST_ACK] = "LAST_ACK",
87         [TIME_WAIT] = "TIME_WAIT"
88 };
89
90 struct buffer {
91         char *data;
92         uint32_t offset;
93         uint32_t used;
94         uint32_t size;
95         uint32_t maxsize;
96 };
97
98 struct sack {
99         uint32_t offset;
100         uint32_t len;
101 };
102
103 struct utcp_connection {
104         void *priv;
105         struct utcp *utcp;
106         uint32_t flags;
107
108         bool reapable;
109         bool do_poll;
110
111         // Callbacks
112
113         utcp_recv_t recv;
114         utcp_poll_t poll;
115
116         // TCP State
117
118         uint16_t src;
119         uint16_t dst;
120         enum state state;
121
122         struct {
123                 uint32_t una;
124                 uint32_t nxt;
125                 uint32_t wnd;
126                 uint32_t iss;
127
128                 uint32_t last;
129                 uint32_t cwnd;
130                 uint32_t ssthresh;
131         } snd;
132
133         struct {
134                 uint32_t nxt;
135                 uint32_t irs;
136         } rcv;
137
138         int dupack;
139
140         // Timers
141
142         struct timespec conn_timeout;
143         struct timespec rtrx_timeout;
144         struct timespec rtt_start;
145         uint32_t rtt_seq;
146
147         // Buffers
148
149         uint32_t prev_free;
150         struct buffer sndbuf;
151         struct buffer rcvbuf;
152         struct sack sacks[NSACKS];
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_pre_accept_t pre_accept;
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         // RTT variables
186
187         uint32_t srtt; // usec
188         uint32_t rttvar; // usec
189         uint32_t rto; // usec
190
191         // Connection management
192
193         struct utcp_connection **connections;
194         int nconnections;
195         int nallocated;
196 };
197
198 #endif