]> git.meshlink.io Git - utcp/blob - utcp.c
Add IPv4 style fragments for UDP style channels.
[utcp] / utcp.c
1 /*
2     utcp.c -- Userspace TCP
3     Copyright (C) 2014-2017 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 #define _GNU_SOURCE
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <time.h>
31
32 #include "utcp_priv.h"
33
34 #ifndef EBADMSG
35 #define EBADMSG         104
36 #endif
37
38 #ifndef SHUT_RDWR
39 #define SHUT_RDWR 2
40 #endif
41
42 #ifdef poll
43 #undef poll
44 #endif
45
46 #ifndef UTCP_CLOCK
47 #if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
48 #define UTCP_CLOCK CLOCK_MONOTONIC_RAW
49 #else
50 #define UTCP_CLOCK CLOCK_MONOTONIC
51 #endif
52 #endif
53
54 static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
55         r->tv_sec = a->tv_sec - b->tv_sec;
56         r->tv_nsec = a->tv_nsec - b->tv_nsec;
57
58         if(r->tv_nsec < 0) {
59                 r->tv_sec--, r->tv_nsec += NSEC_PER_SEC;
60         }
61 }
62
63 static int32_t timespec_diff_usec(const struct timespec *a, const struct timespec *b) {
64         int64_t diff = (a->tv_sec - b->tv_sec) * 1000000000 + a->tv_sec - b->tv_sec;
65         return diff / 1000;
66 }
67
68 static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
69         if(a->tv_sec == b->tv_sec) {
70                 return a->tv_nsec < b->tv_nsec;
71         } else {
72                 return a->tv_sec < b->tv_sec;
73         }
74 }
75
76 static void timespec_clear(struct timespec *a) {
77         a->tv_sec = 0;
78 }
79
80 static bool timespec_isset(const struct timespec *a) {
81         return a->tv_sec;
82 }
83
84 static long CLOCK_GRANULARITY; // usec
85
86 static inline size_t min(size_t a, size_t b) {
87         return a < b ? a : b;
88 }
89
90 static inline size_t max(size_t a, size_t b) {
91         return a > b ? a : b;
92 }
93
94 #ifdef UTCP_DEBUG
95 #include <stdarg.h>
96
97 #ifndef UTCP_DEBUG_DATALEN
98 #define UTCP_DEBUG_DATALEN 20
99 #endif
100
101 static void debug(struct utcp_connection *c, const char *format, ...) {
102         struct timespec tv;
103         char buf[1024];
104         int len;
105
106         clock_gettime(CLOCK_REALTIME, &tv);
107         len = snprintf(buf, sizeof(buf), "%ld.%06lu %u:%u ", (long)tv.tv_sec, tv.tv_nsec / 1000, c ? c->src : 0, c ? c->dst : 0);
108         va_list ap;
109         va_start(ap, format);
110         len += vsnprintf(buf + len, sizeof(buf) - len, format, ap);
111         va_end(ap);
112
113         if(len > 0 && (size_t)len < sizeof(buf)) {
114                 fwrite(buf, len, 1, stderr);
115         }
116 }
117
118 static void print_packet(struct utcp_connection *c, const char *dir, const void *pkt, size_t len) {
119         struct hdr hdr;
120
121         if(len < sizeof(hdr)) {
122                 debug(c, "%s: short packet (%lu bytes)\n", dir, (unsigned long)len);
123                 return;
124         }
125
126         memcpy(&hdr, pkt, sizeof(hdr));
127
128         uint32_t datalen;
129
130         if(len > sizeof(hdr)) {
131                 datalen = min(len - sizeof(hdr), UTCP_DEBUG_DATALEN);
132         } else {
133                 datalen = 0;
134         }
135
136
137         const uint8_t *data = (uint8_t *)pkt + sizeof(hdr);
138         char str[datalen * 2 + 1];
139         char *p = str;
140
141         for(uint32_t i = 0; i < datalen; i++) {
142                 *p++ = "0123456789ABCDEF"[data[i] >> 4];
143                 *p++ = "0123456789ABCDEF"[data[i] & 15];
144         }
145
146         *p = 0;
147
148         debug(c, "%s: len %lu src %u dst %u seq %u ack %u wnd %u aux %x ctl %s%s%s%s%s data %s\n",
149               dir, (unsigned long)len, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd, hdr.aux,
150               hdr.ctl & SYN ? "SYN" : "",
151               hdr.ctl & RST ? "RST" : "",
152               hdr.ctl & FIN ? "FIN" : "",
153               hdr.ctl & ACK ? "ACK" : "",
154               hdr.ctl & MF ? "MF" : "",
155               str
156              );
157 }
158
159 static void debug_cwnd(struct utcp_connection *c) {
160         debug(c, "snd.cwnd %u snd.ssthresh %u\n", c->snd.cwnd, ~c->snd.ssthresh ? c->snd.ssthresh : 0);
161 }
162 #else
163 #define debug(...) do {} while(0)
164 #define print_packet(...) do {} while(0)
165 #define debug_cwnd(...) do {} while(0)
166 #endif
167
168 static void set_state(struct utcp_connection *c, enum state state) {
169         c->state = state;
170
171         if(state == ESTABLISHED) {
172                 timespec_clear(&c->conn_timeout);
173         }
174
175         debug(c, "state %s\n", strstate[state]);
176 }
177
178 static bool fin_wanted(struct utcp_connection *c, uint32_t seq) {
179         if(seq != c->snd.last) {
180                 return false;
181         }
182
183         switch(c->state) {
184         case FIN_WAIT_1:
185         case CLOSING:
186         case LAST_ACK:
187                 return true;
188
189         default:
190                 return false;
191         }
192 }
193
194 static bool is_reliable(struct utcp_connection *c) {
195         return c->flags & UTCP_RELIABLE;
196 }
197
198 static int32_t seqdiff(uint32_t a, uint32_t b) {
199         return a - b;
200 }
201
202 // Buffer functions
203 static bool buffer_wraps(struct buffer *buf) {
204         return buf->size - buf->offset < buf->used;
205 }
206
207 static bool buffer_resize(struct buffer *buf, uint32_t newsize) {
208         char *newdata = realloc(buf->data, newsize);
209
210         if(!newdata) {
211                 return false;
212         }
213
214         buf->data = newdata;
215
216         if(buffer_wraps(buf)) {
217                 // Shift the right part of the buffer until it hits the end of the new buffer.
218                 // Old situation:
219                 // [345......012]
220                 // New situation:
221                 // [345.........|........012]
222                 uint32_t tailsize = buf->size - buf->offset;
223                 uint32_t newoffset = newsize - tailsize;
224                 memmove(buf->data + newoffset, buf->data + buf->offset, tailsize);
225                 buf->offset = newoffset;
226         }
227
228         buf->size = newsize;
229         return true;
230 }
231
232 // Store data into the buffer
233 static ssize_t buffer_put_at(struct buffer *buf, size_t offset, const void *data, size_t len) {
234         debug(NULL, "buffer_put_at %lu %lu %lu\n", (unsigned long)buf->used, (unsigned long)offset, (unsigned long)len);
235
236         // Ensure we don't store more than maxsize bytes in total
237         size_t required = offset + len;
238
239         if(required > buf->maxsize) {
240                 if(offset >= buf->maxsize) {
241                         return 0;
242                 }
243
244                 len = buf->maxsize - offset;
245                 required = buf->maxsize;
246         }
247
248         // Check if we need to resize the buffer
249         if(required > buf->size) {
250                 size_t newsize = buf->size;
251
252                 if(!newsize) {
253                         newsize = 4096;
254                 }
255
256                 do {
257                         newsize *= 2;
258                 } while(newsize < required);
259
260                 if(newsize > buf->maxsize) {
261                         newsize = buf->maxsize;
262                 }
263
264                 if(!buffer_resize(buf, newsize)) {
265                         return -1;
266                 }
267         }
268
269         uint32_t realoffset = buf->offset + offset;
270
271         if(buf->size - buf->offset < offset) {
272                 // The offset wrapped
273                 realoffset -= buf->size;
274         }
275
276         if(buf->size - realoffset < len) {
277                 // The new chunk of data must be wrapped
278                 memcpy(buf->data + realoffset, data, buf->size - realoffset);
279                 memcpy(buf->data, (char *)data + buf->size - realoffset, len - (buf->size - realoffset));
280         } else {
281                 memcpy(buf->data + realoffset, data, len);
282         }
283
284         if(required > buf->used) {
285                 buf->used = required;
286         }
287
288         return len;
289 }
290
291 static ssize_t buffer_put(struct buffer *buf, const void *data, size_t len) {
292         return buffer_put_at(buf, buf->used, data, len);
293 }
294
295 // Copy data from the buffer without removing it.
296 static ssize_t buffer_copy(struct buffer *buf, void *data, size_t offset, size_t len) {
297         // Ensure we don't copy more than is actually stored in the buffer
298         if(offset >= buf->used) {
299                 return 0;
300         }
301
302         if(buf->used - offset < len) {
303                 len = buf->used - offset;
304         }
305
306         uint32_t realoffset = buf->offset + offset;
307
308         if(buf->size - buf->offset < offset) {
309                 // The offset wrapped
310                 realoffset -= buf->size;
311         }
312
313         if(buf->size - realoffset < len) {
314                 // The data is wrapped
315                 memcpy(data, buf->data + realoffset, buf->size - realoffset);
316                 memcpy((char *)data + buf->size - realoffset, buf->data, len - (buf->size - realoffset));
317         } else {
318                 memcpy(data, buf->data + realoffset, len);
319         }
320
321         return len;
322 }
323
324 // Copy data from the buffer without removing it.
325 static ssize_t buffer_call(struct buffer *buf, utcp_recv_t cb, void *arg, size_t offset, size_t len) {
326         // Ensure we don't copy more than is actually stored in the buffer
327         if(offset >= buf->used) {
328                 return 0;
329         }
330
331         if(buf->used - offset < len) {
332                 len = buf->used - offset;
333         }
334
335         uint32_t realoffset = buf->offset + offset;
336
337         if(buf->size - buf->offset < offset) {
338                 // The offset wrapped
339                 realoffset -= buf->size;
340         }
341
342         if(buf->size - realoffset < len) {
343                 // The data is wrapped
344                 ssize_t rx1 = cb(arg, buf->data + realoffset, buf->size - realoffset);
345
346                 if(rx1 < buf->size - realoffset) {
347                         return rx1;
348                 }
349
350                 ssize_t rx2 = cb(arg, buf->data, len - (buf->size - realoffset));
351
352                 if(rx2 < 0) {
353                         return rx2;
354                 } else {
355                         return rx1 + rx2;
356                 }
357         } else {
358                 return cb(arg, buf->data + realoffset, len);
359         }
360 }
361
362 // Discard data from the buffer.
363 static ssize_t buffer_discard(struct buffer *buf, size_t len) {
364         if(buf->used < len) {
365                 len = buf->used;
366         }
367
368         if(buf->size - buf->offset < len) {
369                 buf->offset -= buf->size;
370         }
371
372         if(buf->used == len) {
373                 buf->offset = 0;
374         } else {
375                 buf->offset += len;
376         }
377
378         buf->used -= len;
379
380         return len;
381 }
382
383 static void buffer_clear(struct buffer *buf) {
384         buf->used = 0;
385         buf->offset = 0;
386 }
387
388 static bool buffer_set_size(struct buffer *buf, uint32_t minsize, uint32_t maxsize) {
389         if(maxsize < minsize) {
390                 maxsize = minsize;
391         }
392
393         buf->maxsize = maxsize;
394
395         return buf->size >= minsize || buffer_resize(buf, minsize);
396 }
397
398 static void buffer_exit(struct buffer *buf) {
399         free(buf->data);
400         memset(buf, 0, sizeof(*buf));
401 }
402
403 static uint32_t buffer_free(const struct buffer *buf) {
404         return buf->maxsize - buf->used;
405 }
406
407 // Connections are stored in a sorted list.
408 // This gives O(log(N)) lookup time, O(N log(N)) insertion time and O(N) deletion time.
409
410 static int compare(const void *va, const void *vb) {
411         assert(va && vb);
412
413         const struct utcp_connection *a = *(struct utcp_connection **)va;
414         const struct utcp_connection *b = *(struct utcp_connection **)vb;
415
416         assert(a && b);
417         assert(a->src && b->src);
418
419         int c = (int)a->src - (int)b->src;
420
421         if(c) {
422                 return c;
423         }
424
425         c = (int)a->dst - (int)b->dst;
426         return c;
427 }
428
429 static struct utcp_connection *find_connection(const struct utcp *utcp, uint16_t src, uint16_t dst) {
430         if(!utcp->nconnections) {
431                 return NULL;
432         }
433
434         struct utcp_connection key = {
435                 .src = src,
436                 .dst = dst,
437         }, *keyp = &key;
438         struct utcp_connection **match = bsearch(&keyp, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
439         return match ? *match : NULL;
440 }
441
442 static void free_connection(struct utcp_connection *c) {
443         struct utcp *utcp = c->utcp;
444         struct utcp_connection **cp = bsearch(&c, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
445
446         assert(cp);
447
448         int i = cp - utcp->connections;
449         memmove(cp, cp + 1, (utcp->nconnections - i - 1) * sizeof(*cp));
450         utcp->nconnections--;
451
452         buffer_exit(&c->rcvbuf);
453         buffer_exit(&c->sndbuf);
454         free(c);
455 }
456
457 static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t src, uint16_t dst) {
458         // Check whether this combination of src and dst is free
459
460         if(src) {
461                 if(find_connection(utcp, src, dst)) {
462                         errno = EADDRINUSE;
463                         return NULL;
464                 }
465         } else { // If src == 0, generate a random port number with the high bit set
466                 if(utcp->nconnections >= 32767) {
467                         errno = ENOMEM;
468                         return NULL;
469                 }
470
471                 src = rand() | 0x8000;
472
473                 while(find_connection(utcp, src, dst)) {
474                         src++;
475                 }
476         }
477
478         // Allocate memory for the new connection
479
480         if(utcp->nconnections >= utcp->nallocated) {
481                 if(!utcp->nallocated) {
482                         utcp->nallocated = 4;
483                 } else {
484                         utcp->nallocated *= 2;
485                 }
486
487                 struct utcp_connection **new_array = realloc(utcp->connections, utcp->nallocated * sizeof(*utcp->connections));
488
489                 if(!new_array) {
490                         return NULL;
491                 }
492
493                 utcp->connections = new_array;
494         }
495
496         struct utcp_connection *c = calloc(1, sizeof(*c));
497
498         if(!c) {
499                 return NULL;
500         }
501
502         if(!buffer_set_size(&c->sndbuf, DEFAULT_SNDBUFSIZE, DEFAULT_MAXSNDBUFSIZE)) {
503                 free(c);
504                 return NULL;
505         }
506
507         if(!buffer_set_size(&c->rcvbuf, DEFAULT_RCVBUFSIZE, DEFAULT_MAXRCVBUFSIZE)) {
508                 buffer_exit(&c->sndbuf);
509                 free(c);
510                 return NULL;
511         }
512
513         // Fill in the details
514
515         c->src = src;
516         c->dst = dst;
517 #ifdef UTCP_DEBUG
518         c->snd.iss = 0;
519 #else
520         c->snd.iss = rand();
521 #endif
522         c->snd.una = c->snd.iss;
523         c->snd.nxt = c->snd.iss + 1;
524         c->snd.last = c->snd.nxt;
525         c->snd.cwnd = (utcp->mss > 2190 ? 2 : utcp->mss > 1095 ? 3 : 4) * utcp->mss;
526         c->snd.ssthresh = ~0;
527         debug_cwnd(c);
528         c->utcp = utcp;
529
530         // Add it to the sorted list of connections
531
532         utcp->connections[utcp->nconnections++] = c;
533         qsort(utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
534
535         return c;
536 }
537
538 static inline uint32_t absdiff(uint32_t a, uint32_t b) {
539         if(a > b) {
540                 return a - b;
541         } else {
542                 return b - a;
543         }
544 }
545
546 // Update RTT variables. See RFC 6298.
547 static void update_rtt(struct utcp_connection *c, uint32_t rtt) {
548         if(!rtt) {
549                 debug(c, "invalid rtt\n");
550                 return;
551         }
552
553         struct utcp *utcp = c->utcp;
554
555         if(!utcp->srtt) {
556                 utcp->srtt = rtt;
557                 utcp->rttvar = rtt / 2;
558         } else {
559                 utcp->rttvar = (utcp->rttvar * 3 + absdiff(utcp->srtt, rtt)) / 4;
560                 utcp->srtt = (utcp->srtt * 7 + rtt) / 8;
561         }
562
563         utcp->rto = utcp->srtt + max(4 * utcp->rttvar, CLOCK_GRANULARITY);
564
565         if(utcp->rto > MAX_RTO) {
566                 utcp->rto = MAX_RTO;
567         }
568
569         debug(c, "rtt %u srtt %u rttvar %u rto %u\n", rtt, utcp->srtt, utcp->rttvar, utcp->rto);
570 }
571
572 static void start_retransmit_timer(struct utcp_connection *c) {
573         clock_gettime(UTCP_CLOCK, &c->rtrx_timeout);
574
575         uint32_t rto = c->utcp->rto;
576
577         while(rto > USEC_PER_SEC) {
578                 c->rtrx_timeout.tv_sec++;
579                 rto -= USEC_PER_SEC;
580         }
581
582         c->rtrx_timeout.tv_nsec += c->utcp->rto * 1000;
583
584         if(c->rtrx_timeout.tv_nsec >= NSEC_PER_SEC) {
585                 c->rtrx_timeout.tv_nsec -= NSEC_PER_SEC;
586                 c->rtrx_timeout.tv_sec++;
587         }
588
589         debug(c, "rtrx_timeout %ld.%06lu\n", c->rtrx_timeout.tv_sec, c->rtrx_timeout.tv_nsec);
590 }
591
592 static void stop_retransmit_timer(struct utcp_connection *c) {
593         timespec_clear(&c->rtrx_timeout);
594         debug(c, "rtrx_timeout cleared\n");
595 }
596
597 struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv, uint32_t flags) {
598         struct utcp_connection *c = allocate_connection(utcp, 0, dst);
599
600         if(!c) {
601                 return NULL;
602         }
603
604         assert((flags & ~0x1f) == 0);
605
606         c->flags = flags;
607         c->recv = recv;
608         c->priv = priv;
609
610         struct {
611                 struct hdr hdr;
612                 uint8_t init[4];
613         } pkt;
614
615         pkt.hdr.src = c->src;
616         pkt.hdr.dst = c->dst;
617         pkt.hdr.seq = c->snd.iss;
618         pkt.hdr.ack = 0;
619         pkt.hdr.wnd = c->rcvbuf.maxsize;
620         pkt.hdr.ctl = SYN;
621         pkt.hdr.aux = 0x0101;
622         pkt.init[0] = 1;
623         pkt.init[1] = 0;
624         pkt.init[2] = 0;
625         pkt.init[3] = flags & 0x7;
626
627         set_state(c, SYN_SENT);
628
629         print_packet(c, "send", &pkt, sizeof(pkt));
630         utcp->send(utcp, &pkt, sizeof(pkt));
631
632         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
633         c->conn_timeout.tv_sec += utcp->timeout;
634
635         start_retransmit_timer(c);
636
637         return c;
638 }
639
640 struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
641         return utcp_connect_ex(utcp, dst, recv, priv, UTCP_TCP);
642 }
643
644 void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
645         if(c->reapable || c->state != SYN_RECEIVED) {
646                 debug(c, "accept() called on invalid connection in state %s\n", c, strstate[c->state]);
647                 return;
648         }
649
650         debug(c, "accepted %p %p\n", c, recv, priv);
651         c->recv = recv;
652         c->priv = priv;
653         set_state(c, ESTABLISHED);
654 }
655
656 static void ack(struct utcp_connection *c, bool sendatleastone) {
657         int32_t left = seqdiff(c->snd.last, c->snd.nxt);
658         int32_t cwndleft = is_reliable(c) ? min(c->snd.cwnd, c->snd.wnd) - seqdiff(c->snd.nxt, c->snd.una) : MAX_UNRELIABLE_SIZE;
659
660         assert(left >= 0);
661
662         if(cwndleft <= 0) {
663                 left = 0;
664         } else if(cwndleft < left) {
665                 left = cwndleft;
666
667                 if(!sendatleastone || cwndleft > c->utcp->mss) {
668                         left -= left % c->utcp->mss;
669                 }
670         }
671
672         debug(c, "cwndleft %d left %d\n", cwndleft, left);
673
674         if(!left && !sendatleastone) {
675                 return;
676         }
677
678         struct {
679                 struct hdr hdr;
680                 uint8_t data[];
681         } *pkt = c->utcp->pkt;
682
683         pkt->hdr.src = c->src;
684         pkt->hdr.dst = c->dst;
685         pkt->hdr.ack = c->rcv.nxt;
686         pkt->hdr.wnd = is_reliable(c) ? c->rcvbuf.maxsize : 0;
687         pkt->hdr.ctl = ACK;
688         pkt->hdr.aux = 0;
689
690         do {
691                 uint32_t seglen = left > c->utcp->mss ? c->utcp->mss : left;
692                 pkt->hdr.seq = c->snd.nxt;
693
694                 buffer_copy(&c->sndbuf, pkt->data, seqdiff(c->snd.nxt, c->snd.una), seglen);
695
696                 c->snd.nxt += seglen;
697                 left -= seglen;
698
699                 if(!is_reliable(c)) {
700                         if(left) {
701                                 pkt->hdr.ctl |= MF;
702                         } else {
703                                 pkt->hdr.ctl &= ~MF;
704                         }
705                 }
706
707                 if(seglen && fin_wanted(c, c->snd.nxt)) {
708                         seglen--;
709                         pkt->hdr.ctl |= FIN;
710                 }
711
712                 if(!c->rtt_start.tv_sec) {
713                         // Start RTT measurement
714                         clock_gettime(UTCP_CLOCK, &c->rtt_start);
715                         c->rtt_seq = pkt->hdr.seq + seglen;
716                         debug(c, "starting RTT measurement, expecting ack %u\n", c->rtt_seq);
717                 }
718
719                 print_packet(c, "send", pkt, sizeof(pkt->hdr) + seglen);
720                 c->utcp->send(c->utcp, pkt, sizeof(pkt->hdr) + seglen);
721
722                 if(left && !is_reliable(c)) {
723                         pkt->hdr.wnd += seglen;
724                 }
725         } while(left);
726 }
727
728 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
729         if(c->reapable) {
730                 debug(c, "send() called on closed connection\n");
731                 errno = EBADF;
732                 return -1;
733         }
734
735         switch(c->state) {
736         case CLOSED:
737         case LISTEN:
738                 debug(c, "send() called on unconnected connection\n");
739                 errno = ENOTCONN;
740                 return -1;
741
742         case SYN_SENT:
743         case SYN_RECEIVED:
744         case ESTABLISHED:
745         case CLOSE_WAIT:
746                 break;
747
748         case FIN_WAIT_1:
749         case FIN_WAIT_2:
750         case CLOSING:
751         case LAST_ACK:
752         case TIME_WAIT:
753                 debug(c, "send() called on closed connection\n");
754                 errno = EPIPE;
755                 return -1;
756         }
757
758         // Exit early if we have nothing to send.
759
760         if(!len) {
761                 return 0;
762         }
763
764         if(!data) {
765                 errno = EFAULT;
766                 return -1;
767         }
768
769         // Check if we need to be able to buffer all data
770
771         if(c->flags & UTCP_NO_PARTIAL) {
772                 if(len > buffer_free(&c->sndbuf)) {
773                         if(len > c->sndbuf.maxsize) {
774                                 errno = EMSGSIZE;
775                                 return -1;
776                         } else {
777                                 errno = EWOULDBLOCK;
778                                 return 0;
779                         }
780                 }
781         }
782
783         // Add data to send buffer.
784
785         if(is_reliable(c)) {
786                 len = buffer_put(&c->sndbuf, data, len);
787         } else if(c->state != SYN_SENT && c->state != SYN_RECEIVED) {
788                 if(len > MAX_UNRELIABLE_SIZE || buffer_put(&c->sndbuf, data, len) != (ssize_t)len) {
789                         errno = EMSGSIZE;
790                         return -1;
791                 }
792         } else {
793                 return 0;
794         }
795
796         if(len <= 0) {
797                 if(is_reliable(c)) {
798                         errno = EWOULDBLOCK;
799                         return 0;
800                 } else {
801                         return len;
802                 }
803         }
804
805         c->snd.last += len;
806
807         // Don't send anything yet if the connection has not fully established yet
808
809         if(c->state == SYN_SENT || c->state == SYN_RECEIVED) {
810                 return len;
811         }
812
813         ack(c, false);
814
815         if(!is_reliable(c)) {
816                 c->snd.una = c->snd.nxt = c->snd.last;
817                 buffer_discard(&c->sndbuf, c->sndbuf.used);
818                 c->do_poll = true;
819         }
820
821         if(is_reliable(c) && !timespec_isset(&c->rtrx_timeout)) {
822                 start_retransmit_timer(c);
823         }
824
825         if(is_reliable(c) && !timespec_isset(&c->conn_timeout)) {
826                 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
827                 c->conn_timeout.tv_sec += c->utcp->timeout;
828         }
829
830         return len;
831 }
832
833 static void swap_ports(struct hdr *hdr) {
834         uint16_t tmp = hdr->src;
835         hdr->src = hdr->dst;
836         hdr->dst = tmp;
837 }
838
839 static void fast_retransmit(struct utcp_connection *c) {
840         if(c->state == CLOSED || c->snd.last == c->snd.una) {
841                 debug(c, "fast_retransmit() called but nothing to retransmit!\n");
842                 return;
843         }
844
845         struct utcp *utcp = c->utcp;
846
847         struct {
848                 struct hdr hdr;
849                 uint8_t data[];
850         } *pkt;
851
852         pkt = malloc(c->utcp->mtu);
853
854         if(!pkt) {
855                 return;
856         }
857
858         pkt->hdr.src = c->src;
859         pkt->hdr.dst = c->dst;
860         pkt->hdr.wnd = c->rcvbuf.maxsize;
861         pkt->hdr.aux = 0;
862
863         switch(c->state) {
864         case ESTABLISHED:
865         case FIN_WAIT_1:
866         case CLOSE_WAIT:
867         case CLOSING:
868         case LAST_ACK:
869                 // Send unacked data again.
870                 pkt->hdr.seq = c->snd.una;
871                 pkt->hdr.ack = c->rcv.nxt;
872                 pkt->hdr.ctl = ACK;
873                 uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
874
875                 if(fin_wanted(c, c->snd.una + len)) {
876                         len--;
877                         pkt->hdr.ctl |= FIN;
878                 }
879
880                 buffer_copy(&c->sndbuf, pkt->data, 0, len);
881                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
882                 utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
883                 break;
884
885         default:
886                 break;
887         }
888
889         free(pkt);
890 }
891
892 static void retransmit(struct utcp_connection *c) {
893         if(c->state == CLOSED || c->snd.last == c->snd.una) {
894                 debug(c, "retransmit() called but nothing to retransmit!\n");
895                 stop_retransmit_timer(c);
896                 return;
897         }
898
899         struct utcp *utcp = c->utcp;
900
901         struct {
902                 struct hdr hdr;
903                 uint8_t data[];
904         } *pkt = c->utcp->pkt;
905
906         pkt->hdr.src = c->src;
907         pkt->hdr.dst = c->dst;
908         pkt->hdr.wnd = c->rcvbuf.maxsize;
909         pkt->hdr.aux = 0;
910
911         switch(c->state) {
912         case SYN_SENT:
913                 // Send our SYN again
914                 pkt->hdr.seq = c->snd.iss;
915                 pkt->hdr.ack = 0;
916                 pkt->hdr.ctl = SYN;
917                 pkt->hdr.aux = 0x0101;
918                 pkt->data[0] = 1;
919                 pkt->data[1] = 0;
920                 pkt->data[2] = 0;
921                 pkt->data[3] = c->flags & 0x7;
922                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + 4);
923                 utcp->send(utcp, pkt, sizeof(pkt->hdr) + 4);
924                 break;
925
926         case SYN_RECEIVED:
927                 // Send SYNACK again
928                 pkt->hdr.seq = c->snd.nxt;
929                 pkt->hdr.ack = c->rcv.nxt;
930                 pkt->hdr.ctl = SYN | ACK;
931                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr));
932                 utcp->send(utcp, pkt, sizeof(pkt->hdr));
933                 break;
934
935         case ESTABLISHED:
936         case FIN_WAIT_1:
937         case CLOSE_WAIT:
938         case CLOSING:
939         case LAST_ACK:
940                 // Send unacked data again.
941                 pkt->hdr.seq = c->snd.una;
942                 pkt->hdr.ack = c->rcv.nxt;
943                 pkt->hdr.ctl = ACK;
944                 uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
945
946                 if(fin_wanted(c, c->snd.una + len)) {
947                         len--;
948                         pkt->hdr.ctl |= FIN;
949                 }
950
951                 // RFC 5681 slow start after timeout
952                 uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
953                 c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
954                 c->snd.cwnd = utcp->mss;
955                 debug_cwnd(c);
956
957                 buffer_copy(&c->sndbuf, pkt->data, 0, len);
958                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
959                 utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
960
961                 c->snd.nxt = c->snd.una + len;
962                 break;
963
964         case CLOSED:
965         case LISTEN:
966         case TIME_WAIT:
967         case FIN_WAIT_2:
968                 // We shouldn't need to retransmit anything in this state.
969 #ifdef UTCP_DEBUG
970                 abort();
971 #endif
972                 stop_retransmit_timer(c);
973                 goto cleanup;
974         }
975
976         start_retransmit_timer(c);
977         utcp->rto *= 2;
978
979         if(utcp->rto > MAX_RTO) {
980                 utcp->rto = MAX_RTO;
981         }
982
983         c->rtt_start.tv_sec = 0; // invalidate RTT timer
984         c->dupack = 0; // cancel any ongoing fast recovery
985
986 cleanup:
987         return;
988 }
989
990 /* Update receive buffer and SACK entries after consuming data.
991  *
992  * Situation:
993  *
994  * |.....0000..1111111111.....22222......3333|
995  * |---------------^
996  *
997  * 0..3 represent the SACK entries. The ^ indicates up to which point we want
998  * to remove data from the receive buffer. The idea is to substract "len"
999  * from the offset of all the SACK entries, and then remove/cut down entries
1000  * that are shifted to before the start of the receive buffer.
1001  *
1002  * There are three cases:
1003  * - the SACK entry is after ^, in that case just change the offset.
1004  * - the SACK entry starts before and ends after ^, so we have to
1005  *   change both its offset and size.
1006  * - the SACK entry is completely before ^, in that case delete it.
1007  */
1008 static void sack_consume(struct utcp_connection *c, size_t len) {
1009         debug(c, "sack_consume %lu\n", (unsigned long)len);
1010
1011         if(len > c->rcvbuf.used) {
1012                 debug(c, "all SACK entries consumed\n");
1013                 c->sacks[0].len = 0;
1014                 return;
1015         }
1016
1017         buffer_discard(&c->rcvbuf, len);
1018
1019         for(int i = 0; i < NSACKS && c->sacks[i].len;) {
1020                 if(len < c->sacks[i].offset) {
1021                         c->sacks[i].offset -= len;
1022                         i++;
1023                 } else if(len < c->sacks[i].offset + c->sacks[i].len) {
1024                         c->sacks[i].len -= len - c->sacks[i].offset;
1025                         c->sacks[i].offset = 0;
1026                         i++;
1027                 } else {
1028                         if(i < NSACKS - 1) {
1029                                 memmove(&c->sacks[i], &c->sacks[i + 1], (NSACKS - 1 - i) * sizeof(c->sacks)[i]);
1030                                 c->sacks[NSACKS - 1].len = 0;
1031                         } else {
1032                                 c->sacks[i].len = 0;
1033                                 break;
1034                         }
1035                 }
1036         }
1037
1038         for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
1039                 debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
1040         }
1041 }
1042
1043 static void handle_out_of_order(struct utcp_connection *c, uint32_t offset, const void *data, size_t len) {
1044         debug(c, "out of order packet, offset %u\n", offset);
1045         // Packet loss or reordering occured. Store the data in the buffer.
1046         ssize_t rxd = buffer_put_at(&c->rcvbuf, offset, data, len);
1047
1048         if(rxd < 0 || (size_t)rxd < len) {
1049                 abort();
1050         }
1051
1052         // Make note of where we put it.
1053         for(int i = 0; i < NSACKS; i++) {
1054                 if(!c->sacks[i].len) { // nothing to merge, add new entry
1055                         debug(c, "new SACK entry %d\n", i);
1056                         c->sacks[i].offset = offset;
1057                         c->sacks[i].len = rxd;
1058                         break;
1059                 } else if(offset < c->sacks[i].offset) {
1060                         if(offset + rxd < c->sacks[i].offset) { // insert before
1061                                 if(!c->sacks[NSACKS - 1].len) { // only if room left
1062                                         debug(c, "insert SACK entry at %d\n", i);
1063                                         memmove(&c->sacks[i + 1], &c->sacks[i], (NSACKS - i - 1) * sizeof(c->sacks)[i]);
1064                                         c->sacks[i].offset = offset;
1065                                         c->sacks[i].len = rxd;
1066                                 } else {
1067                                         debug(c, "SACK entries full, dropping packet\n");
1068                                 }
1069
1070                                 break;
1071                         } else { // merge
1072                                 debug(c, "merge with start of SACK entry at %d\n", i);
1073                                 c->sacks[i].offset = offset;
1074                                 break;
1075                         }
1076                 } else if(offset <= c->sacks[i].offset + c->sacks[i].len) {
1077                         if(offset + rxd > c->sacks[i].offset + c->sacks[i].len) { // merge
1078                                 debug(c, "merge with end of SACK entry at %d\n", i);
1079                                 c->sacks[i].len = offset + rxd - c->sacks[i].offset;
1080                                 // TODO: handle potential merge with next entry
1081                         }
1082
1083                         break;
1084                 }
1085         }
1086
1087         for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
1088                 debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
1089         }
1090 }
1091
1092 static void handle_in_order(struct utcp_connection *c, const void *data, size_t len) {
1093         if(c->recv) {
1094                 ssize_t rxd = c->recv(c, data, len);
1095
1096                 if(rxd != (ssize_t)len) {
1097                         // TODO: handle the application not accepting all data.
1098                         abort();
1099                 }
1100         }
1101
1102         // Check if we can process out-of-order data now.
1103         if(c->sacks[0].len && len >= c->sacks[0].offset) {
1104                 debug(c, "incoming packet len %lu connected with SACK at %u\n", (unsigned long)len, c->sacks[0].offset);
1105
1106                 if(len < c->sacks[0].offset + c->sacks[0].len) {
1107                         size_t offset = len;
1108                         len = c->sacks[0].offset + c->sacks[0].len;
1109                         size_t remainder = len - offset;
1110                         ssize_t rxd = buffer_call(&c->rcvbuf, c->recv, c, offset, remainder);
1111
1112                         if(rxd != (ssize_t)remainder) {
1113                                 // TODO: handle the application not accepting all data.
1114                                 abort();
1115                         }
1116                 }
1117         }
1118
1119         if(c->rcvbuf.used) {
1120                 sack_consume(c, len);
1121         }
1122
1123         c->rcv.nxt += len;
1124 }
1125
1126 static void handle_unreliable(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
1127         // Fast path for unfragmented packets
1128         if(!hdr->wnd && !(hdr->ctl & MF)) {
1129                 c->recv(c, data, len);
1130                 c->rcv.nxt = hdr->seq + len;
1131                 return;
1132         }
1133
1134         // Ensure reassembled packet are not larger than 64 kiB
1135         if(hdr->wnd >= MAX_UNRELIABLE_SIZE || hdr->wnd + len > MAX_UNRELIABLE_SIZE) {
1136                 return;
1137         }
1138
1139         // Don't accept out of order fragments
1140         if(hdr->wnd && hdr->seq != c->rcv.nxt) {
1141                 return;
1142         }
1143
1144         // Reset the receive buffer for the first fragment
1145         if(!hdr->wnd) {
1146                 buffer_clear(&c->rcvbuf);
1147         }
1148
1149         ssize_t rxd = buffer_put_at(&c->rcvbuf, hdr->wnd, data, len);
1150
1151         if(rxd != (ssize_t)len) {
1152                 return;
1153         }
1154
1155         // Send the packet if it's the final fragment
1156         if(!(hdr->ctl & MF)) {
1157                 buffer_call(&c->rcvbuf, c->recv, c, 0, hdr->wnd + len);
1158         }
1159
1160         c->rcv.nxt = hdr->seq + len;
1161 }
1162
1163 static void handle_incoming_data(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
1164         if(!is_reliable(c)) {
1165                 handle_unreliable(c, hdr, data, len);
1166                 return;
1167         }
1168
1169         uint32_t offset = seqdiff(hdr->seq, c->rcv.nxt);
1170
1171         if(offset + len > c->rcvbuf.maxsize) {
1172                 abort();
1173         }
1174
1175         if(offset) {
1176                 handle_out_of_order(c, offset, data, len);
1177         } else {
1178                 handle_in_order(c, data, len);
1179         }
1180 }
1181
1182
1183 ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
1184         const uint8_t *ptr = data;
1185
1186         if(!utcp) {
1187                 errno = EFAULT;
1188                 return -1;
1189         }
1190
1191         if(!len) {
1192                 return 0;
1193         }
1194
1195         if(!data) {
1196                 errno = EFAULT;
1197                 return -1;
1198         }
1199
1200         // Drop packets smaller than the header
1201
1202         struct hdr hdr;
1203
1204         if(len < sizeof(hdr)) {
1205                 print_packet(NULL, "recv", data, len);
1206                 errno = EBADMSG;
1207                 return -1;
1208         }
1209
1210         // Make a copy from the potentially unaligned data to a struct hdr
1211
1212         memcpy(&hdr, ptr, sizeof(hdr));
1213
1214         // Try to match the packet to an existing connection
1215
1216         struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
1217         print_packet(c, "recv", data, len);
1218
1219         // Process the header
1220
1221         ptr += sizeof(hdr);
1222         len -= sizeof(hdr);
1223
1224         // Drop packets with an unknown CTL flag
1225
1226         if(hdr.ctl & ~(SYN | ACK | RST | FIN | MF)) {
1227                 print_packet(NULL, "recv", data, len);
1228                 errno = EBADMSG;
1229                 return -1;
1230         }
1231
1232         // Check for auxiliary headers
1233
1234         const uint8_t *init = NULL;
1235
1236         uint16_t aux = hdr.aux;
1237
1238         while(aux) {
1239                 size_t auxlen = 4 * (aux >> 8) & 0xf;
1240                 uint8_t auxtype = aux & 0xff;
1241
1242                 if(len < auxlen) {
1243                         errno = EBADMSG;
1244                         return -1;
1245                 }
1246
1247                 switch(auxtype) {
1248                 case AUX_INIT:
1249                         if(!(hdr.ctl & SYN) || auxlen != 4) {
1250                                 errno = EBADMSG;
1251                                 return -1;
1252                         }
1253
1254                         init = ptr;
1255                         break;
1256
1257                 default:
1258                         errno = EBADMSG;
1259                         return -1;
1260                 }
1261
1262                 len -= auxlen;
1263                 ptr += auxlen;
1264
1265                 if(!(aux & 0x800)) {
1266                         break;
1267                 }
1268
1269                 if(len < 2) {
1270                         errno = EBADMSG;
1271                         return -1;
1272                 }
1273
1274                 memcpy(&aux, ptr, 2);
1275                 len -= 2;
1276                 ptr += 2;
1277         }
1278
1279         bool has_data = len || (hdr.ctl & (SYN | FIN));
1280
1281         // Is it for a new connection?
1282
1283         if(!c) {
1284                 // Ignore RST packets
1285
1286                 if(hdr.ctl & RST) {
1287                         return 0;
1288                 }
1289
1290                 // Is it a SYN packet and are we LISTENing?
1291
1292                 if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept) {
1293                         // If we don't want to accept it, send a RST back
1294                         if((utcp->pre_accept && !utcp->pre_accept(utcp, hdr.dst))) {
1295                                 len = 1;
1296                                 goto reset;
1297                         }
1298
1299                         // Try to allocate memory, otherwise send a RST back
1300                         c = allocate_connection(utcp, hdr.dst, hdr.src);
1301
1302                         if(!c) {
1303                                 len = 1;
1304                                 goto reset;
1305                         }
1306
1307                         // Parse auxilliary information
1308                         if(init) {
1309                                 if(init[0] < 1) {
1310                                         len = 1;
1311                                         goto reset;
1312                                 }
1313
1314                                 c->flags = init[3] & 0x7;
1315                         } else {
1316                                 c->flags = UTCP_TCP;
1317                         }
1318
1319 synack:
1320                         // Return SYN+ACK, go to SYN_RECEIVED state
1321                         c->snd.wnd = hdr.wnd;
1322                         c->rcv.irs = hdr.seq;
1323                         c->rcv.nxt = c->rcv.irs + 1;
1324                         set_state(c, SYN_RECEIVED);
1325
1326                         struct {
1327                                 struct hdr hdr;
1328                                 uint8_t data[4];
1329                         } pkt;
1330
1331                         pkt.hdr.src = c->src;
1332                         pkt.hdr.dst = c->dst;
1333                         pkt.hdr.ack = c->rcv.irs + 1;
1334                         pkt.hdr.seq = c->snd.iss;
1335                         pkt.hdr.wnd = c->rcvbuf.maxsize;
1336                         pkt.hdr.ctl = SYN | ACK;
1337
1338                         if(init) {
1339                                 pkt.hdr.aux = 0x0101;
1340                                 pkt.data[0] = 1;
1341                                 pkt.data[1] = 0;
1342                                 pkt.data[2] = 0;
1343                                 pkt.data[3] = c->flags & 0x7;
1344                                 print_packet(c, "send", &pkt, sizeof(hdr) + 4);
1345                                 utcp->send(utcp, &pkt, sizeof(hdr) + 4);
1346                         } else {
1347                                 pkt.hdr.aux = 0;
1348                                 print_packet(c, "send", &pkt, sizeof(hdr));
1349                                 utcp->send(utcp, &pkt, sizeof(hdr));
1350                         }
1351                 } else {
1352                         // No, we don't want your packets, send a RST back
1353                         len = 1;
1354                         goto reset;
1355                 }
1356
1357                 return 0;
1358         }
1359
1360         debug(c, "state %s\n", strstate[c->state]);
1361
1362         // In case this is for a CLOSED connection, ignore the packet.
1363         // TODO: make it so incoming packets can never match a CLOSED connection.
1364
1365         if(c->state == CLOSED) {
1366                 debug(c, "got packet for closed connection\n");
1367                 return 0;
1368         }
1369
1370         // It is for an existing connection.
1371
1372         // 1. Drop invalid packets.
1373
1374         // 1a. Drop packets that should not happen in our current state.
1375
1376         switch(c->state) {
1377         case SYN_SENT:
1378         case SYN_RECEIVED:
1379         case ESTABLISHED:
1380         case FIN_WAIT_1:
1381         case FIN_WAIT_2:
1382         case CLOSE_WAIT:
1383         case CLOSING:
1384         case LAST_ACK:
1385         case TIME_WAIT:
1386                 break;
1387
1388         default:
1389 #ifdef UTCP_DEBUG
1390                 abort();
1391 #endif
1392                 break;
1393         }
1394
1395         // 1b. Discard data that is not in our receive window.
1396
1397         if(is_reliable(c)) {
1398                 bool acceptable;
1399
1400                 if(c->state == SYN_SENT) {
1401                         acceptable = true;
1402                 } else if(len == 0) {
1403                         acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0;
1404                 } else {
1405                         int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
1406
1407                         // cut already accepted front overlapping
1408                         if(rcv_offset < 0) {
1409                                 acceptable = len > (size_t) - rcv_offset;
1410
1411                                 if(acceptable) {
1412                                         ptr -= rcv_offset;
1413                                         len += rcv_offset;
1414                                         hdr.seq -= rcv_offset;
1415                                 }
1416                         } else {
1417                                 acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt) + len <= c->rcvbuf.maxsize;
1418                         }
1419                 }
1420
1421                 if(!acceptable) {
1422                         debug(c, "packet not acceptable, %u <= %u + %lu < %u\n", c->rcv.nxt, hdr.seq, (unsigned long)len, c->rcv.nxt + c->rcvbuf.maxsize);
1423
1424                         // Ignore unacceptable RST packets.
1425                         if(hdr.ctl & RST) {
1426                                 return 0;
1427                         }
1428
1429                         // Otherwise, continue processing.
1430                         len = 0;
1431                 }
1432         } else {
1433 #if UTCP_DEBUG
1434                 int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
1435
1436                 if(rcv_offset) {
1437                         debug(c, "packet out of order, offset %u bytes", rcv_offset);
1438                 }
1439
1440 #endif
1441         }
1442
1443         c->snd.wnd = hdr.wnd; // TODO: move below
1444
1445         // 1c. Drop packets with an invalid ACK.
1446         // ackno should not roll back, and it should also not be bigger than what we ever could have sent
1447         // (= snd.una + c->sndbuf.used).
1448
1449         if(!is_reliable(c)) {
1450                 if(hdr.ack != c->snd.last && c->state >= ESTABLISHED) {
1451                         hdr.ack = c->snd.una;
1452                 }
1453         }
1454
1455         if(hdr.ctl & ACK && (seqdiff(hdr.ack, c->snd.last) > 0 || seqdiff(hdr.ack, c->snd.una) < 0)) {
1456                 debug(c, "packet ack seqno out of range, %u <= %u < %u\n", c->snd.una, hdr.ack, c->snd.una + c->sndbuf.used);
1457
1458                 // Ignore unacceptable RST packets.
1459                 if(hdr.ctl & RST) {
1460                         return 0;
1461                 }
1462
1463                 goto reset;
1464         }
1465
1466         // 2. Handle RST packets
1467
1468         if(hdr.ctl & RST) {
1469                 switch(c->state) {
1470                 case SYN_SENT:
1471                         if(!(hdr.ctl & ACK)) {
1472                                 return 0;
1473                         }
1474
1475                         // The peer has refused our connection.
1476                         set_state(c, CLOSED);
1477                         errno = ECONNREFUSED;
1478
1479                         if(c->recv) {
1480                                 c->recv(c, NULL, 0);
1481                         }
1482
1483                         if(c->poll && !c->reapable) {
1484                                 c->poll(c, 0);
1485                         }
1486
1487                         return 0;
1488
1489                 case SYN_RECEIVED:
1490                         if(hdr.ctl & ACK) {
1491                                 return 0;
1492                         }
1493
1494                         // We haven't told the application about this connection yet. Silently delete.
1495                         free_connection(c);
1496                         return 0;
1497
1498                 case ESTABLISHED:
1499                 case FIN_WAIT_1:
1500                 case FIN_WAIT_2:
1501                 case CLOSE_WAIT:
1502                         if(hdr.ctl & ACK) {
1503                                 return 0;
1504                         }
1505
1506                         // The peer has aborted our connection.
1507                         set_state(c, CLOSED);
1508                         errno = ECONNRESET;
1509
1510                         if(c->recv) {
1511                                 c->recv(c, NULL, 0);
1512                         }
1513
1514                         if(c->poll && !c->reapable) {
1515                                 c->poll(c, 0);
1516                         }
1517
1518                         return 0;
1519
1520                 case CLOSING:
1521                 case LAST_ACK:
1522                 case TIME_WAIT:
1523                         if(hdr.ctl & ACK) {
1524                                 return 0;
1525                         }
1526
1527                         // As far as the application is concerned, the connection has already been closed.
1528                         // If it has called utcp_close() already, we can immediately free this connection.
1529                         if(c->reapable) {
1530                                 free_connection(c);
1531                                 return 0;
1532                         }
1533
1534                         // Otherwise, immediately move to the CLOSED state.
1535                         set_state(c, CLOSED);
1536                         return 0;
1537
1538                 default:
1539 #ifdef UTCP_DEBUG
1540                         abort();
1541 #endif
1542                         break;
1543                 }
1544         }
1545
1546         uint32_t advanced;
1547
1548         if(!(hdr.ctl & ACK)) {
1549                 advanced = 0;
1550                 goto skip_ack;
1551         }
1552
1553         // 3. Advance snd.una
1554
1555         advanced = seqdiff(hdr.ack, c->snd.una);
1556
1557         if(advanced) {
1558                 // RTT measurement
1559                 if(c->rtt_start.tv_sec) {
1560                         if(c->rtt_seq == hdr.ack) {
1561                                 struct timespec now;
1562                                 clock_gettime(UTCP_CLOCK, &now);
1563                                 int32_t diff = timespec_diff_usec(&now, &c->rtt_start);
1564                                 update_rtt(c, diff);
1565                                 c->rtt_start.tv_sec = 0;
1566                         } else if(c->rtt_seq < hdr.ack) {
1567                                 debug(c, "cancelling RTT measurement: %u < %u\n", c->rtt_seq, hdr.ack);
1568                                 c->rtt_start.tv_sec = 0;
1569                         }
1570                 }
1571
1572                 int32_t data_acked = advanced;
1573
1574                 switch(c->state) {
1575                 case SYN_SENT:
1576                 case SYN_RECEIVED:
1577                         data_acked--;
1578                         break;
1579
1580                 // TODO: handle FIN as well.
1581                 default:
1582                         break;
1583                 }
1584
1585                 assert(data_acked >= 0);
1586
1587 #ifndef NDEBUG
1588                 int32_t bufused = seqdiff(c->snd.last, c->snd.una);
1589                 assert(data_acked <= bufused);
1590 #endif
1591
1592                 if(data_acked) {
1593                         buffer_discard(&c->sndbuf, data_acked);
1594                         c->do_poll = true;
1595                 }
1596
1597                 // Also advance snd.nxt if possible
1598                 if(seqdiff(c->snd.nxt, hdr.ack) < 0) {
1599                         c->snd.nxt = hdr.ack;
1600                 }
1601
1602                 c->snd.una = hdr.ack;
1603
1604                 if(c->dupack) {
1605                         if(c->dupack >= 3) {
1606                                 debug(c, "fast recovery ended\n");
1607                                 c->snd.cwnd = c->snd.ssthresh;
1608                         }
1609
1610                         c->dupack = 0;
1611                 }
1612
1613                 // Increase the congestion window according to RFC 5681
1614                 if(c->snd.cwnd < c->snd.ssthresh) {
1615                         c->snd.cwnd += min(advanced, utcp->mss); // eq. 2
1616                 } else {
1617                         c->snd.cwnd += max(1, (utcp->mss * utcp->mss) / c->snd.cwnd); // eq. 3
1618                 }
1619
1620                 if(c->snd.cwnd > c->sndbuf.maxsize) {
1621                         c->snd.cwnd = c->sndbuf.maxsize;
1622                 }
1623
1624                 debug_cwnd(c);
1625
1626                 // Check if we have sent a FIN that is now ACKed.
1627                 switch(c->state) {
1628                 case FIN_WAIT_1:
1629                         if(c->snd.una == c->snd.last) {
1630                                 set_state(c, FIN_WAIT_2);
1631                         }
1632
1633                         break;
1634
1635                 case CLOSING:
1636                         if(c->snd.una == c->snd.last) {
1637                                 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1638                                 c->conn_timeout.tv_sec += utcp->timeout;
1639                                 set_state(c, TIME_WAIT);
1640                         }
1641
1642                         break;
1643
1644                 default:
1645                         break;
1646                 }
1647         } else {
1648                 if(!len && is_reliable(c) && c->snd.una != c->snd.last) {
1649                         c->dupack++;
1650                         debug(c, "duplicate ACK %d\n", c->dupack);
1651
1652                         if(c->dupack == 3) {
1653                                 // RFC 5681 fast recovery
1654                                 debug(c, "fast recovery started\n", c->dupack);
1655                                 uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
1656                                 c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
1657                                 c->snd.cwnd = min(c->snd.ssthresh + 3 * utcp->mss, c->sndbuf.maxsize);
1658
1659                                 if(c->snd.cwnd > c->sndbuf.maxsize) {
1660                                         c->snd.cwnd = c->sndbuf.maxsize;
1661                                 }
1662
1663                                 debug_cwnd(c);
1664
1665                                 fast_retransmit(c);
1666                         } else if(c->dupack > 3) {
1667                                 c->snd.cwnd += utcp->mss;
1668
1669                                 if(c->snd.cwnd > c->sndbuf.maxsize) {
1670                                         c->snd.cwnd = c->sndbuf.maxsize;
1671                                 }
1672
1673                                 debug_cwnd(c);
1674                         }
1675
1676                         // We got an ACK which indicates the other side did get one of our packets.
1677                         // Reset the retransmission timer to avoid going to slow start,
1678                         // but don't touch the connection timeout.
1679                         start_retransmit_timer(c);
1680                 }
1681         }
1682
1683         // 4. Update timers
1684
1685         if(advanced) {
1686                 if(c->snd.una == c->snd.last) {
1687                         stop_retransmit_timer(c);
1688                         timespec_clear(&c->conn_timeout);
1689                 } else if(is_reliable(c)) {
1690                         start_retransmit_timer(c);
1691                         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1692                         c->conn_timeout.tv_sec += utcp->timeout;
1693                 }
1694         }
1695
1696 skip_ack:
1697         // 5. Process SYN stuff
1698
1699         if(hdr.ctl & SYN) {
1700                 switch(c->state) {
1701                 case SYN_SENT:
1702
1703                         // This is a SYNACK. It should always have ACKed the SYN.
1704                         if(!advanced) {
1705                                 goto reset;
1706                         }
1707
1708                         c->rcv.irs = hdr.seq;
1709                         c->rcv.nxt = hdr.seq;
1710
1711                         if(c->shut_wr) {
1712                                 c->snd.last++;
1713                                 set_state(c, FIN_WAIT_1);
1714                         } else {
1715                                 set_state(c, ESTABLISHED);
1716                         }
1717
1718                         // TODO: notify application of this somehow.
1719                         break;
1720
1721                 case SYN_RECEIVED:
1722                         // This is a retransmit of a SYN, send back the SYNACK.
1723                         goto synack;
1724
1725                 case ESTABLISHED:
1726                 case FIN_WAIT_1:
1727                 case FIN_WAIT_2:
1728                 case CLOSE_WAIT:
1729                 case CLOSING:
1730                 case LAST_ACK:
1731                 case TIME_WAIT:
1732                         // Ehm, no. We should never receive a second SYN.
1733                         return 0;
1734
1735                 default:
1736 #ifdef UTCP_DEBUG
1737                         abort();
1738 #endif
1739                         return 0;
1740                 }
1741
1742                 // SYN counts as one sequence number
1743                 c->rcv.nxt++;
1744         }
1745
1746         // 6. Process new data
1747
1748         if(c->state == SYN_RECEIVED) {
1749                 // This is the ACK after the SYNACK. It should always have ACKed the SYNACK.
1750                 if(!advanced) {
1751                         goto reset;
1752                 }
1753
1754                 // Are we still LISTENing?
1755                 if(utcp->accept) {
1756                         utcp->accept(c, c->src);
1757                 }
1758
1759                 if(c->state != ESTABLISHED) {
1760                         set_state(c, CLOSED);
1761                         c->reapable = true;
1762                         goto reset;
1763                 }
1764         }
1765
1766         if(len) {
1767                 switch(c->state) {
1768                 case SYN_SENT:
1769                 case SYN_RECEIVED:
1770                         // This should never happen.
1771 #ifdef UTCP_DEBUG
1772                         abort();
1773 #endif
1774                         return 0;
1775
1776                 case ESTABLISHED:
1777                 case FIN_WAIT_1:
1778                 case FIN_WAIT_2:
1779                         break;
1780
1781                 case CLOSE_WAIT:
1782                 case CLOSING:
1783                 case LAST_ACK:
1784                 case TIME_WAIT:
1785                         // Ehm no, We should never receive more data after a FIN.
1786                         goto reset;
1787
1788                 default:
1789 #ifdef UTCP_DEBUG
1790                         abort();
1791 #endif
1792                         return 0;
1793                 }
1794
1795                 handle_incoming_data(c, &hdr, ptr, len);
1796         }
1797
1798         // 7. Process FIN stuff
1799
1800         if((hdr.ctl & FIN) && (!is_reliable(c) || hdr.seq + len == c->rcv.nxt)) {
1801                 switch(c->state) {
1802                 case SYN_SENT:
1803                 case SYN_RECEIVED:
1804                         // This should never happen.
1805 #ifdef UTCP_DEBUG
1806                         abort();
1807 #endif
1808                         break;
1809
1810                 case ESTABLISHED:
1811                         set_state(c, CLOSE_WAIT);
1812                         break;
1813
1814                 case FIN_WAIT_1:
1815                         set_state(c, CLOSING);
1816                         break;
1817
1818                 case FIN_WAIT_2:
1819                         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1820                         c->conn_timeout.tv_sec += utcp->timeout;
1821                         set_state(c, TIME_WAIT);
1822                         break;
1823
1824                 case CLOSE_WAIT:
1825                 case CLOSING:
1826                 case LAST_ACK:
1827                 case TIME_WAIT:
1828                         // Ehm, no. We should never receive a second FIN.
1829                         goto reset;
1830
1831                 default:
1832 #ifdef UTCP_DEBUG
1833                         abort();
1834 #endif
1835                         break;
1836                 }
1837
1838                 // FIN counts as one sequence number
1839                 c->rcv.nxt++;
1840                 len++;
1841
1842                 // Inform the application that the peer closed its end of the connection.
1843                 if(c->recv) {
1844                         errno = 0;
1845                         c->recv(c, NULL, 0);
1846                 }
1847         }
1848
1849         // Now we send something back if:
1850         // - we received data, so we have to send back an ACK
1851         //   -> sendatleastone = true
1852         // - or we got an ack, so we should maybe send a bit more data
1853         //   -> sendatleastone = false
1854
1855         if(is_reliable(c) || hdr.ctl & SYN || hdr.ctl & FIN) {
1856                 ack(c, has_data);
1857         }
1858
1859         return 0;
1860
1861 reset:
1862         swap_ports(&hdr);
1863         hdr.wnd = 0;
1864         hdr.aux = 0;
1865
1866         if(hdr.ctl & ACK) {
1867                 hdr.seq = hdr.ack;
1868                 hdr.ctl = RST;
1869         } else {
1870                 hdr.ack = hdr.seq + len;
1871                 hdr.seq = 0;
1872                 hdr.ctl = RST | ACK;
1873         }
1874
1875         print_packet(c, "send", &hdr, sizeof(hdr));
1876         utcp->send(utcp, &hdr, sizeof(hdr));
1877         return 0;
1878
1879 }
1880
1881 int utcp_shutdown(struct utcp_connection *c, int dir) {
1882         debug(c, "shutdown %d at %u\n", dir, c ? c->snd.last : 0);
1883
1884         if(!c) {
1885                 errno = EFAULT;
1886                 return -1;
1887         }
1888
1889         if(c->reapable) {
1890                 debug(c, "shutdown() called on closed connection\n");
1891                 errno = EBADF;
1892                 return -1;
1893         }
1894
1895         if(!(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_WR || dir == UTCP_SHUT_RDWR)) {
1896                 errno = EINVAL;
1897                 return -1;
1898         }
1899
1900         // TCP does not have a provision for stopping incoming packets.
1901         // The best we can do is to just ignore them.
1902         if(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_RDWR) {
1903                 c->recv = NULL;
1904         }
1905
1906         // The rest of the code deals with shutting down writes.
1907         if(dir == UTCP_SHUT_RD) {
1908                 return 0;
1909         }
1910
1911         // Only process shutting down writes once.
1912         if(c->shut_wr) {
1913                 return 0;
1914         }
1915
1916         c->shut_wr = true;
1917
1918         switch(c->state) {
1919         case CLOSED:
1920         case LISTEN:
1921                 errno = ENOTCONN;
1922                 return -1;
1923
1924         case SYN_SENT:
1925                 return 0;
1926
1927         case SYN_RECEIVED:
1928         case ESTABLISHED:
1929                 set_state(c, FIN_WAIT_1);
1930                 break;
1931
1932         case FIN_WAIT_1:
1933         case FIN_WAIT_2:
1934                 return 0;
1935
1936         case CLOSE_WAIT:
1937                 set_state(c, CLOSING);
1938                 break;
1939
1940         case CLOSING:
1941         case LAST_ACK:
1942         case TIME_WAIT:
1943                 return 0;
1944         }
1945
1946         c->snd.last++;
1947
1948         ack(c, false);
1949
1950         if(!timespec_isset(&c->rtrx_timeout)) {
1951                 start_retransmit_timer(c);
1952         }
1953
1954         return 0;
1955 }
1956
1957 static bool reset_connection(struct utcp_connection *c) {
1958         if(!c) {
1959                 errno = EFAULT;
1960                 return false;
1961         }
1962
1963         if(c->reapable) {
1964                 debug(c, "abort() called on closed connection\n");
1965                 errno = EBADF;
1966                 return false;
1967         }
1968
1969         c->recv = NULL;
1970         c->poll = NULL;
1971
1972         switch(c->state) {
1973         case CLOSED:
1974                 return true;
1975
1976         case LISTEN:
1977         case SYN_SENT:
1978         case CLOSING:
1979         case LAST_ACK:
1980         case TIME_WAIT:
1981                 set_state(c, CLOSED);
1982                 return true;
1983
1984         case SYN_RECEIVED:
1985         case ESTABLISHED:
1986         case FIN_WAIT_1:
1987         case FIN_WAIT_2:
1988         case CLOSE_WAIT:
1989                 set_state(c, CLOSED);
1990                 break;
1991         }
1992
1993         // Send RST
1994
1995         struct hdr hdr;
1996
1997         hdr.src = c->src;
1998         hdr.dst = c->dst;
1999         hdr.seq = c->snd.nxt;
2000         hdr.ack = 0;
2001         hdr.wnd = 0;
2002         hdr.ctl = RST;
2003
2004         print_packet(c, "send", &hdr, sizeof(hdr));
2005         c->utcp->send(c->utcp, &hdr, sizeof(hdr));
2006         return true;
2007 }
2008
2009 // Closes all the opened connections
2010 void utcp_abort_all_connections(struct utcp *utcp) {
2011         if(!utcp) {
2012                 errno = EINVAL;
2013                 return;
2014         }
2015
2016         for(int i = 0; i < utcp->nconnections; i++) {
2017                 struct utcp_connection *c = utcp->connections[i];
2018
2019                 if(c->reapable || c->state == CLOSED) {
2020                         continue;
2021                 }
2022
2023                 utcp_recv_t old_recv = c->recv;
2024                 utcp_poll_t old_poll = c->poll;
2025
2026                 reset_connection(c);
2027
2028                 if(old_recv) {
2029                         errno = 0;
2030                         old_recv(c, NULL, 0);
2031                 }
2032
2033                 if(old_poll && !c->reapable) {
2034                         errno = 0;
2035                         old_poll(c, 0);
2036                 }
2037         }
2038
2039         return;
2040 }
2041
2042 int utcp_close(struct utcp_connection *c) {
2043         if(utcp_shutdown(c, SHUT_RDWR) && errno != ENOTCONN) {
2044                 return -1;
2045         }
2046
2047         c->recv = NULL;
2048         c->poll = NULL;
2049         c->reapable = true;
2050         return 0;
2051 }
2052
2053 int utcp_abort(struct utcp_connection *c) {
2054         if(!reset_connection(c)) {
2055                 return -1;
2056         }
2057
2058         c->reapable = true;
2059         return 0;
2060 }
2061
2062 /* Handle timeouts.
2063  * One call to this function will loop through all connections,
2064  * checking if something needs to be resent or not.
2065  * The return value is the time to the next timeout in milliseconds,
2066  * or maybe a negative value if the timeout is infinite.
2067  */
2068 struct timespec utcp_timeout(struct utcp *utcp) {
2069         struct timespec now;
2070         clock_gettime(UTCP_CLOCK, &now);
2071         struct timespec next = {now.tv_sec + 3600, now.tv_nsec};
2072
2073         for(int i = 0; i < utcp->nconnections; i++) {
2074                 struct utcp_connection *c = utcp->connections[i];
2075
2076                 if(!c) {
2077                         continue;
2078                 }
2079
2080                 // delete connections that have been utcp_close()d.
2081                 if(c->state == CLOSED) {
2082                         if(c->reapable) {
2083                                 debug(c, "reaping\n");
2084                                 free_connection(c);
2085                                 i--;
2086                         }
2087
2088                         continue;
2089                 }
2090
2091                 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &now)) {
2092                         errno = ETIMEDOUT;
2093                         c->state = CLOSED;
2094
2095                         if(c->recv) {
2096                                 c->recv(c, NULL, 0);
2097                         }
2098
2099                         if(c->poll && !c->reapable) {
2100                                 c->poll(c, 0);
2101                         }
2102
2103                         continue;
2104                 }
2105
2106                 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &now)) {
2107                         debug(c, "retransmitting after timeout\n");
2108                         retransmit(c);
2109                 }
2110
2111                 if(c->poll) {
2112                         if((c->state == ESTABLISHED || c->state == CLOSE_WAIT) && c->do_poll) {
2113                                 c->do_poll = false;
2114                                 uint32_t len = buffer_free(&c->sndbuf);
2115
2116                                 if(len) {
2117                                         c->poll(c, len);
2118                                 }
2119                         } else if(c->state == CLOSED) {
2120                                 c->poll(c, 0);
2121                         }
2122                 }
2123
2124                 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &next)) {
2125                         next = c->conn_timeout;
2126                 }
2127
2128                 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &next)) {
2129                         next = c->rtrx_timeout;
2130                 }
2131         }
2132
2133         struct timespec diff;
2134
2135         timespec_sub(&next, &now, &diff);
2136
2137         return diff;
2138 }
2139
2140 bool utcp_is_active(struct utcp *utcp) {
2141         if(!utcp) {
2142                 return false;
2143         }
2144
2145         for(int i = 0; i < utcp->nconnections; i++)
2146                 if(utcp->connections[i]->state != CLOSED && utcp->connections[i]->state != TIME_WAIT) {
2147                         return true;
2148                 }
2149
2150         return false;
2151 }
2152
2153 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
2154         if(!send) {
2155                 errno = EFAULT;
2156                 return NULL;
2157         }
2158
2159         struct utcp *utcp = calloc(1, sizeof(*utcp));
2160
2161         if(!utcp) {
2162                 return NULL;
2163         }
2164
2165         if(!CLOCK_GRANULARITY) {
2166                 struct timespec res;
2167                 clock_getres(UTCP_CLOCK, &res);
2168                 CLOCK_GRANULARITY = res.tv_sec * USEC_PER_SEC + res.tv_nsec / 1000;
2169         }
2170
2171         utcp->accept = accept;
2172         utcp->pre_accept = pre_accept;
2173         utcp->send = send;
2174         utcp->priv = priv;
2175         utcp_set_mtu(utcp, DEFAULT_MTU);
2176         utcp->timeout = DEFAULT_USER_TIMEOUT; // sec
2177         utcp->rto = START_RTO; // usec
2178
2179         return utcp;
2180 }
2181
2182 void utcp_exit(struct utcp *utcp) {
2183         if(!utcp) {
2184                 return;
2185         }
2186
2187         for(int i = 0; i < utcp->nconnections; i++) {
2188                 struct utcp_connection *c = utcp->connections[i];
2189
2190                 if(!c->reapable) {
2191                         if(c->recv) {
2192                                 c->recv(c, NULL, 0);
2193                         }
2194
2195                         if(c->poll && !c->reapable) {
2196                                 c->poll(c, 0);
2197                         }
2198                 }
2199
2200                 buffer_exit(&c->rcvbuf);
2201                 buffer_exit(&c->sndbuf);
2202                 free(c);
2203         }
2204
2205         free(utcp->connections);
2206         free(utcp->pkt);
2207         free(utcp);
2208 }
2209
2210 uint16_t utcp_get_mtu(struct utcp *utcp) {
2211         return utcp ? utcp->mtu : 0;
2212 }
2213
2214 uint16_t utcp_get_mss(struct utcp *utcp) {
2215         return utcp ? utcp->mss : 0;
2216 }
2217
2218 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
2219         if(!utcp) {
2220                 return;
2221         }
2222
2223         if(mtu <= sizeof(struct hdr)) {
2224                 return;
2225         }
2226
2227         if(mtu > utcp->mtu) {
2228                 char *new = realloc(utcp->pkt, mtu + sizeof(struct hdr));
2229
2230                 if(!new) {
2231                         return;
2232                 }
2233
2234                 utcp->pkt = new;
2235         }
2236
2237         utcp->mtu = mtu;
2238         utcp->mss = mtu - sizeof(struct hdr);
2239 }
2240
2241 void utcp_reset_timers(struct utcp *utcp) {
2242         if(!utcp) {
2243                 return;
2244         }
2245
2246         struct timespec now, then;
2247
2248         clock_gettime(UTCP_CLOCK, &now);
2249
2250         then = now;
2251
2252         then.tv_sec += utcp->timeout;
2253
2254         for(int i = 0; i < utcp->nconnections; i++) {
2255                 struct utcp_connection *c = utcp->connections[i];
2256
2257                 if(c->reapable) {
2258                         continue;
2259                 }
2260
2261                 if(timespec_isset(&c->rtrx_timeout)) {
2262                         c->rtrx_timeout = now;
2263                 }
2264
2265                 if(timespec_isset(&c->conn_timeout)) {
2266                         c->conn_timeout = then;
2267                 }
2268
2269                 c->rtt_start.tv_sec = 0;
2270         }
2271
2272         if(utcp->rto > START_RTO) {
2273                 utcp->rto = START_RTO;
2274         }
2275 }
2276
2277 int utcp_get_user_timeout(struct utcp *u) {
2278         return u ? u->timeout : 0;
2279 }
2280
2281 void utcp_set_user_timeout(struct utcp *u, int timeout) {
2282         if(u) {
2283                 u->timeout = timeout;
2284         }
2285 }
2286
2287 size_t utcp_get_sndbuf(struct utcp_connection *c) {
2288         return c ? c->sndbuf.maxsize : 0;
2289 }
2290
2291 size_t utcp_get_sndbuf_free(struct utcp_connection *c) {
2292         if(!c) {
2293                 return 0;
2294         }
2295
2296         switch(c->state) {
2297         case SYN_SENT:
2298         case SYN_RECEIVED:
2299         case ESTABLISHED:
2300         case CLOSE_WAIT:
2301                 return buffer_free(&c->sndbuf);
2302
2303         default:
2304                 return 0;
2305         }
2306 }
2307
2308 void utcp_set_sndbuf(struct utcp_connection *c, size_t size) {
2309         if(!c) {
2310                 return;
2311         }
2312
2313         c->sndbuf.maxsize = size;
2314
2315         if(c->sndbuf.maxsize != size) {
2316                 c->sndbuf.maxsize = -1;
2317         }
2318
2319         c->do_poll = buffer_free(&c->sndbuf);
2320 }
2321
2322 size_t utcp_get_rcvbuf(struct utcp_connection *c) {
2323         return c ? c->rcvbuf.maxsize : 0;
2324 }
2325
2326 size_t utcp_get_rcvbuf_free(struct utcp_connection *c) {
2327         if(c && (c->state == ESTABLISHED || c->state == CLOSE_WAIT)) {
2328                 return buffer_free(&c->rcvbuf);
2329         } else {
2330                 return 0;
2331         }
2332 }
2333
2334 void utcp_set_rcvbuf(struct utcp_connection *c, size_t size) {
2335         if(!c) {
2336                 return;
2337         }
2338
2339         c->rcvbuf.maxsize = size;
2340
2341         if(c->rcvbuf.maxsize != size) {
2342                 c->rcvbuf.maxsize = -1;
2343         }
2344 }
2345
2346 size_t utcp_get_sendq(struct utcp_connection *c) {
2347         return c->sndbuf.used;
2348 }
2349
2350 size_t utcp_get_recvq(struct utcp_connection *c) {
2351         return c->rcvbuf.used;
2352 }
2353
2354 bool utcp_get_nodelay(struct utcp_connection *c) {
2355         return c ? c->nodelay : false;
2356 }
2357
2358 void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) {
2359         if(c) {
2360                 c->nodelay = nodelay;
2361         }
2362 }
2363
2364 bool utcp_get_keepalive(struct utcp_connection *c) {
2365         return c ? c->keepalive : false;
2366 }
2367
2368 void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
2369         if(c) {
2370                 c->keepalive = keepalive;
2371         }
2372 }
2373
2374 size_t utcp_get_outq(struct utcp_connection *c) {
2375         return c ? seqdiff(c->snd.nxt, c->snd.una) : 0;
2376 }
2377
2378 void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) {
2379         if(c) {
2380                 c->recv = recv;
2381         }
2382 }
2383
2384 void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) {
2385         if(c) {
2386                 c->poll = poll;
2387                 c->do_poll = buffer_free(&c->sndbuf);
2388         }
2389 }
2390
2391 void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_pre_accept_t pre_accept) {
2392         if(utcp) {
2393                 utcp->accept = accept;
2394                 utcp->pre_accept = pre_accept;
2395         }
2396 }
2397
2398 void utcp_expect_data(struct utcp_connection *c, bool expect) {
2399         if(!c || c->reapable) {
2400                 return;
2401         }
2402
2403         if(!(c->state == ESTABLISHED || c->state == FIN_WAIT_1 || c->state == FIN_WAIT_2)) {
2404                 return;
2405         }
2406
2407         if(expect) {
2408                 // If we expect data, start the connection timer.
2409                 if(!timespec_isset(&c->conn_timeout)) {
2410                         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
2411                         c->conn_timeout.tv_sec += c->utcp->timeout;
2412                 }
2413         } else {
2414                 // If we want to cancel expecting data, only clear the timer when there is no unACKed data.
2415                 if(c->snd.una == c->snd.last) {
2416                         timespec_clear(&c->conn_timeout);
2417                 }
2418         }
2419 }
2420
2421 void utcp_offline(struct utcp *utcp, bool offline) {
2422         struct timespec now;
2423         clock_gettime(UTCP_CLOCK, &now);
2424
2425         for(int i = 0; i < utcp->nconnections; i++) {
2426                 struct utcp_connection *c = utcp->connections[i];
2427
2428                 if(c->reapable) {
2429                         continue;
2430                 }
2431
2432                 utcp_expect_data(c, offline);
2433
2434                 if(!offline) {
2435                         if(timespec_isset(&c->rtrx_timeout)) {
2436                                 c->rtrx_timeout = now;
2437                         }
2438
2439                         utcp->connections[i]->rtt_start.tv_sec = 0;
2440                 }
2441         }
2442
2443         if(!offline && utcp->rto > START_RTO) {
2444                 utcp->rto = START_RTO;
2445         }
2446 }
2447
2448 void utcp_set_clock_granularity(long granularity) {
2449         CLOCK_GRANULARITY = granularity;
2450 }