2 utcp.c -- Userspace TCP
3 Copyright (C) 2014 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.
31 #include <sys/socket.h>
33 #include "utcp_priv.h"
48 #define timersub(a, b, r) do {\
49 (r)->tv_sec = (a)->tv_sec - (b)->tv_sec;\
50 (r)->tv_usec = (a)->tv_usec - (b)->tv_usec;\
52 (r)->tv_sec--, (r)->tv_usec += 1000000;\
59 static void debug(const char *format, ...) {
62 vfprintf(stderr, format, ap);
66 static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, size_t len) {
68 if(len < sizeof hdr) {
69 debug("%p %s: short packet (%zu bytes)\n", utcp, dir, len);
73 memcpy(&hdr, pkt, sizeof hdr);
74 fprintf (stderr, "%p %s: len=%zu, src=%u dst=%u seq=%u ack=%u wnd=%u ctl=", utcp, dir, len, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd);
84 if(len > sizeof hdr) {
86 for(int i = sizeof hdr; i < len; i++) {
87 const char *data = pkt;
88 debug("%c", data[i] >= 32 ? data[i] : '.');
96 #define print_packet(...)
99 static void set_state(struct utcp_connection *c, enum state state) {
101 if(state == ESTABLISHED)
102 timerclear(&c->conn_timeout);
103 debug("%p new state: %s\n", c->utcp, strstate[state]);
106 static bool fin_wanted(struct utcp_connection *c, uint32_t seq) {
107 if(seq != c->snd.last)
119 static inline void list_connections(struct utcp *utcp) {
120 debug("%p has %d connections:\n", utcp, utcp->nconnections);
121 for(int i = 0; i < utcp->nconnections; i++)
122 debug(" %u -> %u state %s\n", utcp->connections[i]->src, utcp->connections[i]->dst, strstate[utcp->connections[i]->state]);
125 static int32_t seqdiff(uint32_t a, uint32_t b) {
130 // TODO: convert to ringbuffers to avoid memmove() operations.
132 // Store data into the buffer
133 static ssize_t buffer_put_at(struct buffer *buf, size_t offset, const void *data, size_t len) {
134 if(buf->maxsize <= buf->used)
137 debug("buffer_put_at %zu %zu %zu\n", buf->used, offset, len);
139 size_t required = offset + len;
140 if(required > buf->maxsize) {
141 if(offset >= buf->maxsize)
144 len = buf->maxsize - offset;
145 required = buf->maxsize;
148 if(required > buf->size) {
149 size_t newsize = buf->size;
155 } while(newsize < buf->used + len);
157 if(newsize > buf->maxsize)
158 newsize = buf->maxsize;
159 char *newdata = realloc(buf->data, newsize);
166 memcpy(buf->data + offset, data, len);
167 if(required > buf->used)
168 buf->used = required;
172 static ssize_t buffer_put(struct buffer *buf, const void *data, size_t len) {
173 return buffer_put_at(buf, buf->used, data, len);
176 // Get data from the buffer. data can be NULL.
177 static ssize_t buffer_get(struct buffer *buf, void *data, size_t len) {
181 memcpy(data, buf->data, len);
183 memmove(buf->data, buf->data + len, buf->used - len);
188 // Copy data from the buffer without removing it.
189 static ssize_t buffer_copy(struct buffer *buf, void *data, size_t offset, size_t len) {
190 if(offset >= buf->used)
192 if(offset + len > buf->used)
193 len = buf->used - offset;
194 memcpy(data, buf->data + offset, len);
198 static bool buffer_init(struct buffer *buf, uint32_t len, uint32_t maxlen) {
199 memset(buf, 0, sizeof *buf);
201 buf->data = malloc(len);
206 buf->maxsize = maxlen;
210 static void buffer_exit(struct buffer *buf) {
212 memset(buf, 0, sizeof *buf);
215 static uint32_t buffer_free(const struct buffer *buf) {
216 return buf->maxsize - buf->used;
219 // Connections are stored in a sorted list.
220 // This gives O(log(N)) lookup time, O(N log(N)) insertion time and O(N) deletion time.
222 static int compare(const void *va, const void *vb) {
225 const struct utcp_connection *a = *(struct utcp_connection **)va;
226 const struct utcp_connection *b = *(struct utcp_connection **)vb;
229 assert(a->src && b->src);
231 int c = (int)a->src - (int)b->src;
234 c = (int)a->dst - (int)b->dst;
238 static struct utcp_connection *find_connection(const struct utcp *utcp, uint16_t src, uint16_t dst) {
239 if(!utcp->nconnections)
241 struct utcp_connection key = {
245 struct utcp_connection **match = bsearch(&keyp, utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
246 return match ? *match : NULL;
249 static void free_connection(struct utcp_connection *c) {
250 struct utcp *utcp = c->utcp;
251 struct utcp_connection **cp = bsearch(&c, utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
255 int i = cp - utcp->connections;
256 memmove(cp, cp + 1, (utcp->nconnections - i - 1) * sizeof *cp);
257 utcp->nconnections--;
259 buffer_exit(&c->sndbuf);
263 static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t src, uint16_t dst) {
264 // Check whether this combination of src and dst is free
267 if(find_connection(utcp, src, dst)) {
271 } else { // If src == 0, generate a random port number with the high bit set
272 if(utcp->nconnections >= 32767) {
276 src = rand() | 0x8000;
277 while(find_connection(utcp, src, dst))
281 // Allocate memory for the new connection
283 if(utcp->nconnections >= utcp->nallocated) {
284 if(!utcp->nallocated)
285 utcp->nallocated = 4;
287 utcp->nallocated *= 2;
288 struct utcp_connection **new_array = realloc(utcp->connections, utcp->nallocated * sizeof *utcp->connections);
291 utcp->connections = new_array;
294 struct utcp_connection *c = calloc(1, sizeof *c);
298 if(!buffer_init(&c->sndbuf, DEFAULT_SNDBUFSIZE, DEFAULT_MAXSNDBUFSIZE)) {
303 // Fill in the details
313 c->snd.una = c->snd.iss;
314 c->snd.nxt = c->snd.iss + 1;
315 c->rcv.wnd = utcp->mtu;
316 c->snd.last = c->snd.nxt;
317 c->snd.cwnd = utcp->mtu;
320 // Add it to the sorted list of connections
322 utcp->connections[utcp->nconnections++] = c;
323 qsort(utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
328 struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
329 struct utcp_connection *c = allocate_connection(utcp, 0, dst);
340 hdr.seq = c->snd.iss;
342 hdr.wnd = c->rcv.wnd;
346 set_state(c, SYN_SENT);
348 print_packet(utcp, "send", &hdr, sizeof hdr);
349 utcp->send(utcp, &hdr, sizeof hdr);
351 gettimeofday(&c->conn_timeout, NULL);
352 c->conn_timeout.tv_sec += utcp->timeout;
357 void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
358 if(c->reapable || c->state != SYN_RECEIVED) {
359 debug("Error: accept() called on invalid connection %p in state %s\n", c, strstate[c->state]);
363 debug("%p accepted, %p %p\n", c, recv, priv);
366 set_state(c, ESTABLISHED);
369 static void ack(struct utcp_connection *c, bool sendatleastone) {
370 int32_t left = seqdiff(c->snd.last, c->snd.nxt);
371 int32_t cwndleft = c->snd.cwnd - seqdiff(c->snd.nxt, c->snd.una);
381 if(!left && !sendatleastone)
389 pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
393 pkt->hdr.src = c->src;
394 pkt->hdr.dst = c->dst;
395 pkt->hdr.ack = c->rcv.nxt;
396 pkt->hdr.wnd = c->snd.wnd;
401 uint32_t seglen = left > c->utcp->mtu ? c->utcp->mtu : left;
402 pkt->hdr.seq = c->snd.nxt;
404 buffer_copy(&c->sndbuf, pkt->data, seqdiff(c->snd.nxt, c->snd.una), seglen);
406 c->snd.nxt += seglen;
409 if(seglen && fin_wanted(c, c->snd.nxt)) {
414 print_packet(c->utcp, "send", pkt, sizeof pkt->hdr + seglen);
415 c->utcp->send(c->utcp, pkt, sizeof pkt->hdr + seglen);
421 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
423 debug("Error: send() called on closed connection %p\n", c);
433 debug("Error: send() called on unconnected connection %p\n", c);
444 debug("Error: send() called on closing connection %p\n", c);
449 // Add data to send buffer
459 len = buffer_put(&c->sndbuf, data, len);
470 static void swap_ports(struct hdr *hdr) {
471 uint16_t tmp = hdr->src;
476 static void retransmit(struct utcp_connection *c) {
477 if(c->state == CLOSED || c->snd.nxt == c->snd.una)
480 struct utcp *utcp = c->utcp;
487 pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
491 pkt->hdr.src = c->src;
492 pkt->hdr.dst = c->dst;
496 // Send our SYN again
497 pkt->hdr.seq = c->snd.iss;
499 pkt->hdr.wnd = c->rcv.wnd;
501 print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr);
502 utcp->send(utcp, pkt, sizeof pkt->hdr);
507 pkt->hdr.seq = c->snd.nxt;
508 pkt->hdr.ack = c->rcv.nxt;
509 pkt->hdr.ctl = SYN | ACK;
510 print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr);
511 utcp->send(utcp, pkt, sizeof pkt->hdr);
519 // Send unacked data again.
520 pkt->hdr.seq = c->snd.una;
521 pkt->hdr.ack = c->rcv.nxt;
523 uint32_t len = seqdiff(c->snd.last, c->snd.una);
526 if(fin_wanted(c, c->snd.una + len)) {
530 c->snd.nxt = c->snd.una + len;
531 buffer_copy(&c->sndbuf, pkt->data, 0, len);
532 print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr + len);
533 utcp->send(utcp, pkt, sizeof pkt->hdr + len);
540 // We shouldn't need to retransmit anything in this state.
544 timerclear(&c->rtrx_timeout);
552 ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
566 print_packet(utcp, "recv", data, len);
568 // Drop packets smaller than the header
571 if(len < sizeof hdr) {
576 // Make a copy from the potentially unaligned data to a struct hdr
578 memcpy(&hdr, data, sizeof hdr);
582 // Drop packets with an unknown CTL flag
584 if(hdr.ctl & ~(SYN | ACK | RST | FIN)) {
589 // Try to match the packet to an existing connection
591 struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
593 // Is it for a new connection?
596 // Ignore RST packets
601 // Is it a SYN packet and are we LISTENing?
603 if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept) {
604 // If we don't want to accept it, send a RST back
605 if((utcp->pre_accept && !utcp->pre_accept(utcp, hdr.dst))) {
610 // Try to allocate memory, otherwise send a RST back
611 c = allocate_connection(utcp, hdr.dst, hdr.src);
617 // Return SYN+ACK, go to SYN_RECEIVED state
618 c->snd.wnd = hdr.wnd;
619 c->rcv.irs = hdr.seq;
620 c->rcv.nxt = c->rcv.irs + 1;
621 set_state(c, SYN_RECEIVED);
625 hdr.ack = c->rcv.irs + 1;
626 hdr.seq = c->snd.iss;
628 print_packet(c->utcp, "send", &hdr, sizeof hdr);
629 utcp->send(utcp, &hdr, sizeof hdr);
631 // No, we don't want your packets, send a RST back
639 debug("%p state %s\n", c->utcp, strstate[c->state]);
641 // In case this is for a CLOSED connection, ignore the packet.
642 // TODO: make it so incoming packets can never match a CLOSED connection.
644 if(c->state == CLOSED)
647 // It is for an existing connection.
649 uint32_t prevrcvnxt = c->rcv.nxt;
651 // 1. Drop invalid packets.
653 // 1a. Drop packets that should not happen in our current state.
673 // 1b. Drop packets with a sequence number not in our receive window.
677 if(c->state == SYN_SENT)
680 // TODO: handle packets overlapping c->rcv.nxt.
682 // Only use this when accepting out-of-order packets.
685 acceptable = hdr.seq == c->rcv.nxt;
687 acceptable = (seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt + c->rcv.wnd) < 0);
690 // We don't accept data when the receive window is zero.
693 // Both start and end of packet must be within the receive window
694 acceptable = (seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt + c->rcv.wnd) < 0)
695 || (seqdiff(hdr.seq + len + 1, c->rcv.nxt) >= 0 && seqdiff(hdr.seq + len - 1, c->rcv.nxt + c->rcv.wnd) < 0);
697 if(c->state != SYN_SENT)
698 acceptable = hdr.seq == c->rcv.nxt;
702 debug("Packet not acceptable, %u <= %u + %zu < %u\n", c->rcv.nxt, hdr.seq, len, c->rcv.nxt + c->rcv.wnd);
703 // Ignore unacceptable RST packets.
706 // Otherwise, send an ACK back in the hope things improve.
711 c->snd.wnd = hdr.wnd; // TODO: move below
713 // 1c. Drop packets with an invalid ACK.
714 // ackno should not roll back, and it should also not be bigger than snd.nxt.
716 if(hdr.ctl & ACK && (seqdiff(hdr.ack, c->snd.nxt) > 0 || seqdiff(hdr.ack, c->snd.una) < 0)) {
717 debug("Packet ack seqno out of range, %u %u %u\n", hdr.ack, c->snd.una, c->snd.nxt);
718 // Ignore unacceptable RST packets.
724 // 2. Handle RST packets
731 // The peer has refused our connection.
732 set_state(c, CLOSED);
733 errno = ECONNREFUSED;
740 // We haven't told the application about this connection yet. Silently delete.
749 // The peer has aborted our connection.
750 set_state(c, CLOSED);
760 // As far as the application is concerned, the connection has already been closed.
761 // If it has called utcp_close() already, we can immediately free this connection.
766 // Otherwise, immediately move to the CLOSED state.
767 set_state(c, CLOSED);
777 // 3. Advance snd.una
779 uint32_t advanced = seqdiff(hdr.ack, c->snd.una);
780 prevrcvnxt = c->rcv.nxt;
783 int32_t data_acked = advanced;
790 // TODO: handle FIN as well.
795 assert(data_acked >= 0);
797 int32_t bufused = seqdiff(c->snd.last, c->snd.una);
798 assert(data_acked <= bufused);
801 buffer_get(&c->sndbuf, NULL, data_acked);
803 c->snd.una = hdr.ack;
806 c->snd.cwnd += utcp->mtu;
807 if(c->snd.cwnd > c->sndbuf.maxsize)
808 c->snd.cwnd = c->sndbuf.maxsize;
810 // Check if we have sent a FIN that is now ACKed.
813 if(c->snd.una == c->snd.last)
814 set_state(c, FIN_WAIT_2);
817 if(c->snd.una == c->snd.last) {
818 gettimeofday(&c->conn_timeout, NULL);
819 c->conn_timeout.tv_sec += 60;
820 set_state(c, TIME_WAIT);
830 debug("Triplicate ACK\n");
831 //TODO: Resend one packet and go to fast recovery mode. See RFC 6582.
832 //We do a very simple variant here; reset the nxt pointer to the last acknowledged packet from the peer.
833 //This will cause us to start retransmitting, but at the same speed as the incoming ACKs arrive,
834 //thus preventing a drop in speed.
835 c->snd.nxt = c->snd.una;
843 timerclear(&c->conn_timeout); // It will be set anew in utcp_timeout() if c->snd.una != c->snd.nxt.
844 if(c->snd.una == c->snd.nxt)
845 timerclear(&c->rtrx_timeout);
848 // 5. Process SYN stuff
853 // This is a SYNACK. It should always have ACKed the SYN.
856 c->rcv.irs = hdr.seq;
857 c->rcv.nxt = hdr.seq;
858 set_state(c, ESTABLISHED);
859 // TODO: notify application of this somehow.
869 // Ehm, no. We should never receive a second SYN.
878 // SYN counts as one sequence number
882 // 6. Process new data
884 if(c->state == SYN_RECEIVED) {
885 // This is the ACK after the SYNACK. It should always have ACKed the SYNACK.
889 // Are we still LISTENing?
891 utcp->accept(c, c->src);
893 if(c->state != ESTABLISHED) {
894 set_state(c, CLOSED);
904 // This should never happen.
917 // Ehm no, We should never receive more data after a FIN.
929 rxd = c->recv(c, data, len);
931 // TODO: once we have a receive buffer, handle the application not accepting all data.
937 rxd = len; // Bad application, bad!
945 // 7. Process FIN stuff
951 // This should never happen.
957 set_state(c, CLOSE_WAIT);
960 set_state(c, CLOSING);
963 gettimeofday(&c->conn_timeout, NULL);
964 c->conn_timeout.tv_sec += 60;
965 set_state(c, TIME_WAIT);
971 // Ehm, no. We should never receive a second FIN.
980 // FIN counts as one sequence number
984 // Inform the application that the peer closed the connection.
991 // Now we send something back if:
992 // - we advanced rcv.nxt (ie, we got some data that needs to be ACKed)
993 // -> sendatleastone = true
994 // - or we got an ack, so we should maybe send a bit more data
995 // -> sendatleastone = false
998 ack(c, prevrcvnxt != c->rcv.nxt);
1008 hdr.ack = hdr.seq + len;
1010 hdr.ctl = RST | ACK;
1012 print_packet(utcp, "send", &hdr, sizeof hdr);
1013 utcp->send(utcp, &hdr, sizeof hdr);
1018 int utcp_shutdown(struct utcp_connection *c, int dir) {
1019 debug("%p shutdown %d at %u\n", c ? c->utcp : NULL, dir, c ? c->snd.last : 0);
1026 debug("Error: shutdown() called on closed connection %p\n", c);
1031 if(!(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_WR || dir == UTCP_SHUT_RDWR)) {
1036 // TCP does not have a provision for stopping incoming packets.
1037 // The best we can do is to just ignore them.
1038 if(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_RDWR)
1041 // The rest of the code deals with shutting down writes.
1042 if(dir == UTCP_SHUT_RD)
1052 set_state(c, CLOSED);
1057 set_state(c, FIN_WAIT_1);
1063 set_state(c, CLOSING);
1078 int utcp_close(struct utcp_connection *c) {
1079 if(utcp_shutdown(c, SHUT_RDWR))
1087 int utcp_abort(struct utcp_connection *c) {
1094 debug("Error: abort() called on closed connection %p\n", c);
1111 set_state(c, CLOSED);
1119 set_state(c, CLOSED);
1129 hdr.seq = c->snd.nxt;
1134 print_packet(c->utcp, "send", &hdr, sizeof hdr);
1135 c->utcp->send(c->utcp, &hdr, sizeof hdr);
1140 * One call to this function will loop through all connections,
1141 * checking if something needs to be resent or not.
1142 * The return value is the time to the next timeout in milliseconds,
1143 * or maybe a negative value if the timeout is infinite.
1145 struct timeval utcp_timeout(struct utcp *utcp) {
1147 gettimeofday(&now, NULL);
1148 struct timeval next = {now.tv_sec + 3600, now.tv_usec};
1150 for(int i = 0; i < utcp->nconnections; i++) {
1151 struct utcp_connection *c = utcp->connections[i];
1155 if(c->state == CLOSED) {
1157 debug("Reaping %p\n", c);
1164 if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &now, <)) {
1168 c->recv(c, NULL, 0);
1172 if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &now, <)) {
1176 if(c->poll && buffer_free(&c->sndbuf) && (c->state == ESTABLISHED || c->state == CLOSE_WAIT))
1177 c->poll(c, buffer_free(&c->sndbuf));
1179 if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &next, <))
1180 next = c->conn_timeout;
1182 if(c->snd.nxt != c->snd.una) {
1183 c->rtrx_timeout = now;
1184 c->rtrx_timeout.tv_sec++;
1186 timerclear(&c->rtrx_timeout);
1189 if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &next, <))
1190 next = c->rtrx_timeout;
1193 struct timeval diff;
1194 timersub(&next, &now, &diff);
1198 bool utcp_is_active(struct utcp *utcp) {
1202 for(int i = 0; i < utcp->nconnections; i++)
1203 if(utcp->connections[i]->state != CLOSED && utcp->connections[i]->state != TIME_WAIT)
1209 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
1210 struct utcp *utcp = calloc(1, sizeof *utcp);
1219 utcp->accept = accept;
1220 utcp->pre_accept = pre_accept;
1229 void utcp_exit(struct utcp *utcp) {
1232 for(int i = 0; i < utcp->nconnections; i++) {
1233 if(!utcp->connections[i]->reapable)
1234 debug("Warning, freeing unclosed connection %p\n", utcp->connections[i]);
1235 buffer_exit(&utcp->connections[i]->sndbuf);
1236 free(utcp->connections[i]);
1238 free(utcp->connections);
1242 uint16_t utcp_get_mtu(struct utcp *utcp) {
1243 return utcp ? utcp->mtu : 0;
1246 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
1247 // TODO: handle overhead of the header
1252 int utcp_get_user_timeout(struct utcp *u) {
1253 return u ? u->timeout : 0;
1256 void utcp_set_user_timeout(struct utcp *u, int timeout) {
1258 u->timeout = timeout;
1261 size_t utcp_get_sndbuf(struct utcp_connection *c) {
1262 return c ? c->sndbuf.maxsize : 0;
1265 size_t utcp_get_sndbuf_free(struct utcp_connection *c) {
1266 if(c && (c->state == ESTABLISHED || c->state == CLOSE_WAIT))
1267 return buffer_free(&c->sndbuf);
1272 void utcp_set_sndbuf(struct utcp_connection *c, size_t size) {
1275 c->sndbuf.maxsize = size;
1276 if(c->sndbuf.maxsize != size)
1277 c->sndbuf.maxsize = -1;
1280 bool utcp_get_nodelay(struct utcp_connection *c) {
1281 return c ? c->nodelay : false;
1284 void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) {
1286 c->nodelay = nodelay;
1289 bool utcp_get_keepalive(struct utcp_connection *c) {
1290 return c ? c->keepalive : false;
1293 void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
1295 c->keepalive = keepalive;
1298 size_t utcp_get_outq(struct utcp_connection *c) {
1299 return c ? seqdiff(c->snd.nxt, c->snd.una) : 0;
1302 void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) {
1307 void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) {
1312 void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_pre_accept_t pre_accept) {
1314 utcp->accept = accept;
1315 utcp->pre_accept = pre_accept;