Skip to content

Commit 851326c

Browse files
committed
mctp-linux: Add blocking sendmsg(), make sendto() call sendmsg
Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
1 parent ec31f9e commit 851326c

1 file changed

Lines changed: 16 additions & 28 deletions

File tree

mctp-linux/src/lib.rs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
//! operations, using MCTP-specific addressing structures.
1414
//!
1515
//! [`MctpSocket`] provides support for blocking socket operations
16-
//! [sendto](MctpSocket::sendto), [`recvfrom`](MctpSocket::recvfrom) and
17-
//! [`bind`](MctpSocket::bind).
16+
//! [`sendto`](MctpSocket::sendto), [`sendmsg`](MctpSocket::sendmsg),
17+
//! ([`recvfrom`](MctpSocket::recvfrom) and [`bind`](MctpSocket::bind).
1818
//!
1919
//! ```no_run
2020
//! use mctp_linux;
@@ -205,27 +205,6 @@ impl MctpSocket {
205205
Ok((len, addr))
206206
}
207207

208-
fn io_sendto(
209-
&self,
210-
buf: &[u8],
211-
addr: &MctpSockAddr,
212-
) -> std::io::Result<usize> {
213-
let (addr_ptr, addr_len) = addr.as_raw();
214-
let buf_ptr = buf.as_ptr() as *const libc::c_void;
215-
let buf_len = buf.len() as libc::size_t;
216-
let fd = self.as_raw_fd();
217-
218-
let rc = unsafe {
219-
libc::sendto(fd, buf_ptr, buf_len, 0, addr_ptr, addr_len)
220-
};
221-
222-
if rc < 0 {
223-
Err(Error::last_os_error())
224-
} else {
225-
Ok(rc as usize)
226-
}
227-
}
228-
229208
fn io_sendmsg(
230209
&self,
231210
bufs: &[&[u8]],
@@ -262,7 +241,19 @@ impl MctpSocket {
262241
///
263242
/// Essentially a wrapper around [libc::sendto].
264243
pub fn sendto(&self, buf: &[u8], addr: &MctpSockAddr) -> Result<usize> {
265-
self.io_sendto(buf, addr).map_err(mctp::Error::Io)
244+
self.sendmsg(&[buf], addr)
245+
}
246+
247+
/// Blocking send to a socket, given a slice of buffers and address, returning
248+
/// the number of bytes sent.
249+
///
250+
/// Essentially a wrapper around [libc::sendmsg].
251+
pub fn sendmsg(
252+
&self,
253+
bufs: &[&[u8]],
254+
addr: &MctpSockAddr,
255+
) -> Result<usize> {
256+
self.io_sendmsg(bufs, addr).map_err(mctp::Error::Io)
266257
}
267258

268259
/// Bind the socket to a local address.
@@ -408,10 +399,7 @@ impl MctpSocketAsync {
408399
buf: &[u8],
409400
addr: &MctpSockAddr,
410401
) -> Result<usize> {
411-
self.0
412-
.write_with(|io| io.io_sendto(buf, addr))
413-
.await
414-
.map_err(mctp::Error::Io)
402+
self.sendmsg(&[buf], addr).await
415403
}
416404

417405
/// Send a message to a given address, from a set of buffers.

0 commit comments

Comments
 (0)