return 0;
}
-bool do_pre_accept(struct utcp *utcp, void *data, size_t len) {
+bool do_pre_accept(struct utcp *utcp, uint16_t port) {
fprintf(stderr, "pre-accept\n");
- if(len != 2 || strncmp(data, "oi", 2))
+ if(port != 7)
return false;
return true;
}
-void do_accept(struct utcp_connection *c, void *data, size_t len) {
+void do_accept(struct utcp_connection *c, uint16_t port) {
fprintf(stderr, "accept\n");
utcp_accept(c, do_recv, NULL);
}
b = utcp_init(NULL, NULL, do_send, NULL);
fprintf(stderr, "Testing connection to closed port\n\n");
- c = utcp_connect(b, "zomg", 4, do_recv, NULL);
+ c = utcp_connect(b, 6, do_recv, NULL);
fprintf(stderr, "\nTesting conection to non-listening side\n\n");
- c = utcp_connect(a, "oi", 2, do_recv, NULL);
+ c = utcp_connect(a, 7, do_recv, NULL);
fprintf(stderr, "\nTesting connection to open port, close\n\n");
- c = utcp_connect(b, "oi", 2, do_recv, NULL);
+ c = utcp_connect(b, 7, do_recv, NULL);
fprintf(stderr, "closing...\n");
utcp_close(c);
fprintf(stderr, "\nTesting connection to open port, abort\n\n");
- c = utcp_connect(b, "oi", 2, do_recv, NULL);
+ c = utcp_connect(b, 7, do_recv, NULL);
fprintf(stderr, "aborting...\n");
utcp_abort(c);
fprintf(stderr, "\nTesting connection with data transfer\n\n");
- c = utcp_connect(b, "oi", 2, do_recv, NULL);
+ c = utcp_connect(b, 7, do_recv, NULL);
utcp_send(c, "Hello world!\n", 13);
utcp_send(c, "This is a test.\n", 16);
fprintf(stderr, "closing...\n");
struct timeval conn_timeout;
struct timeval rtrx_timeout;
+
+ char *sndbuf;
+ uint32_t sndbufsize;
};
struct utcp {
fprintf(stderr, "\n");
}
-static struct utcp_connection *allocate_connection(struct utcp *utcp) {
- struct utcp_connection *c;
+static void list_connections(struct utcp *utcp) {
+ fprintf(stderr, "%p has %d connections:\n", utcp, utcp->nconnections);
+ for(int i = 0; i < utcp->nconnections; i++)
+ fprintf(stderr, " %u -> %u state %s\n", utcp->connections[i]->src, utcp->connections[i]->dst, strstate[utcp->connections[i]->state]);
+}
- // Initial allocation?
+// Connections are stored in a sorted list.
+// This gives O(log(N)) lookup time, O(N log(N)) insertion time and O(N) deletion time.
- if(!utcp->nconnections) {
- utcp->nallocated = 4;
- utcp->nconnections = 1; // Skip 0
- utcp->connections = calloc(utcp->nallocated, sizeof *utcp->connections);
- }
+static int compare(const void *va, const void *vb) {
+ const struct utcp_connection *a = *(struct utcp_connection **)va;
+ const struct utcp_connection *b = *(struct utcp_connection **)vb;
+ if(!a->src || !b->src)
+ abort();
+ int c = (int)a->src - (int)b->src;
+ if(c)
+ return c;
+ c = (int)a->dst - (int)b->dst;
+ return c;
+}
- // If there is a hole in the list of connections, use it.
- // Otherwise, add a new connection to the end.
+static struct utcp_connection *find_connection(const struct utcp *utcp, uint16_t src, uint16_t dst) {
+ if(!utcp->nconnections)
+ return NULL;
+ struct utcp_connection key = {
+ .src = src,
+ .dst = dst,
+ }, *keyp = &key;
+ struct utcp_connection **match = bsearch(&keyp, utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
+ return match ? *match : NULL;
+}
- if(utcp->gap >= 0) {
- c = utcp->connections[utcp->gap] = calloc(1, sizeof *c);
- c->src = utcp->gap;
- while(++utcp->gap < utcp->nconnections)
- if(!utcp->connections[utcp->gap])
- break;
+static void free_connection(struct utcp_connection *c) {
+ struct utcp *utcp = c->utcp;
+ struct utcp_connection **cp = bsearch(&c, utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
+ if(!cp)
+ abort();
- if(utcp->gap >= utcp->nconnections)
- utcp->gap = -1;
- } else {
- // Too many connections?
+ int i = cp - utcp->connections;
+ memmove(cp + i, cp + i + 1, (utcp->nconnections - i - 1) * sizeof *cp);
+ utcp->nconnections--;
- if(utcp->nconnections >= 65536) {
+ free(c);
+}
+
+static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t src, uint16_t dst) {
+ // Check whether this combination of src and dst is free
+
+ if(src) {
+ if(find_connection(utcp, src, dst)) {
+ errno = EADDRINUSE;
+ return NULL;
+ }
+ } else { // If src == 0, generate a random port number with the high bit set
+ if(utcp->nconnections >= 32767) {
errno = ENOMEM;
return NULL;
}
+ src = rand() | 0x8000;
+ while(find_connection(utcp, src, dst))
+ src++;
+ }
- // Need to reserve more memory?
+ // Allocate memory for the new connection
- if(utcp->nconnections >= utcp->nallocated) {
+ if(utcp->nconnections >= utcp->nallocated) {
+ if(!utcp->nallocated)
+ utcp->nallocated = 4;
+ else
utcp->nallocated *= 2;
- utcp->connections = realloc(utcp->connections, utcp->nallocated * sizeof *utcp->connections);
+ struct utcp_connection **new_array = realloc(utcp->connections, utcp->nallocated * sizeof *utcp->connections);
+ if(!new_array) {
+ errno = ENOMEM;
+ return NULL;
}
+ utcp->connections = new_array;
+ }
- c = utcp->connections[utcp->nconnections] = calloc(1, sizeof *c);
- c->src = utcp->nconnections++;
+ struct utcp_connection *c = calloc(1, sizeof *c);
+ if(!c) {
+ errno = ENOMEM;
+ return NULL;
}
+ // Fill in the details
+
+ c->src = src;
+ c->dst = dst;
c->snd.iss = rand();
c->snd.una = c->snd.iss;
c->snd.nxt = c->snd.iss + 1;
c->rcv.wnd = utcp->mtu;
c->utcp = utcp;
- return c;
-}
-static struct utcp_connection *find_connection(struct utcp *utcp, uint16_t src) {
- if(src < utcp->nconnections && utcp->connections[src])
- return utcp->connections[src];
+ // Add it to the sorted list of connections
- errno = EINVAL;
- return NULL;
-}
+ utcp->connections[utcp->nconnections++] = c;
+ qsort(utcp->connections, utcp->nconnections, sizeof *utcp->connections, compare);
-static void free_connection(struct utcp_connection *c) {
- if(!c)
- return;
- if(c->utcp->gap < 0 || c->src < c->utcp->gap)
- c->utcp->gap = c->src;
- c->utcp->connections[c->src] = NULL;
- free(c);
+ return c;
}
-struct utcp_connection *utcp_connect(struct utcp *utcp, void *data, size_t len, utcp_recv_t recv, void *priv) {
- struct utcp_connection *c = allocate_connection(utcp);
+struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv) {
+ struct utcp_connection *c = allocate_connection(utcp, 0, dst);
if(!c)
return NULL;
c->recv = recv;
- struct {
- struct hdr hdr;
- char data[len];
- } pkt;
+ struct hdr hdr;
- pkt.hdr.src = c->src;
- pkt.hdr.dst = 0;
- pkt.hdr.seq = c->snd.iss;
- pkt.hdr.ack = 0;
- pkt.hdr.ctl = SYN;
- pkt.hdr.wnd = c->rcv.wnd;
- memcpy(pkt.data, data, len);
+ hdr.src = c->src;
+ hdr.dst = c->dst;
+ hdr.seq = c->snd.iss;
+ hdr.ack = 0;
+ hdr.ctl = SYN;
+ hdr.wnd = c->rcv.wnd;
set_state(c, SYN_SENT);
- utcp->send(utcp, &pkt, sizeof pkt.hdr + len);
+ utcp->send(utcp, &hdr, sizeof hdr);
// Set timeout?
errno = EPIPE;
return -1;
}
+
+ if(!len)
+ return 0;
+
+ if(!data) {
+ errno = EFAULT;
+ return -1;
+ }
+
struct {
struct hdr hdr;
}
int utcp_recv(struct utcp *utcp, void *data, size_t len) {
+ if(!utcp) {
+ errno = EFAULT;
+ return -1;
+ }
+
+ if(!len)
+ return 0;
+
+ if(!data) {
+ errno = EFAULT;
+ return -1;
+ }
+
fprintf(stderr, "%p got: ", utcp);
print_packet(data, len);
return -1;
}
- struct utcp_connection *c = find_connection(utcp, hdr.dst);
+ //list_connections(utcp);
+
+ struct utcp_connection *c = find_connection(utcp, hdr.dst, hdr.src);
// Is it for a new connection?
if(hdr.ctl & RST)
return 0;
- if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept && (!utcp->pre_accept || utcp->pre_accept(utcp, data, len)) && (c = allocate_connection(utcp))) { // LISTEN
+ if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept && (!utcp->pre_accept || utcp->pre_accept(utcp, hdr.dst)) && (c = allocate_connection(utcp, hdr.dst, hdr.src))) { // LISTEN
// Return SYN+ACK
c->snd.wnd = hdr.wnd;
c->rcv.irs = hdr.seq;
- c->snd.iss = rand();
- c->snd.una = c->snd.iss;
- c->snd.nxt = c->snd.iss + 1;
c->rcv.nxt = c->rcv.irs + 1;
set_state(c, SYN_RECEIVED);
- hdr.dst = c->dst = hdr.src;
+ hdr.dst = c->dst;
hdr.src = c->src;
hdr.ack = c->rcv.irs + 1;
hdr.seq = c->snd.iss;
switch(c->state) {
case SYN_RECEIVED:
if(hdr.ack >= c->snd.una && hdr.ack <= c->snd.nxt)
- c->utcp->accept(c, NULL, 0);
+ c->utcp->accept(c, hdr.dst);
if(c->state != ESTABLISHED)
goto reset;
return 0;
}
-void utcp_shutdown(struct utcp_connection *c, int dir) {
+int utcp_shutdown(struct utcp_connection *c, int dir) {
+ if(!c) {
+ errno = EFAULT;
+ return -1;
+ }
+
if(c->reapable) {
fprintf(stderr, "Error: shutdown() called on closed connection %p\n", c);
- return;
+ errno = EBADF;
+ return -1;
}
// TODO: handle dir
switch(c->state) {
case CLOSED:
- return;
+ return 0;
case LISTEN:
case SYN_SENT:
set_state(c, CLOSED);
- return;
+ return 0;
case SYN_RECEIVED:
case ESTABLISHED:
break;
case FIN_WAIT_1:
case FIN_WAIT_2:
- return;
+ return 0;
case CLOSE_WAIT:
set_state(c, LAST_ACK);
break;
case CLOSING:
case LAST_ACK:
case TIME_WAIT:
- return;
+ return 0;
}
// Send FIN
c->snd.nxt += 1;
c->utcp->send(c->utcp, &hdr, sizeof hdr);
+ return 0;
}
-void utcp_close(struct utcp_connection *c) {
- utcp_shutdown(c, SHUT_RDWR);
+int utcp_close(struct utcp_connection *c) {
+ if(utcp_shutdown(c, SHUT_RDWR))
+ return -1;
c->reapable = true;
+ return 0;
}
-void utcp_abort(struct utcp_connection *c) {
+int utcp_abort(struct utcp_connection *c) {
+ if(!c) {
+ errno = EFAULT;
+ return -1;
+ }
+
if(c->reapable) {
fprintf(stderr, "Error: abort() called on closed connection %p\n", c);
- return;
+ errno = EBADF;
+ return -1;
}
c->reapable = true;
switch(c->state) {
case CLOSED:
- return;
+ return 0;
case LISTEN:
case SYN_SENT:
case CLOSING:
case LAST_ACK:
case TIME_WAIT:
set_state(c, CLOSED);
- return;
+ return 0;
case SYN_RECEIVED:
case ESTABLISHED:
hdr.ctl = RST;
c->utcp->send(c->utcp, &hdr, sizeof hdr);
+ return 0;
}
void utcp_timeout(struct utcp *utcp) {
struct utcp_connection;
#endif
-typedef bool (*utcp_pre_accept_t)(struct utcp *utcp, void *data, size_t len);
-typedef void (*utcp_accept_t)(struct utcp_connection *utcp_connection, void *data, size_t len);
+typedef bool (*utcp_pre_accept_t)(struct utcp *utcp, uint16_t port);
+typedef void (*utcp_accept_t)(struct utcp_connection *utcp_connection, uint16_t port);
typedef int (*utcp_send_t)(struct utcp *utcp, void *data, size_t len);
typedef int (*utcp_recv_t)(struct utcp_connection *connection, void *data, size_t len);
extern struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv);
extern void utcp_exit(struct utcp *utcp);
-extern struct utcp_connection *utcp_connect(struct utcp *utcp, void *data, size_t len, utcp_recv_t recv, void *priv);
+extern struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t port, utcp_recv_t recv, void *priv);
extern void utcp_accept(struct utcp_connection *utcp, utcp_recv_t recv, void *priv);
extern int utcp_send(struct utcp_connection *connection, void *data, size_t len);
extern int utcp_recv(struct utcp *utcp, void *data, size_t len);
-extern void utcp_close(struct utcp_connection *connection);
-extern void utcp_abort(struct utcp_connection *connection);
-extern void utcp_shutdown(struct utcp_connection *connection, int how);
+extern int utcp_close(struct utcp_connection *connection);
+extern int utcp_abort(struct utcp_connection *connection);
+extern int utcp_shutdown(struct utcp_connection *connection, int how);
extern void utcp_timeout(struct utcp *utcp);
#endif