// Store data into the buffer
static ssize_t buffer_put_at(struct buffer *buf, size_t offset, const void *data, size_t len) {
- if(buf->maxsize <= buf->used)
- return 0;
-
debug("buffer_put_at %lu %lu %lu\n", (unsigned long)buf->used, (unsigned long)offset, (unsigned long)len);
size_t required = offset + len;
if(required > buf->maxsize) {
if(offset >= buf->maxsize)
return 0;
- abort();
len = buf->maxsize - offset;
required = buf->maxsize;
}
debug("timeout cleared\n");
}
-struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
+struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv, uint32_t flags) {
struct utcp_connection *c = allocate_connection(utcp, 0, dst);
if(!c)
return NULL;
+ assert((flags & ~0xf) == 0);
+
+ c->flags = flags;
c->recv = recv;
c->priv = priv;
return c;
}
+struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
+ return utcp_connect_ex(utcp, dst, recv, priv, UTCP_TCP);
+}
+
void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) {
if(c->reapable || c->state != SYN_RECEIVED) {
debug("Error: accept() called on invalid connection %p in state %s\n", c, strstate[c->state]);
*/
static void sack_consume(struct utcp_connection *c, size_t len) {
debug("sack_consume %lu\n", (unsigned long)len);
- if(len > c->rcvbuf.used)
- abort();
+ if(len > c->rcvbuf.used) {
+ debug("All SACK entries consumed");
+ c->sacks[0].len = 0;
+ return;
+ }
buffer_get(&c->rcvbuf, NULL, len);
// cut already accepted front overlapping
if(rcv_offset < 0) {
- acceptable = rcv_offset + len >= 0;
+ acceptable = len > -rcv_offset;
if(acceptable) {
data -= rcv_offset;
len += rcv_offset;
+ hdr.seq -= rcv_offset;
}
+ } else {
+ acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt) + len <= c->rcvbuf.maxsize;
}
-
- acceptable = seqdiff(hdr.seq, c->rcv.nxt) >= 0 && seqdiff(hdr.seq, c->rcv.nxt) + len <= c->rcvbuf.maxsize;
}
if(!acceptable) {
// Ignore unacceptable RST packets.
if(hdr.ctl & RST)
return 0;
- // Otherwise, send an ACK back in the hope things improve.
- ack(c, true);
- return 0;
+ // Otherwise, continue processing.
+ len = 0;
}
c->snd.wnd = hdr.wnd; // TODO: move below