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