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