]> git.meshlink.io Git - utcp/blob - utcp.c
Start implementation of congestion avoidance.
[utcp] / utcp.c
1 /*
2     utcp.c -- Userspace TCP
3     Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _GNU_SOURCE
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/time.h>
30 #include <sys/socket.h>
31
32 #include "utcp_priv.h"
33
34 #ifdef UTCP_DEBUG
35 #include <stdarg.h>
36
37 static void debug(const char *format, ...) {
38         va_list ap;
39         va_start(ap, format);
40         vfprintf(stderr, format, ap);
41         va_end(ap);
42 }
43
44 static void print_packet(struct utcp *utcp, const char *dir, const void *pkt, size_t len) {
45         struct hdr hdr;
46         if(len < sizeof hdr) {
47                 debug("%p %s: short packet (%zu bytes)\n", utcp, dir, len);
48                 return;
49         }
50
51         memcpy(&hdr, pkt, sizeof hdr);
52         fprintf (stderr, "%p %s: src=%u dst=%u seq=%u ack=%u wnd=%u ctl=", utcp, dir, hdr.src, hdr.dst, hdr.seq, hdr.ack, hdr.wnd);
53         if(hdr.ctl & SYN)
54                 debug("SYN");
55         if(hdr.ctl & RST)
56                 debug("RST");
57         if(hdr.ctl & FIN)
58                 debug("FIN");
59         if(hdr.ctl & ACK)
60                 debug("ACK");
61
62         if(len > sizeof hdr) {
63                 debug(" data=");
64                 for(int i = sizeof hdr; i < len; i++) {
65                         const char *data = pkt;
66                         debug("%c", data[i] >= 32 ? data[i] : '.');
67                 }
68         }
69
70         debug("\n");
71 }
72 #else
73 #define debug(...)
74 #define print_packet(...)
75 #endif
76
77 static void set_state(struct utcp_connection *c, enum state state) {
78         c->state = state;
79         if(state == ESTABLISHED)
80                 timerclear(&c->conn_timeout);
81         debug("%p new state: %s\n", c->utcp, strstate[state]);
82 }
83
84 static inline void list_connections(struct utcp *utcp) {
85         debug("%p has %d connections:\n", utcp, utcp->nconnections);
86         for(int i = 0; i < utcp->nconnections; i++)
87                 debug("  %u -> %u state %s\n", utcp->connections[i]->src, utcp->connections[i]->dst, strstate[utcp->connections[i]->state]);
88 }
89
90 static int32_t seqdiff(uint32_t a, uint32_t b) {
91         return a - b;
92 }
93
94 // Connections are stored in a sorted list.
95 // This gives O(log(N)) lookup time, O(N log(N)) insertion time and O(N) deletion time.
96
97 static int compare(const void *va, const void *vb) {
98         const struct utcp_connection *a = *(struct utcp_connection **)va;
99         const struct utcp_connection *b = *(struct utcp_connection **)vb;
100         if(!a->src || !b->src)
101                 abort();
102         int c = (int)a->src - (int)b->src;
103         if(c)
104                 return c;
105         c = (int)a->dst - (int)b->dst;
106         return c;
107 }
108
109 static struct utcp_connection *find_connection(const struct utcp *utcp, uint16_t src, uint16_t dst) {
110         if(!utcp->nconnections)
111                 return NULL;
112         struct utcp_connection key = {
113                 .src = src,
114                 .dst = dst,
115         }, *keyp = &key;
116         struct utcp_connection **match = bsearch(&keyp, utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
117         return match ? *match : NULL;
118 }
119
120 static void free_connection(struct utcp_connection *c) {
121         struct utcp *utcp = c->utcp;
122         struct utcp_connection **cp = bsearch(&c, utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
123         if(!cp)
124                 abort();
125
126         int i = cp - utcp->connections;
127         memmove(cp + i, cp + i + 1, (utcp->nconnections - i - 1) * sizeof *cp);
128         utcp->nconnections--;
129
130         free(c);
131 }
132
133 static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t src, uint16_t dst) {
134         // Check whether this combination of src and dst is free
135
136         if(src) {
137                 if(find_connection(utcp, src, dst)) {
138                         errno = EADDRINUSE;
139                         return NULL;
140                 }
141         } else { // If src == 0, generate a random port number with the high bit set
142                 if(utcp->nconnections >= 32767) {
143                         errno = ENOMEM;
144                         return NULL;
145                 }
146                 src = rand() | 0x8000;
147                 while(find_connection(utcp, src, dst))
148                         src++;
149         }
150
151         // Allocate memory for the new connection
152
153         if(utcp->nconnections >= utcp->nallocated) {
154                 if(!utcp->nallocated)
155                         utcp->nallocated = 4;
156                 else
157                         utcp->nallocated *= 2;
158                 struct utcp_connection **new_array = realloc(utcp->connections, utcp->nallocated * sizeof *utcp->connections);
159                 if(!new_array)
160                         return NULL;
161                 utcp->connections = new_array;
162         }
163
164         struct utcp_connection *c = calloc(1, sizeof *c);
165         if(!c)
166                 return NULL;
167
168         c->sndbufsize = DEFAULT_SNDBUFSIZE;
169         c->maxsndbufsize = DEFAULT_MAXSNDBUFSIZE;
170         c->sndbuf = malloc(c->sndbufsize);
171         if(!c->sndbuf) {
172                 free(c);
173                 return NULL;
174         }
175
176         // Fill in the details
177
178         c->src = src;
179         c->dst = dst;
180         c->snd.iss = rand();
181         c->snd.una = c->snd.iss;
182         c->snd.nxt = c->snd.iss + 1;
183         c->rcv.wnd = utcp->mtu;
184         c->snd.last = c->snd.nxt;
185         c->snd.cwnd = utcp->mtu;
186         c->utcp = utcp;
187
188         // Add it to the sorted list of connections
189
190         utcp->connections[utcp->nconnections++] = c;
191         qsort(utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
192
193         return c;
194 }
195
196 struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
197         struct utcp_connection *c = allocate_connection(utcp, 0, dst);
198         if(!c)
199                 return NULL;
200
201         c->recv = recv;
202
203         struct hdr hdr;
204
205         hdr.src = c->src;
206         hdr.dst = c->dst;
207         hdr.seq = c->snd.iss;
208         hdr.ack = 0;
209         hdr.ctl = SYN;
210         hdr.wnd = c->rcv.wnd;
211
212         set_state(c, SYN_SENT);
213
214         print_packet(utcp, "send", &hdr, sizeof hdr);
215         utcp->send(utcp, &hdr, sizeof hdr);
216
217         gettimeofday(&c->conn_timeout, NULL);
218         c->conn_timeout.tv_sec += utcp->timeout;
219
220         return c;
221 }
222
223 void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
224         if(c->reapable || c->state != SYN_RECEIVED) {
225                 debug("Error: accept() called on invalid connection %p in state %s\n", c, strstate[c->state]);
226                 return;
227         }
228
229         debug("%p accepted, %p %p\n", c, recv, priv);
230         c->recv = recv;
231         c->priv = priv;
232         set_state(c, ESTABLISHED);
233 }
234
235 static void ack(struct utcp_connection *c, bool sendatleastone) {
236         uint32_t left = seqdiff(c->snd.last, c->snd.nxt);
237         int32_t cwndleft = c->snd.cwnd - seqdiff(c->snd.nxt, c->snd.una);
238         char *data = c->sndbuf + seqdiff(c->snd.nxt, c->snd.una);
239
240         if(cwndleft <= 0)
241                 cwndleft = 0;
242
243         if(cwndleft < left)
244                 left = cwndleft;
245
246         if(!left && !sendatleastone)
247                 return;
248
249         struct {
250                 struct hdr hdr;
251                 char data[c->utcp->mtu];
252         } pkt;
253
254         pkt.hdr.src = c->src;
255         pkt.hdr.dst = c->dst;
256         pkt.hdr.ack = c->rcv.nxt;
257         pkt.hdr.wnd = c->snd.wnd;
258         pkt.hdr.ctl = ACK;
259
260         do {
261                 uint32_t seglen = left > c->utcp->mtu ? c->utcp->mtu : left;
262                 pkt.hdr.seq = c->snd.nxt;
263
264                 memcpy(pkt.data, data, seglen);
265
266                 c->snd.nxt += seglen;
267                 data += seglen;
268                 left -= seglen;
269
270                 print_packet(c->utcp, "send", &pkt, sizeof pkt.hdr + seglen);
271                 c->utcp->send(c->utcp, &pkt, sizeof pkt.hdr + seglen);
272         } while(left);
273 }
274
275 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
276         if(c->reapable) {
277                 debug("Error: send() called on closed connection %p\n", c);
278                 errno = EBADF;
279                 return -1;
280         }
281
282         switch(c->state) {
283         case CLOSED:
284         case LISTEN:
285         case SYN_SENT:
286         case SYN_RECEIVED:
287                 debug("Error: send() called on unconnected connection %p\n", c);
288                 errno = ENOTCONN;
289                 return -1;
290         case ESTABLISHED:
291         case CLOSE_WAIT:
292                 break;
293         case FIN_WAIT_1:
294         case FIN_WAIT_2:
295         case CLOSING:
296         case LAST_ACK:
297         case TIME_WAIT:
298                 debug("Error: send() called on closing connection %p\n", c);
299                 errno = EPIPE;
300                 return -1;
301         }
302
303         // Add data to send buffer
304
305         if(!len)
306                 return 0;
307
308         if(!data) {
309                 errno = EFAULT;
310                 return -1;
311         }
312
313         uint32_t bufused = seqdiff(c->snd.nxt, c->snd.una);
314
315         /* Check our send buffer.
316          * - If it's big enough, just put the data in there.
317          * - If not, decide whether to enlarge if possible.
318          * - Cap len so it doesn't overflow our buffer.
319          */
320
321         if(len > c->sndbufsize - bufused && c->sndbufsize < c->maxsndbufsize) {
322                 uint32_t newbufsize;
323                 if(c->sndbufsize > c->maxsndbufsize / 2)
324                         newbufsize = c->maxsndbufsize;
325                 else
326                         newbufsize = c->sndbufsize * 2;
327                 char *newbuf = realloc(c->sndbuf, newbufsize);
328                 if(newbuf) {
329                         c->sndbuf = newbuf;
330                         c->sndbufsize = newbufsize;
331                 }
332         }
333
334         if(len > c->sndbufsize - bufused)
335                 len = c->sndbufsize - bufused;
336
337         if(!len) {
338                 errno == EWOULDBLOCK;
339                 return 0;
340         }
341
342         memcpy(c->sndbuf + bufused, data, len);
343         c->snd.last += len;
344
345         ack(c, false);
346         return len;
347 }
348
349 static void swap_ports(struct hdr *hdr) {
350         uint16_t tmp = hdr->src;
351         hdr->src = hdr->dst;
352         hdr->dst = tmp;
353 }
354
355 int utcp_recv(struct utcp *utcp, const void *data, size_t len) {
356         if(!utcp) {
357                 errno = EFAULT;
358                 return -1;
359         }
360
361         if(!len)
362                 return 0;
363
364         if(!data) {
365                 errno = EFAULT;
366                 return -1;
367         }
368
369         print_packet(utcp, "recv", data, len);
370
371         // Drop packets smaller than the header
372
373         struct hdr hdr;
374         if(len < sizeof hdr) {
375                 errno = EBADMSG;
376                 return -1;
377         }
378
379         // Make a copy from the potentially unaligned data to a struct hdr
380
381         memcpy(&hdr, data, sizeof hdr);
382         data += sizeof hdr;
383         len -= sizeof hdr;
384
385         // Drop packets with an unknown CTL flag
386
387         if(hdr.ctl & ~(SYN | ACK | RST | FIN)) {
388                 errno = EBADMSG;
389                 return -1;
390         }
391
392         // Try to match the packet to an existing connection
393
394         struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
395
396         // Is it for a new connection?
397
398         if(!c) {
399                 // Ignore RST packets
400
401                 if(hdr.ctl & RST)
402                         return 0;
403
404                 // Is it a SYN packet and are we LISTENing?
405
406                 if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept) {
407                         // If we don't want to accept it, send a RST back
408                         if((utcp->pre_accept && !utcp->pre_accept(utcp, hdr.dst))) {
409                                 len = 1;
410                                 goto reset;
411                         }
412
413                         // Try to allocate memory, otherwise send a RST back
414                         c = allocate_connection(utcp, hdr.dst, hdr.src);
415                         if(!c) {
416                                 len = 1;
417                                 goto reset;
418                         }
419
420                         // Return SYN+ACK, go to SYN_RECEIVED state
421                         c->snd.wnd = hdr.wnd;
422                         c->rcv.irs = hdr.seq;
423                         c->rcv.nxt = c->rcv.irs + 1;
424                         set_state(c, SYN_RECEIVED);
425
426                         hdr.dst = c->dst;
427                         hdr.src = c->src;
428                         hdr.ack = c->rcv.irs + 1;
429                         hdr.seq = c->snd.iss;
430                         hdr.ctl = SYN | ACK;
431                         print_packet(c->utcp, "send", &hdr, sizeof hdr);
432                         utcp->send(utcp, &hdr, sizeof hdr);
433                 } else {
434                         // No, we don't want your packets, send a RST back
435                         len = 1;
436                         goto reset;
437                 }
438
439                 return 0;
440         }
441
442         debug("%p state %s\n", c->utcp, strstate[c->state]);
443
444         // In case this is for a CLOSED connection, ignore the packet.
445         // TODO: make it so incoming packets can never match a CLOSED connection.
446
447         if(c->state == CLOSED)
448                 return 0;
449
450         // It is for an existing connection.
451
452         // 1. Drop invalid packets.
453
454         // 1a. Drop packets that should not happen in our current state.
455
456         switch(c->state) {
457         case SYN_SENT:
458         case SYN_RECEIVED:
459         case ESTABLISHED:
460         case FIN_WAIT_1:
461         case FIN_WAIT_2:
462         case CLOSE_WAIT:
463         case CLOSING:
464         case LAST_ACK:
465         case TIME_WAIT:
466                 break;
467         default:
468                 abort();
469         }
470
471         // 1b. Drop packets with a sequence number not in our receive window.
472
473         bool acceptable;
474
475         if(c->state == SYN_SENT)
476                 acceptable = true;
477
478         // TODO: handle packets overlapping c->rcv.nxt.
479 #if 0
480         // Only use this when accepting out-of-order packets.
481         else if(len == 0)
482                 if(c->rcv.wnd == 0)
483                         acceptable = hdr.seq == c->rcv.nxt;
484                 else
485                         acceptable = (seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt + c->rcv.wnd) < 0);
486         else
487                 if(c->rcv.wnd == 0)
488                         // We don't accept data when the receive window is zero.
489                         acceptable = false;
490                 else
491                         // Both start and end of packet must be within the receive window
492                         acceptable = (seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt + c->rcv.wnd) < 0)
493                                 || (seqdiff(hdr.seq + len + 1, c->rcv.nxt) >= 0 && seqdiff(hdr.seq + len - 1, c->rcv.nxt + c->rcv.wnd) < 0);
494 #else
495         if(c->state != SYN_SENT)
496                 acceptable = hdr.seq == c->rcv.nxt;
497 #endif
498
499         if(!acceptable) {
500                 debug("Packet not acceptable, %u  <= %u + %zu < %u\n", c->rcv.nxt, hdr.seq, len, c->rcv.nxt + c->rcv.wnd);
501                 // Ignore unacceptable RST packets.
502                 if(hdr.ctl & RST)
503                         return 0;
504                 // Otherwise, send an ACK back in the hope things improve.
505                 goto ack;
506         }
507
508         c->snd.wnd = hdr.wnd; // TODO: move below
509
510         // 1c. Drop packets with an invalid ACK.
511         // ackno should not roll back, and it should also not be bigger than snd.nxt.
512
513         if(hdr.ctl & ACK && (seqdiff(hdr.ack, c->snd.nxt) > 0 || seqdiff(hdr.ack, c->snd.una) < 0)) {
514                 debug("Packet ack seqno out of range, %u %u %u\n", hdr.ack, c->snd.una, c->snd.nxt);
515                 // Ignore unacceptable RST packets.
516                 if(hdr.ctl & RST)
517                         return 0;
518                 goto reset;
519         }
520
521         // 2. Handle RST packets
522
523         if(hdr.ctl & RST) {
524                 switch(c->state) {
525                 case SYN_SENT:
526                         if(!(hdr.ctl & ACK))
527                                 return 0;
528                         // The peer has refused our connection.
529                         set_state(c, CLOSED);
530                         errno = ECONNREFUSED;
531                         if(c->recv)
532                                 c->recv(c, NULL, 0);
533                         return 0;
534                 case SYN_RECEIVED:
535                         if(hdr.ctl & ACK)
536                                 return 0;
537                         // We haven't told the application about this connection yet. Silently delete.
538                         free_connection(c);
539                         return 0;
540                 case ESTABLISHED:
541                 case FIN_WAIT_1:
542                 case FIN_WAIT_2:
543                 case CLOSE_WAIT:
544                         if(hdr.ctl & ACK)
545                                 return 0;
546                         // The peer has aborted our connection.
547                         set_state(c, CLOSED);
548                         errno = ECONNRESET;
549                         if(c->recv)
550                                 c->recv(c, NULL, 0);
551                         return 0;
552                 case CLOSING:
553                 case LAST_ACK:
554                 case TIME_WAIT:
555                         if(hdr.ctl & ACK)
556                                 return 0;
557                         // As far as the application is concerned, the connection has already been closed.
558                         // If it has called utcp_close() already, we can immediately free this connection.
559                         if(c->reapable) {
560                                 free_connection(c);
561                                 return 0;
562                         }
563                         // Otherwise, immediately move to the CLOSED state.
564                         set_state(c, CLOSED);
565                         return 0;
566                 default:
567                         abort();
568                 }
569         }
570
571         // 3. Advance snd.una
572
573         uint32_t advanced = seqdiff(hdr.ack, c->snd.una);
574         c->snd.una = hdr.ack;
575
576         if(advanced) {
577                 debug("%p advanced %u\n", utcp, advanced);
578                 // Make room in the send buffer.
579                 // TODO: try to avoid memmoving too much. Circular buffer?
580                 uint32_t left = seqdiff(c->snd.nxt, hdr.ack);
581                 if(left)
582                         memmove(c->sndbuf, c->sndbuf + advanced, left);
583                 c->dupack = 0;
584                 c->snd.cwnd += utcp->mtu;
585                 if(c->snd.cwnd > c->maxsndbufsize)
586                         c->snd.cwnd = c->maxsndbufsize;
587         } else {
588                 if(!len) {
589                         c->dupack++;
590                         if(c->dupack >= 3) {
591                                 debug("Triplicate ACK\n");
592                                 abort();
593                         }
594                 }
595         }
596
597         // 4. Update timers
598
599         if(advanced) {
600                 timerclear(&c->conn_timeout); // It should be set anew in utcp_timeout() if c->snd.una != c->snd.nxt.
601                 if(c->snd.una == c->snd.nxt)
602                         timerclear(&c->rtrx_timeout);
603         }
604
605         // 5. Process SYN stuff
606
607         if(hdr.ctl & SYN) {
608                 switch(c->state) {
609                 case SYN_SENT:
610                         // This is a SYNACK. It should always have ACKed the SYN.
611                         if(!advanced)
612                                 goto reset;
613                         c->rcv.irs = hdr.seq;
614                         c->rcv.nxt = hdr.seq;
615                         set_state(c, ESTABLISHED);
616                         // TODO: notify application of this somehow.
617                         break;
618                 case SYN_RECEIVED:
619                 case ESTABLISHED:
620                 case FIN_WAIT_1:
621                 case FIN_WAIT_2:
622                 case CLOSE_WAIT:
623                 case CLOSING:
624                 case LAST_ACK:
625                 case TIME_WAIT:
626                         // Ehm, no. We should never receive a second SYN.
627                         goto reset;
628                 default:
629                         abort();
630                 }
631
632                 // SYN counts as one sequence number
633                 c->rcv.nxt++;
634         }
635
636         // 6. Process new data
637
638         if(c->state == SYN_RECEIVED) {
639                 // This is the ACK after the SYNACK. It should always have ACKed the SYNACK.
640                 if(!advanced)
641                         goto reset;
642
643                 // Are we still LISTENing?
644                 if(utcp->accept)
645                         utcp->accept(c, c->src);
646
647                 if(c->state != ESTABLISHED) {
648                         set_state(c, CLOSED);
649                         c->reapable = true;
650                         goto reset;
651                 }
652         }
653
654         if(len) {
655                 switch(c->state) {
656                 case SYN_SENT:
657                 case SYN_RECEIVED:
658                         // This should never happen.
659                         abort();
660                 case ESTABLISHED:
661                 case FIN_WAIT_1:
662                 case FIN_WAIT_2:
663                         break;
664                 case CLOSE_WAIT:
665                 case CLOSING:
666                 case LAST_ACK:
667                 case TIME_WAIT:
668                         // Ehm no, We should never receive more data after a FIN.
669                         goto reset;
670                 default:
671                         abort();
672                 }
673
674                 int rxd;
675
676                 if(c->recv) {
677                         rxd = c->recv(c, data, len);
678                         if(rxd < 0)
679                                 rxd = 0;
680                         else if(rxd > len)
681                                 rxd = len; // Bad application, bad!
682                 } else {
683                         rxd = len;
684                 }
685
686                 c->rcv.nxt += len;
687         }
688
689         // 7. Process FIN stuff
690
691         if(hdr.ctl & FIN) {
692                 switch(c->state) {
693                 case SYN_SENT:
694                 case SYN_RECEIVED:
695                         // This should never happen.
696                         abort();
697                 case ESTABLISHED:
698                         set_state(c, CLOSE_WAIT);
699                         break;
700                 case FIN_WAIT_1:
701                         set_state(c, CLOSING);
702                         break;
703                 case FIN_WAIT_2:
704                         set_state(c, TIME_WAIT);
705                         break;
706                 case CLOSE_WAIT:
707                 case CLOSING:
708                 case LAST_ACK:
709                 case TIME_WAIT:
710                         // Ehm, no. We should never receive a second FIN.
711                         goto reset;
712                 default:
713                         abort();
714                 }
715
716                 // FIN counts as one sequence number
717                 c->rcv.nxt++;
718
719                 // Inform the application that the peer closed the connection.
720                 if(c->recv) {
721                         errno = 0;
722                         c->recv(c, NULL, 0);
723                 }
724         }
725
726         if(!len && !advanced)
727                 return 0;
728
729         if(!len && !(hdr.ctl & SYN) && !(hdr.ctl & FIN))
730                 return 0;
731
732 ack:
733         ack(c, true);
734         return 0;
735
736 reset:
737         swap_ports(&hdr);
738         hdr.wnd = 0;
739         if(hdr.ctl & ACK) {
740                 hdr.seq = hdr.ack;
741                 hdr.ctl = RST;
742         } else {
743                 hdr.ack = hdr.seq + len;
744                 hdr.seq = 0;
745                 hdr.ctl = RST | ACK;
746         }
747         print_packet(utcp, "send", &hdr, sizeof hdr);
748         utcp->send(utcp, &hdr, sizeof hdr);
749         return 0;
750
751 }
752
753 int utcp_shutdown(struct utcp_connection *c, int dir) {
754         debug("%p shutdown %d\n", c->utcp, dir);
755         if(!c) {
756                 errno = EFAULT;
757                 return -1;
758         }
759
760         if(c->reapable) {
761                 debug("Error: shutdown() called on closed connection %p\n", c);
762                 errno = EBADF;
763                 return -1;
764         }
765
766         // TODO: handle dir
767
768         switch(c->state) {
769         case CLOSED:
770                 return 0;
771         case LISTEN:
772         case SYN_SENT:
773                 set_state(c, CLOSED);
774                 return 0;
775
776         case SYN_RECEIVED:
777         case ESTABLISHED:
778                 set_state(c, FIN_WAIT_1);
779                 break;
780         case FIN_WAIT_1:
781         case FIN_WAIT_2:
782                 return 0;
783         case CLOSE_WAIT:
784                 set_state(c, CLOSING);
785                 break;
786
787         case CLOSING:
788         case LAST_ACK:
789         case TIME_WAIT:
790                 return 0;
791         }
792
793         // Send FIN
794
795         struct hdr hdr;
796
797         hdr.src = c->src;
798         hdr.dst = c->dst;
799         hdr.seq = c->snd.nxt;
800         hdr.ack = c->rcv.nxt;
801         hdr.wnd = c->snd.wnd;
802         hdr.ctl = FIN | ACK;
803
804         c->snd.nxt += 1;
805
806         print_packet(c->utcp, "send", &hdr, sizeof hdr);
807         c->utcp->send(c->utcp, &hdr, sizeof hdr);
808         return 0;
809 }
810
811 int utcp_close(struct utcp_connection *c) {
812         if(utcp_shutdown(c, SHUT_RDWR))
813                 return -1;
814         c->reapable = true;
815         return 0;
816 }
817
818 int utcp_abort(struct utcp_connection *c) {
819         if(!c) {
820                 errno = EFAULT;
821                 return -1;
822         }
823
824         if(c->reapable) {
825                 debug("Error: abort() called on closed connection %p\n", c);
826                 errno = EBADF;
827                 return -1;
828         }
829
830         c->reapable = true;
831
832         switch(c->state) {
833         case CLOSED:
834                 return 0;
835         case LISTEN:
836         case SYN_SENT:
837         case CLOSING:
838         case LAST_ACK:
839         case TIME_WAIT:
840                 set_state(c, CLOSED);
841                 return 0;
842
843         case SYN_RECEIVED:
844         case ESTABLISHED:
845         case FIN_WAIT_1:
846         case FIN_WAIT_2:
847         case CLOSE_WAIT:
848                 set_state(c, CLOSED);
849                 break;
850         }
851
852         // Send RST
853
854         struct hdr hdr;
855
856         hdr.src = c->src;
857         hdr.dst = c->dst;
858         hdr.seq = c->snd.nxt;
859         hdr.ack = 0;
860         hdr.wnd = 0;
861         hdr.ctl = RST;
862
863         print_packet(c->utcp, "send", &hdr, sizeof hdr);
864         c->utcp->send(c->utcp, &hdr, sizeof hdr);
865         return 0;
866 }
867
868 static void retransmit(struct utcp_connection *c) {
869         if(c->state == CLOSED || c->snd.nxt == c->snd.una)
870                 return;
871
872         struct utcp *utcp = c->utcp;
873
874         struct {
875                 struct hdr hdr;
876                 char data[c->utcp->mtu];
877         } pkt;
878
879         pkt.hdr.src = c->src;
880         pkt.hdr.dst = c->dst;
881
882         switch(c->state) {
883                 case LISTEN:
884                         // TODO: this should not happen
885                         break;
886
887                 case SYN_SENT:
888                         pkt.hdr.seq = c->snd.iss;
889                         pkt.hdr.ack = 0;
890                         pkt.hdr.wnd = c->rcv.wnd;
891                         pkt.hdr.ctl = SYN;
892                         print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr);
893                         utcp->send(utcp, &pkt, sizeof pkt.hdr);
894                         break;
895
896                 case SYN_RECEIVED:
897                         pkt.hdr.seq = c->snd.nxt;
898                         pkt.hdr.ack = c->rcv.nxt;
899                         pkt.hdr.ctl = SYN | ACK;
900                         print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr);
901                         utcp->send(utcp, &pkt, sizeof pkt.hdr);
902                         break;
903
904                 case ESTABLISHED:
905                 case FIN_WAIT_1:
906                         pkt.hdr.seq = c->snd.una;
907                         pkt.hdr.ack = c->rcv.nxt;
908                         pkt.hdr.ctl = ACK;
909                         uint32_t len = seqdiff(c->snd.nxt, c->snd.una);
910                         if(c->state == FIN_WAIT_1)
911                                 len--;
912                         if(len > utcp->mtu)
913                                 len = utcp->mtu;
914                         else {
915                                 if(c->state == FIN_WAIT_1)
916                                         pkt.hdr.ctl |= FIN;
917                         }
918                         memcpy(pkt.data, c->sndbuf, len);
919                         print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr + len);
920                         utcp->send(utcp, &pkt, sizeof pkt.hdr + len);
921                         break;
922
923                 default:
924                         // TODO: implement
925                         abort();
926         }
927 }
928
929 /* Handle timeouts.
930  * One call to this function will loop through all connections,
931  * checking if something needs to be resent or not.
932  * The return value is the time to the next timeout in milliseconds,
933  * or maybe a negative value if the timeout is infinite.
934  */
935 int utcp_timeout(struct utcp *utcp) {
936         struct timeval now;
937         gettimeofday(&now, NULL);
938         struct timeval next = {now.tv_sec + 3600, now.tv_usec};
939
940         for(int i = 0; i < utcp->nconnections; i++) {
941                 struct utcp_connection *c = utcp->connections[i];
942                 if(!c)
943                         continue;
944
945                 if(c->state == CLOSED) {
946                         if(c->reapable) {
947                                 debug("Reaping %p\n", c);
948                                 free_connection(c);
949                                 i--;
950                         }
951                         continue;
952                 }
953
954                 if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &now, <)) {
955                         errno = ETIMEDOUT;
956                         c->state = CLOSED;
957                         if(c->recv)
958                                 c->recv(c, NULL, 0);
959                         continue;
960                 }
961
962                 if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &now, <)) {
963                         retransmit(c);
964                 }
965
966                 if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &next, <))
967                         next = c->conn_timeout;
968
969                 if(c->snd.nxt != c->snd.una) {
970                         c->rtrx_timeout = now;
971                         c->rtrx_timeout.tv_sec++;
972                 } else {
973                         timerclear(&c->rtrx_timeout);
974                 }
975
976                 if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &next, <))
977                         next = c->rtrx_timeout;
978         }
979
980         struct timeval diff;
981         timersub(&next, &now, &diff);
982         if(diff.tv_sec < 0)
983                 return 0;
984         return diff.tv_sec * 1000 + diff.tv_usec / 1000;
985 }
986
987 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
988         struct utcp *utcp = calloc(1, sizeof *utcp);
989         if(!utcp)
990                 return NULL;
991
992         if(!send) {
993                 errno = EFAULT;
994                 return NULL;
995         }
996
997         utcp->accept = accept;
998         utcp->pre_accept = pre_accept;
999         utcp->send = send;
1000         utcp->priv = priv;
1001         utcp->mtu = 1000;
1002         utcp->timeout = 60;
1003
1004         return utcp;
1005 }
1006
1007 void utcp_exit(struct utcp *utcp) {
1008         if(!utcp)
1009                 return;
1010         for(int i = 0; i < utcp->nconnections; i++)
1011                 free_connection(utcp->connections[i]);
1012         free(utcp);
1013 }
1014
1015 uint16_t utcp_get_mtu(struct utcp *utcp) {
1016         return utcp->mtu;
1017 }
1018
1019 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
1020         // TODO: handle overhead of the header
1021         utcp->mtu = mtu;
1022 }
1023
1024 int utcp_get_user_timeout(struct utcp *u) {
1025         return u->timeout;
1026 }
1027
1028 void utcp_set_user_timeout(struct utcp *u, int timeout) {
1029         u->timeout = timeout;
1030 }
1031
1032 size_t utcp_get_sndbuf(struct utcp_connection *c) {
1033         return c->maxsndbufsize;
1034 }
1035
1036 void utcp_set_sndbuf(struct utcp_connection *c, size_t size) {
1037         c->maxsndbufsize = size;
1038         if(c->maxsndbufsize != size)
1039                 c->maxsndbufsize = -1;
1040 }
1041
1042 bool utcp_get_nodelay(struct utcp_connection *c) {
1043         return c->nodelay;
1044 }
1045
1046 void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) {
1047         c->nodelay = nodelay;
1048 }
1049
1050 bool utcp_get_keepalive(struct utcp_connection *c) {
1051         return c->keepalive;
1052 }
1053
1054 void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
1055         c->keepalive = keepalive;
1056 }
1057
1058 size_t utcp_get_outq(struct utcp_connection *c) {
1059         return seqdiff(c->snd.nxt, c->snd.una);
1060 }