]> git.meshlink.io Git - utcp/commitdiff
Introduce utcp_connect_ex().
authorGuus Sliepen <guus@meshlink.io>
Mon, 3 Jul 2017 20:31:50 +0000 (22:31 +0200)
committerGuus Sliepen <guus@meshlink.io>
Mon, 3 Jul 2017 20:34:22 +0000 (22:34 +0200)
This takes an extra argument "flags", which can be used to change the type
of connection to create. Possible flags are UTCP_INORDER, UTCP_RELIABLE and
UTCP_FRAMED, which can be bitwise-or'd together, or UTCP_TCP or UTCP_UDP as
shortcuts for what is assumed will be common combinations of those flags.

test.c
utcp.c
utcp.h
utcp_priv.h

diff --git a/test.c b/test.c
index 4a1bf16e3bff39ca60ac67472b3c4e0c8f9f0cd5..4e65c1046344ab692d3489ef0f12cd3e763a9715 100644 (file)
--- a/test.c
+++ b/test.c
@@ -127,6 +127,8 @@ int main(int argc, char *argv[]) {
 
        bool server = argc == 2;
        bool connected = false;
 
        bool server = argc == 2;
        bool connected = false;
+       uint32_t flags = UTCP_TCP;
+       size_t read_size = 102400;
 
        if(getenv("DROPIN")) dropin = atof(getenv("DROPIN"));
        if(getenv("DROPOUT")) dropout = atof(getenv("DROPOUT"));
 
        if(getenv("DROPIN")) dropin = atof(getenv("DROPIN"));
        if(getenv("DROPOUT")) dropout = atof(getenv("DROPOUT"));
@@ -134,6 +136,8 @@ int main(int argc, char *argv[]) {
        if(getenv("DROPTO")) dropto = atoi(getenv("DROPTO"));
        if(getenv("REORDER")) reorder = atof(getenv("REORDER"));
        if(getenv("REORDER_DIST")) reorder_dist = atoi(getenv("REORDER_DIST"));
        if(getenv("DROPTO")) dropto = atoi(getenv("DROPTO"));
        if(getenv("REORDER")) reorder = atof(getenv("REORDER"));
        if(getenv("REORDER_DIST")) reorder_dist = atoi(getenv("REORDER_DIST"));
+       if(getenv("FLAGS")) flags = atoi(getenv("FLAGS"));
+       if(getenv("READ_SIZE")) read_size = atoi(getenv("READ_SIZE"));
 
        char *reference_filename = getenv("REFERENCE");
        if(reference_filename)
 
        char *reference_filename = getenv("REFERENCE");
        if(reference_filename)
@@ -175,7 +179,7 @@ int main(int argc, char *argv[]) {
        utcp_set_user_timeout(u, 10);
 
        if(!server)
        utcp_set_user_timeout(u, 10);
 
        if(!server)
-               c = utcp_connect(u, 1, do_recv, NULL);
+               c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
 
        struct pollfd fds[2] = {
                {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
 
        struct pollfd fds[2] = {
                {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
@@ -189,6 +193,8 @@ int main(int argc, char *argv[]) {
                size_t max = c ? utcp_get_sndbuf_free(c) : 0;
                if(max > sizeof buf)
                        max = sizeof buf;
                size_t max = c ? utcp_get_sndbuf_free(c) : 0;
                if(max > sizeof buf)
                        max = sizeof buf;
+               if(max > read_size)
+                       max = read_size;
 
                int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
 
 
                int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
 
diff --git a/utcp.c b/utcp.c
index 0cb68b9da7195495602c1c196c835f2cab46d7b2..8143e0e8eae20906e974d51e4635f34abd2fda87 100644 (file)
--- a/utcp.c
+++ b/utcp.c
@@ -378,11 +378,14 @@ static void stop_retransmit_timer(struct utcp_connection *c) {
        debug("timeout cleared\n");
 }
 
        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;
 
        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;
 
        c->recv = recv;
        c->priv = priv;
 
@@ -407,6 +410,10 @@ struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_
        return c;
 }
 
        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]);
 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]);
diff --git a/utcp.h b/utcp.h
index 334e0bffc7ae2b20ac4b226f14218577b98bb7f1..8fcb28a0227a26086f574ecfaec185992f23f142 100644 (file)
--- a/utcp.h
+++ b/utcp.h
@@ -33,7 +33,8 @@ struct utcp {
 
 struct utcp_connection {
        void *priv;
 
 struct utcp_connection {
        void *priv;
-       struct utcp *utcp;
+       struct utcp *const utcp;
+       const uint32_t flags;
 };
 #else
 struct utcp;
 };
 #else
 struct utcp;
@@ -44,6 +45,14 @@ struct utcp_connection;
 #define UTCP_SHUT_WR 1
 #define UTCP_SHUT_RDWR 2
 
 #define UTCP_SHUT_WR 1
 #define UTCP_SHUT_RDWR 2
 
+#define UTCP_ORDERED 1
+#define UTCP_RELIABLE 2
+#define UTCP_FRAMED 4
+#define UTCP_DROP_LATE 8
+
+#define UTCP_TCP 3
+#define UTCP_UDP 0
+
 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 bool (*utcp_pre_accept_t)(struct utcp *utcp, uint16_t port);
 typedef void (*utcp_accept_t)(struct utcp_connection *utcp_connection, uint16_t port);
 
@@ -55,6 +64,7 @@ typedef void (*utcp_poll_t)(struct utcp_connection *connection, 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 *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_ex(struct utcp *utcp, uint16_t port, utcp_recv_t recv, void *priv, uint32_t flags);
 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 ssize_t utcp_send(struct utcp_connection *connection, const void *data, size_t len);
 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 ssize_t utcp_send(struct utcp_connection *connection, const void *data, size_t len);
index 143ed60191746d36cc39b39ee5a27f7b8420ba7b..fa706bcf84bcad5eed5e825bf3c7bc671cd51542 100644 (file)
@@ -97,6 +97,7 @@ struct sack {
 struct utcp_connection {
        void *priv;
        struct utcp *utcp;
 struct utcp_connection {
        void *priv;
        struct utcp *utcp;
+       uint32_t flags;
 
        bool reapable;
 
 
        bool reapable;