]> git.meshlink.io Git - utcp/blobdiff - utcp.c
Fix check for return value of malloc().
[utcp] / utcp.c
diff --git a/utcp.c b/utcp.c
index 062cc5af09380d4c2fe1d46496dae4200b6bf48b..ce056904169c6923baa95a0e5435626e5da75fd5 100644 (file)
--- a/utcp.c
+++ b/utcp.c
@@ -282,7 +282,12 @@ static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t s
 
        c->src = src;
        c->dst = dst;
+#ifdef UTCP_DEBUG
+#warning debugging
+       c->snd.iss = 0;
+#else
        c->snd.iss = rand();
+#endif
        c->snd.una = c->snd.iss;
        c->snd.nxt = c->snd.iss + 1;
        c->rcv.wnd = utcp->mtu;
@@ -360,7 +365,7 @@ static void ack(struct utcp_connection *c, bool sendatleastone) {
        } *pkt;
 
        pkt = malloc(sizeof pkt->hdr + c->utcp->mtu);
-       if(!pkt->data)
+       if(!pkt)
                return;
 
        pkt->hdr.src = c->src;
@@ -500,6 +505,7 @@ static void retransmit(struct utcp_connection *c) {
                                len--;
                                pkt->hdr.ctl |= FIN;
                        }
+                       c->snd.nxt = c->snd.una + len;
                        buffer_copy(&c->sndbuf, pkt->data, 0, len);
                        print_packet(c->utcp, "rtrx", pkt, sizeof pkt->hdr + len);
                        utcp->send(utcp, pkt, sizeof pkt->hdr + len);
@@ -988,7 +994,7 @@ reset:
 }
 
 int utcp_shutdown(struct utcp_connection *c, int dir) {
-       debug("%p shutdown %d at %u\n", c ? c->utcp : NULL, dir, c->snd.last);
+       debug("%p shutdown %d at %u\n", c ? c->utcp : NULL, dir, c ? c->snd.last : 0);
        if(!c) {
                errno = EFAULT;
                return -1;
@@ -1000,13 +1006,26 @@ int utcp_shutdown(struct utcp_connection *c, int dir) {
                return -1;
        }
 
-       // TODO: handle dir
-       // TODO: check that repeated calls with the same parameters should have no effect
+       if(!(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_WR || dir == UTCP_SHUT_RDWR)) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       // TCP does not have a provision for stopping incoming packets.
+       // The best we can do is to just ignore them.
+       if(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_RDWR)
+               c->recv = NULL;
+
+       // The rest of the code deals with shutting down writes.
+       if(dir == UTCP_SHUT_RD)
+               return 0;
 
        switch(c->state) {
        case CLOSED:
-               return 0;
        case LISTEN:
+               errno = ENOTCONN;
+               return -1;
+
        case SYN_SENT:
                set_state(c, CLOSED);
                return 0;
@@ -1154,6 +1173,17 @@ struct timeval utcp_timeout(struct utcp *utcp) {
        return diff;
 }
 
+bool utcp_is_active(struct utcp *utcp) {
+       if(!utcp)
+               return false;
+
+       for(int i = 0; i < utcp->nconnections; i++)
+               if(utcp->connections[i]->state != CLOSED && utcp->connections[i]->state != TIME_WAIT)
+                       return true;
+
+       return false;
+}
+
 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {
        struct utcp *utcp = calloc(1, sizeof *utcp);
        if(!utcp)