]> git.meshlink.io Git - utcp/blobdiff - utcp.c
Move private struct declarations e.a. to utcp_priv.h.
[utcp] / utcp.c
diff --git a/utcp.c b/utcp.c
index 436c9e8cdcd4703e39e88401fe35fa02a47c0934..966295a7f086a079f96a2d818cad1d8d3c90bad2 100644 (file)
--- a/utcp.c
+++ b/utcp.c
 #include <sys/time.h>
 #include <sys/socket.h>
 
-#define UTCP_INTERNAL
-#include "utcp.h"
-
-#define PREP(l) char pkt[(l) + sizeof struct hdr]; struct hdr *hdr = &pkt;
-
-#define SYN 1
-#define ACK 2
-#define FIN 4
-#define RST 8
-
-struct hdr {
-       uint16_t src; // Source port
-       uint16_t dst; // Destination port
-       uint32_t seq; // Sequence number
-       uint32_t ack; // Acknowledgement number
-       uint32_t wnd; // Window size
-       uint16_t ctl; // Flags (SYN, ACK, FIN, RST)
-       uint16_t aux; // other stuff
-};
-
-enum state {
-       CLOSED,
-       LISTEN,
-       SYN_SENT,
-       SYN_RECEIVED,
-       ESTABLISHED,
-       FIN_WAIT_1,
-       FIN_WAIT_2,
-       CLOSE_WAIT,
-       CLOSING,
-       LAST_ACK,
-       TIME_WAIT
-};
-
-const char *strstate[] = {
-       "CLOSED",
-       "LISTEN",
-       "SYN_SENT",
-       "SYN_RECEIVED",
-       "ESTABLISHED",
-       "FIN_WAIT_1",
-       "FIN_WAIT_2",
-       "CLOSE_WAIT",
-       "CLOSING",
-       "LAST_ACK",
-       "TIME_WAIT"
-};
-
-struct utcp_connection {
-       void *priv;
-       struct utcp *utcp;
-       bool reapable;
-
-       uint16_t src;
-       uint16_t dst;
-       enum state state;
-
-       // The following two structures form the TCB
-
-       struct {
-               uint32_t una;
-               uint32_t nxt;
-               uint32_t wnd;
-               uint32_t iss;
-       } snd;
-
-       struct {
-               uint32_t nxt;
-               uint32_t wnd;
-               uint32_t irs;
-       } rcv;
-
-       utcp_recv_t recv;
-
-       struct timeval conn_timeout;
-       struct timeval rtrx_timeout;
-
-       char *sndbuf;
-       uint32_t sndbufsize;
-};
-
-struct utcp {
-       void *priv;
-
-       utcp_accept_t accept;
-       utcp_pre_accept_t pre_accept;
-       utcp_send_t send;
-
-       uint16_t mtu;
-       int timeout;
-
-       struct utcp_connection **connections;
-       int nconnections;
-       int nallocated;
-};
+#include "utcp_priv.h"
 
 static void set_state(struct utcp_connection *c, enum state state) {
        c->state = state;
@@ -346,10 +252,26 @@ ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
 
        uint32_t bufused = c->snd.nxt - c->snd.una;
 
+       /* Check our send buffer.
+        * - If it's big enough, just put the data in there.
+        * - If not, decide whether to enlarge. (TODO, now we just always enlarge)
+        * - Cap len so it doesn't overflow our buffer.
+        */
+
+       if(len > c->sndbufsize - bufused) {
+               c->sndbufsize *= 2;
+               c->sndbuf = realloc(c->sndbuf, c->sndbufsize);
+       }
+
        if(len > c->sndbufsize - bufused)
                len = c->sndbufsize - bufused;
 
-       memcpy(c->sndbuf + (c->snd.nxt - c->snd.una), data, len);
+       if(!len) {
+               errno == EWOULDBLOCK;
+               return 0;
+       }
+
+       memcpy(c->sndbuf + bufused, data, len);
 
        // Send segments
 
@@ -938,12 +860,19 @@ static void retransmit(struct utcp_connection *c) {
                        break;
 
                case ESTABLISHED:
+               case FIN_WAIT_1:
                        pkt.hdr.seq = c->snd.una;
                        pkt.hdr.ack = c->rcv.nxt;
                        pkt.hdr.ctl = ACK;
                        uint32_t len = seqdiff(c->snd.nxt, c->snd.una);
+                       if(c->state == FIN_WAIT_1)
+                               len--;
                        if(len > utcp->mtu)
                                len = utcp->mtu;
+                       else {
+                               if(c->state == FIN_WAIT_1)
+                                       pkt.hdr.ctl |= FIN;
+                       }
                        memcpy(pkt.data, c->sndbuf, len);
                        print_packet(c->utcp, "rtrx", &pkt, sizeof pkt.hdr + len);
                        utcp->send(utcp, &pkt, sizeof pkt.hdr + len);
@@ -1041,6 +970,11 @@ void utcp_exit(struct utcp *utcp) {
        free(utcp);
 }
 
+void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
+       // TODO: handle overhead of the header
+       utcp->mtu = mtu;
+}
+
 int utcp_set_connection_timeout(struct utcp *u, int timeout) {
        int prev = u->timeout;
        u->timeout = timeout;