Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement tlsuv_stream flow control #182

Merged
merged 6 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/tlsuv/tlsuv.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ int tlsuv_stream_connect(uv_connect_t *req, tlsuv_stream_t *clt, const char *hos

int tlsuv_stream_connect_addr(uv_connect_t *req, tlsuv_stream_t *clt, const struct addrinfo *addr, uv_connect_cb cb);

int tlsuv_stream_read(tlsuv_stream_t *clt, uv_alloc_cb, uv_read_cb);
int tlsuv_stream_read_start(tlsuv_stream_t *clt, uv_alloc_cb alloc_cb, uv_read_cb read_cb);
int tlsuv_stream_read_stop(tlsuv_stream_t *clt);

int tlsuv_stream_write(uv_write_t *req, tlsuv_stream_t *clt, uv_buf_t *buf, uv_write_cb cb);

Expand Down
2 changes: 1 addition & 1 deletion sample/sample-cf.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void on_connect(uv_connect_t *cr, int status) {
}

tlsuv_stream_t *mbed = (tlsuv_stream_t *) cr->handle;
tlsuv_stream_read(mbed, alloc, on_data);
tlsuv_stream_read_start(mbed, alloc, on_data);

uv_write_t *wr = malloc(sizeof(uv_write_t));
char req[] = "GET " PATH " HTTP/1.1\r\n"
Expand Down
2 changes: 1 addition & 1 deletion sample/sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void on_connect(uv_connect_t *cr, int status) {
}

tlsuv_stream_t *mbed = (tlsuv_stream_t *) cr->handle;
tlsuv_stream_read(mbed, alloc, on_data);
tlsuv_stream_read_start(mbed, alloc, on_data);

uv_write_t *wr = malloc(sizeof(uv_write_t));
char req[] = "GET / HTTP/1.1\r\n"
Expand Down
11 changes: 6 additions & 5 deletions src/tls_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ static int tls_write(uv_link_t *link, uv_link_t *source, const uv_buf_t bufs[],
static void tls_close(uv_link_t *link, uv_link_t *source, uv_link_close_cb cb);

static const uv_link_methods_t tls_methods = {
.close = tls_close,
.read_start = tls_read_start,
.write = tls_write,
.alloc_cb_override = tls_alloc,
.read_cb_override = tls_read_cb
.close = tls_close,
.read_start = tls_read_start,
.read_stop = uv_link_default_read_stop,
.write = tls_write,
.alloc_cb_override = tls_alloc,
.read_cb_override = tls_read_cb
};

typedef struct tls_link_write_s {
Expand Down
48 changes: 37 additions & 11 deletions src/tlsuv.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@
#define TLSUV_VERS "<unknown>"
#endif

static void tls_debug_f(void *ctx, int level, const char *file, int line, const char *str);
static void tcp_connect_cb(uv_connect_t* req, int status);
static int mbed_ssl_send(void* ctx, const uint8_t *buf, size_t len);

static const uv_link_methods_t mbed_methods = {
.close = uv_link_default_close,
.read_start = uv_link_default_read_start,
.write = uv_link_default_write,
.alloc_cb_override = uv_link_default_alloc_cb_override,
.read_cb_override = uv_link_default_read_cb_override,
.close = uv_link_default_close,
.read_start = uv_link_default_read_start,
.read_stop = uv_link_default_read_stop,
.write = uv_link_default_write,
.alloc_cb_override = uv_link_default_alloc_cb_override,
.read_cb_override = uv_link_default_read_cb_override,
};

static tls_context *DEFAULT_TLS = NULL;
Expand Down Expand Up @@ -74,6 +73,8 @@

uv_link_init((uv_link_t *) clt, &mbed_methods);
clt->tls = tls != NULL ? tls : get_default_tls();
clt->read_cb = NULL;
clt->alloc_cb = NULL;

return 0;
}
Expand Down Expand Up @@ -131,6 +132,7 @@
}

if (status == TLS_HS_COMPLETE) {
tlsuv_stream_read_stop(stream);
req->cb(req, 0);
} else if (status == TLS_HS_ERROR) {
UM_LOG(WARN, "handshake failed: %s", tls_link->engine->strerror(tls_link->engine));
Expand Down Expand Up @@ -198,13 +200,37 @@
return clt->socket->connect((tlsuv_src_t *) clt->socket, host, portstr, on_src_connect, clt);
}

int tlsuv_stream_read(tlsuv_stream_t *clt, uv_alloc_cb alloc_cb, uv_read_cb read_cb) {
clt->alloc_cb = (uv_link_alloc_cb) alloc_cb;
clt->read_cb = (uv_link_read_cb) read_cb;
return 0;
int tlsuv_stream_read_start(tlsuv_stream_t *clt, uv_alloc_cb alloc_cb, uv_read_cb read_cb) {
if (clt == NULL || alloc_cb == NULL || read_cb == NULL) {
return UV_EINVAL;
}

if (clt->read_cb) {
return UV_EALREADY;
}

int rc = uv_link_read_start((uv_link_t *)clt);
if (rc == 0) {
clt->alloc_cb = (uv_link_alloc_cb) alloc_cb;
clt->read_cb = (uv_link_read_cb) read_cb;
}
return rc;
}

int tlsuv_stream_read_stop(tlsuv_stream_t *clt) {
if (clt == NULL) {
return UV_EINVAL;
}

if (clt->read_cb == NULL) {
return 0;
}
clt->read_cb = NULL;
clt->alloc_cb = NULL;
return uv_link_read_stop((uv_link_t *) clt);
}

static void on_mbed_link_write(uv_link_t* l, int status, void *ctx) {

Check warning on line 233 in src/tlsuv.c

View workflow job for this annotation

GitHub Actions / build (windows, mbedtls)

'l': unreferenced formal parameter

Check warning on line 233 in src/tlsuv.c

View workflow job for this annotation

GitHub Actions / build (windows, openssl)

'l': unreferenced formal parameter
uv_write_t *wr = ctx;
wr->cb(wr, status);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ set(test_srcs
http_tests.cpp
ws_tests.cpp
engine_tests.cpp
uv_mbed_tests.cpp
stream_tests.cpp
compression_tests.cpp
key_tests.cpp
)
Expand Down Expand Up @@ -80,4 +80,4 @@ add_test(key_tests all_tests [key])
add_test(engine_tests all_tests [engine])
add_test(http_tests all_tests [http])
add_test(ws_tests all_tests [websocket])
add_test(uv_mbed all_tests [uv-mbed])
add_test(uv_mbed all_tests [stream])
1 change: 1 addition & 0 deletions tests/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The test server opens the following endpoints:
| 8080 | HTTP test endpoint (httpbin API) |
| 8443 | HTTPS test endpoint (httpbin API) |
| 9443 | client auth endpoint: checks supplied client certificate |
| 7443 | TLS echo server |


Start test server:
Expand Down
Loading
Loading