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