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
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);