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