]> git.meshlink.io Git - utcp/blob - utcp.c
Set FIN bit in ack().
[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         int32_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         fprintf(stderr, "ack, left=%d, cwndleft=%d, sendatleastone=%d\n", left, cwndleft, sendatleastone);
241         if(left < 0)
242                 abort();
243
244         if(cwndleft <= 0)
245                 cwndleft = 0;
246
247         if(cwndleft < left)
248                 left = cwndleft;
249
250         if(!left && !sendatleastone)
251                 return;
252
253         struct {
254                 struct hdr hdr;
255                 char data[c->utcp->mtu];
256         } pkt;
257
258         pkt.hdr.src = c->src;
259         pkt.hdr.dst = c->dst;
260         pkt.hdr.ack = c->rcv.nxt;
261         pkt.hdr.wnd = c->snd.wnd;
262         pkt.hdr.ctl = ACK;
263
264         do {
265                 uint32_t seglen = left > c->utcp->mtu ? c->utcp->mtu : left;
266                 pkt.hdr.seq = c->snd.nxt;
267
268                 memcpy(pkt.data, data, seglen);
269
270                 c->snd.nxt += seglen;
271                 data += seglen;
272                 left -= seglen;
273
274                 if(c->state != ESTABLISHED && !left && seglen) {
275                         switch(c->state) {
276                         case FIN_WAIT_1:
277                         case CLOSING:
278                                 seglen--;
279                                 pkt.hdr.ctl |= FIN;
280                                 break;
281                         default:
282                                 break;
283                         }
284                 }
285
286                 print_packet(c->utcp, "send", &pkt, sizeof pkt.hdr + seglen);
287                 c->utcp->send(c->utcp, &pkt, sizeof pkt.hdr + seglen);
288         } while(left);
289 }
290
291 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
292         if(c->reapable) {
293                 debug("Error: send() called on closed connection %p\n", c);
294                 errno = EBADF;
295                 return -1;
296         }
297
298         switch(c->state) {
299         case CLOSED:
300         case LISTEN:
301         case SYN_SENT:
302         case SYN_RECEIVED:
303                 debug("Error: send() called on unconnected connection %p\n", c);
304                 errno = ENOTCONN;
305                 return -1;
306         case ESTABLISHED:
307         case CLOSE_WAIT:
308                 break;
309         case FIN_WAIT_1:
310         case FIN_WAIT_2:
311         case CLOSING:
312         case LAST_ACK:
313         case TIME_WAIT:
314                 debug("Error: send() called on closing connection %p\n", c);
315                 errno = EPIPE;
316                 return -1;
317         }
318
319         // Add data to send buffer
320
321         if(!len)
322                 return 0;
323
324         if(!data) {
325                 errno = EFAULT;
326                 return -1;
327         }
328
329         uint32_t bufused = seqdiff(c->snd.nxt, c->snd.una);
330
331         /* Check our send buffer.
332          * - If it's big enough, just put the data in there.
333          * - If not, decide whether to enlarge if possible.
334          * - Cap len so it doesn't overflow our buffer.
335          */
336
337         if(len > c->sndbufsize - bufused && c->sndbufsize < c->maxsndbufsize) {
338                 uint32_t newbufsize;
339                 if(c->sndbufsize > c->maxsndbufsize / 2)
340                         newbufsize = c->maxsndbufsize;
341                 else
342                         newbufsize = c->sndbufsize * 2;
343                 if(bufused + len > newbufsize) {
344                         if(bufused + len > c->maxsndbufsize)
345                                 newbufsize = c->maxsndbufsize;
346                         else
347                                 newbufsize = bufused + len;
348                 }
349                 char *newbuf = realloc(c->sndbuf, newbufsize);
350                 if(newbuf) {
351                         c->sndbuf = newbuf;
352                         c->sndbufsize = newbufsize;
353                 }
354         }
355
356         if(len > c->sndbufsize - bufused)
357                 len = c->sndbufsize - bufused;
358
359         if(!len) {
360                 errno == EWOULDBLOCK;
361                 return 0;
362         }
363
364         memcpy(c->sndbuf + bufused, data, len);
365         c->snd.last += len;
366
367         ack(c, false);
368         return len;
369 }
370
371 static void swap_ports(struct hdr *hdr) {
372         uint16_t tmp = hdr->src;
373         hdr->src = hdr->dst;
374         hdr->dst = tmp;
375 }
376
377 int utcp_recv(struct utcp *utcp, const void *data, size_t len) {
378         if(!utcp) {
379                 errno = EFAULT;
380                 return -1;
381         }
382
383         if(!len)
384                 return 0;
385
386         if(!data) {
387                 errno = EFAULT;
388                 return -1;
389         }
390
391         print_packet(utcp, "recv", data, len);
392
393         // Drop packets smaller than the header
394
395         struct hdr hdr;
396         if(len < sizeof hdr) {
397                 errno = EBADMSG;
398                 return -1;
399         }
400
401         // Make a copy from the potentially unaligned data to a struct hdr
402
403         memcpy(&hdr, data, sizeof hdr);
404         data += sizeof hdr;
405         len -= sizeof hdr;
406
407         // Drop packets with an unknown CTL flag
408
409         if(hdr.ctl & ~(SYN | ACK | RST | FIN)) {
410                 errno = EBADMSG;
411                 return -1;
412         }
413
414         // Try to match the packet to an existing connection
415
416         struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
417
418         // Is it for a new connection?
419
420         if(!c) {
421                 // Ignore RST packets
422
423                 if(hdr.ctl & RST)
424                         return 0;
425
426                 // Is it a SYN packet and are we LISTENing?
427
428                 if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept) {
429                         // If we don't want to accept it, send a RST back
430                         if((utcp->pre_accept && !utcp->pre_accept(utcp, hdr.dst))) {
431                                 len = 1;
432                                 goto reset;
433                         }
434
435                         // Try to allocate memory, otherwise send a RST back
436                         c = allocate_connection(utcp, hdr.dst, hdr.src);
437                         if(!c) {
438                                 len = 1;
439                                 goto reset;
440                         }
441
442                         // Return SYN+ACK, go to SYN_RECEIVED state
443                         c->snd.wnd = hdr.wnd;
444                         c->rcv.irs = hdr.seq;
445                         c->rcv.nxt = c->rcv.irs + 1;
446                         set_state(c, SYN_RECEIVED);
447
448                         hdr.dst = c->dst;
449                         hdr.src = c->src;
450                         hdr.ack = c->rcv.irs + 1;
451                         hdr.seq = c->snd.iss;
452                         hdr.ctl = SYN | ACK;
453                         print_packet(c->utcp, "send", &hdr, sizeof hdr);
454                         utcp->send(utcp, &hdr, sizeof hdr);
455                 } else {
456                         // No, we don't want your packets, send a RST back
457                         len = 1;
458                         goto reset;
459                 }
460
461                 return 0;
462         }
463
464         debug("%p state %s\n", c->utcp, strstate[c->state]);
465
466         // In case this is for a CLOSED connection, ignore the packet.
467         // TODO: make it so incoming packets can never match a CLOSED connection.
468
469         if(c->state == CLOSED)
470                 return 0;
471
472         // It is for an existing connection.
473
474         // 1. Drop invalid packets.
475
476         // 1a. Drop packets that should not happen in our current state.
477
478         switch(c->state) {
479         case SYN_SENT:
480         case SYN_RECEIVED:
481         case ESTABLISHED:
482         case FIN_WAIT_1:
483         case FIN_WAIT_2:
484         case CLOSE_WAIT:
485         case CLOSING:
486         case LAST_ACK:
487         case TIME_WAIT:
488                 break;
489         default:
490                 abort();
491         }
492
493         // 1b. Drop packets with a sequence number not in our receive window.
494
495         bool acceptable;
496
497         if(c->state == SYN_SENT)
498                 acceptable = true;
499
500         // TODO: handle packets overlapping c->rcv.nxt.
501 #if 0
502         // Only use this when accepting out-of-order packets.
503         else if(len == 0)
504                 if(c->rcv.wnd == 0)
505                         acceptable = hdr.seq == c->rcv.nxt;
506                 else
507                         acceptable = (seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt + c->rcv.wnd) < 0);
508         else
509                 if(c->rcv.wnd == 0)
510                         // We don't accept data when the receive window is zero.
511                         acceptable = false;
512                 else
513                         // Both start and end of packet must be within the receive window
514                         acceptable = (seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt + c->rcv.wnd) < 0)
515                                 || (seqdiff(hdr.seq + len + 1, c->rcv.nxt) >= 0 && seqdiff(hdr.seq + len - 1, c->rcv.nxt + c->rcv.wnd) < 0);
516 #else
517         if(c->state != SYN_SENT)
518                 acceptable = hdr.seq == c->rcv.nxt;
519 #endif
520
521         if(!acceptable) {
522                 debug("Packet not acceptable, %u  <= %u + %zu < %u\n", c->rcv.nxt, hdr.seq, len, c->rcv.nxt + c->rcv.wnd);
523                 // Ignore unacceptable RST packets.
524                 if(hdr.ctl & RST)
525                         return 0;
526                 // Otherwise, send an ACK back in the hope things improve.
527                 goto ack;
528         }
529
530         c->snd.wnd = hdr.wnd; // TODO: move below
531
532         // 1c. Drop packets with an invalid ACK.
533         // ackno should not roll back, and it should also not be bigger than snd.nxt.
534
535         if(hdr.ctl & ACK && (seqdiff(hdr.ack, c->snd.nxt) > 0 || seqdiff(hdr.ack, c->snd.una) < 0)) {
536                 debug("Packet ack seqno out of range, %u %u %u\n", hdr.ack, c->snd.una, c->snd.nxt);
537                 // Ignore unacceptable RST packets.
538                 if(hdr.ctl & RST)
539                         return 0;
540                 goto reset;
541         }
542
543         // 2. Handle RST packets
544
545         if(hdr.ctl & RST) {
546                 switch(c->state) {
547                 case SYN_SENT:
548                         if(!(hdr.ctl & ACK))
549                                 return 0;
550                         // The peer has refused our connection.
551                         set_state(c, CLOSED);
552                         errno = ECONNREFUSED;
553                         if(c->recv)
554                                 c->recv(c, NULL, 0);
555                         return 0;
556                 case SYN_RECEIVED:
557                         if(hdr.ctl & ACK)
558                                 return 0;
559                         // We haven't told the application about this connection yet. Silently delete.
560                         free_connection(c);
561                         return 0;
562                 case ESTABLISHED:
563                 case FIN_WAIT_1:
564                 case FIN_WAIT_2:
565                 case CLOSE_WAIT:
566                         if(hdr.ctl & ACK)
567                                 return 0;
568                         // The peer has aborted our connection.
569                         set_state(c, CLOSED);
570                         errno = ECONNRESET;
571                         if(c->recv)
572                                 c->recv(c, NULL, 0);
573                         return 0;
574                 case CLOSING:
575                 case LAST_ACK:
576                 case TIME_WAIT:
577                         if(hdr.ctl & ACK)
578                                 return 0;
579                         // As far as the application is concerned, the connection has already been closed.
580                         // If it has called utcp_close() already, we can immediately free this connection.
581                         if(c->reapable) {
582                                 free_connection(c);
583                                 return 0;
584                         }
585                         // Otherwise, immediately move to the CLOSED state.
586                         set_state(c, CLOSED);
587                         return 0;
588                 default:
589                         abort();
590                 }
591         }
592
593         // 3. Advance snd.una
594
595         uint32_t advanced = seqdiff(hdr.ack, c->snd.una);
596         c->snd.una = hdr.ack;
597
598         if(advanced) {
599                 debug("%p advanced %u\n", utcp, advanced);
600                 // Make room in the send buffer.
601                 // TODO: try to avoid memmoving too much. Circular buffer?
602                 uint32_t left = seqdiff(c->snd.nxt, hdr.ack);
603                 if(left)
604                         memmove(c->sndbuf, c->sndbuf + advanced, left);
605                 c->dupack = 0;
606                 c->snd.cwnd += utcp->mtu;
607                 if(c->snd.cwnd > c->maxsndbufsize)
608                         c->snd.cwnd = c->maxsndbufsize;
609                 debug("%p increasing cwnd to %u\n", utcp, c->snd.cwnd);
610
611                 // Check if we have sent a FIN that is now ACKed.
612                 switch(c->state) {
613                 case FIN_WAIT_1:
614                         if(c->snd.una == c->snd.last)
615                                 set_state(c, FIN_WAIT_2);
616                         break;
617                 case CLOSING:
618                         if(c->snd.una == c->snd.last) {
619                                 gettimeofday(&c->conn_timeout, NULL);
620                                 c->conn_timeout.tv_sec += 60;
621                                 set_state(c, TIME_WAIT);
622                         }
623                         break;
624                 default:
625                         break;
626                 }
627         } else {
628                 if(!len) {
629                         c->dupack++;
630                         if(c->dupack >= 3) {
631                                 debug("Triplicate ACK\n");
632                                 abort();
633                         }
634                 }
635         }
636
637         // 4. Update timers
638
639         if(advanced) {
640                 timerclear(&c->conn_timeout); // It should be set anew in utcp_timeout() if c->snd.una != c->snd.nxt.
641                 if(c->snd.una == c->snd.nxt)
642                         timerclear(&c->rtrx_timeout);
643         }
644
645         // 5. Process SYN stuff
646
647         if(hdr.ctl & SYN) {
648                 switch(c->state) {
649                 case SYN_SENT:
650                         // This is a SYNACK. It should always have ACKed the SYN.
651                         if(!advanced)
652                                 goto reset;
653                         c->rcv.irs = hdr.seq;
654                         c->rcv.nxt = hdr.seq;
655                         set_state(c, ESTABLISHED);
656                         // TODO: notify application of this somehow.
657                         break;
658                 case SYN_RECEIVED:
659                 case ESTABLISHED:
660                 case FIN_WAIT_1:
661                 case FIN_WAIT_2:
662                 case CLOSE_WAIT:
663                 case CLOSING:
664                 case LAST_ACK:
665                 case TIME_WAIT:
666                         // Ehm, no. We should never receive a second SYN.
667                         goto reset;
668                 default:
669                         abort();
670                 }
671
672                 // SYN counts as one sequence number
673                 c->rcv.nxt++;
674         }
675
676         // 6. Process new data
677
678         if(c->state == SYN_RECEIVED) {
679                 // This is the ACK after the SYNACK. It should always have ACKed the SYNACK.
680                 if(!advanced)
681                         goto reset;
682
683                 // Are we still LISTENing?
684                 if(utcp->accept)
685                         utcp->accept(c, c->src);
686
687                 if(c->state != ESTABLISHED) {
688                         set_state(c, CLOSED);
689                         c->reapable = true;
690                         goto reset;
691                 }
692         }
693
694         if(len) {
695                 switch(c->state) {
696                 case SYN_SENT:
697                 case SYN_RECEIVED:
698                         // This should never happen.
699                         abort();
700                 case ESTABLISHED:
701                 case FIN_WAIT_1:
702                 case FIN_WAIT_2:
703                         break;
704                 case CLOSE_WAIT:
705                 case CLOSING:
706                 case LAST_ACK:
707                 case TIME_WAIT:
708                         // Ehm no, We should never receive more data after a FIN.
709                         goto reset;
710                 default:
711                         abort();
712                 }
713
714                 int rxd;
715
716                 if(c->recv) {
717                         rxd = c->recv(c, data, len);
718                         if(rxd < 0)
719                                 rxd = 0;
720                         else if(rxd > len)
721                                 rxd = len; // Bad application, bad!
722                 } else {
723                         rxd = len;
724                 }
725
726                 c->rcv.nxt += len;
727         }
728
729         // 7. Process FIN stuff
730
731         if(hdr.ctl & FIN) {
732                 switch(c->state) {
733                 case SYN_SENT:
734                 case SYN_RECEIVED:
735                         // This should never happen.
736                         abort();
737                 case ESTABLISHED:
738                         set_state(c, CLOSE_WAIT);
739                         break;
740                 case FIN_WAIT_1:
741                         set_state(c, CLOSING);
742                         break;
743                 case FIN_WAIT_2:
744                         gettimeofday(&c->conn_timeout, NULL);
745                         c->conn_timeout.tv_sec += 60;
746                         set_state(c, TIME_WAIT);
747                         break;
748                 case CLOSE_WAIT:
749                 case CLOSING:
750                 case LAST_ACK:
751                 case TIME_WAIT:
752                         // Ehm, no. We should never receive a second FIN.
753                         goto reset;
754                 default:
755                         abort();
756                 }
757
758                 // FIN counts as one sequence number
759                 c->rcv.nxt++;
760
761                 // Inform the application that the peer closed the connection.
762                 if(c->recv) {
763                         errno = 0;
764                         c->recv(c, NULL, 0);
765                 }
766         }
767
768         if(!len && !advanced)
769                 return 0;
770
771         if(!len && !(hdr.ctl & SYN) && !(hdr.ctl & FIN))
772                 return 0;
773
774 ack:
775         ack(c, true);
776         return 0;
777
778 reset:
779         swap_ports(&hdr);
780         hdr.wnd = 0;
781         if(hdr.ctl & ACK) {
782                 hdr.seq = hdr.ack;
783                 hdr.ctl = RST;
784         } else {
785                 hdr.ack = hdr.seq + len;
786                 hdr.seq = 0;
787                 hdr.ctl = RST | ACK;
788         }
789         print_packet(utcp, "send", &hdr, sizeof hdr);
790         utcp->send(utcp, &hdr, sizeof hdr);
791         return 0;
792
793 }
794
795 int utcp_shutdown(struct utcp_connection *c, int dir) {
796         debug("%p shutdown %d\n", c->utcp, dir);
797         if(!c) {
798                 errno = EFAULT;
799                 return -1;
800         }
801
802         if(c->reapable) {
803                 debug("Error: shutdown() called on closed connection %p\n", c);
804                 errno = EBADF;
805                 return -1;
806         }
807
808         // TODO: handle dir
809
810         switch(c->state) {
811         case CLOSED:
812                 return 0;
813         case LISTEN:
814         case SYN_SENT:
815                 set_state(c, CLOSED);
816                 return 0;
817
818         case SYN_RECEIVED:
819         case ESTABLISHED:
820                 set_state(c, FIN_WAIT_1);
821                 break;
822         case FIN_WAIT_1:
823         case FIN_WAIT_2:
824                 return 0;
825         case CLOSE_WAIT:
826                 set_state(c, CLOSING);
827                 break;
828
829         case CLOSING:
830         case LAST_ACK:
831         case TIME_WAIT:
832                 return 0;
833         }
834
835         c->snd.last++;
836
837         ack(c, false);
838         return 0;
839 }
840
841 int utcp_close(struct utcp_connection *c) {
842         if(utcp_shutdown(c, SHUT_RDWR))
843                 return -1;
844         c->reapable = true;
845         return 0;
846 }
847
848 int utcp_abort(struct utcp_connection *c) {
849         if(!c) {
850                 errno = EFAULT;
851                 return -1;
852         }
853
854         if(c->reapable) {
855                 debug("Error: abort() called on closed connection %p\n", c);
856                 errno = EBADF;
857                 return -1;
858         }
859
860         c->reapable = true;
861
862         switch(c->state) {
863         case CLOSED:
864                 return 0;
865         case LISTEN:
866         case SYN_SENT:
867         case CLOSING:
868         case LAST_ACK:
869         case TIME_WAIT:
870                 set_state(c, CLOSED);
871                 return 0;
872
873         case SYN_RECEIVED:
874         case ESTABLISHED:
875         case FIN_WAIT_1:
876         case FIN_WAIT_2:
877         case CLOSE_WAIT:
878                 set_state(c, CLOSED);
879                 break;
880         }
881
882         // Send RST
883
884         struct hdr hdr;
885
886         hdr.src = c->src;
887         hdr.dst = c->dst;
888         hdr.seq = c->snd.nxt;
889         hdr.ack = 0;
890         hdr.wnd = 0;
891         hdr.ctl = RST;
892
893         print_packet(c->utcp, "send", &hdr, sizeof hdr);
894         c->utcp->send(c->utcp, &hdr, sizeof hdr);
895         return 0;
896 }
897
898 static void retransmit(struct utcp_connection *c) {
899         if(c->state == CLOSED || c->snd.nxt == c->snd.una)
900                 return;
901
902         struct utcp *utcp = c->utcp;
903
904         struct {
905                 struct hdr hdr;
906                 char data[c->utcp->mtu];
907         } pkt;
908
909         pkt.hdr.src = c->src;
910         pkt.hdr.dst = c->dst;
911
912         switch(c->state) {
913                 case LISTEN:
914                         // TODO: this should not happen
915                         break;
916
917                 case SYN_SENT:
918                         pkt.hdr.seq = c->snd.iss;
919                         pkt.hdr.ack = 0;
920                         pkt.hdr.wnd = c->rcv.wnd;
921                         pkt.hdr.ctl = SYN;
922                         print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr);
923                         utcp->send(utcp, &pkt, sizeof pkt.hdr);
924                         break;
925
926                 case SYN_RECEIVED:
927                         pkt.hdr.seq = c->snd.nxt;
928                         pkt.hdr.ack = c->rcv.nxt;
929                         pkt.hdr.ctl = SYN | ACK;
930                         print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr);
931                         utcp->send(utcp, &pkt, sizeof pkt.hdr);
932                         break;
933
934                 case ESTABLISHED:
935                 case FIN_WAIT_1:
936                         pkt.hdr.seq = c->snd.una;
937                         pkt.hdr.ack = c->rcv.nxt;
938                         pkt.hdr.ctl = ACK;
939                         uint32_t len = seqdiff(c->snd.nxt, c->snd.una);
940                         if(c->state == FIN_WAIT_1)
941                                 len--;
942                         if(len > utcp->mtu)
943                                 len = utcp->mtu;
944                         else {
945                                 if(c->state == FIN_WAIT_1)
946                                         pkt.hdr.ctl |= FIN;
947                         }
948                         memcpy(pkt.data, c->sndbuf, len);
949                         print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr + len);
950                         utcp->send(utcp, &pkt, sizeof pkt.hdr + len);
951                         break;
952
953                 default:
954                         // TODO: implement
955                         abort();
956         }
957 }
958
959 /* Handle timeouts.
960  * One call to this function will loop through all connections,
961  * checking if something needs to be resent or not.
962  * The return value is the time to the next timeout in milliseconds,
963  * or maybe a negative value if the timeout is infinite.
964  */
965 int utcp_timeout(struct utcp *utcp) {
966         struct timeval now;
967         gettimeofday(&now, NULL);
968         struct timeval next = {now.tv_sec + 3600, now.tv_usec};
969
970         for(int i = 0; i < utcp->nconnections; i++) {
971                 struct utcp_connection *c = utcp->connections[i];
972                 if(!c)
973                         continue;
974
975                 if(c->state == CLOSED) {
976                         if(c->reapable) {
977                                 debug("Reaping %p\n", c);
978                                 free_connection(c);
979                                 i--;
980                         }
981                         continue;
982                 }
983
984                 if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &now, <)) {
985                         errno = ETIMEDOUT;
986                         c->state = CLOSED;
987                         if(c->recv)
988                                 c->recv(c, NULL, 0);
989                         continue;
990                 }
991
992                 if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &now, <)) {
993                         retransmit(c);
994                 }
995
996                 if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &next, <))
997                         next = c->conn_timeout;
998
999                 if(c->snd.nxt != c->snd.una) {
1000                         c->rtrx_timeout = now;
1001                         c->rtrx_timeout.tv_sec++;
1002                 } else {
1003                         timerclear(&c->rtrx_timeout);
1004                 }
1005
1006                 if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &next, <))
1007                         next = c->rtrx_timeout;
1008         }
1009
1010         struct timeval diff;
1011         timersub(&next, &now, &diff);
1012         if(diff.tv_sec < 0)
1013                 return 0;
1014         return diff.tv_sec * 1000 + diff.tv_usec / 1000;
1015 }
1016
1017 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
1018         struct utcp *utcp = calloc(1, sizeof *utcp);
1019         if(!utcp)
1020                 return NULL;
1021
1022         if(!send) {
1023                 errno = EFAULT;
1024                 return NULL;
1025         }
1026
1027         utcp->accept = accept;
1028         utcp->pre_accept = pre_accept;
1029         utcp->send = send;
1030         utcp->priv = priv;
1031         utcp->mtu = 1000;
1032         utcp->timeout = 60;
1033
1034         return utcp;
1035 }
1036
1037 void utcp_exit(struct utcp *utcp) {
1038         if(!utcp)
1039                 return;
1040         for(int i = 0; i < utcp->nconnections; i++)
1041                 free_connection(utcp->connections[i]);
1042         free(utcp);
1043 }
1044
1045 uint16_t utcp_get_mtu(struct utcp *utcp) {
1046         return utcp->mtu;
1047 }
1048
1049 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
1050         // TODO: handle overhead of the header
1051         utcp->mtu = mtu;
1052 }
1053
1054 int utcp_get_user_timeout(struct utcp *u) {
1055         return u->timeout;
1056 }
1057
1058 void utcp_set_user_timeout(struct utcp *u, int timeout) {
1059         u->timeout = timeout;
1060 }
1061
1062 size_t utcp_get_sndbuf(struct utcp_connection *c) {
1063         return c->maxsndbufsize;
1064 }
1065
1066 void utcp_set_sndbuf(struct utcp_connection *c, size_t size) {
1067         c->maxsndbufsize = size;
1068         if(c->maxsndbufsize != size)
1069                 c->maxsndbufsize = -1;
1070 }
1071
1072 bool utcp_get_nodelay(struct utcp_connection *c) {
1073         return c->nodelay;
1074 }
1075
1076 void utcp_set_nodelay(struct utcp_connection *c, bool nodelay) {
1077         c->nodelay = nodelay;
1078 }
1079
1080 bool utcp_get_keepalive(struct utcp_connection *c) {
1081         return c->keepalive;
1082 }
1083
1084 void utcp_set_keepalive(struct utcp_connection *c, bool keepalive) {
1085         c->keepalive = keepalive;
1086 }
1087
1088 size_t utcp_get_outq(struct utcp_connection *c) {
1089         return seqdiff(c->snd.nxt, c->snd.una);
1090 }