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