Skip to content

Commit 7533cc9

Browse files
committed
Add local_addr host function
1 parent 6a98883 commit 7533cc9

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
target_name: lunatic
1616
asset_name: lunatic-linux-amd64.tar.gz
1717
content_type: application/gzip
18-
- os: macos-11
18+
- os: my-macbook
1919
target_name: lunatic
2020
asset_name: lunatic-macos-amd64.tar.gz
2121
content_type: application/gzip

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lunatic-runtime"
3-
version = "0.7.3"
3+
version = "0.7.4"
44
authors = ["Bernard Kolobara <bernard@lunatic.solutions>"]
55
edition = "2018"
66
description = "An actor platform built on WebAssembly"

RELEASES.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
---
44

5+
## v0.7.4
6+
7+
Released 2021-01-15.
8+
9+
### Changes
10+
11+
Adds `local_addr` host function for TCP listeners.
12+
Adds `version` host function.
13+
Adds check if processes are spawned before the Wasm module was initialized.
14+
15+
Process traps are now logged by default to stdout.
16+
517
## v0.7.0
618

719
Released 2021-12-01.

src/api/networking.rs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ pub(crate) fn register(
8787
drop_tcp_listener,
8888
namespace_filter,
8989
)?;
90+
link_if_match(
91+
linker,
92+
"lunatic::networking",
93+
"local_addr",
94+
FuncType::new([ValType::I64, ValType::I32], [ValType::I32]),
95+
local_addr,
96+
namespace_filter,
97+
)?;
9098
link_async3_if_match(
9199
linker,
92100
"lunatic::networking",
@@ -417,7 +425,7 @@ fn tcp_bind(
417425
//% Drops the TCP listener resource.
418426
//%
419427
//% Traps:
420-
//% * If the DNS iterator ID doesn't exist.
428+
//% * If the TCP listener ID doesn't exist.
421429
fn drop_tcp_listener(mut caller: Caller<ProcessState>, tcp_listener_id: u64) -> Result<(), Trap> {
422430
caller
423431
.data_mut()
@@ -428,6 +436,53 @@ fn drop_tcp_listener(mut caller: Caller<ProcessState>, tcp_listener_id: u64) ->
428436
Ok(())
429437
}
430438

439+
//% lunatic::networking::local_addr(tcp_listener_id: i64, id_u64_ptr: u32) -> i64
440+
//%
441+
//% Returns the local address that this listener is bound to as an DNS iterator with just one
442+
//% element.
443+
//% * 0 on success - The local address that this listener is bound to is returned as an DNS
444+
//% iterator with just one element and written to **id_ptr**.
445+
//%
446+
//% * 1 on error - The error ID is written to **id_u64_ptr**
447+
//%
448+
//% Traps:
449+
//% * If the tcp listener ID doesn't exist.
450+
//% * If **peer_socket_addr_id_ptr** is outside the memory.
451+
fn local_addr(
452+
mut caller: Caller<ProcessState>,
453+
tcp_listener_id: u64,
454+
id_u64_ptr: u32,
455+
) -> Result<u32, Trap> {
456+
let tcp_listener = caller
457+
.data()
458+
.resources
459+
.tcp_listeners
460+
.get(tcp_listener_id)
461+
.or_trap("lunatic::network::local_addr: listener ID doesn't exist")?;
462+
let (dns_iter_or_error_id, result) = match tcp_listener.local_addr() {
463+
Ok(socket_addr) => {
464+
let dns_iter_id = caller
465+
.data_mut()
466+
.resources
467+
.dns_iterators
468+
.add(DnsIterator::new(vec![socket_addr].into_iter()));
469+
(dns_iter_id, 0)
470+
}
471+
Err(error) => (caller.data_mut().errors.add(error.into()), 1),
472+
};
473+
474+
let memory = get_memory(&mut caller)?;
475+
memory
476+
.write(
477+
&mut caller,
478+
id_u64_ptr as usize,
479+
&dns_iter_or_error_id.to_le_bytes(),
480+
)
481+
.or_trap("lunatic::network::local_addr")?;
482+
483+
Ok(result)
484+
}
485+
431486
//% lunatic::networking::tcp_accept(
432487
//% listener_id: u64,
433488
//% id_u64_ptr: u32,
@@ -447,7 +502,7 @@ fn drop_tcp_listener(mut caller: Caller<ProcessState>, tcp_listener_id: u64) ->
447502
fn tcp_accept(
448503
mut caller: Caller<ProcessState>,
449504
listener_id: u64,
450-
id_ptr: u32,
505+
id_u64_ptr: u32,
451506
socket_addr_id_ptr: u32,
452507
) -> Box<dyn Future<Output = Result<u32, Trap>> + Send + '_> {
453508
Box::new(async move {
@@ -475,7 +530,7 @@ fn tcp_accept(
475530
memory
476531
.write(
477532
&mut caller,
478-
id_ptr as usize,
533+
id_u64_ptr as usize,
479534
&tcp_stream_or_error_id.to_le_bytes(),
480535
)
481536
.or_trap("lunatic::networking::tcp_accept")?;

wat/all_imports.wat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
(import "lunatic::networking" "resolve_next" (func (param i64 i32 i32 i32 i32 i32) (result i32)))
2525
(import "lunatic::networking" "tcp_bind" (func (param i32 i32 i32 i32 i32 i32) (result i32)))
2626
(import "lunatic::networking" "drop_tcp_listener" (func (param i64)))
27+
(import "lunatic::networking" "local_addr" (func (param i64 i32) (result i32)))
2728
(import "lunatic::networking" "tcp_accept" (func (param i64 i32 i32) (result i32)))
2829
(import "lunatic::networking" "tcp_connect" (func (param i32 i32 i32 i32 i32 i32 i32) (result i32)))
2930
(import "lunatic::networking" "drop_tcp_stream" (func (param i64)))

0 commit comments

Comments
 (0)