Skip to content

Commit 6e41968

Browse files
authored
Merge pull request #24 from reflyable/udp+dns
Add udp and dns support 添加udp和dns支持
2 parents a1b4664 + e6a61cf commit 6e41968

File tree

20 files changed

+559
-17
lines changed

20 files changed

+559
-17
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"apps/fs/shell",
99
"apps/net/echoserver",
1010
"apps/net/httpclient",
11+
"apps/net/udpserver",
1112
"apps/net/httpserver",
1213
"apps/task/parallel",
1314
"apps/task/sleep",

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ArceOS was inspired a lot by [Unikraft](https://github.com/unikraft/unikraft).
1717
* [x] Multi-thread
1818
* [x] Cooperative/preemptive scheduler
1919
* [x] VirtIO net/blk/gpu drivers
20-
* [x] TCP net stack using [smoltcp](https://github.com/smoltcp-rs/smoltcp)
20+
* [x] TCP/UDP net stack using [smoltcp](https://github.com/smoltcp-rs/smoltcp)
2121
* [x] Synchronization/Mutex
2222
* [x] SMP scheduling with single run queue
2323
* [x] File system

apps/net/httpclient/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
name = "arceos-httpclient"
33
version = "0.1.0"
44
edition = "2021"
5-
authors = ["Yuekai Jia <[email protected]>"]
6-
5+
authors = ["Yuekai Jia <[email protected]>", "Dashuai Wu <[email protected]>"]
76
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
87

98
[dependencies]
109
libax = { path = "../../../ulib/libax", features = ["paging", "net"] }
10+
11+
[features]
12+
default = []
13+
dns = []

apps/net/httpclient/expect_info.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ created net interface "eth0":
2525
Initialize interrupt handlers...
2626
Primary CPU 0 init OK.
2727
Hello, simple http client!
28+
IP:
29+
2830
HTTP/1.1 200 OK
2931
Server: nginx
3032
Date:
@@ -33,5 +35,6 @@ Content-Length:
3335
Connection: keep-alive
3436
Access-Control-Allow-Origin: *
3537
Cache-Control: no-cache, no-store, must-revalidate
38+
3639
^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+
3740
Shutting down...
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
smp = 1
2+
build_mode = release
3+
log_level = info
4+
5+
Primary CPU 0 started,
6+
Found physcial memory regions:
7+
.text (READ | EXECUTE | RESERVED)
8+
.rodata (READ | RESERVED)
9+
.data (READ | WRITE | RESERVED)
10+
.percpu (READ | WRITE | RESERVED)
11+
boot stack (READ | WRITE | RESERVED)
12+
.bss (READ | WRITE | RESERVED)
13+
free memory (READ | WRITE | FREE)
14+
Initialize global memory allocator...
15+
Initialize kernel page table...
16+
Initialize device drivers...
17+
created a new Net device: "virtio-net"
18+
Initialize network subsystem...
19+
number of NICs: 1
20+
NIC 0: "virtio-net"
21+
created net interface "eth0":
22+
ether: 52-54-00-12-34-56
23+
ip: 10.0.2.15/24
24+
gateway: 10.0.2.2
25+
Initialize interrupt handlers...
26+
Primary CPU 0 init OK.
27+
Hello, simple http client!
28+
ident.me IP:
29+
30+
HTTP/1.1 200 OK
31+
Server: nginx
32+
Date:
33+
Content-Type: text/plain
34+
Content-Length:
35+
Connection: keep-alive
36+
Access-Control-Allow-Origin: *
37+
Cache-Control: no-cache, no-store, must-revalidate
38+
39+
^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+
40+
Shutting down...

apps/net/httpclient/src/main.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,39 @@
44
#[macro_use]
55
extern crate libax;
66

7-
use core::str::FromStr;
7+
extern crate alloc;
88

9+
use alloc::vec::Vec;
910
use libax::io::{self, prelude::*};
10-
use libax::net::{IpAddr, TcpStream};
11+
use libax::net::{SocketAddr, TcpStream, ToSocketAddrs};
1112

12-
const DEST_IP: &str = "49.12.234.183"; // ident.me
13+
const DEST_HOST: &str = "ident.me";
14+
const DEST_IP: &str = "49.12.234.183";
1315
const REQUEST: &str = "\
1416
GET / HTTP/1.1\r\n\
1517
Host: ident.me\r\n\
1618
Accept: */*\r\n\
1719
\r\n";
1820

21+
fn get_addr() -> SocketAddr {
22+
let dest = if cfg!(feature = "dns") {
23+
print!("{} ", DEST_HOST);
24+
DEST_HOST
25+
} else {
26+
DEST_IP
27+
};
28+
let addr_iter = (dest, 80).to_socket_addrs().unwrap().collect::<Vec<_>>();
29+
println!("IP:{}\n", addr_iter[0].addr);
30+
addr_iter[0]
31+
}
32+
1933
fn client() -> io::Result {
20-
let (addr, port) = (IpAddr::from_str(DEST_IP).unwrap(), 80);
21-
let mut stream = TcpStream::connect((addr, port).into())?;
34+
let mut stream = TcpStream::connect(get_addr())?;
2235
stream.write(REQUEST.as_bytes())?;
23-
24-
let mut buf = [0; 1024];
36+
let mut buf = [0; 2048];
2537
let n = stream.read(&mut buf)?;
2638
let response = core::str::from_utf8(&buf[..n]).unwrap();
27-
println!("{}", response);
28-
39+
println!("{}", response); // longer response need to handle tcp package problems.
2940
Ok(())
3041
}
3142

apps/net/httpclient/test_cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
test_one "LOG=info NET=y" "expect_info.out"
2+
test_one "LOG=info NET=y APP_FEATURES=dns" "expect_info_dns.out"

apps/net/udpserver/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "arceos-udpserver"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Dashuai Wu <[email protected]>"]
6+
7+
[dependencies]
8+
libax = { path = "../../../ulib/libax", features = ["paging", "multitask", "net"] }

apps/net/udpserver/src/main.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
#[macro_use]
5+
extern crate libax;
6+
extern crate alloc;
7+
8+
use core::str::FromStr;
9+
10+
use libax::io;
11+
use libax::net::{IpAddr, UdpSocket};
12+
13+
const LOCAL_IP: &str = "10.0.2.15";
14+
const LOCAL_PORT: u16 = 5555;
15+
16+
fn receive_loop() -> io::Result {
17+
let (addr, port) = (IpAddr::from_str(LOCAL_IP).unwrap(), LOCAL_PORT);
18+
let socket = UdpSocket::bind((addr, port).into())?;
19+
println!("listen on: {}", socket.local_addr().unwrap());
20+
let mut buf = [0u8; 1024];
21+
loop {
22+
match socket.recv_from(&mut buf) {
23+
Ok((size, addr)) => {
24+
println!("recv: {}Bytes from {}", size, addr);
25+
let mid = core::str::from_utf8(&buf).unwrap();
26+
println!("{}", mid);
27+
let mid = ["response_", mid].join("");
28+
socket.send_to(mid.as_bytes(), addr)?;
29+
buf = [0u8; 1024];
30+
}
31+
Err(e) => return Err(e),
32+
};
33+
}
34+
}
35+
36+
#[no_mangle]
37+
fn main() {
38+
println!("Hello, simple udp client!");
39+
receive_loop().expect("test udp client failed");
40+
}

0 commit comments

Comments
 (0)