Skip to content

Commit b5b7459

Browse files
committed
移植
1 parent 6124fdb commit b5b7459

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

docs/asserts/0313/socket.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// socket.rs
2+
3+
use crate::net::{SocketSetWrapper, TcpSocket, UdpSocket, dns_query};
4+
use axnet::smoltcp::socket::{self, AnySocket};
5+
use axnet::smoltcp::iface::SocketHandle;
6+
use axnet::smoltcp::wire::{IpAddress, IpCidr};
7+
8+
// A wrapper struct to represent a socket in our application
9+
pub struct Socket {
10+
handle: SocketHandle,
11+
socket_type: SocketType,
12+
}
13+
14+
pub enum SocketType {
15+
Tcp(TcpSocket),
16+
Udp(UdpSocket),
17+
Dns(socket::dns::Socket),
18+
}
19+
20+
impl Socket {
21+
pub fn new_tcp(socket_set: &SocketSetWrapper) -> Self {
22+
let tcp_socket = socket_set.new_tcp_socket();
23+
let handle = socket_set.add(tcp_socket);
24+
Socket {
25+
handle,
26+
socket_type: SocketType::Tcp(tcp_socket),
27+
}
28+
}
29+
30+
pub fn new_udp(socket_set: &SocketSetWrapper) -> Self {
31+
let udp_socket = socket_set.new_udp_socket();
32+
let handle = socket_set.add(udp_socket);
33+
Socket {
34+
handle,
35+
socket_type: SocketType::Udp(udp_socket),
36+
}
37+
}
38+
39+
pub fn new_dns(socket_set: &SocketSetWrapper) -> Self {
40+
let dns_socket = socket_set.new_dns_socket();
41+
let handle = socket_set.add(dns_socket);
42+
Socket {
43+
handle,
44+
socket_type: SocketType::Dns(dns_socket),
45+
}
46+
}
47+
48+
pub fn send(&self, data: &[u8]) -> Result<(), String> {
49+
match &self.socket_type {
50+
SocketType::Tcp(socket) => {
51+
// Here you could implement logic for sending data over a TCP socket
52+
unimplemented!("TCP send not implemented")
53+
}
54+
SocketType::Udp(socket) => {
55+
// Here you could implement logic for sending data over a UDP socket
56+
unimplemented!("UDP send not implemented")
57+
}
58+
SocketType::Dns(socket) => {
59+
// For DNS socket, implement logic to query DNS
60+
dns_query(socket, data).map_err(|e| format!("DNS query failed: {}", e))
61+
}
62+
}
63+
}
64+
65+
pub fn receive(&self) -> Result<Vec<u8>, String> {
66+
match &self.socket_type {
67+
SocketType::Tcp(socket) => {
68+
// Here you could implement logic for receiving data from a TCP socket
69+
unimplemented!("TCP receive not implemented")
70+
}
71+
SocketType::Udp(socket) => {
72+
// Here you could implement logic for receiving data from a UDP socket
73+
unimplemented!("UDP receive not implemented")
74+
}
75+
SocketType::Dns(socket) => {
76+
// For DNS socket, implement logic to receive DNS responses
77+
unimplemented!("DNS receive not implemented")
78+
}
79+
}
80+
}
81+
82+
pub fn close(self) {
83+
// Implement logic to clean up and close the socket
84+
unimplemented!("Socket close not implemented")
85+
}
86+
}
87+
88+
// This function initializes the socket set wrapper and adds TCP/UDP/DNS sockets to it
89+
pub fn create_socket_set() -> SocketSetWrapper<'static> {
90+
let socket_set = SocketSetWrapper::new();
91+
socket_set
92+
}

docs/docs/day/0313.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
1. 编译运行asterinas、DragonOs,比较二者的net部分的实现和starry-old的不同
44
2. 编写syscall_imp下面的net部分,如socket.rs
55

6+
[filename](../../asserts/0313/socket.rs ':include :type=code')
7+
68
![](../../asserts/0313/1.jpg ':class=myImageClass')

0 commit comments

Comments
 (0)