]> git.meshlink.io Git - utcp/blob - utcp.c
Ensure buf->offset is always smaller than buf->size.
[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         return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_nsec - b->tv_nsec) / 1000;
65 }
66
67 static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
68         if(a->tv_sec == b->tv_sec) {
69                 return a->tv_nsec < b->tv_nsec;
70         } else {
71                 return a->tv_sec < b->tv_sec;
72         }
73 }
74
75 static void timespec_clear(struct timespec *a) {
76         a->tv_sec = 0;
77         a->tv_nsec = 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 ? buf->maxsize - buf->used : 0;
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->srtt = 0;
529         c->rttvar = 0;
530         c->rto = START_RTO;
531         c->utcp = utcp;
532
533         // Add it to the sorted list of connections
534
535         utcp->connections[utcp->nconnections++] = c;
536         qsort(utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
537
538         return c;
539 }
540
541 static inline uint32_t absdiff(uint32_t a, uint32_t b) {
542         if(a > b) {
543                 return a - b;
544         } else {
545                 return b - a;
546         }
547 }
548
549 // Update RTT variables. See RFC 6298.
550 static void update_rtt(struct utcp_connection *c, uint32_t rtt) {
551         if(!rtt) {
552                 debug(c, "invalid rtt\n");
553                 return;
554         }
555
556         if(!c->srtt) {
557                 c->srtt = rtt;
558                 c->rttvar = rtt / 2;
559         } else {
560                 c->rttvar = (c->rttvar * 3 + absdiff(c->srtt, rtt)) / 4;
561                 c->srtt = (c->srtt * 7 + rtt) / 8;
562         }
563
564         c->rto = c->srtt + max(4 * c->rttvar, CLOCK_GRANULARITY);
565
566         if(c->rto > MAX_RTO) {
567                 c->rto = MAX_RTO;
568         }
569
570         debug(c, "rtt %u srtt %u rttvar %u rto %u\n", rtt, c->srtt, c->rttvar, c->rto);
571 }
572
573 static void start_retransmit_timer(struct utcp_connection *c) {
574         clock_gettime(UTCP_CLOCK, &c->rtrx_timeout);
575
576         uint32_t rto = c->rto;
577
578         while(rto > USEC_PER_SEC) {
579                 c->rtrx_timeout.tv_sec++;
580                 rto -= USEC_PER_SEC;
581         }
582
583         c->rtrx_timeout.tv_nsec += rto * 1000;
584
585         if(c->rtrx_timeout.tv_nsec >= NSEC_PER_SEC) {
586                 c->rtrx_timeout.tv_nsec -= NSEC_PER_SEC;
587                 c->rtrx_timeout.tv_sec++;
588         }
589
590         debug(c, "rtrx_timeout %ld.%06lu\n", c->rtrx_timeout.tv_sec, c->rtrx_timeout.tv_nsec);
591 }
592
593 static void stop_retransmit_timer(struct utcp_connection *c) {
594         timespec_clear(&c->rtrx_timeout);
595         debug(c, "rtrx_timeout cleared\n");
596 }
597
598 struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv, uint32_t flags) {
599         struct utcp_connection *c = allocate_connection(utcp, 0, dst);
600
601         if(!c) {
602                 return NULL;
603         }
604
605         assert((flags & ~0x1f) == 0);
606
607         c->flags = flags;
608         c->recv = recv;
609         c->priv = priv;
610
611         struct {
612                 struct hdr hdr;
613                 uint8_t init[4];
614         } pkt;
615
616         pkt.hdr.src = c->src;
617         pkt.hdr.dst = c->dst;
618         pkt.hdr.seq = c->snd.iss;
619         pkt.hdr.ack = 0;
620         pkt.hdr.wnd = c->rcvbuf.maxsize;
621         pkt.hdr.ctl = SYN;
622         pkt.hdr.aux = 0x0101;
623         pkt.init[0] = 1;
624         pkt.init[1] = 0;
625         pkt.init[2] = 0;
626         pkt.init[3] = flags & 0x7;
627
628         set_state(c, SYN_SENT);
629
630         print_packet(c, "send", &pkt, sizeof(pkt));
631         utcp->send(utcp, &pkt, sizeof(pkt));
632
633         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
634         c->conn_timeout.tv_sec += utcp->timeout;
635
636         start_retransmit_timer(c);
637
638         return c;
639 }
640
641 struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
642         return utcp_connect_ex(utcp, dst, recv, priv, UTCP_TCP);
643 }
644
645 void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
646         if(c->reapable || c->state != SYN_RECEIVED) {
647                 debug(c, "accept() called on invalid connection in state %s\n", c, strstate[c->state]);
648                 return;
649         }
650
651         debug(c, "accepted %p %p\n", c, recv, priv);
652         c->recv = recv;
653         c->priv = priv;
654         set_state(c, ESTABLISHED);
655 }
656
657 static void ack(struct utcp_connection *c, bool sendatleastone) {
658         int32_t left = seqdiff(c->snd.last, c->snd.nxt);
659         int32_t cwndleft = is_reliable(c) ? min(c->snd.cwnd, c->snd.wnd) - seqdiff(c->snd.nxt, c->snd.una) : MAX_UNRELIABLE_SIZE;
660
661         assert(left >= 0);
662
663         if(cwndleft <= 0) {
664                 left = 0;
665         } else if(cwndleft < left) {
666                 left = cwndleft;
667
668                 if(!sendatleastone || cwndleft > c->utcp->mss) {
669                         left -= left % c->utcp->mss;
670                 }
671         }
672
673         debug(c, "cwndleft %d left %d\n", cwndleft, left);
674
675         if(!left && !sendatleastone) {
676                 return;
677         }
678
679         struct {
680                 struct hdr hdr;
681                 uint8_t data[];
682         } *pkt = c->utcp->pkt;
683
684         pkt->hdr.src = c->src;
685         pkt->hdr.dst = c->dst;
686         pkt->hdr.ack = c->rcv.nxt;
687         pkt->hdr.wnd = is_reliable(c) ? c->rcvbuf.maxsize : 0;
688         pkt->hdr.ctl = ACK;
689         pkt->hdr.aux = 0;
690
691         do {
692                 uint32_t seglen = left > c->utcp->mss ? c->utcp->mss : left;
693                 pkt->hdr.seq = c->snd.nxt;
694
695                 buffer_copy(&c->sndbuf, pkt->data, seqdiff(c->snd.nxt, c->snd.una), seglen);
696
697                 c->snd.nxt += seglen;
698                 left -= seglen;
699
700                 if(!is_reliable(c)) {
701                         if(left) {
702                                 pkt->hdr.ctl |= MF;
703                         } else {
704                                 pkt->hdr.ctl &= ~MF;
705                         }
706                 }
707
708                 if(seglen && fin_wanted(c, c->snd.nxt)) {
709                         seglen--;
710                         pkt->hdr.ctl |= FIN;
711                 }
712
713                 if(!c->rtt_start.tv_sec) {
714                         // Start RTT measurement
715                         clock_gettime(UTCP_CLOCK, &c->rtt_start);
716                         c->rtt_seq = pkt->hdr.seq + seglen;
717                         debug(c, "starting RTT measurement, expecting ack %u\n", c->rtt_seq);
718                 }
719
720                 print_packet(c, "send", pkt, sizeof(pkt->hdr) + seglen);
721                 c->utcp->send(c->utcp, pkt, sizeof(pkt->hdr) + seglen);
722
723                 if(left && !is_reliable(c)) {
724                         pkt->hdr.wnd += seglen;
725                 }
726         } while(left);
727 }
728
729 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
730         if(c->reapable) {
731                 debug(c, "send() called on closed connection\n");
732                 errno = EBADF;
733                 return -1;
734         }
735
736         switch(c->state) {
737         case CLOSED:
738         case LISTEN:
739                 debug(c, "send() called on unconnected connection\n");
740                 errno = ENOTCONN;
741                 return -1;
742
743         case SYN_SENT:
744         case SYN_RECEIVED:
745         case ESTABLISHED:
746         case CLOSE_WAIT:
747                 break;
748
749         case FIN_WAIT_1:
750         case FIN_WAIT_2:
751         case CLOSING:
752         case LAST_ACK:
753         case TIME_WAIT:
754                 debug(c, "send() called on closed connection\n");
755                 errno = EPIPE;
756                 return -1;
757         }
758
759         // Exit early if we have nothing to send.
760
761         if(!len) {
762                 return 0;
763         }
764
765         if(!data) {
766                 errno = EFAULT;
767                 return -1;
768         }
769
770         // Check if we need to be able to buffer all data
771
772         if(c->flags & UTCP_NO_PARTIAL) {
773                 if(len > buffer_free(&c->sndbuf)) {
774                         if(len > c->sndbuf.maxsize) {
775                                 errno = EMSGSIZE;
776                                 return -1;
777                         } else {
778                                 errno = EWOULDBLOCK;
779                                 return 0;
780                         }
781                 }
782         }
783
784         // Add data to send buffer.
785
786         if(is_reliable(c)) {
787                 len = buffer_put(&c->sndbuf, data, len);
788         } else if(c->state != SYN_SENT && c->state != SYN_RECEIVED) {
789                 if(len > MAX_UNRELIABLE_SIZE || buffer_put(&c->sndbuf, data, len) != (ssize_t)len) {
790                         errno = EMSGSIZE;
791                         return -1;
792                 }
793         } else {
794                 return 0;
795         }
796
797         if(len <= 0) {
798                 if(is_reliable(c)) {
799                         errno = EWOULDBLOCK;
800                         return 0;
801                 } else {
802                         return len;
803                 }
804         }
805
806         c->snd.last += len;
807
808         // Don't send anything yet if the connection has not fully established yet
809
810         if(c->state == SYN_SENT || c->state == SYN_RECEIVED) {
811                 return len;
812         }
813
814         ack(c, false);
815
816         if(!is_reliable(c)) {
817                 c->snd.una = c->snd.nxt = c->snd.last;
818                 buffer_discard(&c->sndbuf, c->sndbuf.used);
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 = c->utcp->pkt;
851
852         pkt->hdr.src = c->src;
853         pkt->hdr.dst = c->dst;
854         pkt->hdr.wnd = c->rcvbuf.maxsize;
855         pkt->hdr.aux = 0;
856
857         switch(c->state) {
858         case ESTABLISHED:
859         case FIN_WAIT_1:
860         case CLOSE_WAIT:
861         case CLOSING:
862         case LAST_ACK:
863                 // Send unacked data again.
864                 pkt->hdr.seq = c->snd.una;
865                 pkt->hdr.ack = c->rcv.nxt;
866                 pkt->hdr.ctl = ACK;
867                 uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
868
869                 if(fin_wanted(c, c->snd.una + len)) {
870                         len--;
871                         pkt->hdr.ctl |= FIN;
872                 }
873
874                 buffer_copy(&c->sndbuf, pkt->data, 0, len);
875                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
876                 utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
877                 break;
878
879         default:
880                 break;
881         }
882 }
883
884 static void retransmit(struct utcp_connection *c) {
885         if(c->state == CLOSED || c->snd.last == c->snd.una) {
886                 debug(c, "retransmit() called but nothing to retransmit!\n");
887                 stop_retransmit_timer(c);
888                 return;
889         }
890
891         struct utcp *utcp = c->utcp;
892
893         if(utcp->retransmit) {
894                 utcp->retransmit(c);
895         }
896
897         struct {
898                 struct hdr hdr;
899                 uint8_t data[];
900         } *pkt = c->utcp->pkt;
901
902         pkt->hdr.src = c->src;
903         pkt->hdr.dst = c->dst;
904         pkt->hdr.wnd = c->rcvbuf.maxsize;
905         pkt->hdr.aux = 0;
906
907         switch(c->state) {
908         case SYN_SENT:
909                 // Send our SYN again
910                 pkt->hdr.seq = c->snd.iss;
911                 pkt->hdr.ack = 0;
912                 pkt->hdr.ctl = SYN;
913                 pkt->hdr.aux = 0x0101;
914                 pkt->data[0] = 1;
915                 pkt->data[1] = 0;
916                 pkt->data[2] = 0;
917                 pkt->data[3] = c->flags & 0x7;
918                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + 4);
919                 utcp->send(utcp, pkt, sizeof(pkt->hdr) + 4);
920                 break;
921
922         case SYN_RECEIVED:
923                 // Send SYNACK again
924                 pkt->hdr.seq = c->snd.nxt;
925                 pkt->hdr.ack = c->rcv.nxt;
926                 pkt->hdr.ctl = SYN | ACK;
927                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr));
928                 utcp->send(utcp, pkt, sizeof(pkt->hdr));
929                 break;
930
931         case ESTABLISHED:
932         case FIN_WAIT_1:
933         case CLOSE_WAIT:
934         case CLOSING:
935         case LAST_ACK:
936                 // Send unacked data again.
937                 pkt->hdr.seq = c->snd.una;
938                 pkt->hdr.ack = c->rcv.nxt;
939                 pkt->hdr.ctl = ACK;
940                 uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
941
942                 if(fin_wanted(c, c->snd.una + len)) {
943                         len--;
944                         pkt->hdr.ctl |= FIN;
945                 }
946
947                 // RFC 5681 slow start after timeout
948                 uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
949                 c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
950                 c->snd.cwnd = utcp->mss;
951                 debug_cwnd(c);
952
953                 buffer_copy(&c->sndbuf, pkt->data, 0, len);
954                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
955                 utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
956
957                 c->snd.nxt = c->snd.una + len;
958                 break;
959
960         case CLOSED:
961         case LISTEN:
962         case TIME_WAIT:
963         case FIN_WAIT_2:
964                 // We shouldn't need to retransmit anything in this state.
965 #ifdef UTCP_DEBUG
966                 abort();
967 #endif
968                 stop_retransmit_timer(c);
969                 goto cleanup;
970         }
971
972         start_retransmit_timer(c);
973         c->rto *= 2;
974
975         if(c->rto > MAX_RTO) {
976                 c->rto = MAX_RTO;
977         }
978
979         c->rtt_start.tv_sec = 0; // invalidate RTT timer
980         c->dupack = 0; // cancel any ongoing fast recovery
981
982 cleanup:
983         return;
984 }
985
986 /* Update receive buffer and SACK entries after consuming data.
987  *
988  * Situation:
989  *
990  * |.....0000..1111111111.....22222......3333|
991  * |---------------^
992  *
993  * 0..3 represent the SACK entries. The ^ indicates up to which point we want
994  * to remove data from the receive buffer. The idea is to substract "len"
995  * from the offset of all the SACK entries, and then remove/cut down entries
996  * that are shifted to before the start of the receive buffer.
997  *
998  * There are three cases:
999  * - the SACK entry is after ^, in that case just change the offset.
1000  * - the SACK entry starts before and ends after ^, so we have to
1001  *   change both its offset and size.
1002  * - the SACK entry is completely before ^, in that case delete it.
1003  */
1004 static void sack_consume(struct utcp_connection *c, size_t len) {
1005         debug(c, "sack_consume %lu\n", (unsigned long)len);
1006
1007         if(len > c->rcvbuf.used) {
1008                 debug(c, "all SACK entries consumed\n");
1009                 c->sacks[0].len = 0;
1010                 return;
1011         }
1012
1013         buffer_discard(&c->rcvbuf, len);
1014
1015         for(int i = 0; i < NSACKS && c->sacks[i].len;) {
1016                 if(len < c->sacks[i].offset) {
1017                         c->sacks[i].offset -= len;
1018                         i++;
1019                 } else if(len < c->sacks[i].offset + c->sacks[i].len) {
1020                         c->sacks[i].len -= len - c->sacks[i].offset;
1021                         c->sacks[i].offset = 0;
1022                         i++;
1023                 } else {
1024                         if(i < NSACKS - 1) {
1025                                 memmove(&c->sacks[i], &c->sacks[i + 1], (NSACKS - 1 - i) * sizeof(c->sacks)[i]);
1026                                 c->sacks[NSACKS - 1].len = 0;
1027                         } else {
1028                                 c->sacks[i].len = 0;
1029                                 break;
1030                         }
1031                 }
1032         }
1033
1034         for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
1035                 debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
1036         }
1037 }
1038
1039 static void handle_out_of_order(struct utcp_connection *c, uint32_t offset, const void *data, size_t len) {
1040         debug(c, "out of order packet, offset %u\n", offset);
1041         // Packet loss or reordering occured. Store the data in the buffer.
1042         ssize_t rxd = buffer_put_at(&c->rcvbuf, offset, data, len);
1043
1044         if(rxd <= 0) {
1045                 debug(c, "packet outside receive buffer, dropping\n");
1046                 return;
1047         }
1048
1049         if((size_t)rxd < len) {
1050                 debug(c, "packet partially outside receive buffer\n");
1051                 len = rxd;
1052         }
1053
1054         // Make note of where we put it.
1055         for(int i = 0; i < NSACKS; i++) {
1056                 if(!c->sacks[i].len) { // nothing to merge, add new entry
1057                         debug(c, "new SACK entry %d\n", i);
1058                         c->sacks[i].offset = offset;
1059                         c->sacks[i].len = rxd;
1060                         break;
1061                 } else if(offset < c->sacks[i].offset) {
1062                         if(offset + rxd < c->sacks[i].offset) { // insert before
1063                                 if(!c->sacks[NSACKS - 1].len) { // only if room left
1064                                         debug(c, "insert SACK entry at %d\n", i);
1065                                         memmove(&c->sacks[i + 1], &c->sacks[i], (NSACKS - i - 1) * sizeof(c->sacks)[i]);
1066                                         c->sacks[i].offset = offset;
1067                                         c->sacks[i].len = rxd;
1068                                 } else {
1069                                         debug(c, "SACK entries full, dropping packet\n");
1070                                 }
1071
1072                                 break;
1073                         } else { // merge
1074                                 debug(c, "merge with start of SACK entry at %d\n", i);
1075                                 c->sacks[i].offset = offset;
1076                                 break;
1077                         }
1078                 } else if(offset <= c->sacks[i].offset + c->sacks[i].len) {
1079                         if(offset + rxd > c->sacks[i].offset + c->sacks[i].len) { // merge
1080                                 debug(c, "merge with end of SACK entry at %d\n", i);
1081                                 c->sacks[i].len = offset + rxd - c->sacks[i].offset;
1082                                 // TODO: handle potential merge with next entry
1083                         }
1084
1085                         break;
1086                 }
1087         }
1088
1089         for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
1090                 debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
1091         }
1092 }
1093
1094 static void handle_in_order(struct utcp_connection *c, const void *data, size_t len) {
1095         if(c->recv) {
1096                 ssize_t rxd = c->recv(c, data, len);
1097
1098                 if(rxd != (ssize_t)len) {
1099                         // TODO: handle the application not accepting all data.
1100                         abort();
1101                 }
1102         }
1103
1104         // Check if we can process out-of-order data now.
1105         if(c->sacks[0].len && len >= c->sacks[0].offset) {
1106                 debug(c, "incoming packet len %lu connected with SACK at %u\n", (unsigned long)len, c->sacks[0].offset);
1107
1108                 if(len < c->sacks[0].offset + c->sacks[0].len) {
1109                         size_t offset = len;
1110                         len = c->sacks[0].offset + c->sacks[0].len;
1111                         size_t remainder = len - offset;
1112
1113                         if(c->recv) {
1114                                 ssize_t rxd = buffer_call(&c->rcvbuf, c->recv, c, offset, remainder);
1115
1116                                 if(rxd != (ssize_t)remainder) {
1117                                         // TODO: handle the application not accepting all data.
1118                                         abort();
1119                                 }
1120                         }
1121                 }
1122         }
1123
1124         if(c->rcvbuf.used) {
1125                 sack_consume(c, len);
1126         }
1127
1128         c->rcv.nxt += len;
1129 }
1130
1131 static void handle_unreliable(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
1132         // Fast path for unfragmented packets
1133         if(!hdr->wnd && !(hdr->ctl & MF)) {
1134                 if(c->recv) {
1135                         c->recv(c, data, len);
1136                 }
1137
1138                 c->rcv.nxt = hdr->seq + len;
1139                 return;
1140         }
1141
1142         // Ensure reassembled packet are not larger than 64 kiB
1143         if(hdr->wnd >= MAX_UNRELIABLE_SIZE || hdr->wnd + len > MAX_UNRELIABLE_SIZE) {
1144                 return;
1145         }
1146
1147         // Don't accept out of order fragments
1148         if(hdr->wnd && hdr->seq != c->rcv.nxt) {
1149                 return;
1150         }
1151
1152         // Reset the receive buffer for the first fragment
1153         if(!hdr->wnd) {
1154                 buffer_clear(&c->rcvbuf);
1155         }
1156
1157         ssize_t rxd = buffer_put_at(&c->rcvbuf, hdr->wnd, data, len);
1158
1159         if(rxd != (ssize_t)len) {
1160                 return;
1161         }
1162
1163         // Send the packet if it's the final fragment
1164         if(!(hdr->ctl & MF) && c->recv) {
1165                 buffer_call(&c->rcvbuf, c->recv, c, 0, hdr->wnd + len);
1166         }
1167
1168         c->rcv.nxt = hdr->seq + len;
1169 }
1170
1171 static void handle_incoming_data(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
1172         if(!is_reliable(c)) {
1173                 handle_unreliable(c, hdr, data, len);
1174                 return;
1175         }
1176
1177         uint32_t offset = seqdiff(hdr->seq, c->rcv.nxt);
1178
1179         if(offset) {
1180                 handle_out_of_order(c, offset, data, len);
1181         } else {
1182                 handle_in_order(c, data, len);
1183         }
1184 }
1185
1186
1187 ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
1188         const uint8_t *ptr = data;
1189
1190         if(!utcp) {
1191                 errno = EFAULT;
1192                 return -1;
1193         }
1194
1195         if(!len) {
1196                 return 0;
1197         }
1198
1199         if(!data) {
1200                 errno = EFAULT;
1201                 return -1;
1202         }
1203
1204         // Drop packets smaller than the header
1205
1206         struct hdr hdr;
1207
1208         if(len < sizeof(hdr)) {
1209                 print_packet(NULL, "recv", data, len);
1210                 errno = EBADMSG;
1211                 return -1;
1212         }
1213
1214         // Make a copy from the potentially unaligned data to a struct hdr
1215
1216         memcpy(&hdr, ptr, sizeof(hdr));
1217
1218         // Try to match the packet to an existing connection
1219
1220         struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
1221         print_packet(c, "recv", data, len);
1222
1223         // Process the header
1224
1225         ptr += sizeof(hdr);
1226         len -= sizeof(hdr);
1227
1228         // Drop packets with an unknown CTL flag
1229
1230         if(hdr.ctl & ~(SYN | ACK | RST | FIN | MF)) {
1231                 print_packet(NULL, "recv", data, len);
1232                 errno = EBADMSG;
1233                 return -1;
1234         }
1235
1236         // Check for auxiliary headers
1237
1238         const uint8_t *init = NULL;
1239
1240         uint16_t aux = hdr.aux;
1241
1242         while(aux) {
1243                 size_t auxlen = 4 * (aux >> 8) & 0xf;
1244                 uint8_t auxtype = aux & 0xff;
1245
1246                 if(len < auxlen) {
1247                         errno = EBADMSG;
1248                         return -1;
1249                 }
1250
1251                 switch(auxtype) {
1252                 case AUX_INIT:
1253                         if(!(hdr.ctl & SYN) || auxlen != 4) {
1254                                 errno = EBADMSG;
1255                                 return -1;
1256                         }
1257
1258                         init = ptr;
1259                         break;
1260
1261                 default:
1262                         errno = EBADMSG;
1263                         return -1;
1264                 }
1265
1266                 len -= auxlen;
1267                 ptr += auxlen;
1268
1269                 if(!(aux & 0x800)) {
1270                         break;
1271                 }
1272
1273                 if(len < 2) {
1274                         errno = EBADMSG;
1275                         return -1;
1276                 }
1277
1278                 memcpy(&aux, ptr, 2);
1279                 len -= 2;
1280                 ptr += 2;
1281         }
1282
1283         bool has_data = len || (hdr.ctl & (SYN | FIN));
1284
1285         // Is it for a new connection?
1286
1287         if(!c) {
1288                 // Ignore RST packets
1289
1290                 if(hdr.ctl & RST) {
1291                         return 0;
1292                 }
1293
1294                 // Is it a SYN packet and are we LISTENing?
1295
1296                 if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept) {
1297                         // If we don't want to accept it, send a RST back
1298                         if((utcp->pre_accept && !utcp->pre_accept(utcp, hdr.dst))) {
1299                                 len = 1;
1300                                 goto reset;
1301                         }
1302
1303                         // Try to allocate memory, otherwise send a RST back
1304                         c = allocate_connection(utcp, hdr.dst, hdr.src);
1305
1306                         if(!c) {
1307                                 len = 1;
1308                                 goto reset;
1309                         }
1310
1311                         // Parse auxilliary information
1312                         if(init) {
1313                                 if(init[0] < 1) {
1314                                         len = 1;
1315                                         goto reset;
1316                                 }
1317
1318                                 c->flags = init[3] & 0x7;
1319                         } else {
1320                                 c->flags = UTCP_TCP;
1321                         }
1322
1323 synack:
1324                         // Return SYN+ACK, go to SYN_RECEIVED state
1325                         c->snd.wnd = hdr.wnd;
1326                         c->rcv.irs = hdr.seq;
1327                         c->rcv.nxt = c->rcv.irs + 1;
1328                         set_state(c, SYN_RECEIVED);
1329
1330                         struct {
1331                                 struct hdr hdr;
1332                                 uint8_t data[4];
1333                         } pkt;
1334
1335                         pkt.hdr.src = c->src;
1336                         pkt.hdr.dst = c->dst;
1337                         pkt.hdr.ack = c->rcv.irs + 1;
1338                         pkt.hdr.seq = c->snd.iss;
1339                         pkt.hdr.wnd = c->rcvbuf.maxsize;
1340                         pkt.hdr.ctl = SYN | ACK;
1341
1342                         if(init) {
1343                                 pkt.hdr.aux = 0x0101;
1344                                 pkt.data[0] = 1;
1345                                 pkt.data[1] = 0;
1346                                 pkt.data[2] = 0;
1347                                 pkt.data[3] = c->flags & 0x7;
1348                                 print_packet(c, "send", &pkt, sizeof(hdr) + 4);
1349                                 utcp->send(utcp, &pkt, sizeof(hdr) + 4);
1350                         } else {
1351                                 pkt.hdr.aux = 0;
1352                                 print_packet(c, "send", &pkt, sizeof(hdr));
1353                                 utcp->send(utcp, &pkt, sizeof(hdr));
1354                         }
1355
1356                         start_retransmit_timer(c);
1357                 } else {
1358                         // No, we don't want your packets, send a RST back
1359                         len = 1;
1360                         goto reset;
1361                 }
1362
1363                 return 0;
1364         }
1365
1366         debug(c, "state %s\n", strstate[c->state]);
1367
1368         // In case this is for a CLOSED connection, ignore the packet.
1369         // TODO: make it so incoming packets can never match a CLOSED connection.
1370
1371         if(c->state == CLOSED) {
1372                 debug(c, "got packet for closed connection\n");
1373                 return 0;
1374         }
1375
1376         // It is for an existing connection.
1377
1378         // 1. Drop invalid packets.
1379
1380         // 1a. Drop packets that should not happen in our current state.
1381
1382         switch(c->state) {
1383         case SYN_SENT:
1384         case SYN_RECEIVED:
1385         case ESTABLISHED:
1386         case FIN_WAIT_1:
1387         case FIN_WAIT_2:
1388         case CLOSE_WAIT:
1389         case CLOSING:
1390         case LAST_ACK:
1391         case TIME_WAIT:
1392                 break;
1393
1394         default:
1395 #ifdef UTCP_DEBUG
1396                 abort();
1397 #endif
1398                 break;
1399         }
1400
1401         // 1b. Discard data that is not in our receive window.
1402
1403         if(is_reliable(c)) {
1404                 bool acceptable;
1405
1406                 if(c->state == SYN_SENT) {
1407                         acceptable = true;
1408                 } else if(len == 0) {
1409                         acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0;
1410                 } else {
1411                         int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
1412
1413                         // cut already accepted front overlapping
1414                         if(rcv_offset < 0) {
1415                                 acceptable = len > (size_t) - rcv_offset;
1416
1417                                 if(acceptable) {
1418                                         ptr -= rcv_offset;
1419                                         len += rcv_offset;
1420                                         hdr.seq -= rcv_offset;
1421                                 }
1422                         } else {
1423                                 acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt) + len <= c->rcvbuf.maxsize;
1424                         }
1425                 }
1426
1427                 if(!acceptable) {
1428                         debug(c, "packet not acceptable, %u <= %u + %lu < %u\n", c->rcv.nxt, hdr.seq, (unsigned long)len, c->rcv.nxt + c->rcvbuf.maxsize);
1429
1430                         // Ignore unacceptable RST packets.
1431                         if(hdr.ctl & RST) {
1432                                 return 0;
1433                         }
1434
1435                         // Otherwise, continue processing.
1436                         len = 0;
1437                 }
1438         } else {
1439 #if UTCP_DEBUG
1440                 int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
1441
1442                 if(rcv_offset) {
1443                         debug(c, "packet out of order, offset %u bytes", rcv_offset);
1444                 }
1445
1446 #endif
1447         }
1448
1449         c->snd.wnd = hdr.wnd; // TODO: move below
1450
1451         // 1c. Drop packets with an invalid ACK.
1452         // ackno should not roll back, and it should also not be bigger than what we ever could have sent
1453         // (= snd.una + c->sndbuf.used).
1454
1455         if(!is_reliable(c)) {
1456                 if(hdr.ack != c->snd.last && c->state >= ESTABLISHED) {
1457                         hdr.ack = c->snd.una;
1458                 }
1459         }
1460
1461         if(hdr.ctl & ACK && (seqdiff(hdr.ack, c->snd.last) > 0 || seqdiff(hdr.ack, c->snd.una) < 0)) {
1462                 debug(c, "packet ack seqno out of range, %u <= %u < %u\n", c->snd.una, hdr.ack, c->snd.una + c->sndbuf.used);
1463
1464                 // Ignore unacceptable RST packets.
1465                 if(hdr.ctl & RST) {
1466                         return 0;
1467                 }
1468
1469                 goto reset;
1470         }
1471
1472         // 2. Handle RST packets
1473
1474         if(hdr.ctl & RST) {
1475                 switch(c->state) {
1476                 case SYN_SENT:
1477                         if(!(hdr.ctl & ACK)) {
1478                                 return 0;
1479                         }
1480
1481                         // The peer has refused our connection.
1482                         set_state(c, CLOSED);
1483                         errno = ECONNREFUSED;
1484
1485                         if(c->recv) {
1486                                 c->recv(c, NULL, 0);
1487                         }
1488
1489                         if(c->poll && !c->reapable) {
1490                                 c->poll(c, 0);
1491                         }
1492
1493                         return 0;
1494
1495                 case SYN_RECEIVED:
1496                         if(hdr.ctl & ACK) {
1497                                 return 0;
1498                         }
1499
1500                         // We haven't told the application about this connection yet. Silently delete.
1501                         free_connection(c);
1502                         return 0;
1503
1504                 case ESTABLISHED:
1505                 case FIN_WAIT_1:
1506                 case FIN_WAIT_2:
1507                 case CLOSE_WAIT:
1508                         if(hdr.ctl & ACK) {
1509                                 return 0;
1510                         }
1511
1512                         // The peer has aborted our connection.
1513                         set_state(c, CLOSED);
1514                         errno = ECONNRESET;
1515
1516                         if(c->recv) {
1517                                 c->recv(c, NULL, 0);
1518                         }
1519
1520                         if(c->poll && !c->reapable) {
1521                                 c->poll(c, 0);
1522                         }
1523
1524                         return 0;
1525
1526                 case CLOSING:
1527                 case LAST_ACK:
1528                 case TIME_WAIT:
1529                         if(hdr.ctl & ACK) {
1530                                 return 0;
1531                         }
1532
1533                         // As far as the application is concerned, the connection has already been closed.
1534                         // If it has called utcp_close() already, we can immediately free this connection.
1535                         if(c->reapable) {
1536                                 free_connection(c);
1537                                 return 0;
1538                         }
1539
1540                         // Otherwise, immediately move to the CLOSED state.
1541                         set_state(c, CLOSED);
1542                         return 0;
1543
1544                 default:
1545 #ifdef UTCP_DEBUG
1546                         abort();
1547 #endif
1548                         break;
1549                 }
1550         }
1551
1552         uint32_t advanced;
1553
1554         if(!(hdr.ctl & ACK)) {
1555                 advanced = 0;
1556                 goto skip_ack;
1557         }
1558
1559         // 3. Advance snd.una
1560
1561         advanced = seqdiff(hdr.ack, c->snd.una);
1562
1563         if(advanced) {
1564                 // RTT measurement
1565                 if(c->rtt_start.tv_sec) {
1566                         if(c->rtt_seq == hdr.ack) {
1567                                 struct timespec now;
1568                                 clock_gettime(UTCP_CLOCK, &now);
1569                                 int32_t diff = timespec_diff_usec(&now, &c->rtt_start);
1570                                 update_rtt(c, diff);
1571                                 c->rtt_start.tv_sec = 0;
1572                         } else if(c->rtt_seq < hdr.ack) {
1573                                 debug(c, "cancelling RTT measurement: %u < %u\n", c->rtt_seq, hdr.ack);
1574                                 c->rtt_start.tv_sec = 0;
1575                         }
1576                 }
1577
1578                 int32_t data_acked = advanced;
1579
1580                 switch(c->state) {
1581                 case SYN_SENT:
1582                 case SYN_RECEIVED:
1583                         data_acked--;
1584                         break;
1585
1586                 // TODO: handle FIN as well.
1587                 default:
1588                         break;
1589                 }
1590
1591                 assert(data_acked >= 0);
1592
1593 #ifndef NDEBUG
1594                 int32_t bufused = seqdiff(c->snd.last, c->snd.una);
1595                 assert(data_acked <= bufused);
1596 #endif
1597
1598                 if(data_acked) {
1599                         buffer_discard(&c->sndbuf, data_acked);
1600
1601                         if(is_reliable(c)) {
1602                                 c->do_poll = true;
1603                         }
1604                 }
1605
1606                 // Also advance snd.nxt if possible
1607                 if(seqdiff(c->snd.nxt, hdr.ack) < 0) {
1608                         c->snd.nxt = hdr.ack;
1609                 }
1610
1611                 c->snd.una = hdr.ack;
1612
1613                 if(c->dupack) {
1614                         if(c->dupack >= 3) {
1615                                 debug(c, "fast recovery ended\n");
1616                                 c->snd.cwnd = c->snd.ssthresh;
1617                         }
1618
1619                         c->dupack = 0;
1620                 }
1621
1622                 // Increase the congestion window according to RFC 5681
1623                 if(c->snd.cwnd < c->snd.ssthresh) {
1624                         c->snd.cwnd += min(advanced, utcp->mss); // eq. 2
1625                 } else {
1626                         c->snd.cwnd += max(1, (utcp->mss * utcp->mss) / c->snd.cwnd); // eq. 3
1627                 }
1628
1629                 if(c->snd.cwnd > c->sndbuf.maxsize) {
1630                         c->snd.cwnd = c->sndbuf.maxsize;
1631                 }
1632
1633                 debug_cwnd(c);
1634
1635                 // Check if we have sent a FIN that is now ACKed.
1636                 switch(c->state) {
1637                 case FIN_WAIT_1:
1638                         if(c->snd.una == c->snd.last) {
1639                                 set_state(c, FIN_WAIT_2);
1640                         }
1641
1642                         break;
1643
1644                 case CLOSING:
1645                         if(c->snd.una == c->snd.last) {
1646                                 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1647                                 c->conn_timeout.tv_sec += utcp->timeout;
1648                                 set_state(c, TIME_WAIT);
1649                         }
1650
1651                         break;
1652
1653                 default:
1654                         break;
1655                 }
1656         } else {
1657                 if(!len && is_reliable(c) && c->snd.una != c->snd.last) {
1658                         c->dupack++;
1659                         debug(c, "duplicate ACK %d\n", c->dupack);
1660
1661                         if(c->dupack == 3) {
1662                                 // RFC 5681 fast recovery
1663                                 debug(c, "fast recovery started\n", c->dupack);
1664                                 uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
1665                                 c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
1666                                 c->snd.cwnd = min(c->snd.ssthresh + 3 * utcp->mss, c->sndbuf.maxsize);
1667
1668                                 if(c->snd.cwnd > c->sndbuf.maxsize) {
1669                                         c->snd.cwnd = c->sndbuf.maxsize;
1670                                 }
1671
1672                                 debug_cwnd(c);
1673
1674                                 fast_retransmit(c);
1675                         } else if(c->dupack > 3) {
1676                                 c->snd.cwnd += utcp->mss;
1677
1678                                 if(c->snd.cwnd > c->sndbuf.maxsize) {
1679                                         c->snd.cwnd = c->sndbuf.maxsize;
1680                                 }
1681
1682                                 debug_cwnd(c);
1683                         }
1684
1685                         // We got an ACK which indicates the other side did get one of our packets.
1686                         // Reset the retransmission timer to avoid going to slow start,
1687                         // but don't touch the connection timeout.
1688                         start_retransmit_timer(c);
1689                 }
1690         }
1691
1692         // 4. Update timers
1693
1694         if(advanced) {
1695                 if(c->snd.una == c->snd.last) {
1696                         stop_retransmit_timer(c);
1697                         timespec_clear(&c->conn_timeout);
1698                 } else if(is_reliable(c)) {
1699                         start_retransmit_timer(c);
1700                         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1701                         c->conn_timeout.tv_sec += utcp->timeout;
1702                 }
1703         }
1704
1705 skip_ack:
1706         // 5. Process SYN stuff
1707
1708         if(hdr.ctl & SYN) {
1709                 switch(c->state) {
1710                 case SYN_SENT:
1711
1712                         // This is a SYNACK. It should always have ACKed the SYN.
1713                         if(!advanced) {
1714                                 goto reset;
1715                         }
1716
1717                         c->rcv.irs = hdr.seq;
1718                         c->rcv.nxt = hdr.seq + 1;
1719
1720                         if(c->shut_wr) {
1721                                 c->snd.last++;
1722                                 set_state(c, FIN_WAIT_1);
1723                         } else {
1724                                 set_state(c, ESTABLISHED);
1725                         }
1726
1727                         break;
1728
1729                 case SYN_RECEIVED:
1730                         // This is a retransmit of a SYN, send back the SYNACK.
1731                         goto synack;
1732
1733                 case ESTABLISHED:
1734                 case FIN_WAIT_1:
1735                 case FIN_WAIT_2:
1736                 case CLOSE_WAIT:
1737                 case CLOSING:
1738                 case LAST_ACK:
1739                 case TIME_WAIT:
1740                         // This could be a retransmission. Ignore the SYN flag, but send an ACK back.
1741                         break;
1742
1743                 default:
1744 #ifdef UTCP_DEBUG
1745                         abort();
1746 #endif
1747                         return 0;
1748                 }
1749         }
1750
1751         // 6. Process new data
1752
1753         if(c->state == SYN_RECEIVED) {
1754                 // This is the ACK after the SYNACK. It should always have ACKed the SYNACK.
1755                 if(!advanced) {
1756                         goto reset;
1757                 }
1758
1759                 // Are we still LISTENing?
1760                 if(utcp->accept) {
1761                         utcp->accept(c, c->src);
1762                 }
1763
1764                 if(c->state != ESTABLISHED) {
1765                         set_state(c, CLOSED);
1766                         c->reapable = true;
1767                         goto reset;
1768                 }
1769         }
1770
1771         if(len) {
1772                 switch(c->state) {
1773                 case SYN_SENT:
1774                 case SYN_RECEIVED:
1775                         // This should never happen.
1776 #ifdef UTCP_DEBUG
1777                         abort();
1778 #endif
1779                         return 0;
1780
1781                 case ESTABLISHED:
1782                 case FIN_WAIT_1:
1783                 case FIN_WAIT_2:
1784                         break;
1785
1786                 case CLOSE_WAIT:
1787                 case CLOSING:
1788                 case LAST_ACK:
1789                 case TIME_WAIT:
1790                         // Ehm no, We should never receive more data after a FIN.
1791                         goto reset;
1792
1793                 default:
1794 #ifdef UTCP_DEBUG
1795                         abort();
1796 #endif
1797                         return 0;
1798                 }
1799
1800                 handle_incoming_data(c, &hdr, ptr, len);
1801         }
1802
1803         // 7. Process FIN stuff
1804
1805         if((hdr.ctl & FIN) && (!is_reliable(c) || hdr.seq + len == c->rcv.nxt)) {
1806                 switch(c->state) {
1807                 case SYN_SENT:
1808                 case SYN_RECEIVED:
1809                         // This should never happen.
1810 #ifdef UTCP_DEBUG
1811                         abort();
1812 #endif
1813                         break;
1814
1815                 case ESTABLISHED:
1816                         set_state(c, CLOSE_WAIT);
1817                         break;
1818
1819                 case FIN_WAIT_1:
1820                         set_state(c, CLOSING);
1821                         break;
1822
1823                 case FIN_WAIT_2:
1824                         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1825                         c->conn_timeout.tv_sec += utcp->timeout;
1826                         set_state(c, TIME_WAIT);
1827                         break;
1828
1829                 case CLOSE_WAIT:
1830                 case CLOSING:
1831                 case LAST_ACK:
1832                 case TIME_WAIT:
1833                         // Ehm, no. We should never receive a second FIN.
1834                         goto reset;
1835
1836                 default:
1837 #ifdef UTCP_DEBUG
1838                         abort();
1839 #endif
1840                         break;
1841                 }
1842
1843                 // FIN counts as one sequence number
1844                 c->rcv.nxt++;
1845                 len++;
1846
1847                 // Inform the application that the peer closed its end of the connection.
1848                 if(c->recv) {
1849                         errno = 0;
1850                         c->recv(c, NULL, 0);
1851                 }
1852         }
1853
1854         // Now we send something back if:
1855         // - we received data, so we have to send back an ACK
1856         //   -> sendatleastone = true
1857         // - or we got an ack, so we should maybe send a bit more data
1858         //   -> sendatleastone = false
1859
1860         if(is_reliable(c) || hdr.ctl & SYN || hdr.ctl & FIN) {
1861                 ack(c, has_data);
1862         }
1863
1864         return 0;
1865
1866 reset:
1867         swap_ports(&hdr);
1868         hdr.wnd = 0;
1869         hdr.aux = 0;
1870
1871         if(hdr.ctl & ACK) {
1872                 hdr.seq = hdr.ack;
1873                 hdr.ctl = RST;
1874         } else {
1875                 hdr.ack = hdr.seq + len;
1876                 hdr.seq = 0;
1877                 hdr.ctl = RST | ACK;
1878         }
1879
1880         print_packet(c, "send", &hdr, sizeof(hdr));
1881         utcp->send(utcp, &hdr, sizeof(hdr));
1882         return 0;
1883
1884 }
1885
1886 int utcp_shutdown(struct utcp_connection *c, int dir) {
1887         debug(c, "shutdown %d at %u\n", dir, c ? c->snd.last : 0);
1888
1889         if(!c) {
1890                 errno = EFAULT;
1891                 return -1;
1892         }
1893
1894         if(c->reapable) {
1895                 debug(c, "shutdown() called on closed connection\n");
1896                 errno = EBADF;
1897                 return -1;
1898         }
1899
1900         if(!(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_WR || dir == UTCP_SHUT_RDWR)) {
1901                 errno = EINVAL;
1902                 return -1;
1903         }
1904
1905         // TCP does not have a provision for stopping incoming packets.
1906         // The best we can do is to just ignore them.
1907         if(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_RDWR) {
1908                 c->recv = NULL;
1909         }
1910
1911         // The rest of the code deals with shutting down writes.
1912         if(dir == UTCP_SHUT_RD) {
1913                 return 0;
1914         }
1915
1916         // Only process shutting down writes once.
1917         if(c->shut_wr) {
1918                 return 0;
1919         }
1920
1921         c->shut_wr = true;
1922
1923         switch(c->state) {
1924         case CLOSED:
1925         case LISTEN:
1926                 errno = ENOTCONN;
1927                 return -1;
1928
1929         case SYN_SENT:
1930                 return 0;
1931
1932         case SYN_RECEIVED:
1933         case ESTABLISHED:
1934                 set_state(c, FIN_WAIT_1);
1935                 break;
1936
1937         case FIN_WAIT_1:
1938         case FIN_WAIT_2:
1939                 return 0;
1940
1941         case CLOSE_WAIT:
1942                 set_state(c, CLOSING);
1943                 break;
1944
1945         case CLOSING:
1946         case LAST_ACK:
1947         case TIME_WAIT:
1948                 return 0;
1949         }
1950
1951         c->snd.last++;
1952
1953         ack(c, false);
1954
1955         if(!timespec_isset(&c->rtrx_timeout)) {
1956                 start_retransmit_timer(c);
1957         }
1958
1959         return 0;
1960 }
1961
1962 static bool reset_connection(struct utcp_connection *c) {
1963         if(!c) {
1964                 errno = EFAULT;
1965                 return false;
1966         }
1967
1968         if(c->reapable) {
1969                 debug(c, "abort() called on closed connection\n");
1970                 errno = EBADF;
1971                 return false;
1972         }
1973
1974         c->recv = NULL;
1975         c->poll = NULL;
1976
1977         switch(c->state) {
1978         case CLOSED:
1979                 return true;
1980
1981         case LISTEN:
1982         case SYN_SENT:
1983         case CLOSING:
1984         case LAST_ACK:
1985         case TIME_WAIT:
1986                 set_state(c, CLOSED);
1987                 return true;
1988
1989         case SYN_RECEIVED:
1990         case ESTABLISHED:
1991         case FIN_WAIT_1:
1992         case FIN_WAIT_2:
1993         case CLOSE_WAIT:
1994                 set_state(c, CLOSED);
1995                 break;
1996         }
1997
1998         // Send RST
1999
2000         struct hdr hdr;
2001
2002         hdr.src = c->src;
2003         hdr.dst = c->dst;
2004         hdr.seq = c->snd.nxt;
2005         hdr.ack = 0;
2006         hdr.wnd = 0;
2007         hdr.ctl = RST;
2008
2009         print_packet(c, "send", &hdr, sizeof(hdr));
2010         c->utcp->send(c->utcp, &hdr, sizeof(hdr));
2011         return true;
2012 }
2013
2014 // Closes all the opened connections
2015 void utcp_abort_all_connections(struct utcp *utcp) {
2016         if(!utcp) {
2017                 errno = EINVAL;
2018                 return;
2019         }
2020
2021         for(int i = 0; i < utcp->nconnections; i++) {
2022                 struct utcp_connection *c = utcp->connections[i];
2023
2024                 if(c->reapable || c->state == CLOSED) {
2025                         continue;
2026                 }
2027
2028                 utcp_recv_t old_recv = c->recv;
2029                 utcp_poll_t old_poll = c->poll;
2030
2031                 reset_connection(c);
2032
2033                 if(old_recv) {
2034                         errno = 0;
2035                         old_recv(c, NULL, 0);
2036                 }
2037
2038                 if(old_poll && !c->reapable) {
2039                         errno = 0;
2040                         old_poll(c, 0);
2041                 }
2042         }
2043
2044         return;
2045 }
2046
2047 int utcp_close(struct utcp_connection *c) {
2048         if(utcp_shutdown(c, SHUT_RDWR) && errno != ENOTCONN) {
2049                 return -1;
2050         }
2051
2052         c->recv = NULL;
2053         c->poll = NULL;
2054         c->reapable = true;
2055         return 0;
2056 }
2057
2058 int utcp_abort(struct utcp_connection *c) {
2059         if(!reset_connection(c)) {
2060                 return -1;
2061         }
2062
2063         c->reapable = true;
2064         return 0;
2065 }
2066
2067 /* Handle timeouts.
2068  * One call to this function will loop through all connections,
2069  * checking if something needs to be resent or not.
2070  * The return value is the time to the next timeout in milliseconds,
2071  * or maybe a negative value if the timeout is infinite.
2072  */
2073 struct timespec utcp_timeout(struct utcp *utcp) {
2074         struct timespec now;
2075         clock_gettime(UTCP_CLOCK, &now);
2076         struct timespec next = {now.tv_sec + 3600, now.tv_nsec};
2077
2078         for(int i = 0; i < utcp->nconnections; i++) {
2079                 struct utcp_connection *c = utcp->connections[i];
2080
2081                 if(!c) {
2082                         continue;
2083                 }
2084
2085                 // delete connections that have been utcp_close()d.
2086                 if(c->state == CLOSED) {
2087                         if(c->reapable) {
2088                                 debug(c, "reaping\n");
2089                                 free_connection(c);
2090                                 i--;
2091                         }
2092
2093                         continue;
2094                 }
2095
2096                 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &now)) {
2097                         errno = ETIMEDOUT;
2098                         c->state = CLOSED;
2099
2100                         if(c->recv) {
2101                                 c->recv(c, NULL, 0);
2102                         }
2103
2104                         if(c->poll && !c->reapable) {
2105                                 c->poll(c, 0);
2106                         }
2107
2108                         continue;
2109                 }
2110
2111                 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &now)) {
2112                         debug(c, "retransmitting after timeout\n");
2113                         retransmit(c);
2114                 }
2115
2116                 if(c->poll) {
2117                         if((c->state == ESTABLISHED || c->state == CLOSE_WAIT) && c->do_poll) {
2118                                 c->do_poll = false;
2119                                 uint32_t len = buffer_free(&c->sndbuf);
2120
2121                                 if(len) {
2122                                         c->poll(c, len);
2123                                 }
2124                         } else if(c->state == CLOSED) {
2125                                 c->poll(c, 0);
2126                         }
2127                 }
2128
2129                 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &next)) {
2130                         next = c->conn_timeout;
2131                 }
2132
2133                 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &next)) {
2134                         next = c->rtrx_timeout;
2135                 }
2136         }
2137
2138         struct timespec diff;
2139
2140         timespec_sub(&next, &now, &diff);
2141
2142         return diff;
2143 }
2144
2145 bool utcp_is_active(struct utcp *utcp) {
2146         if(!utcp) {
2147                 return false;
2148         }
2149
2150         for(int i = 0; i < utcp->nconnections; i++)
2151                 if(utcp->connections[i]->state != CLOSED && utcp->connections[i]->state != TIME_WAIT) {
2152                         return true;
2153                 }
2154
2155         return false;
2156 }
2157
2158 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
2159         if(!send) {
2160                 errno = EFAULT;
2161                 return NULL;
2162         }
2163
2164         struct utcp *utcp = calloc(1, sizeof(*utcp));
2165
2166         if(!utcp) {
2167                 return NULL;
2168         }
2169
2170         utcp_set_mtu(utcp, DEFAULT_MTU);
2171
2172         if(!utcp->pkt) {
2173                 free(utcp);
2174                 return NULL;
2175         }
2176
2177         if(!CLOCK_GRANULARITY) {
2178                 struct timespec res;
2179                 clock_getres(UTCP_CLOCK, &res);
2180                 CLOCK_GRANULARITY = res.tv_sec * USEC_PER_SEC + res.tv_nsec / 1000;
2181         }
2182
2183         utcp->accept = accept;
2184         utcp->pre_accept = pre_accept;
2185         utcp->send = send;
2186         utcp->priv = priv;
2187         utcp->timeout = DEFAULT_USER_TIMEOUT; // sec
2188
2189         return utcp;
2190 }
2191
2192 void utcp_exit(struct utcp *utcp) {
2193         if(!utcp) {
2194                 return;
2195         }
2196
2197         for(int i = 0; i < utcp->nconnections; i++) {
2198                 struct utcp_connection *c = utcp->connections[i];
2199
2200                 if(!c->reapable) {
2201                         if(c->recv) {
2202                                 c->recv(c, NULL, 0);
2203                         }
2204
2205                         if(c->poll && !c->reapable) {
2206                                 c->poll(c, 0);
2207                         }
2208                 }
2209
2210                 buffer_exit(&c->rcvbuf);
2211                 buffer_exit(&c->sndbuf);
2212                 free(c);
2213         }
2214
2215         free(utcp->connections);
2216         free(utcp->pkt);
2217         free(utcp);
2218 }
2219
2220 uint16_t utcp_get_mtu(struct utcp *utcp) {
2221         return utcp ? utcp->mtu : 0;
2222 }
2223
2224 uint16_t utcp_get_mss(struct utcp *utcp) {
2225         return utcp ? utcp->mss : 0;
2226 }
2227
2228 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
2229         if(!utcp) {
2230                 return;
2231         }
2232
2233         if(mtu <= sizeof(struct hdr)) {
2234                 return;
2235         }
2236
2237         if(mtu > utcp->mtu) {
2238                 char *new = realloc(utcp->pkt, mtu + sizeof(struct hdr));
2239
2240                 if(!new) {
2241                         return;
2242                 }
2243
2244                 utcp->pkt = new;
2245         }
2246
2247         utcp->mtu = mtu;
2248         utcp->mss = mtu - sizeof(struct hdr);
2249 }
2250
2251 void utcp_reset_timers(struct utcp *utcp) {
2252         if(!utcp) {
2253                 return;
2254         }
2255
2256         struct timespec now, then;
2257
2258         clock_gettime(UTCP_CLOCK, &now);
2259
2260         then = now;
2261
2262         then.tv_sec += utcp->timeout;
2263
2264         for(int i = 0; i < utcp->nconnections; i++) {
2265                 struct utcp_connection *c = utcp->connections[i];
2266
2267                 if(c->reapable) {
2268                         continue;
2269                 }
2270
2271                 if(timespec_isset(&c->rtrx_timeout)) {
2272                         c->rtrx_timeout = now;
2273                 }
2274
2275                 if(timespec_isset(&c->conn_timeout)) {
2276                         c->conn_timeout = then;
2277                 }
2278
2279                 c->rtt_start.tv_sec = 0;
2280
2281                 if(c->rto > START_RTO) {
2282                         c->rto = START_RTO;
2283                 }
2284         }
2285 }
2286
2287 int utcp_get_user_timeout(struct utcp *u) {
2288         return u ? u->timeout : 0;
2289 }
2290
2291 void utcp_set_user_timeout(struct utcp *u, int timeout) {
2292         if(u) {
2293                 u->timeout = timeout;
2294         }
2295 }
2296
2297 size_t utcp_get_sndbuf(struct utcp_connection *c) {
2298         return c ? c->sndbuf.maxsize : 0;
2299 }
2300
2301 size_t utcp_get_sndbuf_free(struct utcp_connection *c) {
2302         if(!c) {
2303                 return 0;
2304         }
2305
2306         switch(c->state) {
2307         case SYN_SENT:
2308         case SYN_RECEIVED:
2309         case ESTABLISHED:
2310         case CLOSE_WAIT:
2311                 return buffer_free(&c->sndbuf);
2312
2313         default:
2314                 return 0;
2315         }
2316 }
2317
2318 void utcp_set_sndbuf(struct utcp_connection *c, size_t size) {
2319         if(!c) {
2320                 return;
2321         }
2322
2323         c->sndbuf.maxsize = size;
2324
2325         if(c->sndbuf.maxsize != size) {
2326                 c->sndbuf.maxsize = -1;
2327         }
2328
2329         c->do_poll = is_reliable(c) && buffer_free(&c->sndbuf);
2330 }
2331
2332 size_t utcp_get_rcvbuf(struct utcp_connection *c) {
2333         return c ? c->rcvbuf.maxsize : 0;
2334 }
2335
2336 size_t utcp_get_rcvbuf_free(struct utcp_connection *c) {
2337         if(c && (c->state == ESTABLISHED || c->state == CLOSE_WAIT)) {
2338                 return buffer_free(&c->rcvbuf);
2339         } else {
2340                 return 0;
2341         }
2342 }
2343
2344 void utcp_set_rcvbuf(struct utcp_connection *c, size_t size) {
2345         if(!c) {
2346                 return;
2347         }
2348
2349         c->rcvbuf.maxsize = size;
2350
2351         if(c->rcvbuf.maxsize != size) {
2352                 c->rcvbuf.maxsize = -1;
2353         }
2354 }
2355
2356 size_t utcp_get_sendq(struct utcp_connection *c) {
2357         return c->sndbuf.used;
2358 }
2359
2360 size_t utcp_get_recvq(struct utcp_connection *c) {
2361         return c->rcvbuf.used;
2362 }
2363
2364 bool utcp_get_nodelay(struct utcp_connection *c) {
2365         return c ? c->nodelay : false;
2366 }
2367
2368 void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) {
2369         if(c) {
2370                 c->nodelay = nodelay;
2371         }
2372 }
2373
2374 bool utcp_get_keepalive(struct utcp_connection *c) {
2375         return c ? c->keepalive : false;
2376 }
2377
2378 void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
2379         if(c) {
2380                 c->keepalive = keepalive;
2381         }
2382 }
2383
2384 size_t utcp_get_outq(struct utcp_connection *c) {
2385         return c ? seqdiff(c->snd.nxt, c->snd.una) : 0;
2386 }
2387
2388 void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) {
2389         if(c) {
2390                 c->recv = recv;
2391         }
2392 }
2393
2394 void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) {
2395         if(c) {
2396                 c->poll = poll;
2397                 c->do_poll = is_reliable(c) && buffer_free(&c->sndbuf);
2398         }
2399 }
2400
2401 void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_pre_accept_t pre_accept) {
2402         if(utcp) {
2403                 utcp->accept = accept;
2404                 utcp->pre_accept = pre_accept;
2405         }
2406 }
2407
2408 void utcp_expect_data(struct utcp_connection *c, bool expect) {
2409         if(!c || c->reapable) {
2410                 return;
2411         }
2412
2413         if(!(c->state == ESTABLISHED || c->state == FIN_WAIT_1 || c->state == FIN_WAIT_2)) {
2414                 return;
2415         }
2416
2417         if(expect) {
2418                 // If we expect data, start the connection timer.
2419                 if(!timespec_isset(&c->conn_timeout)) {
2420                         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
2421                         c->conn_timeout.tv_sec += c->utcp->timeout;
2422                 }
2423         } else {
2424                 // If we want to cancel expecting data, only clear the timer when there is no unACKed data.
2425                 if(c->snd.una == c->snd.last) {
2426                         timespec_clear(&c->conn_timeout);
2427                 }
2428         }
2429 }
2430
2431 void utcp_offline(struct utcp *utcp, bool offline) {
2432         struct timespec now;
2433         clock_gettime(UTCP_CLOCK, &now);
2434
2435         for(int i = 0; i < utcp->nconnections; i++) {
2436                 struct utcp_connection *c = utcp->connections[i];
2437
2438                 if(c->reapable) {
2439                         continue;
2440                 }
2441
2442                 utcp_expect_data(c, offline);
2443
2444                 if(!offline) {
2445                         if(timespec_isset(&c->rtrx_timeout)) {
2446                                 c->rtrx_timeout = now;
2447                         }
2448
2449                         utcp->connections[i]->rtt_start.tv_sec = 0;
2450
2451                         if(c->rto > START_RTO) {
2452                                 c->rto = START_RTO;
2453                         }
2454                 }
2455         }
2456 }
2457
2458 void utcp_set_retransmit_cb(struct utcp *utcp, utcp_retransmit_t retransmit) {
2459         utcp->retransmit = retransmit;
2460 }
2461
2462 void utcp_set_clock_granularity(long granularity) {
2463         CLOCK_GRANULARITY = granularity;
2464 }