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