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