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

Add support for TLS streams #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
[submodule "subprojects/lolibc"]
path = subprojects/lolibc
url = http://github.com/dimkr/lolibc
[submodule "src/mbedtls"]
path = src/mbedtls
url = https://github.com/ARMmbed/mbedtls
[submodule "src/curl"]
path = src/curl
url = https://github.com/curl/curl
9 changes: 9 additions & 0 deletions docs/ssl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{$ssl.client 3 localhost 0}

*ssl.client* creates a TLS client stream, using the file descriptor of a TCP client stream. The optional third argument determines whether or not certificate validation is enabled.

**If the third argument is omitted**, certificate validation is enabled.

{$ssl.server server.crt server.key}

*ssl.server* creates a TLS server.
22 changes: 18 additions & 4 deletions examples/httpd.b6b
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{#
This file is part of b6b.

Copyright 2017, 2018 Dima Krasner
Copyright 2017, 2018, 2022 Dima Krasner

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -206,19 +206,33 @@
{$return}
}}

{$http.loop add $client $on_request {} $on_error}
{$proc _ {
{$try {
{$local ssl_client [$ssl_server accept [$. fd]]}
{$http.loop add $ssl_client $on_request {} $on_error}
} {
{$http.loop remove $.}
{$. close}
}}
} $client}

{$spawn {
{$_}
}}
}}
}}

{$proc on_banner {
{$global http.banner [$choice $http.banners]}
}}

{$if [$!= [$list.len $@] 4] {
{$stderr writeln {Usage: httpd addr port backlog}}
{$if [$!= [$list.len $@] 6] {
{$stderr writeln {Usage: httpd addr port backlog cert key}}
{$exit 1}
}}

{$global ssl_server [$ssl.server $4 $5]}

{$http.loop add [$signal $SIGTERM $SIGINT] $on_term {} {}}
{$http.loop add [$inet.server tcp $1 $2 $3] $on_clients {} {}}
{# periodically change the server banner}
Expand Down
3 changes: 2 additions & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is part of b6b.
#
# Copyright 2017 Dima Krasner
# Copyright 2017, 2022 Dima Krasner
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,3 +19,4 @@ option('with_miniz', type: 'boolean')
option('with_linenoise', type: 'boolean')
option('with_valgrind', type: 'boolean', value: false)
option('optimistic_alloc', type: 'boolean')
option('with_mbedtls', type: 'boolean')
30 changes: 1 addition & 29 deletions src/b6b_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,6 @@ int b6b_offload(struct b6b_interp *interp,
void (*fn)(void *),
void *arg)
{
struct b6b_thread *t;
int swap, pending = 0;
struct b6b_offload_thread *offth = NULL;

if (!b6b_threaded(interp))
Expand Down Expand Up @@ -519,31 +517,6 @@ int b6b_offload(struct b6b_interp *interp,
if (!b6b_threaded(interp))
break;

/* check whether there is at least one thread that waits for us to
* complete the blocking operation and at least one thread which
* doesn't */
pending = swap = 0;
b6b_thread_foreach(&interp->threads, t) {
if (t != interp->fg) {
if (b6b_thread_blocked(t)) {
pending = 1;
if (swap)
break;
}
else {
swap = 1;
if (pending)
break;
}
}
}

/* if all other threads are blocked by us, we want to stop this
* polling loop since all they do is repeatedly calling
* b6b_yield() */
if (!swap)
break;

/* if all other threads exited during the previous iterations of
* this loop, we can (and should) block */
if (!b6b_yield(interp))
Expand All @@ -560,8 +533,7 @@ int b6b_offload(struct b6b_interp *interp,
* the next statement of the current thread also calls
* b6b_offload_start(), that thread will spend more time in its
* b6b_offload_ready() loop */
if (pending)
b6b_yield(interp);
b6b_yield(interp);
}

return 1;
Expand Down
Loading