2 utcp.c -- Userspace TCP
3 Copyright (C) 2014-2017 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
32 #include "utcp_priv.h"
47 #if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
48 #define UTCP_CLOCK CLOCK_MONOTONIC_RAW
50 #define UTCP_CLOCK CLOCK_MONOTONIC
54 static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
55 r->tv_sec = a->tv_sec - b->tv_sec;
56 r->tv_nsec = a->tv_nsec - b->tv_nsec;
59 r->tv_sec--, r->tv_nsec += NSEC_PER_SEC;
63 static int32_t timespec_diff_usec(const struct timespec *a, const struct timespec *b) {
64 return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_nsec - b->tv_nsec) / 1000;
67 static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
68 if(a->tv_sec == b->tv_sec) {
69 return a->tv_nsec < b->tv_nsec;
71 return a->tv_sec < b->tv_sec;
75 static void timespec_clear(struct timespec *a) {
79 static bool timespec_isset(const struct timespec *a) {
83 static long CLOCK_GRANULARITY; // usec
85 static inline size_t min(size_t a, size_t b) {
89 static inline size_t max(size_t a, size_t b) {
96 #ifndef UTCP_DEBUG_DATALEN
97 #define UTCP_DEBUG_DATALEN 20
100 static void debug(struct utcp_connection *c, const char *format, ...) {
105 clock_gettime(CLOCK_REALTIME, &tv);
106 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);
108 va_start(ap, format);
109 len += vsnprintf(buf + len, sizeof(buf) - len, format, ap);
112 if(len > 0 && (size_t)len < sizeof(buf)) {
113 fwrite(buf, len, 1, stderr);
117 static void print_packet(struct utcp_connection *c, const char *dir, const void *pkt, size_t len) {
120 if(len < sizeof(hdr)) {
121 debug(c, "%s: short packet (%lu bytes)\n", dir, (unsigned long)len);
125 memcpy(&hdr, pkt, sizeof(hdr));
129 if(len > sizeof(hdr)) {
130 datalen = min(len - sizeof(hdr), UTCP_DEBUG_DATALEN);
136 const uint8_t *data = (uint8_t *)pkt + sizeof(hdr);
137 char str[datalen * 2 + 1];
140 for(uint32_t i = 0; i < datalen; i++) {
141 *p++ = "0123456789ABCDEF"[data[i] >> 4];
142 *p++ = "0123456789ABCDEF"[data[i] & 15];
147 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",
148 dir, (unsigned long)len, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd, hdr.aux,
149 hdr.ctl & SYN ? "SYN" : "",
150 hdr.ctl & RST ? "RST" : "",
151 hdr.ctl & FIN ? "FIN" : "",
152 hdr.ctl & ACK ? "ACK" : "",
153 hdr.ctl & MF ? "MF" : "",
158 static void debug_cwnd(struct utcp_connection *c) {
159 debug(c, "snd.cwnd %u snd.ssthresh %u\n", c->snd.cwnd, ~c->snd.ssthresh ? c->snd.ssthresh : 0);
162 #define debug(...) do {} while(0)
163 #define print_packet(...) do {} while(0)
164 #define debug_cwnd(...) do {} while(0)
167 static void set_state(struct utcp_connection *c, enum state state) {
170 if(state == ESTABLISHED) {
171 timespec_clear(&c->conn_timeout);
174 debug(c, "state %s\n", strstate[state]);
177 static bool fin_wanted(struct utcp_connection *c, uint32_t seq) {
178 if(seq != c->snd.last) {
193 static bool is_reliable(struct utcp_connection *c) {
194 return c->flags & UTCP_RELIABLE;
197 static int32_t seqdiff(uint32_t a, uint32_t b) {
202 static bool buffer_wraps(struct buffer *buf) {
203 return buf->size - buf->offset < buf->used;
206 static bool buffer_resize(struct buffer *buf, uint32_t newsize) {
207 char *newdata = realloc(buf->data, newsize);
215 if(buffer_wraps(buf)) {
216 // Shift the right part of the buffer until it hits the end of the new buffer.
220 // [345.........|........012]
221 uint32_t tailsize = buf->size - buf->offset;
222 uint32_t newoffset = newsize - tailsize;
223 memmove(buf->data + newoffset, buf->data + buf->offset, tailsize);
224 buf->offset = newoffset;
231 // Store data into the buffer
232 static ssize_t buffer_put_at(struct buffer *buf, size_t offset, const void *data, size_t len) {
233 debug(NULL, "buffer_put_at %lu %lu %lu\n", (unsigned long)buf->used, (unsigned long)offset, (unsigned long)len);
235 // Ensure we don't store more than maxsize bytes in total
236 size_t required = offset + len;
238 if(required > buf->maxsize) {
239 if(offset >= buf->maxsize) {
243 len = buf->maxsize - offset;
244 required = buf->maxsize;
247 // Check if we need to resize the buffer
248 if(required > buf->size) {
249 size_t newsize = buf->size;
257 } while(newsize < required);
259 if(newsize > buf->maxsize) {
260 newsize = buf->maxsize;
263 if(!buffer_resize(buf, newsize)) {
268 uint32_t realoffset = buf->offset + offset;
270 if(buf->size - buf->offset < offset) {
271 // The offset wrapped
272 realoffset -= buf->size;
275 if(buf->size - realoffset < len) {
276 // The new chunk of data must be wrapped
277 memcpy(buf->data + realoffset, data, buf->size - realoffset);
278 memcpy(buf->data, (char *)data + buf->size - realoffset, len - (buf->size - realoffset));
280 memcpy(buf->data + realoffset, data, len);
283 if(required > buf->used) {
284 buf->used = required;
290 static ssize_t buffer_put(struct buffer *buf, const void *data, size_t len) {
291 return buffer_put_at(buf, buf->used, data, len);
294 // Copy data from the buffer without removing it.
295 static ssize_t buffer_copy(struct buffer *buf, void *data, size_t offset, size_t len) {
296 // Ensure we don't copy more than is actually stored in the buffer
297 if(offset >= buf->used) {
301 if(buf->used - offset < len) {
302 len = buf->used - offset;
305 uint32_t realoffset = buf->offset + offset;
307 if(buf->size - buf->offset < offset) {
308 // The offset wrapped
309 realoffset -= buf->size;
312 if(buf->size - realoffset < len) {
313 // The data is wrapped
314 memcpy(data, buf->data + realoffset, buf->size - realoffset);
315 memcpy((char *)data + buf->size - realoffset, buf->data, len - (buf->size - realoffset));
317 memcpy(data, buf->data + realoffset, len);
323 // Copy data from the buffer without removing it.
324 static ssize_t buffer_call(struct buffer *buf, utcp_recv_t cb, void *arg, size_t offset, size_t len) {
325 // Ensure we don't copy more than is actually stored in the buffer
326 if(offset >= buf->used) {
330 if(buf->used - offset < len) {
331 len = buf->used - offset;
334 uint32_t realoffset = buf->offset + offset;
336 if(buf->size - buf->offset < offset) {
337 // The offset wrapped
338 realoffset -= buf->size;
341 if(buf->size - realoffset < len) {
342 // The data is wrapped
343 ssize_t rx1 = cb(arg, buf->data + realoffset, buf->size - realoffset);
345 if(rx1 < buf->size - realoffset) {
349 ssize_t rx2 = cb(arg, buf->data, len - (buf->size - realoffset));
357 return cb(arg, buf->data + realoffset, len);
361 // Discard data from the buffer.
362 static ssize_t buffer_discard(struct buffer *buf, size_t len) {
363 if(buf->used < len) {
367 if(buf->size - buf->offset < len) {
368 buf->offset -= buf->size;
371 if(buf->used == len) {
382 static void buffer_clear(struct buffer *buf) {
387 static bool buffer_set_size(struct buffer *buf, uint32_t minsize, uint32_t maxsize) {
388 if(maxsize < minsize) {
392 buf->maxsize = maxsize;
394 return buf->size >= minsize || buffer_resize(buf, minsize);
397 static void buffer_exit(struct buffer *buf) {
399 memset(buf, 0, sizeof(*buf));
402 static uint32_t buffer_free(const struct buffer *buf) {
403 return buf->maxsize - buf->used;
406 // Connections are stored in a sorted list.
407 // This gives O(log(N)) lookup time, O(N log(N)) insertion time and O(N) deletion time.
409 static int compare(const void *va, const void *vb) {
412 const struct utcp_connection *a = *(struct utcp_connection **)va;
413 const struct utcp_connection *b = *(struct utcp_connection **)vb;
416 assert(a->src && b->src);
418 int c = (int)a->src - (int)b->src;
424 c = (int)a->dst - (int)b->dst;
428 static struct utcp_connection *find_connection(const struct utcp *utcp, uint16_t src, uint16_t dst) {
429 if(!utcp->nconnections) {
433 struct utcp_connection key = {
437 struct utcp_connection **match = bsearch(&keyp, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
438 return match ? *match : NULL;
441 static void free_connection(struct utcp_connection *c) {
442 struct utcp *utcp = c->utcp;
443 struct utcp_connection **cp = bsearch(&c, utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
447 int i = cp - utcp->connections;
448 memmove(cp, cp + 1, (utcp->nconnections - i - 1) * sizeof(*cp));
449 utcp->nconnections--;
451 buffer_exit(&c->rcvbuf);
452 buffer_exit(&c->sndbuf);
456 static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t src, uint16_t dst) {
457 // Check whether this combination of src and dst is free
460 if(find_connection(utcp, src, dst)) {
464 } else { // If src == 0, generate a random port number with the high bit set
465 if(utcp->nconnections >= 32767) {
470 src = rand() | 0x8000;
472 while(find_connection(utcp, src, dst)) {
477 // Allocate memory for the new connection
479 if(utcp->nconnections >= utcp->nallocated) {
480 if(!utcp->nallocated) {
481 utcp->nallocated = 4;
483 utcp->nallocated *= 2;
486 struct utcp_connection **new_array = realloc(utcp->connections, utcp->nallocated * sizeof(*utcp->connections));
492 utcp->connections = new_array;
495 struct utcp_connection *c = calloc(1, sizeof(*c));
501 if(!buffer_set_size(&c->sndbuf, DEFAULT_SNDBUFSIZE, DEFAULT_MAXSNDBUFSIZE)) {
506 if(!buffer_set_size(&c->rcvbuf, DEFAULT_RCVBUFSIZE, DEFAULT_MAXRCVBUFSIZE)) {
507 buffer_exit(&c->sndbuf);
512 // Fill in the details
521 c->snd.una = c->snd.iss;
522 c->snd.nxt = c->snd.iss + 1;
523 c->snd.last = c->snd.nxt;
524 c->snd.cwnd = (utcp->mss > 2190 ? 2 : utcp->mss > 1095 ? 3 : 4) * utcp->mss;
525 c->snd.ssthresh = ~0;
532 // Add it to the sorted list of connections
534 utcp->connections[utcp->nconnections++] = c;
535 qsort(utcp->connections, utcp->nconnections, sizeof(*utcp->connections), compare);
540 static inline uint32_t absdiff(uint32_t a, uint32_t b) {
548 // Update RTT variables. See RFC 6298.
549 static void update_rtt(struct utcp_connection *c, uint32_t rtt) {
551 debug(c, "invalid rtt\n");
559 c->rttvar = (c->rttvar * 3 + absdiff(c->srtt, rtt)) / 4;
560 c->srtt = (c->srtt * 7 + rtt) / 8;
563 c->rto = c->srtt + max(4 * c->rttvar, CLOCK_GRANULARITY);
565 if(c->rto > MAX_RTO) {
569 debug(c, "rtt %u srtt %u rttvar %u rto %u\n", rtt, c->srtt, c->rttvar, c->rto);
572 static void start_retransmit_timer(struct utcp_connection *c) {
573 clock_gettime(UTCP_CLOCK, &c->rtrx_timeout);
575 uint32_t rto = c->rto;
577 while(rto > USEC_PER_SEC) {
578 c->rtrx_timeout.tv_sec++;
582 c->rtrx_timeout.tv_nsec += c->rto * 1000;
584 if(c->rtrx_timeout.tv_nsec >= NSEC_PER_SEC) {
585 c->rtrx_timeout.tv_nsec -= NSEC_PER_SEC;
586 c->rtrx_timeout.tv_sec++;
589 debug(c, "rtrx_timeout %ld.%06lu\n", c->rtrx_timeout.tv_sec, c->rtrx_timeout.tv_nsec);
592 static void stop_retransmit_timer(struct utcp_connection *c) {
593 timespec_clear(&c->rtrx_timeout);
594 debug(c, "rtrx_timeout cleared\n");
597 struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv, uint32_t flags) {
598 struct utcp_connection *c = allocate_connection(utcp, 0, dst);
604 assert((flags & ~0x1f) == 0);
615 pkt.hdr.src = c->src;
616 pkt.hdr.dst = c->dst;
617 pkt.hdr.seq = c->snd.iss;
619 pkt.hdr.wnd = c->rcvbuf.maxsize;
621 pkt.hdr.aux = 0x0101;
625 pkt.init[3] = flags & 0x7;
627 set_state(c, SYN_SENT);
629 print_packet(c, "send", &pkt, sizeof(pkt));
630 utcp->send(utcp, &pkt, sizeof(pkt));
632 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
633 c->conn_timeout.tv_sec += utcp->timeout;
635 start_retransmit_timer(c);
640 struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
641 return utcp_connect_ex(utcp, dst, recv, priv, UTCP_TCP);
644 void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
645 if(c->reapable || c->state != SYN_RECEIVED) {
646 debug(c, "accept() called on invalid connection in state %s\n", c, strstate[c->state]);
650 debug(c, "accepted %p %p\n", c, recv, priv);
653 set_state(c, ESTABLISHED);
656 static void ack(struct utcp_connection *c, bool sendatleastone) {
657 int32_t left = seqdiff(c->snd.last, c->snd.nxt);
658 int32_t cwndleft = is_reliable(c) ? min(c->snd.cwnd, c->snd.wnd) - seqdiff(c->snd.nxt, c->snd.una) : MAX_UNRELIABLE_SIZE;
664 } else if(cwndleft < left) {
667 if(!sendatleastone || cwndleft > c->utcp->mss) {
668 left -= left % c->utcp->mss;
672 debug(c, "cwndleft %d left %d\n", cwndleft, left);
674 if(!left && !sendatleastone) {
681 } *pkt = c->utcp->pkt;
683 pkt->hdr.src = c->src;
684 pkt->hdr.dst = c->dst;
685 pkt->hdr.ack = c->rcv.nxt;
686 pkt->hdr.wnd = is_reliable(c) ? c->rcvbuf.maxsize : 0;
691 uint32_t seglen = left > c->utcp->mss ? c->utcp->mss : left;
692 pkt->hdr.seq = c->snd.nxt;
694 buffer_copy(&c->sndbuf, pkt->data, seqdiff(c->snd.nxt, c->snd.una), seglen);
696 c->snd.nxt += seglen;
699 if(!is_reliable(c)) {
707 if(seglen && fin_wanted(c, c->snd.nxt)) {
712 if(!c->rtt_start.tv_sec) {
713 // Start RTT measurement
714 clock_gettime(UTCP_CLOCK, &c->rtt_start);
715 c->rtt_seq = pkt->hdr.seq + seglen;
716 debug(c, "starting RTT measurement, expecting ack %u\n", c->rtt_seq);
719 print_packet(c, "send", pkt, sizeof(pkt->hdr) + seglen);
720 c->utcp->send(c->utcp, pkt, sizeof(pkt->hdr) + seglen);
722 if(left && !is_reliable(c)) {
723 pkt->hdr.wnd += seglen;
728 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
730 debug(c, "send() called on closed connection\n");
738 debug(c, "send() called on unconnected connection\n");
753 debug(c, "send() called on closed connection\n");
758 // Exit early if we have nothing to send.
769 // Check if we need to be able to buffer all data
771 if(c->flags & UTCP_NO_PARTIAL) {
772 if(len > buffer_free(&c->sndbuf)) {
773 if(len > c->sndbuf.maxsize) {
783 // Add data to send buffer.
786 len = buffer_put(&c->sndbuf, data, len);
787 } else if(c->state != SYN_SENT && c->state != SYN_RECEIVED) {
788 if(len > MAX_UNRELIABLE_SIZE || buffer_put(&c->sndbuf, data, len) != (ssize_t)len) {
807 // Don't send anything yet if the connection has not fully established yet
809 if(c->state == SYN_SENT || c->state == SYN_RECEIVED) {
815 if(!is_reliable(c)) {
816 c->snd.una = c->snd.nxt = c->snd.last;
817 buffer_discard(&c->sndbuf, c->sndbuf.used);
821 if(is_reliable(c) && !timespec_isset(&c->rtrx_timeout)) {
822 start_retransmit_timer(c);
825 if(is_reliable(c) && !timespec_isset(&c->conn_timeout)) {
826 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
827 c->conn_timeout.tv_sec += c->utcp->timeout;
833 static void swap_ports(struct hdr *hdr) {
834 uint16_t tmp = hdr->src;
839 static void fast_retransmit(struct utcp_connection *c) {
840 if(c->state == CLOSED || c->snd.last == c->snd.una) {
841 debug(c, "fast_retransmit() called but nothing to retransmit!\n");
845 struct utcp *utcp = c->utcp;
852 pkt = malloc(c->utcp->mtu);
858 pkt->hdr.src = c->src;
859 pkt->hdr.dst = c->dst;
860 pkt->hdr.wnd = c->rcvbuf.maxsize;
869 // Send unacked data again.
870 pkt->hdr.seq = c->snd.una;
871 pkt->hdr.ack = c->rcv.nxt;
873 uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
875 if(fin_wanted(c, c->snd.una + len)) {
880 buffer_copy(&c->sndbuf, pkt->data, 0, len);
881 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
882 utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
892 static void retransmit(struct utcp_connection *c) {
893 if(c->state == CLOSED || c->snd.last == c->snd.una) {
894 debug(c, "retransmit() called but nothing to retransmit!\n");
895 stop_retransmit_timer(c);
899 struct utcp *utcp = c->utcp;
904 } *pkt = c->utcp->pkt;
906 pkt->hdr.src = c->src;
907 pkt->hdr.dst = c->dst;
908 pkt->hdr.wnd = c->rcvbuf.maxsize;
913 // Send our SYN again
914 pkt->hdr.seq = c->snd.iss;
917 pkt->hdr.aux = 0x0101;
921 pkt->data[3] = c->flags & 0x7;
922 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + 4);
923 utcp->send(utcp, pkt, sizeof(pkt->hdr) + 4);
928 pkt->hdr.seq = c->snd.nxt;
929 pkt->hdr.ack = c->rcv.nxt;
930 pkt->hdr.ctl = SYN | ACK;
931 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr));
932 utcp->send(utcp, pkt, sizeof(pkt->hdr));
940 // Send unacked data again.
941 pkt->hdr.seq = c->snd.una;
942 pkt->hdr.ack = c->rcv.nxt;
944 uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
946 if(fin_wanted(c, c->snd.una + len)) {
951 // RFC 5681 slow start after timeout
952 uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
953 c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
954 c->snd.cwnd = utcp->mss;
957 buffer_copy(&c->sndbuf, pkt->data, 0, len);
958 print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
959 utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
961 c->snd.nxt = c->snd.una + len;
968 // We shouldn't need to retransmit anything in this state.
972 stop_retransmit_timer(c);
976 start_retransmit_timer(c);
979 if(c->rto > MAX_RTO) {
983 c->rtt_start.tv_sec = 0; // invalidate RTT timer
984 c->dupack = 0; // cancel any ongoing fast recovery
990 /* Update receive buffer and SACK entries after consuming data.
994 * |.....0000..1111111111.....22222......3333|
997 * 0..3 represent the SACK entries. The ^ indicates up to which point we want
998 * to remove data from the receive buffer. The idea is to substract "len"
999 * from the offset of all the SACK entries, and then remove/cut down entries
1000 * that are shifted to before the start of the receive buffer.
1002 * There are three cases:
1003 * - the SACK entry is after ^, in that case just change the offset.
1004 * - the SACK entry starts before and ends after ^, so we have to
1005 * change both its offset and size.
1006 * - the SACK entry is completely before ^, in that case delete it.
1008 static void sack_consume(struct utcp_connection *c, size_t len) {
1009 debug(c, "sack_consume %lu\n", (unsigned long)len);
1011 if(len > c->rcvbuf.used) {
1012 debug(c, "all SACK entries consumed\n");
1013 c->sacks[0].len = 0;
1017 buffer_discard(&c->rcvbuf, len);
1019 for(int i = 0; i < NSACKS && c->sacks[i].len;) {
1020 if(len < c->sacks[i].offset) {
1021 c->sacks[i].offset -= len;
1023 } else if(len < c->sacks[i].offset + c->sacks[i].len) {
1024 c->sacks[i].len -= len - c->sacks[i].offset;
1025 c->sacks[i].offset = 0;
1028 if(i < NSACKS - 1) {
1029 memmove(&c->sacks[i], &c->sacks[i + 1], (NSACKS - 1 - i) * sizeof(c->sacks)[i]);
1030 c->sacks[NSACKS - 1].len = 0;
1032 c->sacks[i].len = 0;
1038 for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
1039 debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
1043 static void handle_out_of_order(struct utcp_connection *c, uint32_t offset, const void *data, size_t len) {
1044 debug(c, "out of order packet, offset %u\n", offset);
1045 // Packet loss or reordering occured. Store the data in the buffer.
1046 ssize_t rxd = buffer_put_at(&c->rcvbuf, offset, data, len);
1048 if(rxd < 0 || (size_t)rxd < len) {
1052 // Make note of where we put it.
1053 for(int i = 0; i < NSACKS; i++) {
1054 if(!c->sacks[i].len) { // nothing to merge, add new entry
1055 debug(c, "new SACK entry %d\n", i);
1056 c->sacks[i].offset = offset;
1057 c->sacks[i].len = rxd;
1059 } else if(offset < c->sacks[i].offset) {
1060 if(offset + rxd < c->sacks[i].offset) { // insert before
1061 if(!c->sacks[NSACKS - 1].len) { // only if room left
1062 debug(c, "insert SACK entry at %d\n", i);
1063 memmove(&c->sacks[i + 1], &c->sacks[i], (NSACKS - i - 1) * sizeof(c->sacks)[i]);
1064 c->sacks[i].offset = offset;
1065 c->sacks[i].len = rxd;
1067 debug(c, "SACK entries full, dropping packet\n");
1072 debug(c, "merge with start of SACK entry at %d\n", i);
1073 c->sacks[i].offset = offset;
1076 } else if(offset <= c->sacks[i].offset + c->sacks[i].len) {
1077 if(offset + rxd > c->sacks[i].offset + c->sacks[i].len) { // merge
1078 debug(c, "merge with end of SACK entry at %d\n", i);
1079 c->sacks[i].len = offset + rxd - c->sacks[i].offset;
1080 // TODO: handle potential merge with next entry
1087 for(int i = 0; i < NSACKS && c->sacks[i].len; i++) {
1088 debug(c, "SACK[%d] offset %u len %u\n", i, c->sacks[i].offset, c->sacks[i].len);
1092 static void handle_in_order(struct utcp_connection *c, const void *data, size_t len) {
1094 ssize_t rxd = c->recv(c, data, len);
1096 if(rxd != (ssize_t)len) {
1097 // TODO: handle the application not accepting all data.
1102 // Check if we can process out-of-order data now.
1103 if(c->sacks[0].len && len >= c->sacks[0].offset) {
1104 debug(c, "incoming packet len %lu connected with SACK at %u\n", (unsigned long)len, c->sacks[0].offset);
1106 if(len < c->sacks[0].offset + c->sacks[0].len) {
1107 size_t offset = len;
1108 len = c->sacks[0].offset + c->sacks[0].len;
1109 size_t remainder = len - offset;
1110 ssize_t rxd = buffer_call(&c->rcvbuf, c->recv, c, offset, remainder);
1112 if(rxd != (ssize_t)remainder) {
1113 // TODO: handle the application not accepting all data.
1119 if(c->rcvbuf.used) {
1120 sack_consume(c, len);
1126 static void handle_unreliable(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
1127 // Fast path for unfragmented packets
1128 if(!hdr->wnd && !(hdr->ctl & MF)) {
1129 c->recv(c, data, len);
1130 c->rcv.nxt = hdr->seq + len;
1134 // Ensure reassembled packet are not larger than 64 kiB
1135 if(hdr->wnd >= MAX_UNRELIABLE_SIZE || hdr->wnd + len > MAX_UNRELIABLE_SIZE) {
1139 // Don't accept out of order fragments
1140 if(hdr->wnd && hdr->seq != c->rcv.nxt) {
1144 // Reset the receive buffer for the first fragment
1146 buffer_clear(&c->rcvbuf);
1149 ssize_t rxd = buffer_put_at(&c->rcvbuf, hdr->wnd, data, len);
1151 if(rxd != (ssize_t)len) {
1155 // Send the packet if it's the final fragment
1156 if(!(hdr->ctl & MF)) {
1157 buffer_call(&c->rcvbuf, c->recv, c, 0, hdr->wnd + len);
1160 c->rcv.nxt = hdr->seq + len;
1163 static void handle_incoming_data(struct utcp_connection *c, const struct hdr *hdr, const void *data, size_t len) {
1164 if(!is_reliable(c)) {
1165 handle_unreliable(c, hdr, data, len);
1169 uint32_t offset = seqdiff(hdr->seq, c->rcv.nxt);
1171 if(offset + len > c->rcvbuf.maxsize) {
1176 handle_out_of_order(c, offset, data, len);
1178 handle_in_order(c, data, len);
1183 ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
1184 const uint8_t *ptr = data;
1200 // Drop packets smaller than the header
1204 if(len < sizeof(hdr)) {
1205 print_packet(NULL, "recv", data, len);
1210 // Make a copy from the potentially unaligned data to a struct hdr
1212 memcpy(&hdr, ptr, sizeof(hdr));
1214 // Try to match the packet to an existing connection
1216 struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
1217 print_packet(c, "recv", data, len);
1219 // Process the header
1224 // Drop packets with an unknown CTL flag
1226 if(hdr.ctl & ~(SYN | ACK | RST | FIN | MF)) {
1227 print_packet(NULL, "recv", data, len);
1232 // Check for auxiliary headers
1234 const uint8_t *init = NULL;
1236 uint16_t aux = hdr.aux;
1239 size_t auxlen = 4 * (aux >> 8) & 0xf;
1240 uint8_t auxtype = aux & 0xff;
1249 if(!(hdr.ctl & SYN) || auxlen != 4) {
1265 if(!(aux & 0x800)) {
1274 memcpy(&aux, ptr, 2);
1279 bool has_data = len || (hdr.ctl & (SYN | FIN));
1281 // Is it for a new connection?
1284 // Ignore RST packets
1290 // Is it a SYN packet and are we LISTENing?
1292 if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept) {
1293 // If we don't want to accept it, send a RST back
1294 if((utcp->pre_accept && !utcp->pre_accept(utcp, hdr.dst))) {
1299 // Try to allocate memory, otherwise send a RST back
1300 c = allocate_connection(utcp, hdr.dst, hdr.src);
1307 // Parse auxilliary information
1314 c->flags = init[3] & 0x7;
1316 c->flags = UTCP_TCP;
1320 // Return SYN+ACK, go to SYN_RECEIVED state
1321 c->snd.wnd = hdr.wnd;
1322 c->rcv.irs = hdr.seq;
1323 c->rcv.nxt = c->rcv.irs + 1;
1324 set_state(c, SYN_RECEIVED);
1331 pkt.hdr.src = c->src;
1332 pkt.hdr.dst = c->dst;
1333 pkt.hdr.ack = c->rcv.irs + 1;
1334 pkt.hdr.seq = c->snd.iss;
1335 pkt.hdr.wnd = c->rcvbuf.maxsize;
1336 pkt.hdr.ctl = SYN | ACK;
1339 pkt.hdr.aux = 0x0101;
1343 pkt.data[3] = c->flags & 0x7;
1344 print_packet(c, "send", &pkt, sizeof(hdr) + 4);
1345 utcp->send(utcp, &pkt, sizeof(hdr) + 4);
1348 print_packet(c, "send", &pkt, sizeof(hdr));
1349 utcp->send(utcp, &pkt, sizeof(hdr));
1352 // No, we don't want your packets, send a RST back
1360 debug(c, "state %s\n", strstate[c->state]);
1362 // In case this is for a CLOSED connection, ignore the packet.
1363 // TODO: make it so incoming packets can never match a CLOSED connection.
1365 if(c->state == CLOSED) {
1366 debug(c, "got packet for closed connection\n");
1370 // It is for an existing connection.
1372 // 1. Drop invalid packets.
1374 // 1a. Drop packets that should not happen in our current state.
1395 // 1b. Discard data that is not in our receive window.
1397 if(is_reliable(c)) {
1400 if(c->state == SYN_SENT) {
1402 } else if(len == 0) {
1403 acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0;
1405 int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
1407 // cut already accepted front overlapping
1408 if(rcv_offset < 0) {
1409 acceptable = len > (size_t) - rcv_offset;
1414 hdr.seq -= rcv_offset;
1417 acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt) + len <= c->rcvbuf.maxsize;
1422 debug(c, "packet not acceptable, %u <= %u + %lu < %u\n", c->rcv.nxt, hdr.seq, (unsigned long)len, c->rcv.nxt + c->rcvbuf.maxsize);
1424 // Ignore unacceptable RST packets.
1429 // Otherwise, continue processing.
1434 int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
1437 debug(c, "packet out of order, offset %u bytes", rcv_offset);
1443 c->snd.wnd = hdr.wnd; // TODO: move below
1445 // 1c. Drop packets with an invalid ACK.
1446 // ackno should not roll back, and it should also not be bigger than what we ever could have sent
1447 // (= snd.una + c->sndbuf.used).
1449 if(!is_reliable(c)) {
1450 if(hdr.ack != c->snd.last && c->state >= ESTABLISHED) {
1451 hdr.ack = c->snd.una;
1455 if(hdr.ctl & ACK && (seqdiff(hdr.ack, c->snd.last) > 0 || seqdiff(hdr.ack, c->snd.una) < 0)) {
1456 debug(c, "packet ack seqno out of range, %u <= %u < %u\n", c->snd.una, hdr.ack, c->snd.una + c->sndbuf.used);
1458 // Ignore unacceptable RST packets.
1466 // 2. Handle RST packets
1471 if(!(hdr.ctl & ACK)) {
1475 // The peer has refused our connection.
1476 set_state(c, CLOSED);
1477 errno = ECONNREFUSED;
1480 c->recv(c, NULL, 0);
1483 if(c->poll && !c->reapable) {
1494 // We haven't told the application about this connection yet. Silently delete.
1506 // The peer has aborted our connection.
1507 set_state(c, CLOSED);
1511 c->recv(c, NULL, 0);
1514 if(c->poll && !c->reapable) {
1527 // As far as the application is concerned, the connection has already been closed.
1528 // If it has called utcp_close() already, we can immediately free this connection.
1534 // Otherwise, immediately move to the CLOSED state.
1535 set_state(c, CLOSED);
1548 if(!(hdr.ctl & ACK)) {
1553 // 3. Advance snd.una
1555 advanced = seqdiff(hdr.ack, c->snd.una);
1559 if(c->rtt_start.tv_sec) {
1560 if(c->rtt_seq == hdr.ack) {
1561 struct timespec now;
1562 clock_gettime(UTCP_CLOCK, &now);
1563 int32_t diff = timespec_diff_usec(&now, &c->rtt_start);
1564 update_rtt(c, diff);
1565 c->rtt_start.tv_sec = 0;
1566 } else if(c->rtt_seq < hdr.ack) {
1567 debug(c, "cancelling RTT measurement: %u < %u\n", c->rtt_seq, hdr.ack);
1568 c->rtt_start.tv_sec = 0;
1572 int32_t data_acked = advanced;
1580 // TODO: handle FIN as well.
1585 assert(data_acked >= 0);
1588 int32_t bufused = seqdiff(c->snd.last, c->snd.una);
1589 assert(data_acked <= bufused);
1593 buffer_discard(&c->sndbuf, data_acked);
1597 // Also advance snd.nxt if possible
1598 if(seqdiff(c->snd.nxt, hdr.ack) < 0) {
1599 c->snd.nxt = hdr.ack;
1602 c->snd.una = hdr.ack;
1605 if(c->dupack >= 3) {
1606 debug(c, "fast recovery ended\n");
1607 c->snd.cwnd = c->snd.ssthresh;
1613 // Increase the congestion window according to RFC 5681
1614 if(c->snd.cwnd < c->snd.ssthresh) {
1615 c->snd.cwnd += min(advanced, utcp->mss); // eq. 2
1617 c->snd.cwnd += max(1, (utcp->mss * utcp->mss) / c->snd.cwnd); // eq. 3
1620 if(c->snd.cwnd > c->sndbuf.maxsize) {
1621 c->snd.cwnd = c->sndbuf.maxsize;
1626 // Check if we have sent a FIN that is now ACKed.
1629 if(c->snd.una == c->snd.last) {
1630 set_state(c, FIN_WAIT_2);
1636 if(c->snd.una == c->snd.last) {
1637 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1638 c->conn_timeout.tv_sec += utcp->timeout;
1639 set_state(c, TIME_WAIT);
1648 if(!len && is_reliable(c) && c->snd.una != c->snd.last) {
1650 debug(c, "duplicate ACK %d\n", c->dupack);
1652 if(c->dupack == 3) {
1653 // RFC 5681 fast recovery
1654 debug(c, "fast recovery started\n", c->dupack);
1655 uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
1656 c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
1657 c->snd.cwnd = min(c->snd.ssthresh + 3 * utcp->mss, c->sndbuf.maxsize);
1659 if(c->snd.cwnd > c->sndbuf.maxsize) {
1660 c->snd.cwnd = c->sndbuf.maxsize;
1666 } else if(c->dupack > 3) {
1667 c->snd.cwnd += utcp->mss;
1669 if(c->snd.cwnd > c->sndbuf.maxsize) {
1670 c->snd.cwnd = c->sndbuf.maxsize;
1676 // We got an ACK which indicates the other side did get one of our packets.
1677 // Reset the retransmission timer to avoid going to slow start,
1678 // but don't touch the connection timeout.
1679 start_retransmit_timer(c);
1686 if(c->snd.una == c->snd.last) {
1687 stop_retransmit_timer(c);
1688 timespec_clear(&c->conn_timeout);
1689 } else if(is_reliable(c)) {
1690 start_retransmit_timer(c);
1691 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1692 c->conn_timeout.tv_sec += utcp->timeout;
1697 // 5. Process SYN stuff
1703 // This is a SYNACK. It should always have ACKed the SYN.
1708 c->rcv.irs = hdr.seq;
1709 c->rcv.nxt = hdr.seq;
1713 set_state(c, FIN_WAIT_1);
1715 set_state(c, ESTABLISHED);
1718 // TODO: notify application of this somehow.
1722 // This is a retransmit of a SYN, send back the SYNACK.
1732 // Ehm, no. We should never receive a second SYN.
1742 // SYN counts as one sequence number
1746 // 6. Process new data
1748 if(c->state == SYN_RECEIVED) {
1749 // This is the ACK after the SYNACK. It should always have ACKed the SYNACK.
1754 // Are we still LISTENing?
1756 utcp->accept(c, c->src);
1759 if(c->state != ESTABLISHED) {
1760 set_state(c, CLOSED);
1770 // This should never happen.
1785 // Ehm no, We should never receive more data after a FIN.
1795 handle_incoming_data(c, &hdr, ptr, len);
1798 // 7. Process FIN stuff
1800 if((hdr.ctl & FIN) && (!is_reliable(c) || hdr.seq + len == c->rcv.nxt)) {
1804 // This should never happen.
1811 set_state(c, CLOSE_WAIT);
1815 set_state(c, CLOSING);
1819 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
1820 c->conn_timeout.tv_sec += utcp->timeout;
1821 set_state(c, TIME_WAIT);
1828 // Ehm, no. We should never receive a second FIN.
1838 // FIN counts as one sequence number
1842 // Inform the application that the peer closed its end of the connection.
1845 c->recv(c, NULL, 0);
1849 // Now we send something back if:
1850 // - we received data, so we have to send back an ACK
1851 // -> sendatleastone = true
1852 // - or we got an ack, so we should maybe send a bit more data
1853 // -> sendatleastone = false
1855 if(is_reliable(c) || hdr.ctl & SYN || hdr.ctl & FIN) {
1870 hdr.ack = hdr.seq + len;
1872 hdr.ctl = RST | ACK;
1875 print_packet(c, "send", &hdr, sizeof(hdr));
1876 utcp->send(utcp, &hdr, sizeof(hdr));
1881 int utcp_shutdown(struct utcp_connection *c, int dir) {
1882 debug(c, "shutdown %d at %u\n", dir, c ? c->snd.last : 0);
1890 debug(c, "shutdown() called on closed connection\n");
1895 if(!(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_WR || dir == UTCP_SHUT_RDWR)) {
1900 // TCP does not have a provision for stopping incoming packets.
1901 // The best we can do is to just ignore them.
1902 if(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_RDWR) {
1906 // The rest of the code deals with shutting down writes.
1907 if(dir == UTCP_SHUT_RD) {
1911 // Only process shutting down writes once.
1929 set_state(c, FIN_WAIT_1);
1937 set_state(c, CLOSING);
1950 if(!timespec_isset(&c->rtrx_timeout)) {
1951 start_retransmit_timer(c);
1957 static bool reset_connection(struct utcp_connection *c) {
1964 debug(c, "abort() called on closed connection\n");
1981 set_state(c, CLOSED);
1989 set_state(c, CLOSED);
1999 hdr.seq = c->snd.nxt;
2004 print_packet(c, "send", &hdr, sizeof(hdr));
2005 c->utcp->send(c->utcp, &hdr, sizeof(hdr));
2009 // Closes all the opened connections
2010 void utcp_abort_all_connections(struct utcp *utcp) {
2016 for(int i = 0; i < utcp->nconnections; i++) {
2017 struct utcp_connection *c = utcp->connections[i];
2019 if(c->reapable || c->state == CLOSED) {
2023 utcp_recv_t old_recv = c->recv;
2024 utcp_poll_t old_poll = c->poll;
2026 reset_connection(c);
2030 old_recv(c, NULL, 0);
2033 if(old_poll && !c->reapable) {
2042 int utcp_close(struct utcp_connection *c) {
2043 if(utcp_shutdown(c, SHUT_RDWR) && errno != ENOTCONN) {
2053 int utcp_abort(struct utcp_connection *c) {
2054 if(!reset_connection(c)) {
2063 * One call to this function will loop through all connections,
2064 * checking if something needs to be resent or not.
2065 * The return value is the time to the next timeout in milliseconds,
2066 * or maybe a negative value if the timeout is infinite.
2068 struct timespec utcp_timeout(struct utcp *utcp) {
2069 struct timespec now;
2070 clock_gettime(UTCP_CLOCK, &now);
2071 struct timespec next = {now.tv_sec + 3600, now.tv_nsec};
2073 for(int i = 0; i < utcp->nconnections; i++) {
2074 struct utcp_connection *c = utcp->connections[i];
2080 // delete connections that have been utcp_close()d.
2081 if(c->state == CLOSED) {
2083 debug(c, "reaping\n");
2091 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &now)) {
2096 c->recv(c, NULL, 0);
2099 if(c->poll && !c->reapable) {
2106 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &now)) {
2107 debug(c, "retransmitting after timeout\n");
2112 if((c->state == ESTABLISHED || c->state == CLOSE_WAIT) && c->do_poll) {
2114 uint32_t len = buffer_free(&c->sndbuf);
2119 } else if(c->state == CLOSED) {
2124 if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &next)) {
2125 next = c->conn_timeout;
2128 if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &next)) {
2129 next = c->rtrx_timeout;
2133 struct timespec diff;
2135 timespec_sub(&next, &now, &diff);
2140 bool utcp_is_active(struct utcp *utcp) {
2145 for(int i = 0; i < utcp->nconnections; i++)
2146 if(utcp->connections[i]->state != CLOSED && utcp->connections[i]->state != TIME_WAIT) {
2153 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
2159 struct utcp *utcp = calloc(1, sizeof(*utcp));
2165 if(!CLOCK_GRANULARITY) {
2166 struct timespec res;
2167 clock_getres(UTCP_CLOCK, &res);
2168 CLOCK_GRANULARITY = res.tv_sec * USEC_PER_SEC + res.tv_nsec / 1000;
2171 utcp->accept = accept;
2172 utcp->pre_accept = pre_accept;
2175 utcp_set_mtu(utcp, DEFAULT_MTU);
2176 utcp->timeout = DEFAULT_USER_TIMEOUT; // sec
2181 void utcp_exit(struct utcp *utcp) {
2186 for(int i = 0; i < utcp->nconnections; i++) {
2187 struct utcp_connection *c = utcp->connections[i];
2191 c->recv(c, NULL, 0);
2194 if(c->poll && !c->reapable) {
2199 buffer_exit(&c->rcvbuf);
2200 buffer_exit(&c->sndbuf);
2204 free(utcp->connections);
2209 uint16_t utcp_get_mtu(struct utcp *utcp) {
2210 return utcp ? utcp->mtu : 0;
2213 uint16_t utcp_get_mss(struct utcp *utcp) {
2214 return utcp ? utcp->mss : 0;
2217 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
2222 if(mtu <= sizeof(struct hdr)) {
2226 if(mtu > utcp->mtu) {
2227 char *new = realloc(utcp->pkt, mtu + sizeof(struct hdr));
2237 utcp->mss = mtu - sizeof(struct hdr);
2240 void utcp_reset_timers(struct utcp *utcp) {
2245 struct timespec now, then;
2247 clock_gettime(UTCP_CLOCK, &now);
2251 then.tv_sec += utcp->timeout;
2253 for(int i = 0; i < utcp->nconnections; i++) {
2254 struct utcp_connection *c = utcp->connections[i];
2260 if(timespec_isset(&c->rtrx_timeout)) {
2261 c->rtrx_timeout = now;
2264 if(timespec_isset(&c->conn_timeout)) {
2265 c->conn_timeout = then;
2268 c->rtt_start.tv_sec = 0;
2270 if(c->rto > START_RTO) {
2276 int utcp_get_user_timeout(struct utcp *u) {
2277 return u ? u->timeout : 0;
2280 void utcp_set_user_timeout(struct utcp *u, int timeout) {
2282 u->timeout = timeout;
2286 size_t utcp_get_sndbuf(struct utcp_connection *c) {
2287 return c ? c->sndbuf.maxsize : 0;
2290 size_t utcp_get_sndbuf_free(struct utcp_connection *c) {
2300 return buffer_free(&c->sndbuf);
2307 void utcp_set_sndbuf(struct utcp_connection *c, size_t size) {
2312 c->sndbuf.maxsize = size;
2314 if(c->sndbuf.maxsize != size) {
2315 c->sndbuf.maxsize = -1;
2318 c->do_poll = buffer_free(&c->sndbuf);
2321 size_t utcp_get_rcvbuf(struct utcp_connection *c) {
2322 return c ? c->rcvbuf.maxsize : 0;
2325 size_t utcp_get_rcvbuf_free(struct utcp_connection *c) {
2326 if(c && (c->state == ESTABLISHED || c->state == CLOSE_WAIT)) {
2327 return buffer_free(&c->rcvbuf);
2333 void utcp_set_rcvbuf(struct utcp_connection *c, size_t size) {
2338 c->rcvbuf.maxsize = size;
2340 if(c->rcvbuf.maxsize != size) {
2341 c->rcvbuf.maxsize = -1;
2345 size_t utcp_get_sendq(struct utcp_connection *c) {
2346 return c->sndbuf.used;
2349 size_t utcp_get_recvq(struct utcp_connection *c) {
2350 return c->rcvbuf.used;
2353 bool utcp_get_nodelay(struct utcp_connection *c) {
2354 return c ? c->nodelay : false;
2357 void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) {
2359 c->nodelay = nodelay;
2363 bool utcp_get_keepalive(struct utcp_connection *c) {
2364 return c ? c->keepalive : false;
2367 void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
2369 c->keepalive = keepalive;
2373 size_t utcp_get_outq(struct utcp_connection *c) {
2374 return c ? seqdiff(c->snd.nxt, c->snd.una) : 0;
2377 void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) {
2383 void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) {
2386 c->do_poll = buffer_free(&c->sndbuf);
2390 void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_pre_accept_t pre_accept) {
2392 utcp->accept = accept;
2393 utcp->pre_accept = pre_accept;
2397 void utcp_expect_data(struct utcp_connection *c, bool expect) {
2398 if(!c || c->reapable) {
2402 if(!(c->state == ESTABLISHED || c->state == FIN_WAIT_1 || c->state == FIN_WAIT_2)) {
2407 // If we expect data, start the connection timer.
2408 if(!timespec_isset(&c->conn_timeout)) {
2409 clock_gettime(UTCP_CLOCK, &c->conn_timeout);
2410 c->conn_timeout.tv_sec += c->utcp->timeout;
2413 // If we want to cancel expecting data, only clear the timer when there is no unACKed data.
2414 if(c->snd.una == c->snd.last) {
2415 timespec_clear(&c->conn_timeout);
2420 void utcp_offline(struct utcp *utcp, bool offline) {
2421 struct timespec now;
2422 clock_gettime(UTCP_CLOCK, &now);
2424 for(int i = 0; i < utcp->nconnections; i++) {
2425 struct utcp_connection *c = utcp->connections[i];
2431 utcp_expect_data(c, offline);
2434 if(timespec_isset(&c->rtrx_timeout)) {
2435 c->rtrx_timeout = now;
2438 utcp->connections[i]->rtt_start.tv_sec = 0;
2440 if(c->rto > START_RTO) {
2447 void utcp_set_clock_granularity(long granularity) {
2448 CLOCK_GRANULARITY = granularity;