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