Skip to content

Commit ab20bf0

Browse files
committed
reduce code needed in evt-tls examples
** after further research of `openssl` and the `libuv` integration implementations from https://github.com/deleisha/evt-tls and another from https://github.com/openziti/tlsuv both will need major rewriting based on how callbacks are structured, and most likely eventually dropped altogether, only now used for working reference for something custom. multiple issues showing up with `tcp-echo-server.c` example, not consistently working. **
1 parent 74fa004 commit ab20bf0

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

examples/tcp-echo-server.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
void new_connection(uv_stream_t *socket) {
77
string data = stream_read(socket);
8+
printf(CLR_LN"%s\n", data);
89
if (data)
910
stream_write(socket, data);
1011
}
@@ -16,8 +17,8 @@ int uv_main(int argc, char **argv) {
1617
string_t host = is_secure ? "tls://127.0.0.1:%d" : "0.0.0.0:%d";
1718

1819
if (snprintf(addr, sizeof(addr), host, DEFAULT_PORT)) {
19-
if (is_secure)
20-
use_certificate(nullptr, 0);
20+
//if (is_secure)
21+
// use_certificate(nullptr, 0);
2122

2223
server = stream_bind(addr, 0);
2324
while (server) {

examples/uv_tls_client.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,17 @@ int uv_main(int argc, char **argv) {
6969
yield();
7070
uv_loop_t *loop = asio_loop();
7171
//free on uv_close_cb via uv_tls_close call
72-
uv_tcp_t *client = malloc(sizeof *client);
73-
uv_tcp_init(loop, client);
72+
uv_tcp_t *client = tcp_create();
7473
int port = 7000;
7574

7675
evt_ctx_t ctx;
7776

7877
evt_ctx_init_ex(&ctx, cert_file(), pkey_file());
7978
evt_ctx_set_nio(&ctx, NULL, uv_tls_writer);
8079

81-
struct sockaddr_in conn_addr;
82-
uv_ip4_addr("127.0.0.1", port, &conn_addr);
83-
8480
uv_connect_t req;
8581
req.data = &ctx;
86-
uv_tcp_connect(&req, client,(const struct sockaddr*)&conn_addr, on_connect);
82+
uv_tcp_connect(&req, client, sockaddr("127.0.0.1", port), on_connect);
8783

8884
uv_run(loop, UV_RUN_DEFAULT);
8985
evt_ctx_free(&ctx);

examples/uv_tls_server.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,16 @@ int uv_main(int argc, char **argv) {
5050
uv_loop_t *loop = asio_loop();
5151
int port = 7000, r = 0;
5252
evt_ctx_t ctx;
53-
struct sockaddr_in bind_local;
5453

5554
evt_ctx_init_ex(&ctx, cert_file(), pkey_file());
5655
evt_ctx_set_nio(&ctx, NULL, uv_tls_writer);
5756

58-
uv_tcp_t listener_local;
59-
uv_tcp_init(loop, &listener_local);
60-
listener_local.data = &ctx;
61-
uv_ip4_addr("127.0.0.1", port, &bind_local);
62-
if ((r = uv_tcp_bind(&listener_local, (struct sockaddr*)&bind_local, 0)))
57+
uv_tcp_t *listener_local = tcp_create();
58+
listener_local->data = &ctx;
59+
if ((r = uv_tcp_bind(listener_local, sockaddr("127.0.0.1", port), 0)))
6360
fprintf( stderr, "bind: %s\n", uv_strerror(r));
6461

65-
if ((r = uv_listen((uv_stream_t*)&listener_local, 1024, on_connect_cb)))
62+
if ((r = uv_listen((uv_stream_t*)listener_local, 1024, on_connect_cb)))
6663
fprintf( stderr, "listen: %s\n", uv_strerror(r));
6764
printf("Listening on %d\n", port);
6865
uv_run(loop, UV_RUN_DEFAULT);

include/asio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ C_API u32 delay(u32 ms);
527527
C_API void tls_generator_set(uv_tls_t *);
528528
C_API void tls_yielding(uv_tls_t *, ssize_t nread, bool is_err);
529529
C_API uv_tls_t *tls_handle(uv_stream_t *);
530+
C_API sockaddr_t *sockaddr(string_t host, int port);
530531

531532
#ifdef _WIN32
532533
#define _BIO_MODE_R(flags) (((flags) & PKCS7_BINARY) ? "rb" : "r")

src/asio.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ int stream_write(uv_stream_t *handle, string_t text) {
14801480
return RAII_ERR;
14811481

14821482
uv_args_t *uv_args = (uv_args_t *)uv_handle_get_data(handler(handle));
1483-
if (is_defined(uv_args) || is_tls(handle)) {
1483+
if (is_defined(uv_args)) {
14841484
if ((!uv_is_writable(handle) || uv_is_closing(handler(handle))))
14851485
return UV_EBADF;
14861486

@@ -1669,12 +1669,17 @@ static string stream_get(uv_stream_t *handle) {
16691669
return yield_for(gen).char_ptr;
16701670
}
16711671

1672+
sockaddr_t *sockaddr(string_t host, int port) {
1673+
uv_args_t *uv_args = uv_arguments(0, true);
1674+
return (sockaddr_t *)asio_sockaddr(host, port, uv_args->dns->in6, uv_args->dns->in4);
1675+
}
1676+
16721677
int stream_shutdown(uv_stream_t *handle) {
16731678
if (is_empty(handle))
16741679
return coro_err_code();
16751680

16761681
uv_args_t *uv_args = (uv_args_t *)uv_handle_get_data(handler(handle));
1677-
if (is_defined(uv_args) || is_tls(handle)) {
1682+
if (is_defined(uv_args)) {
16781683
uv_args->args[0].object = handle;
16791684
} else {
16801685
uv_args = uv_arguments(1, true);
@@ -2666,10 +2671,10 @@ RAII_INLINE bool is_tcp(void_t self) {
26662671

26672672
RAII_INLINE bool is_tls(uv_stream_t *self) {
26682673
if (!self)
2669-
return false;
2674+
return false;
26702675

2671-
void_t check = uv_handle_get_data(handler(self));
2672-
return is_defined(check) && ((uv_args_t *)check)->bind_type == UV_TLS;
2676+
uv_args_t *uv_args = (uv_args_t *)uv_handle_get_data(handler(self));
2677+
return is_defined(uv_args) && uv_args->bind_type == UV_TLS;
26732678
}
26742679

26752680
RAII_INLINE bool is_udp(void_t self) {

0 commit comments

Comments
 (0)