Skip to content

Commit 0cb2ed9

Browse files
committed
chore(deps): upgrade async-io
1 parent 3d5879c commit 0cb2ed9

File tree

3 files changed

+76
-56
lines changed

3 files changed

+76
-56
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ features = ["os-poll", "os-ext"]
3333

3434
[dependencies.async-io]
3535
optional = true
36-
version = "1.13"
36+
version = "2"
3737

3838
[features]
3939
default = []
4040
mio_socket = ["mio"]
4141
tokio_socket = ["tokio", "futures"]
42-
smol_socket = ["async-io","futures"]
42+
smol_socket = ["async-io", "futures"]
4343

4444
[dev-dependencies]
4545
netlink-packet-audit = "0.4.1"

src/smol.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use std::{
44
io,
5-
os::unix::io::{AsRawFd, FromRawFd, RawFd},
65
task::{Context, Poll},
76
};
87

@@ -17,20 +16,6 @@ use crate::{AsyncSocket, Socket, SocketAddr};
1716
/// An I/O object representing a Netlink socket.
1817
pub struct SmolSocket(Async<Socket>);
1918

20-
impl FromRawFd for SmolSocket {
21-
unsafe fn from_raw_fd(fd: RawFd) -> Self {
22-
let socket = Socket::from_raw_fd(fd);
23-
socket.set_non_blocking(true).unwrap();
24-
SmolSocket(Async::new(socket).unwrap())
25-
}
26-
}
27-
28-
impl AsRawFd for SmolSocket {
29-
fn as_raw_fd(&self) -> RawFd {
30-
self.0.get_ref().as_raw_fd()
31-
}
32-
}
33-
3419
// async_io::Async<..>::{read,write}_with[_mut] functions try IO first,
3520
// and only register context if it would block.
3621
// replicate this in these poll functions:
@@ -79,7 +64,7 @@ impl AsyncSocket for SmolSocket {
7964

8065
/// Mutable access to underyling [`Socket`]
8166
fn socket_mut(&mut self) -> &mut Socket {
82-
self.0.get_mut()
67+
unsafe { self.0.get_mut() }
8368
}
8469

8570
fn new(protocol: isize) -> io::Result<Self> {
@@ -92,7 +77,7 @@ impl AsyncSocket for SmolSocket {
9277
cx: &mut Context<'_>,
9378
buf: &[u8],
9479
) -> Poll<io::Result<usize>> {
95-
self.poll_write_with(cx, |this| this.0.get_mut().send(buf, 0))
80+
self.poll_write_with(cx, |this| this.socket_mut().send(buf, 0))
9681
}
9782

9883
fn poll_send_to(
@@ -101,7 +86,7 @@ impl AsyncSocket for SmolSocket {
10186
buf: &[u8],
10287
addr: &SocketAddr,
10388
) -> Poll<io::Result<usize>> {
104-
self.poll_write_with(cx, |this| this.0.get_mut().send_to(buf, addr, 0))
89+
self.poll_write_with(cx, |this| this.socket_mut().send_to(buf, addr, 0))
10590
}
10691

10792
fn poll_recv<B>(
@@ -113,7 +98,7 @@ impl AsyncSocket for SmolSocket {
11398
B: bytes::BufMut,
11499
{
115100
self.poll_read_with(cx, |this| {
116-
this.0.get_mut().recv(buf, 0).map(|_len| ())
101+
this.socket_mut().recv(buf, 0).map(|_len| ())
117102
})
118103
}
119104

@@ -126,7 +111,7 @@ impl AsyncSocket for SmolSocket {
126111
B: bytes::BufMut,
127112
{
128113
self.poll_read_with(cx, |this| {
129-
let x = this.0.get_mut().recv_from(buf, 0);
114+
let x = this.socket_mut().recv_from(buf, 0);
130115
trace!("poll_recv_from: {:?}", x);
131116
x.map(|(_len, addr)| addr)
132117
})
@@ -136,6 +121,6 @@ impl AsyncSocket for SmolSocket {
136121
&mut self,
137122
cx: &mut Context<'_>,
138123
) -> Poll<io::Result<(Vec<u8>, SocketAddr)>> {
139-
self.poll_read_with(cx, |this| this.0.get_mut().recv_from_full())
124+
self.poll_read_with(cx, |this| this.socket_mut().recv_from_full())
140125
}
141126
}

src/socket.rs

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
use std::{
44
io::{Error, Result},
55
mem,
6-
os::unix::io::{AsRawFd, FromRawFd, RawFd},
6+
os::{
7+
fd::{AsFd, BorrowedFd},
8+
unix::io::{AsRawFd, RawFd},
9+
},
710
};
811

912
use crate::SocketAddr;
@@ -51,7 +54,7 @@ use crate::SocketAddr;
5154
/// }
5255
/// }
5356
/// ```
54-
#[derive(Clone, Debug)]
57+
#[derive(Debug)]
5558
pub struct Socket(RawFd);
5659

5760
impl AsRawFd for Socket {
@@ -60,15 +63,15 @@ impl AsRawFd for Socket {
6063
}
6164
}
6265

63-
impl FromRawFd for Socket {
64-
unsafe fn from_raw_fd(fd: RawFd) -> Self {
65-
Socket(fd)
66+
impl AsFd for Socket {
67+
fn as_fd(&self) -> BorrowedFd<'_> {
68+
unsafe { BorrowedFd::borrow_raw(self.0) }
6669
}
6770
}
6871

6972
impl Drop for Socket {
7073
fn drop(&mut self) {
71-
unsafe { libc::close(self.0) };
74+
unsafe { libc::close(self.as_raw_fd()) };
7275
}
7376
}
7477

@@ -94,7 +97,7 @@ impl Socket {
9497
/// Bind the socket to the given address
9598
pub fn bind(&mut self, addr: &SocketAddr) -> Result<()> {
9699
let (addr_ptr, addr_len) = addr.as_raw();
97-
let res = unsafe { libc::bind(self.0, addr_ptr, addr_len) };
100+
let res = unsafe { libc::bind(self.as_raw_fd(), addr_ptr, addr_len) };
98101
if res < 0 {
99102
return Err(Error::last_os_error());
100103
}
@@ -115,7 +118,9 @@ impl Socket {
115118
let (addr_ptr, mut addr_len) = addr.as_raw_mut();
116119
let addr_len_copy = addr_len;
117120
let addr_len_ptr = &mut addr_len as *mut libc::socklen_t;
118-
let res = unsafe { libc::getsockname(self.0, addr_ptr, addr_len_ptr) };
121+
let res = unsafe {
122+
libc::getsockname(self.as_raw_fd(), addr_ptr, addr_len_ptr)
123+
};
119124
if res < 0 {
120125
return Err(Error::last_os_error());
121126
}
@@ -128,8 +133,9 @@ impl Socket {
128133
/// Make this socket non-blocking
129134
pub fn set_non_blocking(&self, non_blocking: bool) -> Result<()> {
130135
let mut non_blocking = non_blocking as libc::c_int;
131-
let res =
132-
unsafe { libc::ioctl(self.0, libc::FIONBIO, &mut non_blocking) };
136+
let res = unsafe {
137+
libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut non_blocking)
138+
};
133139
if res < 0 {
134140
return Err(Error::last_os_error());
135141
}
@@ -152,7 +158,7 @@ impl Socket {
152158
/// 2. connect it to the kernel with [`Socket::connect`]
153159
/// 3. send a request to the kernel with [`Socket::send`]
154160
/// 4. read the response (which can span over several messages)
155-
/// [`Socket::recv`]
161+
/// [`Socket::recv`]
156162
///
157163
/// ```rust
158164
/// use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};
@@ -216,7 +222,7 @@ impl Socket {
216222
// - https://stackoverflow.com/a/14046386/1836144
217223
// - https://lists.isc.org/pipermail/bind-users/2009-August/077527.html
218224
let (addr, addr_len) = remote_addr.as_raw();
219-
let res = unsafe { libc::connect(self.0, addr, addr_len) };
225+
let res = unsafe { libc::connect(self.as_raw_fd(), addr, addr_len) };
220226
if res < 0 {
221227
return Err(Error::last_os_error());
222228
}
@@ -293,7 +299,7 @@ impl Socket {
293299

294300
let res = unsafe {
295301
libc::recvfrom(
296-
self.0,
302+
self.as_raw_fd(),
297303
buf_ptr,
298304
buf_len,
299305
flags,
@@ -324,7 +330,8 @@ impl Socket {
324330
let buf_ptr = chunk.as_mut_ptr() as *mut libc::c_void;
325331
let buf_len = chunk.len() as libc::size_t;
326332

327-
let res = unsafe { libc::recv(self.0, buf_ptr, buf_len, flags) };
333+
let res =
334+
unsafe { libc::recv(self.as_raw_fd(), buf_ptr, buf_len, flags) };
328335
if res < 0 {
329336
return Err(Error::last_os_error());
330337
} else {
@@ -367,7 +374,14 @@ impl Socket {
367374
let buf_len = buf.len() as libc::size_t;
368375

369376
let res = unsafe {
370-
libc::sendto(self.0, buf_ptr, buf_len, flags, addr_ptr, addr_len)
377+
libc::sendto(
378+
self.as_raw_fd(),
379+
buf_ptr,
380+
buf_len,
381+
flags,
382+
addr_ptr,
383+
addr_len,
384+
)
371385
};
372386
if res < 0 {
373387
return Err(Error::last_os_error());
@@ -382,7 +396,8 @@ impl Socket {
382396
let buf_ptr = buf.as_ptr() as *const libc::c_void;
383397
let buf_len = buf.len() as libc::size_t;
384398

385-
let res = unsafe { libc::send(self.0, buf_ptr, buf_len, flags) };
399+
let res =
400+
unsafe { libc::send(self.as_raw_fd(), buf_ptr, buf_len, flags) };
386401
if res < 0 {
387402
return Err(Error::last_os_error());
388403
}
@@ -391,12 +406,17 @@ impl Socket {
391406

392407
pub fn set_pktinfo(&mut self, value: bool) -> Result<()> {
393408
let value: libc::c_int = value.into();
394-
setsockopt(self.0, libc::SOL_NETLINK, libc::NETLINK_PKTINFO, value)
409+
setsockopt(
410+
self.as_raw_fd(),
411+
libc::SOL_NETLINK,
412+
libc::NETLINK_PKTINFO,
413+
value,
414+
)
395415
}
396416

397417
pub fn get_pktinfo(&self) -> Result<bool> {
398418
let res = getsockopt::<libc::c_int>(
399-
self.0,
419+
self.as_raw_fd(),
400420
libc::SOL_NETLINK,
401421
libc::NETLINK_PKTINFO,
402422
)?;
@@ -405,7 +425,7 @@ impl Socket {
405425

406426
pub fn add_membership(&mut self, group: u32) -> Result<()> {
407427
setsockopt(
408-
self.0,
428+
self.as_raw_fd(),
409429
libc::SOL_NETLINK,
410430
libc::NETLINK_ADD_MEMBERSHIP,
411431
group,
@@ -414,7 +434,7 @@ impl Socket {
414434

415435
pub fn drop_membership(&mut self, group: u32) -> Result<()> {
416436
setsockopt(
417-
self.0,
437+
self.as_raw_fd(),
418438
libc::SOL_NETLINK,
419439
libc::NETLINK_DROP_MEMBERSHIP,
420440
group,
@@ -434,7 +454,7 @@ impl Socket {
434454
pub fn set_broadcast_error(&mut self, value: bool) -> Result<()> {
435455
let value: libc::c_int = value.into();
436456
setsockopt(
437-
self.0,
457+
self.as_raw_fd(),
438458
libc::SOL_NETLINK,
439459
libc::NETLINK_BROADCAST_ERROR,
440460
value,
@@ -443,7 +463,7 @@ impl Socket {
443463

444464
pub fn get_broadcast_error(&self) -> Result<bool> {
445465
let res = getsockopt::<libc::c_int>(
446-
self.0,
466+
self.as_raw_fd(),
447467
libc::SOL_NETLINK,
448468
libc::NETLINK_BROADCAST_ERROR,
449469
)?;
@@ -454,12 +474,17 @@ impl Socket {
454474
/// unicast and broadcast listeners to avoid receiving `ENOBUFS` errors.
455475
pub fn set_no_enobufs(&mut self, value: bool) -> Result<()> {
456476
let value: libc::c_int = value.into();
457-
setsockopt(self.0, libc::SOL_NETLINK, libc::NETLINK_NO_ENOBUFS, value)
477+
setsockopt(
478+
self.as_raw_fd(),
479+
libc::SOL_NETLINK,
480+
libc::NETLINK_NO_ENOBUFS,
481+
value,
482+
)
458483
}
459484

460485
pub fn get_no_enobufs(&self) -> Result<bool> {
461486
let res = getsockopt::<libc::c_int>(
462-
self.0,
487+
self.as_raw_fd(),
463488
libc::SOL_NETLINK,
464489
libc::NETLINK_NO_ENOBUFS,
465490
)?;
@@ -474,7 +499,7 @@ impl Socket {
474499
pub fn set_listen_all_namespaces(&mut self, value: bool) -> Result<()> {
475500
let value: libc::c_int = value.into();
476501
setsockopt(
477-
self.0,
502+
self.as_raw_fd(),
478503
libc::SOL_NETLINK,
479504
libc::NETLINK_LISTEN_ALL_NSID,
480505
value,
@@ -483,7 +508,7 @@ impl Socket {
483508

484509
pub fn get_listen_all_namespaces(&self) -> Result<bool> {
485510
let res = getsockopt::<libc::c_int>(
486-
self.0,
511+
self.as_raw_fd(),
487512
libc::SOL_NETLINK,
488513
libc::NETLINK_LISTEN_ALL_NSID,
489514
)?;
@@ -498,12 +523,17 @@ impl Socket {
498523
/// acknowledgment.
499524
pub fn set_cap_ack(&mut self, value: bool) -> Result<()> {
500525
let value: libc::c_int = value.into();
501-
setsockopt(self.0, libc::SOL_NETLINK, libc::NETLINK_CAP_ACK, value)
526+
setsockopt(
527+
self.as_raw_fd(),
528+
libc::SOL_NETLINK,
529+
libc::NETLINK_CAP_ACK,
530+
value,
531+
)
502532
}
503533

504534
pub fn get_cap_ack(&self) -> Result<bool> {
505535
let res = getsockopt::<libc::c_int>(
506-
self.0,
536+
self.as_raw_fd(),
507537
libc::SOL_NETLINK,
508538
libc::NETLINK_CAP_ACK,
509539
)?;
@@ -515,12 +545,17 @@ impl Socket {
515545
/// NLMSG_ERROR and NLMSG_DONE messages.
516546
pub fn set_ext_ack(&mut self, value: bool) -> Result<()> {
517547
let value: libc::c_int = value.into();
518-
setsockopt(self.0, libc::SOL_NETLINK, libc::NETLINK_EXT_ACK, value)
548+
setsockopt(
549+
self.as_raw_fd(),
550+
libc::SOL_NETLINK,
551+
libc::NETLINK_EXT_ACK,
552+
value,
553+
)
519554
}
520555

521556
pub fn get_ext_ack(&self) -> Result<bool> {
522557
let res = getsockopt::<libc::c_int>(
523-
self.0,
558+
self.as_raw_fd(),
524559
libc::SOL_NETLINK,
525560
libc::NETLINK_EXT_ACK,
526561
)?;
@@ -534,13 +569,13 @@ impl Socket {
534569
/// the maximum allowed value is set by the /proc/sys/net/core/rmem_max
535570
/// file. The minimum (doubled) value for this option is 256.
536571
pub fn set_rx_buf_sz<T>(&self, size: T) -> Result<()> {
537-
setsockopt(self.0, libc::SOL_SOCKET, libc::SO_RCVBUF, size)
572+
setsockopt(self.as_raw_fd(), libc::SOL_SOCKET, libc::SO_RCVBUF, size)
538573
}
539574

540575
/// Gets socket receive buffer in bytes
541576
pub fn get_rx_buf_sz(&self) -> Result<usize> {
542577
let res = getsockopt::<libc::c_int>(
543-
self.0,
578+
self.as_raw_fd(),
544579
libc::SOL_SOCKET,
545580
libc::SO_RCVBUF,
546581
)?;
@@ -552,7 +587,7 @@ impl Socket {
552587
pub fn set_netlink_get_strict_chk(&self, value: bool) -> Result<()> {
553588
let value: u32 = value.into();
554589
setsockopt(
555-
self.0,
590+
self.as_raw_fd(),
556591
libc::SOL_NETLINK,
557592
libc::NETLINK_GET_STRICT_CHK,
558593
value,

0 commit comments

Comments
 (0)