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