]> git.meshlink.io Git - utcp/blob - utcp_priv.h
Fix SYN packet handling.
[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 #define USEC_PER_SEC 1000000
47 #define DEFAULT_USER_TIMEOUT 60 // sec
48 #define CLOCK_GRANULARITY 1000 // usec
49 #define START_RTO 1000000 // usec
50 #define MAX_RTO 3000000 // 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 used;
93         uint32_t size;
94         uint32_t maxsize;
95 };
96
97 struct sack {
98         uint32_t offset;
99         uint32_t len;
100 };
101
102 struct utcp_connection {
103         void *priv;
104         struct utcp *utcp;
105         uint32_t flags;
106
107         bool reapable;
108
109         // Callbacks
110
111         utcp_recv_t recv;
112         utcp_poll_t poll;
113
114         // TCP State
115
116         uint16_t src;
117         uint16_t dst;
118         enum state state;
119
120         struct {
121                 uint32_t una;
122                 uint32_t nxt;
123                 uint32_t wnd;
124                 uint32_t iss;
125
126                 uint32_t last;
127                 uint32_t cwnd;
128         } snd;
129
130         struct {
131                 uint32_t nxt;
132                 uint32_t wnd;
133                 uint32_t irs;
134         } rcv;
135
136         int dupack;
137
138         // Timers
139
140         struct timeval conn_timeout;
141         struct timeval rtrx_timeout;
142         struct timeval rtt_start;
143         uint32_t rtt_seq;
144
145         // Buffers
146
147         struct buffer sndbuf;
148         struct buffer rcvbuf;
149         struct sack sacks[NSACKS];
150
151         // Per-socket options
152
153         bool nodelay;
154         bool keepalive;
155
156         // Congestion avoidance state
157
158         struct timeval tlast;
159         uint64_t bandwidth;
160 };
161
162 struct utcp {
163         void *priv;
164
165         // Callbacks
166
167         utcp_accept_t accept;
168         utcp_pre_accept_t pre_accept;
169         utcp_send_t send;
170
171         // Global socket options
172
173         uint16_t mtu;
174         int timeout; // sec
175
176         // RTT variables
177
178         uint32_t srtt; // usec
179         uint32_t rttvar; // usec
180         uint32_t rto; // usec
181
182         // Connection management
183
184         struct utcp_connection **connections;
185         int nconnections;
186         int nallocated;
187 };
188
189 #endif