]> git.meshlink.io Git - utcp/blob - utcp_priv.h
Various small fixes, clarifications.
[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 DEFAULT_SNDBUFSIZE 4096
34 #define DEFAULT_MAXSNDBUFSIZE 131072
35
36 struct hdr {
37         uint16_t src; // Source port
38         uint16_t dst; // Destination port
39         uint32_t seq; // Sequence number
40         uint32_t ack; // Acknowledgement number
41         uint32_t wnd; // Window size
42         uint16_t ctl; // Flags (SYN, ACK, FIN, RST)
43         uint16_t aux; // other stuff
44 };
45
46 enum state {
47         CLOSED,
48         LISTEN,
49         SYN_SENT,
50         SYN_RECEIVED,
51         ESTABLISHED,
52         FIN_WAIT_1,
53         FIN_WAIT_2,
54         CLOSE_WAIT,
55         CLOSING,
56         LAST_ACK,
57         TIME_WAIT
58 };
59
60 static const char *strstate[] __attribute__((unused)) = {
61         [CLOSED] = "CLOSED",
62         [LISTEN] = "LISTEN",
63         [SYN_SENT] = "SYN_SENT",
64         [SYN_RECEIVED] = "SYN_RECEIVED",
65         [ESTABLISHED] = "ESTABLISHED",
66         [FIN_WAIT_1] = "FIN_WAIT_1",
67         [FIN_WAIT_2] = "FIN_WAIT_2",
68         [CLOSE_WAIT] = "CLOSE_WAIT",
69         [CLOSING] = "CLOSING",
70         [LAST_ACK] = "LAST_ACK",
71         [TIME_WAIT] = "TIME_WAIT"
72 };
73
74 struct utcp_connection {
75         void *priv;
76         struct utcp *utcp;
77
78         bool reapable;
79
80         // Callbacks
81
82         utcp_recv_t recv;
83
84         // TCP State
85
86         uint16_t src;
87         uint16_t dst;
88         enum state state;
89
90         struct {
91                 uint32_t una;
92                 uint32_t nxt;
93                 uint32_t wnd;
94                 uint32_t iss;
95
96                 uint32_t last;
97                 uint32_t cwnd;
98         } snd;
99
100         struct {
101                 uint32_t nxt;
102                 uint32_t wnd;
103                 uint32_t irs;
104         } rcv;
105
106         int dupack;
107
108         // Timers
109
110         struct timeval conn_timeout;
111         struct timeval rtrx_timeout;
112
113         // Send buffer
114
115         char *sndbuf;
116         uint32_t sndbufsize;
117         uint32_t maxsndbufsize;
118
119         // Per-socket options
120
121         bool nodelay;
122         bool keepalive;
123
124         // Congestion avoidance state
125
126         struct timeval tlast;
127         uint64_t bandwidth;
128 };
129
130 struct utcp {
131         void *priv;
132
133         // Callbacks
134
135         utcp_accept_t accept;
136         utcp_pre_accept_t pre_accept;
137         utcp_send_t send;
138
139         // Global socket options
140
141         uint16_t mtu;
142         int timeout;
143
144         // Connection management
145
146         struct utcp_connection **connections;
147         int nconnections;
148         int nallocated;
149 };
150
151 #endif