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