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