arceos_api/imp/
net.rs

1use crate::io::AxPollState;
2use axerrno::AxResult;
3use axnet::{UdpSocket, TcpSocket};
4use core::net::{IpAddr, SocketAddr};
5
6/// A handle to a TCP socket.
7pub struct AxTcpSocketHandle(TcpSocket);
8
9/// A handle to a UDP socket.
10pub struct AxUdpSocketHandle(UdpSocket);
11
12////////////////////////////////////////////////////////////////////////////////
13// TCP socket
14////////////////////////////////////////////////////////////////////////////////
15
16pub fn ax_tcp_socket() -> AxTcpSocketHandle {
17    AxTcpSocketHandle(TcpSocket::new())
18}
19
20pub fn ax_tcp_socket_addr(socket: &AxTcpSocketHandle) -> AxResult<SocketAddr> {
21    socket.0.local_addr()
22}
23
24pub fn ax_tcp_peer_addr(socket: &AxTcpSocketHandle) -> AxResult<SocketAddr> {
25    socket.0.peer_addr()
26}
27
28pub fn ax_tcp_set_nonblocking(socket: &AxTcpSocketHandle, nonblocking: bool) -> AxResult {
29    socket.0.set_nonblocking(nonblocking);
30    Ok(())
31}
32
33pub fn ax_tcp_connect(socket: &AxTcpSocketHandle, addr: SocketAddr) -> AxResult {
34    socket.0.connect(addr)
35}
36
37pub fn ax_tcp_bind(socket: &AxTcpSocketHandle, addr: SocketAddr) -> AxResult {
38    socket.0.bind(addr)
39}
40
41pub fn ax_tcp_listen(socket: &AxTcpSocketHandle, _backlog: usize) -> AxResult {
42    socket.0.listen()
43}
44
45pub fn ax_tcp_accept(socket: &AxTcpSocketHandle) -> AxResult<(AxTcpSocketHandle, SocketAddr)> {
46    let new_sock = socket.0.accept()?;
47    let addr = new_sock.peer_addr()?;
48    Ok((AxTcpSocketHandle(new_sock), addr))
49}
50
51pub fn ax_tcp_send(socket: &AxTcpSocketHandle, buf: &[u8]) -> AxResult<usize> {
52    socket.0.send(buf)
53}
54
55pub fn ax_tcp_recv(socket: &AxTcpSocketHandle, buf: &mut [u8]) -> AxResult<usize> {
56    socket.0.recv(buf)
57}
58
59pub fn ax_tcp_poll(socket: &AxTcpSocketHandle) -> AxResult<AxPollState> {
60    socket.0.poll()
61}
62
63pub fn ax_tcp_shutdown(socket: &AxTcpSocketHandle) -> AxResult {
64    socket.0.shutdown()
65}
66
67////////////////////////////////////////////////////////////////////////////////
68// UDP socket
69////////////////////////////////////////////////////////////////////////////////
70
71pub fn ax_udp_socket() -> AxUdpSocketHandle {
72    AxUdpSocketHandle(UdpSocket::new())
73}
74
75pub fn ax_udp_socket_addr(socket: &AxUdpSocketHandle) -> AxResult<SocketAddr> {
76    socket.0.local_addr()
77}
78
79pub fn ax_udp_peer_addr(socket: &AxUdpSocketHandle) -> AxResult<SocketAddr> {
80    socket.0.peer_addr()
81}
82
83pub fn ax_udp_set_nonblocking(socket: &AxUdpSocketHandle, nonblocking: bool) -> AxResult {
84    socket.0.set_nonblocking(nonblocking);
85    Ok(())
86}
87
88pub fn ax_udp_bind(socket: &AxUdpSocketHandle, addr: SocketAddr) -> AxResult {
89    socket.0.bind(addr)
90}
91
92pub fn ax_udp_recv_from(socket: &AxUdpSocketHandle, buf: &mut [u8]) -> AxResult<(usize, SocketAddr)> {
93    socket.0.recv_from(buf)
94}
95
96pub fn ax_udp_peek_from(socket: &AxUdpSocketHandle, buf: &mut [u8]) -> AxResult<(usize, SocketAddr)> {
97    socket.0.peek_from(buf)
98}
99
100pub fn ax_udp_send_to(socket: &AxUdpSocketHandle, buf: &[u8], addr: SocketAddr) -> AxResult<usize> {
101    socket.0.send_to(buf, addr)
102}
103
104pub fn ax_udp_connect(socket: &AxUdpSocketHandle, addr: SocketAddr) -> AxResult {
105    socket.0.connect(addr)
106}
107
108pub fn ax_udp_send(socket: &AxUdpSocketHandle, buf: &[u8]) -> AxResult<usize> {
109    socket.0.send(buf)
110}
111
112pub fn ax_udp_recv(socket: &AxUdpSocketHandle, buf: &mut [u8]) -> AxResult<usize> {
113    socket.0.recv(buf)
114}
115
116pub fn ax_udp_poll(socket: &AxUdpSocketHandle) -> AxResult<AxPollState> {
117    socket.0.poll()
118}
119
120////////////////////////////////////////////////////////////////////////////////
121// Miscellaneous
122////////////////////////////////////////////////////////////////////////////////
123
124pub fn ax_dns_query(domain_name: &str) -> AxResult<alloc::vec::Vec<IpAddr>> {
125    axnet::dns_query(domain_name)
126}
127
128pub fn ax_poll_interfaces() -> AxResult {
129    axnet::poll_interfaces();
130    Ok(())
131}