]> git.meshlink.io Git - meshlink-tiny/blob - src/utcp.c
Remove logging when compiled with -DNDEBUG.
[meshlink-tiny] / 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 #include "system.h"
21 #include <time.h>
22
23 #include "utcp_priv.h"
24
25 #ifndef EBADMSG
26 #define EBADMSG         104
27 #endif
28
29 #ifndef SHUT_RDWR
30 #define SHUT_RDWR 2
31 #endif
32
33 #ifdef poll
34 #undef poll
35 #endif
36
37 #ifndef UTCP_CLOCK
38 #if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
39 #define UTCP_CLOCK CLOCK_MONOTONIC_RAW
40 #else
41 #define UTCP_CLOCK CLOCK_MONOTONIC
42 #endif
43 #endif
44
45 static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
46         r->tv_sec = a->tv_sec - b->tv_sec;
47         r->tv_nsec = a->tv_nsec - b->tv_nsec;
48
49         if(r->tv_nsec < 0) {
50                 r->tv_sec--, r->tv_nsec += NSEC_PER_SEC;
51         }
52 }
53
54 static int32_t timespec_diff_usec(const struct timespec *a, const struct timespec *b) {
55         return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_nsec - b->tv_nsec) / 1000;
56 }
57
58 static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
59         if(a->tv_sec == b->tv_sec) {
60                 return a->tv_nsec < b->tv_nsec;
61         } else {
62                 return a->tv_sec < b->tv_sec;
63         }
64 }
65
66 static void timespec_clear(struct timespec *a) {
67         a->tv_sec = 0;
68         a->tv_nsec = 0;
69 }
70
71 static bool timespec_isset(const struct timespec *a) {
72         return a->tv_sec;
73 }
74
75 static long CLOCK_GRANULARITY; // usec
76
77 static inline size_t min(size_t a, size_t b) {
78         return a < b ? a : b;
79 }
80
81 static inline size_t max(size_t a, size_t b) {
82         return a > b ? a : b;
83 }
84
85 #ifdef UTCP_DEBUG
86 #include <stdarg.h>
87
88 #ifndef UTCP_DEBUG_DATALEN
89 #define UTCP_DEBUG_DATALEN 20
90 #endif
91
92 static void debug(struct utcp_connection *c, const char *format, ...) {
93         struct timespec tv;
94         char buf[1024];
95         int len;
96
97         clock_gettime(CLOCK_REALTIME, &tv);
98         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);
99         va_list ap;
100         va_start(ap, format);
101         len += vsnprintf(buf + len, sizeof(buf) - len, format, ap);
102         va_end(ap);
103
104         if(len > 0 && (size_t)len < sizeof(buf)) {
105                 fwrite(buf, len, 1, stderr);
106         }
107 }
108
109 static void print_packet(struct utcp_connection *c, const char *dir, const void *pkt, size_t len) {
110         struct hdr hdr;
111
112         if(len < sizeof(hdr)) {
113                 debug(c, "%s: short packet (%lu bytes)\n", dir, (unsigned long)len);
114                 return;
115         }
116
117         memcpy(&hdr, pkt, sizeof(hdr));
118
119         uint32_t datalen;
120
121         if(len > sizeof(hdr)) {
122                 datalen = min(len - sizeof(hdr), UTCP_DEBUG_DATALEN);
123         } else {
124                 datalen = 0;
125         }
126
127
128         const uint8_t *data = (uint8_t *)pkt + sizeof(hdr);
129         char str[datalen * 2 + 1];
130         char *p = str;
131
132         for(uint32_t i = 0; i < datalen; i++) {
133                 *p++ = "0123456789ABCDEF"[data[i] >> 4];
134                 *p++ = "0123456789ABCDEF"[data[i] & 15];
135         }
136
137         *p = 0;
138
139         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",
140               dir, (unsigned long)len, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd, hdr.aux,
141               hdr.ctl & SYN ? "SYN" : "",
142               hdr.ctl & RST ? "RST" : "",
143               hdr.ctl & FIN ? "FIN" : "",
144               hdr.ctl & ACK ? "ACK" : "",
145               hdr.ctl & MF ? "MF" : "",
146               str
147              );
148 }
149
150 static void debug_cwnd(struct utcp_connection *c) {
151         debug(c, "snd.cwnd %u snd.ssthresh %u\n", c->snd.cwnd, ~c->snd.ssthresh ? c->snd.ssthresh : 0);
152 }
153 #else
154 #define debug(...) do {} while(0)
155 #define print_packet(...) do {} while(0)
156 #define debug_cwnd(...) do {} while(0)
157 #endif
158
159 static void set_state(struct utcp_connection *c, enum state state) {
160         c->state = state;
161
162         if(state == ESTABLISHED) {
163                 timespec_clear(&c->conn_timeout);
164         }
165
166         debug(c, "state %s\n", strstate[state]);
167 }
168
169 static bool fin_wanted(struct utcp_connection *c, uint32_t seq) {
170         if(seq != c->snd.last) {
171                 return false;
172         }
173
174         switch(c->state) {
175         case FIN_WAIT_1:
176         case CLOSING:
177         case LAST_ACK:
178                 return true;
179
180         default:
181                 return false;
182         }
183 }
184
185 static int32_t seqdiff(uint32_t a, uint32_t b) {
186         return a - b;
187 }
188
189 // Connections are stored in a sorted list.
190 // This gives O(log(N)) lookup time, O(N log(N)) insertion time and O(N) deletion time.
191
192 static int compare(const void *va, const void *vb) {
193         assert(va && vb);
194
195         const struct utcp_connection *a = *(struct utcp_connection **)va;
196         const struct utcp_connection *b = *(struct utcp_connection **)vb;
197
198         assert(a && b);
199
200         int c = (int)a->src - (int)b->src;
201
202         if(c) {
203                 return c;
204         }
205
206         c = (int)a->dst - (int)b->dst;
207         return c;
208 }
209
210 static struct utcp_connection *find_connection(const struct utcp *utcp, uint16_t src, uint16_t dst) {
211         if(!utcp->nconnections) {
212                 return NULL;
213         }
214
215         struct utcp_connection key = {
216                 .src = src,
217                 .dst = dst,
218         }, *keyp = &key;
219         struct utcp_connection **match = bsearch(&keyp, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
220         return match ? *match : NULL;
221 }
222
223 static void free_connection(struct utcp_connection *c) {
224         struct utcp *utcp = c->utcp;
225         struct utcp_connection **cp = bsearch(&c, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
226
227         assert(cp);
228
229         int i = cp - utcp->connections;
230         memmove(cp, cp + 1, (utcp->nconnections - i - 1) * sizeof(*cp));
231         utcp->nconnections--;
232
233         free(c);
234 }
235
236 static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t src, uint16_t dst) {
237         // Check whether this combination of src and dst is free
238
239         if(src) {
240                 if(find_connection(utcp, src, dst)) {
241                         errno = EADDRINUSE;
242                         return NULL;
243                 }
244         } else { // If src == 0, generate a random port number with the high bit set
245                 if(utcp->nconnections >= 32767) {
246                         errno = ENOMEM;
247                         return NULL;
248                 }
249
250                 src = rand() | 0x8000;
251
252                 while(find_connection(utcp, src, dst)) {
253                         src++;
254                 }
255         }
256
257         // Allocate memory for the new connection
258
259         if(utcp->nconnections >= utcp->nallocated) {
260                 if(!utcp->nallocated) {
261                         utcp->nallocated = 4;
262                 } else {
263                         utcp->nallocated *= 2;
264                 }
265
266                 struct utcp_connection **new_array = realloc(utcp->connections, utcp->nallocated * sizeof(*utcp->connections));
267
268                 if(!new_array) {
269                         return NULL;
270                 }
271
272                 utcp->connections = new_array;
273         }
274
275         struct utcp_connection *c = calloc(1, sizeof(*c));
276
277         if(!c) {
278                 return NULL;
279         }
280
281         // Fill in the details
282
283         c->src = src;
284         c->dst = dst;
285 #ifdef UTCP_DEBUG
286         c->snd.iss = 0;
287 #else
288         c->snd.iss = rand();
289 #endif
290         c->snd.una = c->snd.iss;
291         c->snd.nxt = c->snd.iss + 1;
292         c->snd.last = c->snd.nxt;
293         c->snd.cwnd = (utcp->mss > 2190 ? 2 : utcp->mss > 1095 ? 3 : 4) * utcp->mss;
294         c->snd.ssthresh = ~0;
295         debug_cwnd(c);
296         c->srtt = 0;
297         c->rttvar = 0;
298         c->rto = START_RTO;
299         c->utcp = utcp;
300
301         // Add it to the sorted list of connections
302
303         utcp->connections[utcp->nconnections++] = c;
304         qsort(utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
305
306         return c;
307 }
308
309 static inline uint32_t absdiff(uint32_t a, uint32_t b) {
310         if(a > b) {
311                 return a - b;
312         } else {
313                 return b - a;
314         }
315 }
316
317 // Update RTT variables. See RFC 6298.
318 static void update_rtt(struct utcp_connection *c, uint32_t rtt) {
319         if(!rtt) {
320                 debug(c, "invalid rtt\n");
321                 return;
322         }
323
324         if(!c->srtt) {
325                 c->srtt = rtt;
326                 c->rttvar = rtt / 2;
327         } else {
328                 c->rttvar = (c->rttvar * 3 + absdiff(c->srtt, rtt)) / 4;
329                 c->srtt = (c->srtt * 7 + rtt) / 8;
330         }
331
332         c->rto = c->srtt + max(4 * c->rttvar, CLOCK_GRANULARITY);
333
334         if(c->rto > MAX_RTO) {
335                 c->rto = MAX_RTO;
336         }
337
338         debug(c, "rtt %u srtt %u rttvar %u rto %u\n", rtt, c->srtt, c->rttvar, c->rto);
339 }
340
341 static void start_retransmit_timer(struct utcp_connection *c) {
342         clock_gettime(UTCP_CLOCK, &c->rtrx_timeout);
343
344         uint32_t rto = c->rto;
345
346         while(rto > USEC_PER_SEC) {
347                 c->rtrx_timeout.tv_sec++;
348                 rto -= USEC_PER_SEC;
349         }
350
351         c->rtrx_timeout.tv_nsec += rto * 1000;
352
353         if(c->rtrx_timeout.tv_nsec >= NSEC_PER_SEC) {
354                 c->rtrx_timeout.tv_nsec -= NSEC_PER_SEC;
355                 c->rtrx_timeout.tv_sec++;
356         }
357
358         debug(c, "rtrx_timeout %ld.%06lu\n", c->rtrx_timeout.tv_sec, c->rtrx_timeout.tv_nsec);
359 }
360
361 static void stop_retransmit_timer(struct utcp_connection *c) {
362         timespec_clear(&c->rtrx_timeout);
363         debug(c, "rtrx_timeout cleared\n");
364 }
365
366 struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv, uint32_t flags) {
367         struct utcp_connection *c = allocate_connection(utcp, 0, dst);
368
369         if(!c) {
370                 return NULL;
371         }
372
373         assert(flags == 0); // UDP only
374
375         c->flags = flags;
376         c->recv = recv;
377         c->priv = priv;
378
379         struct {
380                 struct hdr hdr;
381                 uint8_t init[4];
382         } pkt;
383
384         pkt.hdr.src = c->src;
385         pkt.hdr.dst = c->dst;
386         pkt.hdr.seq = c->snd.iss;
387         pkt.hdr.ack = 0;
388         pkt.hdr.wnd = c->utcp->mtu;
389         pkt.hdr.ctl = SYN;
390         pkt.hdr.aux = 0x0101;
391         pkt.init[0] = 1;
392         pkt.init[1] = 0;
393         pkt.init[2] = 0;
394         pkt.init[3] = flags & 0x7;
395
396         set_state(c, SYN_SENT);
397
398         print_packet(c, "send", &pkt, sizeof(pkt));
399         utcp->send(utcp, &pkt, sizeof(pkt));
400
401         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
402         c->conn_timeout.tv_sec += utcp->timeout;
403
404         start_retransmit_timer(c);
405
406         return c;
407 }
408
409 void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
410         if(c->reapable || c->state != SYN_RECEIVED) {
411                 debug(c, "accept() called on invalid connection in state %s\n", c, strstate[c->state]);
412                 return;
413         }
414
415         debug(c, "accepted %p %p\n", c, recv, priv);
416         c->recv = recv;
417         c->priv = priv;
418         set_state(c, ESTABLISHED);
419 }
420
421 static void ack(struct utcp_connection *c, const void *data, size_t len) {
422         struct {
423                 struct hdr hdr;
424                 uint8_t data[];
425         } *pkt = c->utcp->pkt;
426
427         pkt->hdr.src = c->src;
428         pkt->hdr.dst = c->dst;
429         pkt->hdr.ack = c->rcv.nxt;
430         pkt->hdr.wnd = 0;
431         pkt->hdr.ctl = ACK;
432         pkt->hdr.aux = 0;
433
434         uint32_t seglen = len;
435         pkt->hdr.seq = c->snd.nxt;
436
437         c->snd.nxt += seglen;
438
439         if(fin_wanted(c, c->snd.nxt)) {
440                 pkt->hdr.ctl |= FIN;
441         }
442
443         if(data && len) {
444                 assert(len <= c->utcp->mtu);
445                 memcpy(pkt->data, data, len);
446         } else {
447                 assert(!data && !len);
448         }
449
450         if(!c->rtt_start.tv_sec) {
451                 // Start RTT measurement
452                 clock_gettime(UTCP_CLOCK, &c->rtt_start);
453                 c->rtt_seq = pkt->hdr.seq + seglen;
454                 debug(c, "starting RTT measurement, expecting ack %u\n", c->rtt_seq);
455         }
456
457         print_packet(c, "send", pkt, sizeof(pkt->hdr) + seglen);
458         c->utcp->send(c->utcp, pkt, sizeof(pkt->hdr) + seglen);
459 }
460
461 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
462         if(c->reapable) {
463                 debug(c, "send() called on closed connection\n");
464                 errno = EBADF;
465                 return -1;
466         }
467
468         switch(c->state) {
469         case CLOSED:
470         case LISTEN:
471                 debug(c, "send() called on unconnected connection\n");
472                 errno = ENOTCONN;
473                 return -1;
474
475         case SYN_SENT:
476         case SYN_RECEIVED:
477         case ESTABLISHED:
478         case CLOSE_WAIT:
479                 break;
480
481         case FIN_WAIT_1:
482         case FIN_WAIT_2:
483         case CLOSING:
484         case LAST_ACK:
485         case TIME_WAIT:
486                 debug(c, "send() called on closed connection\n");
487                 errno = EPIPE;
488                 return -1;
489         }
490
491         // Exit early if we have nothing to send.
492
493         if(!len) {
494                 return 0;
495         }
496
497         if(!data) {
498                 errno = EFAULT;
499                 return -1;
500         }
501
502         if(len > MAX_UNRELIABLE_SIZE || len > c->utcp->mtu) {
503                 errno = EMSGSIZE;
504                 return -1;
505         }
506
507         if(len <= 0) {
508                 return len;
509         }
510
511         c->snd.last += len;
512
513         // Don't send anything yet if the connection has not fully established yet
514
515         if(c->state == SYN_SENT || c->state == SYN_RECEIVED) {
516                 return len;
517         }
518
519         ack(c, data, len);
520
521         c->snd.una = c->snd.nxt = c->snd.last;
522
523         return len;
524 }
525
526 static void swap_ports(struct hdr *hdr) {
527         uint16_t tmp = hdr->src;
528         hdr->src = hdr->dst;
529         hdr->dst = tmp;
530 }
531
532 static void retransmit(struct utcp_connection *c) {
533         if(c->state == CLOSED || c->snd.last == c->snd.una) {
534                 debug(c, "retransmit() called but nothing to retransmit!\n");
535                 stop_retransmit_timer(c);
536                 return;
537         }
538
539         struct utcp *utcp = c->utcp;
540
541         struct {
542                 struct hdr hdr;
543                 uint8_t data[];
544         } *pkt = c->utcp->pkt;
545
546         pkt->hdr.src = c->src;
547         pkt->hdr.dst = c->dst;
548         pkt->hdr.wnd = c->utcp->mtu;
549         pkt->hdr.aux = 0;
550
551         switch(c->state) {
552         case SYN_SENT:
553                 // Send our SYN again
554                 pkt->hdr.seq = c->snd.iss;
555                 pkt->hdr.ack = 0;
556                 pkt->hdr.ctl = SYN;
557                 pkt->hdr.aux = 0x0101;
558                 pkt->data[0] = 1;
559                 pkt->data[1] = 0;
560                 pkt->data[2] = 0;
561                 pkt->data[3] = c->flags & 0x7;
562                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + 4);
563                 utcp->send(utcp, pkt, sizeof(pkt->hdr) + 4);
564                 break;
565
566         case SYN_RECEIVED:
567                 // Send SYNACK again
568                 pkt->hdr.seq = c->snd.nxt;
569                 pkt->hdr.ack = c->rcv.nxt;
570                 pkt->hdr.ctl = SYN | ACK;
571                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr));
572                 utcp->send(utcp, pkt, sizeof(pkt->hdr));
573                 break;
574
575         case ESTABLISHED:
576                 break;
577
578         case FIN_WAIT_1:
579         case CLOSE_WAIT:
580         case CLOSING:
581         case LAST_ACK:
582                 // Send unacked data again.
583                 pkt->hdr.seq = c->snd.una;
584                 pkt->hdr.ack = c->rcv.nxt;
585                 pkt->hdr.ctl = ACK;
586                 uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
587
588                 if(fin_wanted(c, c->snd.una + len)) {
589                         len--;
590                         pkt->hdr.ctl |= FIN;
591                 } else {
592                         break;
593                 }
594
595                 assert(len == 0);
596
597                 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
598                 utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
599                 break;
600
601         case CLOSED:
602         case LISTEN:
603         case TIME_WAIT:
604         case FIN_WAIT_2:
605                 // We shouldn't need to retransmit anything in this state.
606 #ifdef UTCP_DEBUG
607                 abort();
608 #endif
609                 stop_retransmit_timer(c);
610                 goto cleanup;
611         }
612
613         start_retransmit_timer(c);
614         c->rto *= 2;
615
616         if(c->rto > MAX_RTO) {
617                 c->rto = MAX_RTO;
618         }
619
620         c->rtt_start.tv_sec = 0; // invalidate RTT timer
621         c->dupack = 0; // cancel any ongoing fast recovery
622
623 cleanup:
624         return;
625 }
626
627 static void handle_unreliable(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
628         // Fast path for unfragmented packets
629         if(!hdr->wnd && !(hdr->ctl & MF)) {
630                 if(c->recv) {
631                         c->recv(c, data, len);
632                 }
633
634                 c->rcv.nxt = hdr->seq + len;
635                 return;
636         }
637 }
638
639 static void handle_incoming_data(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
640         handle_unreliable(c, hdr, data, len);
641 }
642
643 ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
644         const uint8_t *ptr = data;
645
646         if(!utcp) {
647                 errno = EFAULT;
648                 return -1;
649         }
650
651         if(!len) {
652                 return 0;
653         }
654
655         if(!data) {
656                 errno = EFAULT;
657                 return -1;
658         }
659
660         // Drop packets smaller than the header
661
662         struct hdr hdr;
663
664         if(len < sizeof(hdr)) {
665                 print_packet(NULL, "recv", data, len);
666                 errno = EBADMSG;
667                 return -1;
668         }
669
670         // Make a copy from the potentially unaligned data to a struct hdr
671
672         memcpy(&hdr, ptr, sizeof(hdr));
673
674         // Try to match the packet to an existing connection
675
676         struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
677         print_packet(c, "recv", data, len);
678
679         // Process the header
680
681         ptr += sizeof(hdr);
682         len -= sizeof(hdr);
683
684         // Drop packets with an unknown CTL flag
685
686         if(hdr.ctl & ~(SYN | ACK | RST | FIN | MF)) {
687                 print_packet(NULL, "recv", data, len);
688                 errno = EBADMSG;
689                 return -1;
690         }
691
692         // Check for auxiliary headers
693
694         const uint8_t *init = NULL;
695
696         uint16_t aux = hdr.aux;
697
698         while(aux) {
699                 size_t auxlen = 4 * (aux >> 8) & 0xf;
700                 uint8_t auxtype = aux & 0xff;
701
702                 if(len < auxlen) {
703                         errno = EBADMSG;
704                         return -1;
705                 }
706
707                 switch(auxtype) {
708                 case AUX_INIT:
709                         if(!(hdr.ctl & SYN) || auxlen != 4) {
710                                 errno = EBADMSG;
711                                 return -1;
712                         }
713
714                         init = ptr;
715                         break;
716
717                 default:
718                         errno = EBADMSG;
719                         return -1;
720                 }
721
722                 len -= auxlen;
723                 ptr += auxlen;
724
725                 if(!(aux & 0x800)) {
726                         break;
727                 }
728
729                 if(len < 2) {
730                         errno = EBADMSG;
731                         return -1;
732                 }
733
734                 memcpy(&aux, ptr, 2);
735                 len -= 2;
736                 ptr += 2;
737         }
738
739         // Is it for a new connection?
740
741         if(!c) {
742                 // Ignore RST packets
743
744                 if(hdr.ctl & RST) {
745                         return 0;
746                 }
747
748                 // Is it a SYN packet and are we LISTENing?
749
750                 if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept) {
751                         // If we don't want to accept it, send a RST back
752                         if((utcp->listen && !utcp->listen(utcp, hdr.dst))) {
753                                 len = 1;
754                                 goto reset;
755                         }
756
757                         // Try to allocate memory, otherwise send a RST back
758                         c = allocate_connection(utcp, hdr.dst, hdr.src);
759
760                         if(!c) {
761                                 len = 1;
762                                 goto reset;
763                         }
764
765                         // Parse auxilliary information
766                         if(init) {
767                                 if(init[0] < 1) {
768                                         len = 1;
769                                         goto reset;
770                                 }
771
772                                 c->flags = init[3] & 0x7;
773                         } else {
774                                 c->flags = UTCP_UDP;
775                         }
776
777 synack:
778                         // Return SYN+ACK, go to SYN_RECEIVED state
779                         c->snd.wnd = hdr.wnd;
780                         c->rcv.irs = hdr.seq;
781                         c->rcv.nxt = c->rcv.irs + 1;
782                         set_state(c, SYN_RECEIVED);
783
784                         struct {
785                                 struct hdr hdr;
786                                 uint8_t data[4];
787                         } pkt;
788
789                         pkt.hdr.src = c->src;
790                         pkt.hdr.dst = c->dst;
791                         pkt.hdr.ack = c->rcv.irs + 1;
792                         pkt.hdr.seq = c->snd.iss;
793                         pkt.hdr.wnd = c->utcp->mtu;
794                         pkt.hdr.ctl = SYN | ACK;
795
796                         if(init) {
797                                 pkt.hdr.aux = 0x0101;
798                                 pkt.data[0] = 1;
799                                 pkt.data[1] = 0;
800                                 pkt.data[2] = 0;
801                                 pkt.data[3] = c->flags & 0x7;
802                                 print_packet(c, "send", &pkt, sizeof(hdr) + 4);
803                                 utcp->send(utcp, &pkt, sizeof(hdr) + 4);
804                         } else {
805                                 pkt.hdr.aux = 0;
806                                 print_packet(c, "send", &pkt, sizeof(hdr));
807                                 utcp->send(utcp, &pkt, sizeof(hdr));
808                         }
809
810                         start_retransmit_timer(c);
811                 } else {
812                         // No, we don't want your packets, send a RST back
813                         len = 1;
814                         goto reset;
815                 }
816
817                 return 0;
818         }
819
820         debug(c, "state %s\n", strstate[c->state]);
821
822         // In case this is for a CLOSED connection, ignore the packet.
823         // TODO: make it so incoming packets can never match a CLOSED connection.
824
825         if(c->state == CLOSED) {
826                 debug(c, "got packet for closed connection\n");
827                 goto reset;
828         }
829
830         // It is for an existing connection.
831
832         // 1. Drop invalid packets.
833
834         // 1a. Drop packets that should not happen in our current state.
835
836         switch(c->state) {
837         case SYN_SENT:
838         case SYN_RECEIVED:
839         case ESTABLISHED:
840         case FIN_WAIT_1:
841         case FIN_WAIT_2:
842         case CLOSE_WAIT:
843         case CLOSING:
844         case LAST_ACK:
845         case TIME_WAIT:
846                 break;
847
848         default:
849 #ifdef UTCP_DEBUG
850                 abort();
851 #endif
852                 break;
853         }
854
855 #if UTCP_DEBUG
856         int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
857
858         if(rcv_offset) {
859                 debug(c, "packet out of order, offset %u bytes", rcv_offset);
860         }
861
862 #endif
863
864         c->snd.wnd = hdr.wnd; // TODO: move below
865
866         // 1c. Drop packets with an invalid ACK.
867         // ackno should not roll back, and it should also not be bigger than what we ever could have sent
868         // (= snd.una + c->sndbuf.used).
869
870         if(hdr.ack != c->snd.last && c->state >= ESTABLISHED) {
871                 hdr.ack = c->snd.una;
872         }
873
874         // 2. Handle RST packets
875
876         if(hdr.ctl & RST) {
877                 switch(c->state) {
878                 case SYN_SENT:
879                         if(!(hdr.ctl & ACK)) {
880                                 return 0;
881                         }
882
883                         // The peer has refused our connection.
884                         set_state(c, CLOSED);
885                         errno = ECONNREFUSED;
886
887                         if(c->recv) {
888                                 c->recv(c, NULL, 0);
889                         }
890
891                         return 0;
892
893                 case SYN_RECEIVED:
894                         if(hdr.ctl & ACK) {
895                                 return 0;
896                         }
897
898                         // We haven't told the application about this connection yet. Silently delete.
899                         free_connection(c);
900                         return 0;
901
902                 case ESTABLISHED:
903                 case FIN_WAIT_1:
904                 case FIN_WAIT_2:
905                 case CLOSE_WAIT:
906                         if(hdr.ctl & ACK) {
907                                 return 0;
908                         }
909
910                         // The peer has aborted our connection.
911                         set_state(c, CLOSED);
912                         errno = ECONNRESET;
913
914                         if(c->recv) {
915                                 c->recv(c, NULL, 0);
916                         }
917
918                         return 0;
919
920                 case CLOSING:
921                 case LAST_ACK:
922                 case TIME_WAIT:
923                         if(hdr.ctl & ACK) {
924                                 return 0;
925                         }
926
927                         // As far as the application is concerned, the connection has already been closed.
928                         // If it has called utcp_close() already, we can immediately free this connection.
929                         if(c->reapable) {
930                                 free_connection(c);
931                                 return 0;
932                         }
933
934                         // Otherwise, immediately move to the CLOSED state.
935                         set_state(c, CLOSED);
936                         return 0;
937
938                 default:
939 #ifdef UTCP_DEBUG
940                         abort();
941 #endif
942                         break;
943                 }
944         }
945
946         uint32_t advanced;
947
948         if(!(hdr.ctl & ACK)) {
949                 advanced = 0;
950                 goto skip_ack;
951         }
952
953         // 3. Advance snd.una
954
955         if(seqdiff(hdr.ack, c->snd.last) > 0 || seqdiff(hdr.ack, c->snd.una) < 0) {
956                 debug(c, "packet ack seqno out of range, %u <= %u < %u\n", c->snd.una, hdr.ack, c->snd.una + c->sndbuf.used);
957                 goto reset;
958         }
959
960         advanced = seqdiff(hdr.ack, c->snd.una);
961
962         if(advanced) {
963                 // RTT measurement
964                 if(c->rtt_start.tv_sec) {
965                         if(c->rtt_seq == hdr.ack) {
966                                 struct timespec now;
967                                 clock_gettime(UTCP_CLOCK, &now);
968                                 int32_t diff = timespec_diff_usec(&now, &c->rtt_start);
969                                 update_rtt(c, diff);
970                                 c->rtt_start.tv_sec = 0;
971                         } else if(c->rtt_seq < hdr.ack) {
972                                 debug(c, "cancelling RTT measurement: %u < %u\n", c->rtt_seq, hdr.ack);
973                                 c->rtt_start.tv_sec = 0;
974                         }
975                 }
976
977                 int32_t data_acked = advanced;
978
979                 switch(c->state) {
980                 case SYN_SENT:
981                 case SYN_RECEIVED:
982                         data_acked--;
983                         break;
984
985                 // TODO: handle FIN as well.
986                 default:
987                         break;
988                 }
989
990                 assert(data_acked >= 0);
991
992 #ifndef NDEBUG
993                 int32_t bufused = seqdiff(c->snd.last, c->snd.una);
994                 assert(data_acked <= bufused);
995 #endif
996
997                 // Also advance snd.nxt if possible
998                 if(seqdiff(c->snd.nxt, hdr.ack) < 0) {
999                         c->snd.nxt = hdr.ack;
1000                 }
1001
1002                 c->snd.una = hdr.ack;
1003
1004                 if(c->dupack) {
1005                         if(c->dupack >= 3) {
1006                                 debug(c, "fast recovery ended\n");
1007                                 c->snd.cwnd = c->snd.ssthresh;
1008                         }
1009
1010                         c->dupack = 0;
1011                 }
1012
1013                 // Increase the congestion window according to RFC 5681
1014                 if(c->snd.cwnd < c->snd.ssthresh) {
1015                         c->snd.cwnd += min(advanced, utcp->mss); // eq. 2
1016                 } else {
1017                         c->snd.cwnd += max(1, (utcp->mss * utcp->mss) / c->snd.cwnd); // eq. 3
1018                 }
1019
1020                 if(c->snd.cwnd > c->utcp->mtu) {
1021                         c->snd.cwnd = c->utcp->mtu;
1022                 }
1023
1024                 debug_cwnd(c);
1025
1026                 // Check if we have sent a FIN that is now ACKed.
1027                 switch(c->state) {
1028                 case FIN_WAIT_1:
1029                         if(c->snd.una == c->snd.last) {
1030                                 set_state(c, FIN_WAIT_2);
1031                         }
1032
1033                         break;
1034
1035                 case CLOSING:
1036                         if(c->snd.una == c->snd.last) {
1037                                 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1038                                 c->conn_timeout.tv_sec += utcp->timeout;
1039                                 set_state(c, TIME_WAIT);
1040                         }
1041
1042                         break;
1043
1044                 default:
1045                         break;
1046                 }
1047         }
1048
1049         // 4. Update timers
1050
1051         if(advanced) {
1052                 if(c->snd.una == c->snd.last) {
1053                         stop_retransmit_timer(c);
1054                         timespec_clear(&c->conn_timeout);
1055                 }
1056         }
1057
1058 skip_ack:
1059         // 5. Process SYN stuff
1060
1061         if(hdr.ctl & SYN) {
1062                 switch(c->state) {
1063                 case SYN_SENT:
1064
1065                         // This is a SYNACK. It should always have ACKed the SYN.
1066                         if(!advanced) {
1067                                 goto reset;
1068                         }
1069
1070                         c->rcv.irs = hdr.seq;
1071                         c->rcv.nxt = hdr.seq + 1;
1072
1073                         if(c->shut_wr) {
1074                                 c->snd.last++;
1075                                 set_state(c, FIN_WAIT_1);
1076                         } else {
1077                                 set_state(c, ESTABLISHED);
1078                         }
1079
1080                         break;
1081
1082                 case SYN_RECEIVED:
1083                         // This is a retransmit of a SYN, send back the SYNACK.
1084                         goto synack;
1085
1086                 case ESTABLISHED:
1087                 case FIN_WAIT_1:
1088                 case FIN_WAIT_2:
1089                 case CLOSE_WAIT:
1090                 case CLOSING:
1091                 case LAST_ACK:
1092                 case TIME_WAIT:
1093                         // This could be a retransmission. Ignore the SYN flag, but send an ACK back.
1094                         break;
1095
1096                 default:
1097 #ifdef UTCP_DEBUG
1098                         abort();
1099 #endif
1100                         return 0;
1101                 }
1102         }
1103
1104         // 6. Process new data
1105
1106         if(c->state == SYN_RECEIVED) {
1107                 // This is the ACK after the SYNACK. It should always have ACKed the SYNACK.
1108                 if(!advanced) {
1109                         goto reset;
1110                 }
1111
1112                 // Are we still LISTENing?
1113                 if(utcp->accept) {
1114                         utcp->accept(c, c->src);
1115                 }
1116
1117                 if(c->state != ESTABLISHED) {
1118                         set_state(c, CLOSED);
1119                         c->reapable = true;
1120                         goto reset;
1121                 }
1122         }
1123
1124         if(len) {
1125                 switch(c->state) {
1126                 case SYN_SENT:
1127                 case SYN_RECEIVED:
1128                         // This should never happen.
1129 #ifdef UTCP_DEBUG
1130                         abort();
1131 #endif
1132                         return 0;
1133
1134                 case ESTABLISHED:
1135                         break;
1136
1137                 case FIN_WAIT_1:
1138                 case FIN_WAIT_2:
1139                         if(c->reapable) {
1140                                 // We already closed the connection and are not interested in more data.
1141                                 goto reset;
1142                         }
1143
1144                         break;
1145
1146                 case CLOSE_WAIT:
1147                 case CLOSING:
1148                 case LAST_ACK:
1149                 case TIME_WAIT:
1150                         // Ehm no, We should never receive more data after a FIN.
1151                         goto reset;
1152
1153                 default:
1154 #ifdef UTCP_DEBUG
1155                         abort();
1156 #endif
1157                         return 0;
1158                 }
1159
1160                 handle_incoming_data(c, &hdr, ptr, len);
1161         }
1162
1163         // 7. Process FIN stuff
1164
1165         if(hdr.ctl & FIN) {
1166                 switch(c->state) {
1167                 case SYN_SENT:
1168                 case SYN_RECEIVED:
1169                         // This should never happen.
1170 #ifdef UTCP_DEBUG
1171                         abort();
1172 #endif
1173                         break;
1174
1175                 case ESTABLISHED:
1176                         set_state(c, CLOSE_WAIT);
1177                         break;
1178
1179                 case FIN_WAIT_1:
1180                         set_state(c, CLOSING);
1181                         break;
1182
1183                 case FIN_WAIT_2:
1184                         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1185                         c->conn_timeout.tv_sec += utcp->timeout;
1186                         set_state(c, TIME_WAIT);
1187                         break;
1188
1189                 case CLOSE_WAIT:
1190                 case CLOSING:
1191                 case LAST_ACK:
1192                 case TIME_WAIT:
1193                         // Ehm, no. We should never receive a second FIN.
1194                         goto reset;
1195
1196                 default:
1197 #ifdef UTCP_DEBUG
1198                         abort();
1199 #endif
1200                         break;
1201                 }
1202
1203                 // FIN counts as one sequence number
1204                 c->rcv.nxt++;
1205                 len++;
1206
1207                 // Inform the application that the peer closed its end of the connection.
1208                 if(c->recv) {
1209                         errno = 0;
1210                         c->recv(c, NULL, 0);
1211                 }
1212         }
1213
1214         // Now we send something back if:
1215         // - we received data, so we have to send back an ACK
1216         //   -> sendatleastone = true
1217         // - or we got an ack, so we should maybe send a bit more data
1218         //   -> sendatleastone = false
1219
1220         if(hdr.ctl & SYN || hdr.ctl & FIN) {
1221                 ack(c, NULL, 0);
1222         }
1223
1224         return 0;
1225
1226 reset:
1227         swap_ports(&hdr);
1228         hdr.wnd = 0;
1229         hdr.aux = 0;
1230
1231         if(hdr.ctl & ACK) {
1232                 hdr.seq = hdr.ack;
1233                 hdr.ctl = RST;
1234         } else {
1235                 hdr.ack = hdr.seq + len;
1236                 hdr.seq = 0;
1237                 hdr.ctl = RST | ACK;
1238         }
1239
1240         print_packet(c, "send", &hdr, sizeof(hdr));
1241         utcp->send(utcp, &hdr, sizeof(hdr));
1242         return 0;
1243
1244 }
1245
1246 int utcp_shutdown(struct utcp_connection *c, int dir) {
1247         debug(c, "shutdown %d at %u\n", dir, c ? c->snd.last : 0);
1248
1249         if(!c) {
1250                 errno = EFAULT;
1251                 return -1;
1252         }
1253
1254         if(c->reapable) {
1255                 debug(c, "shutdown() called on closed connection\n");
1256                 errno = EBADF;
1257                 return -1;
1258         }
1259
1260         if(!(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_WR || dir == UTCP_SHUT_RDWR)) {
1261                 errno = EINVAL;
1262                 return -1;
1263         }
1264
1265         // TCP does not have a provision for stopping incoming packets.
1266         // The best we can do is to just ignore them.
1267         if(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_RDWR) {
1268                 c->recv = NULL;
1269         }
1270
1271         // The rest of the code deals with shutting down writes.
1272         if(dir == UTCP_SHUT_RD) {
1273                 return 0;
1274         }
1275
1276         // Only process shutting down writes once.
1277         if(c->shut_wr) {
1278                 return 0;
1279         }
1280
1281         c->shut_wr = true;
1282
1283         switch(c->state) {
1284         case CLOSED:
1285         case LISTEN:
1286                 errno = ENOTCONN;
1287                 return -1;
1288
1289         case SYN_SENT:
1290                 return 0;
1291
1292         case SYN_RECEIVED:
1293         case ESTABLISHED:
1294                 set_state(c, FIN_WAIT_1);
1295                 break;
1296
1297         case FIN_WAIT_1:
1298         case FIN_WAIT_2:
1299                 return 0;
1300
1301         case CLOSE_WAIT:
1302                 set_state(c, CLOSING);
1303                 break;
1304
1305         case CLOSING:
1306         case LAST_ACK:
1307         case TIME_WAIT:
1308                 return 0;
1309         }
1310
1311         c->snd.last++;
1312
1313         ack(c, NULL, 0);
1314
1315         if(!timespec_isset(&c->rtrx_timeout)) {
1316                 start_retransmit_timer(c);
1317         }
1318
1319         return 0;
1320 }
1321
1322 static bool reset_connection(struct utcp_connection *c) {
1323         if(!c) {
1324                 errno = EFAULT;
1325                 return false;
1326         }
1327
1328         if(c->reapable) {
1329                 debug(c, "abort() called on closed connection\n");
1330                 errno = EBADF;
1331                 return false;
1332         }
1333
1334         switch(c->state) {
1335         case CLOSED:
1336                 return true;
1337
1338         case LISTEN:
1339         case SYN_SENT:
1340         case CLOSING:
1341         case LAST_ACK:
1342         case TIME_WAIT:
1343                 set_state(c, CLOSED);
1344                 return true;
1345
1346         case SYN_RECEIVED:
1347         case ESTABLISHED:
1348         case FIN_WAIT_1:
1349         case FIN_WAIT_2:
1350         case CLOSE_WAIT:
1351                 set_state(c, CLOSED);
1352                 break;
1353         }
1354
1355         // Send RST
1356
1357         struct hdr hdr;
1358
1359         hdr.src = c->src;
1360         hdr.dst = c->dst;
1361         hdr.seq = c->snd.nxt;
1362         hdr.ack = c->rcv.nxt;
1363         hdr.wnd = 0;
1364         hdr.ctl = RST;
1365         hdr.aux = 0;
1366
1367         print_packet(c, "send", &hdr, sizeof(hdr));
1368         c->utcp->send(c->utcp, &hdr, sizeof(hdr));
1369         return true;
1370 }
1371
1372 static void set_reapable(struct utcp_connection *c) {
1373         c->recv = NULL;
1374         c->reapable = true;
1375 }
1376
1377 // Resets all connections, but does not invalidate connection handles
1378 void utcp_reset_all_connections(struct utcp *utcp) {
1379         if(!utcp) {
1380                 errno = EINVAL;
1381                 return;
1382         }
1383
1384         for(int i = 0; i < utcp->nconnections; i++) {
1385                 struct utcp_connection *c = utcp->connections[i];
1386
1387                 if(c->reapable || c->state == CLOSED) {
1388                         continue;
1389                 }
1390
1391                 reset_connection(c);
1392
1393                 if(c->recv) {
1394                         errno = 0;
1395                         c->recv(c, NULL, 0);
1396                 }
1397         }
1398
1399         return;
1400 }
1401
1402 int utcp_close(struct utcp_connection *c) {
1403         if(utcp_shutdown(c, SHUT_RDWR) && errno != ENOTCONN) {
1404                 return -1;
1405         }
1406
1407         set_reapable(c);
1408         return 0;
1409 }
1410
1411 int utcp_abort(struct utcp_connection *c) {
1412         if(!reset_connection(c)) {
1413                 return -1;
1414         }
1415
1416         set_reapable(c);
1417         return 0;
1418 }
1419
1420 /* Handle timeouts.
1421  * One call to this function will loop through all connections,
1422  * checking if something needs to be resent or not.
1423  * The return value is the time to the next timeout in milliseconds,
1424  * or maybe a negative value if the timeout is infinite.
1425  */
1426 struct timespec utcp_timeout(struct utcp *utcp) {
1427         struct timespec now;
1428         clock_gettime(UTCP_CLOCK, &now);
1429         struct timespec next = {now.tv_sec + 3600, now.tv_nsec};
1430
1431         for(int i = 0; i < utcp->nconnections; i++) {
1432                 struct utcp_connection *c = utcp->connections[i];
1433
1434                 if(!c) {
1435                         continue;
1436                 }
1437
1438                 // delete connections that have been utcp_close()d.
1439                 if(c->state == CLOSED) {
1440                         if(c->reapable) {
1441                                 debug(c, "reaping\n");
1442                                 free_connection(c);
1443                                 i--;
1444                         }
1445
1446                         continue;
1447                 }
1448
1449                 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &now)) {
1450                         errno = ETIMEDOUT;
1451                         c->state = CLOSED;
1452
1453                         if(c->recv) {
1454                                 c->recv(c, NULL, 0);
1455                         }
1456
1457                         continue;
1458                 }
1459
1460                 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &now)) {
1461                         debug(c, "retransmitting after timeout\n");
1462                         retransmit(c);
1463                 }
1464
1465                 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &next)) {
1466                         next = c->conn_timeout;
1467                 }
1468
1469                 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &next)) {
1470                         next = c->rtrx_timeout;
1471                 }
1472         }
1473
1474         struct timespec diff;
1475
1476         timespec_sub(&next, &now, &diff);
1477
1478         return diff;
1479 }
1480
1481 bool utcp_is_active(struct utcp *utcp) {
1482         if(!utcp) {
1483                 return false;
1484         }
1485
1486         for(int i = 0; i < utcp->nconnections; i++)
1487                 if(utcp->connections[i]->state != CLOSED && utcp->connections[i]->state != TIME_WAIT) {
1488                         return true;
1489                 }
1490
1491         return false;
1492 }
1493
1494 struct utcp *utcp_init(utcp_accept_t accept, utcp_listen_t listen, utcp_send_t send, void *priv) {
1495         if(!send) {
1496                 errno = EFAULT;
1497                 return NULL;
1498         }
1499
1500         struct utcp *utcp = calloc(1, sizeof(*utcp));
1501
1502         if(!utcp) {
1503                 return NULL;
1504         }
1505
1506         utcp_set_mtu(utcp, DEFAULT_MTU);
1507
1508         if(!utcp->pkt) {
1509                 free(utcp);
1510                 return NULL;
1511         }
1512
1513         if(!CLOCK_GRANULARITY) {
1514                 struct timespec res;
1515                 clock_getres(UTCP_CLOCK, &res);
1516                 CLOCK_GRANULARITY = res.tv_sec * USEC_PER_SEC + res.tv_nsec / 1000;
1517         }
1518
1519         utcp->accept = accept;
1520         utcp->listen = listen;
1521         utcp->send = send;
1522         utcp->priv = priv;
1523         utcp->timeout = DEFAULT_USER_TIMEOUT; // sec
1524
1525         return utcp;
1526 }
1527
1528 void utcp_exit(struct utcp *utcp) {
1529         if(!utcp) {
1530                 return;
1531         }
1532
1533         for(int i = 0; i < utcp->nconnections; i++) {
1534                 struct utcp_connection *c = utcp->connections[i];
1535
1536                 if(!c->reapable) {
1537                         if(c->recv) {
1538                                 c->recv(c, NULL, 0);
1539                         }
1540                 }
1541
1542                 free(c);
1543         }
1544
1545         free(utcp->connections);
1546         free(utcp->pkt);
1547         free(utcp);
1548 }
1549
1550 uint16_t utcp_get_mtu(struct utcp *utcp) {
1551         return utcp ? utcp->mtu : 0;
1552 }
1553
1554 uint16_t utcp_get_mss(struct utcp *utcp) {
1555         return utcp ? utcp->mss : 0;
1556 }
1557
1558 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
1559         if(!utcp) {
1560                 return;
1561         }
1562
1563         if(mtu <= sizeof(struct hdr)) {
1564                 return;
1565         }
1566
1567         if(mtu > utcp->mtu) {
1568                 char *new = realloc(utcp->pkt, mtu + sizeof(struct hdr));
1569
1570                 if(!new) {
1571                         return;
1572                 }
1573
1574                 utcp->pkt = new;
1575         }
1576
1577         utcp->mtu = mtu;
1578         utcp->mss = mtu - sizeof(struct hdr);
1579 }
1580
1581 void utcp_reset_timers(struct utcp *utcp) {
1582         if(!utcp) {
1583                 return;
1584         }
1585
1586         struct timespec now, then;
1587
1588         clock_gettime(UTCP_CLOCK, &now);
1589
1590         then = now;
1591
1592         then.tv_sec += utcp->timeout;
1593
1594         for(int i = 0; i < utcp->nconnections; i++) {
1595                 struct utcp_connection *c = utcp->connections[i];
1596
1597                 if(c->reapable) {
1598                         continue;
1599                 }
1600
1601                 if(timespec_isset(&c->rtrx_timeout)) {
1602                         c->rtrx_timeout = now;
1603                 }
1604
1605                 if(timespec_isset(&c->conn_timeout)) {
1606                         c->conn_timeout = then;
1607                 }
1608
1609                 c->rtt_start.tv_sec = 0;
1610
1611                 if(c->rto > START_RTO) {
1612                         c->rto = START_RTO;
1613                 }
1614         }
1615 }
1616
1617 int utcp_get_user_timeout(struct utcp *u) {
1618         return u ? u->timeout : 0;
1619 }
1620
1621 void utcp_set_user_timeout(struct utcp *u, int timeout) {
1622         if(u) {
1623                 u->timeout = timeout;
1624         }
1625 }
1626
1627 bool utcp_get_nodelay(struct utcp_connection *c) {
1628         return c ? c->nodelay : false;
1629 }
1630
1631 void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) {
1632         if(c) {
1633                 c->nodelay = nodelay;
1634         }
1635 }
1636
1637 bool utcp_get_keepalive(struct utcp_connection *c) {
1638         return c ? c->keepalive : false;
1639 }
1640
1641 void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
1642         if(c) {
1643                 c->keepalive = keepalive;
1644         }
1645 }
1646
1647 void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) {
1648         if(c) {
1649                 c->recv = recv;
1650         }
1651 }
1652
1653 void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_listen_t listen) {
1654         if(utcp) {
1655                 utcp->accept = accept;
1656                 utcp->listen = listen;
1657         }
1658 }
1659
1660 void utcp_expect_data(struct utcp_connection *c, bool expect) {
1661         if(!c || c->reapable) {
1662                 return;
1663         }
1664
1665         if(!(c->state == ESTABLISHED || c->state == FIN_WAIT_1 || c->state == FIN_WAIT_2)) {
1666                 return;
1667         }
1668
1669         if(expect) {
1670                 // If we expect data, start the connection timer.
1671                 if(!timespec_isset(&c->conn_timeout)) {
1672                         clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1673                         c->conn_timeout.tv_sec += c->utcp->timeout;
1674                 }
1675         } else {
1676                 // If we want to cancel expecting data, only clear the timer when there is no unACKed data.
1677                 if(c->snd.una == c->snd.last) {
1678                         timespec_clear(&c->conn_timeout);
1679                 }
1680         }
1681 }
1682
1683 void utcp_set_flags(struct utcp_connection *c, uint32_t flags) {
1684         c->flags &= ~UTCP_CHANGEABLE_FLAGS;
1685         c->flags |= flags & UTCP_CHANGEABLE_FLAGS;
1686 }
1687
1688 void utcp_offline(struct utcp *utcp, bool offline) {
1689         struct timespec now;
1690         clock_gettime(UTCP_CLOCK, &now);
1691
1692         for(int i = 0; i < utcp->nconnections; i++) {
1693                 struct utcp_connection *c = utcp->connections[i];
1694
1695                 if(c->reapable) {
1696                         continue;
1697                 }
1698
1699                 utcp_expect_data(c, offline);
1700
1701                 if(!offline) {
1702                         if(timespec_isset(&c->rtrx_timeout)) {
1703                                 c->rtrx_timeout = now;
1704                         }
1705
1706                         utcp->connections[i]->rtt_start.tv_sec = 0;
1707
1708                         if(c->rto > START_RTO) {
1709                                 c->rto = START_RTO;
1710                         }
1711                 }
1712         }
1713 }
1714
1715 void utcp_set_clock_granularity(long granularity) {
1716         CLOCK_GRANULARITY = granularity;
1717 }