]> git.meshlink.io Git - utcp/blob - utcp_priv.h
Fix and refactor send buffer code.
[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 buffer {
75         char *data;
76         uint32_t used;
77         uint32_t size;
78         uint32_t maxsize;
79 };
80
81 struct utcp_connection {
82         void *priv;
83         struct utcp *utcp;
84
85         bool reapable;
86
87         // Callbacks
88
89         utcp_recv_t recv;
90         utcp_poll_t poll;
91
92         // TCP State
93
94         uint16_t src;
95         uint16_t dst;
96         enum state state;
97
98         struct {
99                 uint32_t una;
100                 uint32_t nxt;
101                 uint32_t wnd;
102                 uint32_t iss;
103
104                 uint32_t last;
105                 uint32_t cwnd;
106         } snd;
107
108         struct {
109                 uint32_t nxt;
110                 uint32_t wnd;
111                 uint32_t irs;
112         } rcv;
113
114         int dupack;
115
116         // Timers
117
118         struct timeval conn_timeout;
119         struct timeval rtrx_timeout;
120
121         // Send buffer
122
123         struct buffer sndbuf;
124
125         // Per-socket options
126
127         bool nodelay;
128         bool keepalive;
129
130         // Congestion avoidance state
131
132         struct timeval tlast;
133         uint64_t bandwidth;
134 };
135
136 struct utcp {
137         void *priv;
138
139         // Callbacks
140
141         utcp_accept_t accept;
142         utcp_pre_accept_t pre_accept;
143         utcp_send_t send;
144
145         // Global socket options
146
147         uint16_t mtu;
148         int timeout;
149
150         // Connection management
151
152         struct utcp_connection **connections;
153         int nconnections;
154         int nallocated;
155 };
156
157 #endif