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