Skip to main content

os/syscall/
net.rs

1use crate::net::port_table::{PortFd, accept, listen, port_acceptable};
2use crate::net::udp::UDP;
3use crate::net::{IPv4, net_interrupt_handler};
4use crate::task::{current_process, current_task, current_trap_cx};
5use alloc::sync::Arc;
6
7// just support udp
8pub fn sys_connect(raddr: u32, lport: u16, rport: u16) -> isize {
9    let process = current_process();
10    let mut inner = process.inner_exclusive_access();
11    let fd = inner.alloc_fd();
12    let udp_node = UDP::new(IPv4::from_u32(raddr), lport, rport);
13    inner.fd_table[fd] = Some(Arc::new(udp_node));
14    fd as isize
15}
16
17// listen a port
18pub fn sys_listen(port: u16) -> isize {
19    match listen(port) {
20        Some(port_index) => {
21            let process = current_process();
22            let mut inner = process.inner_exclusive_access();
23            let fd = inner.alloc_fd();
24            let port_fd = PortFd::new(port_index);
25            inner.fd_table[fd] = Some(Arc::new(port_fd));
26
27            // NOTICE: this return the port index, not the fd
28            port_index as isize
29        }
30        None => -1,
31    }
32}
33
34// accept a tcp connection
35pub fn sys_accept(port_index: usize) -> isize {
36    println!("accepting port {}", port_index);
37
38    let task = current_task().unwrap();
39    accept(port_index, task);
40    // block_current_and_run_next();
41
42    // NOTICE: There does not have interrupt handler, just call it munually.
43    loop {
44        net_interrupt_handler();
45
46        if !port_acceptable(port_index) {
47            break;
48        }
49    }
50
51    let cx = current_trap_cx();
52    cx.x[10] as isize
53}