Skip to content

Commit

Permalink
[Lib] fix Socket::get_peer & Socket::get_local (#88)
Browse files Browse the repository at this point in the history
* [Lib] fix `Socket::get_peer` & `Socket::get_local`

* [Misc] Bump to 0.5.1

* [CI] add 0.12

* [Lib] compatible with version 0.12.1 and earlier ver.
  • Loading branch information
L-jasmine authored Jul 24, 2023
1 parent 7e49c11 commit 66df13d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
84 changes: 84 additions & 0 deletions .github/workflows/example_0.12.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: examples 0.12

on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'info'
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Install apt-get packages
run: |
sudo ACCEPT_EULA=Y apt-get update
sudo ACCEPT_EULA=Y apt-get upgrade
sudo apt-get install wget git curl software-properties-common build-essential
- name: Install Rust target
run: |
rustup target add wasm32-wasi
- name: Install WasmEdge
run: |
VERSION=0.12.1
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | sudo bash -s -- -e all --version=$VERSION --tf-version=$VERSION --tf-deps-version=$VERSION --tf-tools-version=$VERSION --image-version=$VERSION -p /usr/local
# Disable this example due to it relies on wasmedge_http_req, which is a cyclic dependence
# - name: HTTP client example
# run: |
# cd examples/http_client/
# cargo build --target wasm32-wasi --release
# wasmedge target/wasm32-wasi/release/http_client.wasm

- name: HTTP async client example
run: |
cd examples/nonblock_http_client/
cargo build --target wasm32-wasi --release
wasmedge target/wasm32-wasi/release/nonblock_http_client.wasm
- name: HTTP server example
run: |
cd examples/http_server/
cargo build --target wasm32-wasi --release
nohup wasmedge target/wasm32-wasi/release/http_server.wasm &
echo $! > wasmedge.pid
wasmedge_pid=$(cat wasmedge.pid)
sleep 5
echo "fds:"
ls /proc/$wasmedge_pid/fd
resp=$(curl -X POST http://127.0.0.1:1234 -d "name=WasmEdge")
echo "Server response is $resp"
resp=$(curl -X POST http://127.0.0.1:1234 -d "name=WasmEdge")
resp=$(curl -X POST http://127.0.0.1:1234 -d "name=WasmEdge")
resp=$(curl -X POST http://127.0.0.1:1234 -d "name=WasmEdge")
resp=$(curl -X POST http://127.0.0.1:1234 -d "name=WasmEdge")
resp=$(curl -X POST http://127.0.0.1:1234 -d "name=WasmEdge")
echo "after 6 request, fds:"
ls /proc/$wasmedge_pid/fd
kill -9 $wasmedge_pid
rm wasmedge.pid
- name: UDP Socket Example
run: |
cargo build --target wasm32-wasi --example=udp_socket --release --no-default-features --features=built-in-dns
wasmedge target/wasm32-wasi/release/examples/udp_socket.wasm
- name: DNS Example
run: |
cargo build --target wasm32-wasi --example=get_addrinfo --release
wasmedge target/wasm32-wasi/release/examples/get_addrinfo.wasm
- name: ToSocketAddrs Example
run: |
cargo build --target wasm32-wasi --example=socket_addr --release
wasmedge target/wasm32-wasi/release/examples/socket_addr.wasm
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmedge_wasi_socket"
version = "0.5.0"
version = "0.5.1"
authors = ["Yi <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
2 changes: 2 additions & 0 deletions examples/tcp_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ fn main() -> std::io::Result<()> {
let port = std::env::var("PORT").unwrap_or("1234".to_string());
println!("connect to 127.0.0.1:{}", port);
let mut stream = TcpStream::connect(format!("127.0.0.1:{}", port))?;
println!("local address {}", stream.local_addr().unwrap());
println!("peer address {}", stream.peer_addr().unwrap());
println!("sending hello message...");
stream.write(b"hello")?;
stream.shutdown(Shutdown::Both)?;
Expand Down
8 changes: 4 additions & 4 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,10 @@ impl Socket {
if res != 0 {
Err(io::Error::from_raw_os_error(res as i32))
} else {
if addr_type == 4 {
if addr_type == 4 || addr_type == AddressFamily::Inet4 as u8 as u32 {
let ip_addr = Ipv4Addr::new(addr_buf[0], addr_buf[1], addr_buf[2], addr_buf[3]);
Ok(SocketAddr::V4(SocketAddrV4::new(ip_addr, port as u16)))
} else if addr_type == 6 {
} else if addr_type == 6 || addr_type == AddressFamily::Inet6 as u8 as u32 {
let ip_addr = Ipv6Addr::from(addr_buf);
Ok(SocketAddr::V6(SocketAddrV6::new(
ip_addr,
Expand Down Expand Up @@ -848,10 +848,10 @@ impl Socket {
if res != 0 {
Err(io::Error::from_raw_os_error(res as i32))
} else {
if addr_type == 4 {
if addr_type == 4 || addr_type == AddressFamily::Inet4 as u8 as u32 {
let ip_addr = Ipv4Addr::new(addr_buf[0], addr_buf[1], addr_buf[2], addr_buf[3]);
Ok(SocketAddr::V4(SocketAddrV4::new(ip_addr, port as u16)))
} else if addr_type == 6 {
} else if addr_type == 6 || addr_type == AddressFamily::Inet6 as u8 as u32 {
let ip_addr = Ipv6Addr::from(addr_buf);
Ok(SocketAddr::V6(SocketAddrV6::new(
ip_addr,
Expand Down

0 comments on commit 66df13d

Please sign in to comment.