]> git.meshlink.io Git - meshlink-tiny/blob - src/utcp_priv.h
Remove logging when compiled with -DNDEBUG.
[meshlink-tiny] / src / 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 MAX_UNRELIABLE_SIZE 16777215
40 #define DEFAULT_MTU 1000
41
42 #define USEC_PER_SEC 1000000L
43 #define NSEC_PER_SEC 1000000000L
44 #define DEFAULT_USER_TIMEOUT 60
45 #define START_RTO (1 * USEC_PER_SEC)
46 #define MAX_RTO (3 * USEC_PER_SEC)
47
48 struct hdr {
49         uint16_t src; // Source port
50         uint16_t dst; // Destination port
51         uint32_t seq; // Sequence number
52         uint32_t ack; // Acknowledgement number
53         uint32_t wnd; // Window size
54         uint16_t ctl; // Flags (SYN, ACK, FIN, RST)
55         uint16_t aux; // other stuff
56 };
57
58 enum state {
59         CLOSED,
60         LISTEN,
61         SYN_SENT,
62         SYN_RECEIVED,
63         ESTABLISHED,
64         FIN_WAIT_1,
65         FIN_WAIT_2,
66         CLOSE_WAIT,
67         CLOSING,
68         LAST_ACK,
69         TIME_WAIT
70 };
71
72 static const char *strstate[] __attribute__((unused)) = {
73         [CLOSED] = "CLOSED",
74         [LISTEN] = "LISTEN",
75         [SYN_SENT] = "SYN_SENT",
76         [SYN_RECEIVED] = "SYN_RECEIVED",
77         [ESTABLISHED] = "ESTABLISHED",
78         [FIN_WAIT_1] = "FIN_WAIT_1",
79         [FIN_WAIT_2] = "FIN_WAIT_2",
80         [CLOSE_WAIT] = "CLOSE_WAIT",
81         [CLOSING] = "CLOSING",
82         [LAST_ACK] = "LAST_ACK",
83         [TIME_WAIT] = "TIME_WAIT"
84 };
85
86 struct utcp_connection {
87         void *priv;
88         struct utcp *utcp;
89         uint32_t flags;
90
91         bool reapable;
92
93         // Callbacks
94
95         utcp_recv_t recv;
96
97         // TCP State
98
99         uint16_t src;
100         uint16_t dst;
101         enum state state;
102
103         struct {
104                 uint32_t una;
105                 uint32_t nxt;
106                 uint32_t wnd;
107                 uint32_t iss;
108
109                 uint32_t last;
110                 uint32_t cwnd;
111                 uint32_t ssthresh;
112         } snd;
113
114         struct {
115                 uint32_t nxt;
116                 uint32_t irs;
117         } rcv;
118
119         int dupack;
120
121         // Timers
122
123         struct timespec conn_timeout;
124         struct timespec rtrx_timeout;
125         struct timespec rtt_start;
126         uint32_t rtt_seq;
127
128         // RTT variables
129
130         uint32_t srtt; // usec
131         uint32_t rttvar; // usec
132         uint32_t rto; // usec
133
134         // Per-socket options
135
136         bool nodelay;
137         bool keepalive;
138         bool shut_wr;
139
140         // Congestion avoidance state
141
142         struct timespec tlast;
143         uint64_t bandwidth;
144 };
145
146 struct utcp {
147         void *priv;
148
149         // Callbacks
150
151         utcp_accept_t accept;
152         utcp_listen_t listen;
153         utcp_send_t send;
154
155         // Packet buffer
156
157         void *pkt;
158
159         // Global socket options
160
161         uint16_t mtu; // The maximum size of a UTCP packet, including headers.
162         uint16_t mss; // The maximum size of the payload of a UTCP packet.
163         int timeout; // sec
164
165         // Connection management
166
167         struct utcp_connection **connections;
168         int nconnections;
169         int nallocated;
170 };
171
172 #endif