Skip to content

Commit 3a7e0b9

Browse files
ssrlivecavivie
authored andcommitted
remove all expect call
1 parent 1da226b commit 3a7e0b9

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

examples/forward.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async fn main_exec(opt: Opt) {
9292
if fd >= 0 {
9393
cfg.raw_fd(fd);
9494
} else {
95-
cfg.tun_name("utun8")
95+
cfg.tun_name(&opt.interface)
9696
.address("10.10.10.2")
9797
.destination("10.10.10.1")
9898
.mtu(tun2::DEFAULT_MTU);

src/runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ impl<T> Future for BoxFuture<'_, T> {
3838
}
3939
}
4040

41-
pub type Runner = BoxFuture<'static, ()>;
41+
pub type Runner = BoxFuture<'static, std::io::Result<()>>;

src/stack.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,8 @@ impl StackBuilder {
119119
// ICMP is handled by TCP's Interface.
120120
// smoltcp's interface will always send replies to EchoRequest
121121
if self.enable_icmp && !self.enable_tcp {
122-
return Err(std::io::Error::new(
123-
std::io::ErrorKind::InvalidInput,
124-
"Enabling icmp requires enabling tcp",
125-
));
122+
use std::io::{Error, ErrorKind::InvalidInput};
123+
return Err(Error::new(InvalidInput, "ICMP requires TCP"));
126124
}
127125
let icmp_tx = if self.enable_icmp {
128126
tcp_tx.clone()
@@ -133,7 +131,7 @@ impl StackBuilder {
133131
let udp_socket = udp_rx.map(|udp_rx| UdpSocket::new(udp_rx, stack_tx.clone()));
134132

135133
let (tcp_runner, tcp_listener) = if let Some(tcp_rx) = tcp_rx {
136-
let (tcp_runner, tcp_listener) = TcpListener::new(tcp_rx, stack_tx);
134+
let (tcp_runner, tcp_listener) = TcpListener::new(tcp_rx, stack_tx)?;
137135
(Some(tcp_runner), Some(tcp_listener))
138136
} else {
139137
(None, None)

src/tcp.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ impl TcpListenerRunner {
7878
Runner::new(async move {
7979
let notify = Arc::new(Notify::new());
8080
let (socket_tx, socket_rx) = unbounded_channel::<TcpSocketCreation>();
81-
tokio::select! {
82-
_ = Self::handle_packet(notify.clone(), iface_ingress_tx, iface_ingress_tx_avail.clone(), tcp_rx, stream_tx, socket_tx) => {}
83-
_ = Self::handle_socket(notify, device, iface, iface_ingress_tx_avail, sockets, socket_rx) => {}
84-
}
81+
let res = tokio::select! {
82+
v = Self::handle_packet(notify.clone(), iface_ingress_tx, iface_ingress_tx_avail.clone(), tcp_rx, stream_tx, socket_tx) => v,
83+
v = Self::handle_socket(notify, device, iface, iface_ingress_tx_avail, sockets, socket_rx) => v,
84+
};
85+
res?;
8586
trace!("VirtDevice::poll thread exited");
87+
Ok(())
8688
})
8789
}
8890

@@ -93,7 +95,7 @@ impl TcpListenerRunner {
9395
mut tcp_rx: Receiver<AnyIpPktFrame>,
9496
stream_tx: UnboundedSender<TcpStream>,
9597
socket_tx: UnboundedSender<TcpSocketCreation>,
96-
) {
98+
) -> std::io::Result<()> {
9799
while let Some(frame) = tcp_rx.recv().await {
98100
let packet = match IpPacket::new_checked(frame.as_slice()) {
99101
Ok(p) => p,
@@ -107,7 +109,7 @@ impl TcpListenerRunner {
107109
if matches!(packet.protocol(), IpProtocol::Icmp | IpProtocol::Icmpv6) {
108110
iface_ingress_tx
109111
.send(frame)
110-
.expect("channel already closed");
112+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::BrokenPipe, e))?;
111113
iface_ingress_tx_avail.store(true, Ordering::Release);
112114
notify.notify_one();
113115
continue;
@@ -165,19 +167,20 @@ impl TcpListenerRunner {
165167
notify: notify.clone(),
166168
control: control.clone(),
167169
})
168-
.expect("channel already closed");
170+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::BrokenPipe, e))?;
169171
socket_tx
170172
.send(TcpSocketCreation { control, socket })
171-
.expect("channel already closed");
173+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::BrokenPipe, e))?;
172174
}
173175

174176
// Pipeline tcp stream packet
175177
iface_ingress_tx
176178
.send(frame)
177-
.expect("channel already closed");
179+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::BrokenPipe, e))?;
178180
iface_ingress_tx_avail.store(true, Ordering::Release);
179181
notify.notify_one();
180182
}
183+
Ok(())
181184
}
182185

183186
async fn handle_socket(
@@ -187,7 +190,7 @@ impl TcpListenerRunner {
187190
iface_ingress_tx_avail: Arc<AtomicBool>,
188191
mut sockets: HashMap<SocketHandle, SharedControl>,
189192
mut socket_rx: UnboundedReceiver<TcpSocketCreation>,
190-
) {
193+
) -> std::io::Result<()> {
191194
let mut socket_set = SocketSet::new(vec![]);
192195
loop {
193196
while let Ok(TcpSocketCreation { control, socket }) = socket_rx.try_recv() {
@@ -354,9 +357,9 @@ impl TcpListener {
354357
pub(super) fn new(
355358
tcp_rx: Receiver<AnyIpPktFrame>,
356359
stack_tx: Sender<AnyIpPktFrame>,
357-
) -> (Runner, Self) {
360+
) -> std::io::Result<(Runner, Self)> {
358361
let (mut device, iface_ingress_tx, iface_ingress_tx_avail) = VirtualDevice::new(stack_tx);
359-
let iface = Self::create_interface(&mut device);
362+
let iface = Self::create_interface(&mut device)?;
360363

361364
let (stream_tx, stream_rx) = unbounded_channel();
362365

@@ -370,10 +373,10 @@ impl TcpListener {
370373
HashMap::new(),
371374
);
372375

373-
(runner, Self { stream_rx })
376+
Ok((runner, Self { stream_rx }))
374377
}
375378

376-
fn create_interface<D>(device: &mut D) -> Interface
379+
fn create_interface<D>(device: &mut D) -> std::io::Result<Interface>
377380
where
378381
D: Device + ?Sized,
379382
{
@@ -391,13 +394,13 @@ impl TcpListener {
391394
iface
392395
.routes_mut()
393396
.add_default_ipv4_route(Ipv4Address::new(0, 0, 0, 1))
394-
.expect("IPv4 default route");
397+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::AddrNotAvailable, e))?;
395398
iface
396399
.routes_mut()
397400
.add_default_ipv6_route(Ipv6Address::new(0, 0, 0, 0, 0, 0, 0, 1))
398-
.expect("IPv6 default route");
401+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::AddrNotAvailable, e))?;
399402
iface.set_any_ip(true);
400-
iface
403+
Ok(iface)
401404
}
402405
}
403406

0 commit comments

Comments
 (0)