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