1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::{io, net::UdpSocket};
const VEC_SIZE: usize = 2;
pub trait IOVecExt {
fn writev(&self, bufs: [&[u8]; 2]) -> io::Result<usize>;
fn readv(&self, bufs: [&mut [u8]; 2]) -> io::Result<usize>;
}
#[cfg(unix)]
mod imp {
use super::{io, IOVecExt, UdpSocket, VEC_SIZE};
use std::os::unix::io::AsRawFd;
impl IOVecExt for UdpSocket {
fn writev(&self, bufs: [&[u8]; VEC_SIZE]) -> io::Result<usize> {
let iovecs: [libc::iovec; VEC_SIZE] = [
libc::iovec {
iov_base: bufs[0].as_ptr().cast_mut().cast(),
iov_len: bufs[0].len(),
},
libc::iovec {
iov_base: bufs[1].as_ptr().cast_mut().cast(),
iov_len: bufs[1].len(),
},
];
// SAFETY: All params are setup in this function safely.
#[allow(clippy::cast_possible_truncation)] // SAFETY: Length is always VEC_SIZE.
#[allow(clippy::cast_possible_wrap)]
let r = unsafe { libc::writev(self.as_raw_fd(), iovecs.as_ptr(), VEC_SIZE as _) };
if r < 0 {
Err(io::Error::last_os_error())
} else {
Ok(r.unsigned_abs())
}
}
fn readv(&self, bufs: [&mut [u8]; VEC_SIZE]) -> io::Result<usize> {
let mut iovecs: [libc::iovec; VEC_SIZE] = [
libc::iovec {
iov_base: bufs[0].as_mut_ptr().cast(),
iov_len: bufs[0].len(),
},
libc::iovec {
iov_base: bufs[1].as_mut_ptr().cast(),
iov_len: bufs[1].len(),
},
];
// SAFETY: All params are setup in this function safely.
#[allow(clippy::cast_possible_truncation)] // SAFETY: Length is always VEC_SIZE.
#[allow(clippy::cast_possible_wrap)]
let r = unsafe { libc::readv(self.as_raw_fd(), iovecs.as_mut_ptr(), VEC_SIZE as _) };
if r < 0 {
Err(io::Error::last_os_error())
} else {
Ok(r.unsigned_abs())
}
}
}
}
#[cfg(windows)]
mod imp {
use super::{io, IOVecExt, UdpSocket, VEC_SIZE};
use crate::Error;
use std::{os::windows::io::AsRawSocket, ptr};
use windows_sys::Win32::Networking::WinSock::{WSARecv, WSASend, WSABUF};
impl IOVecExt for UdpSocket {
fn writev(&self, bufs: [&[u8]; VEC_SIZE]) -> io::Result<usize> {
let bufs_lens: [u32; VEC_SIZE] = [
bufs[0].len().try_into().map_err(|_| {
Error::WinUDP4GiBLimit {
size: bufs[0].len(),
}
.into_io()
})?,
bufs[1].len().try_into().map_err(|_| {
Error::WinUDP4GiBLimit {
size: bufs[1].len(),
}
.into_io()
})?,
];
let mut wsabufs: [WSABUF; VEC_SIZE] = [
WSABUF {
len: bufs_lens[0],
buf: bufs[0].as_ptr().cast_mut(),
},
WSABUF {
len: bufs_lens[1],
buf: bufs[1].as_ptr().cast_mut(),
},
];
let mut sent = 0_u32;
// SAFETY: All params are setup in this function safely.
// SAFETY: Length is always VEC_SIZE.
// SAFETY: On 32 bit systems self.as_raw_socket() returns a u32.
// (https://doc.rust-lang.org/src/std/os/windows/raw.rs.html#16)
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_possible_wrap)]
let r = unsafe {
WSASend(
self.as_raw_socket() as _,
wsabufs.as_mut_ptr(),
VEC_SIZE as _,
&mut sent,
0,
ptr::null_mut(),
None,
)
};
if r == 0 {
Ok(sent as usize)
} else {
Err(io::Error::last_os_error())
}
}
fn readv(&self, bufs: [&mut [u8]; VEC_SIZE]) -> io::Result<usize> {
let bufs_lens: [u32; VEC_SIZE] = [
bufs[0].len().try_into().map_err(|_| {
Error::WinUDP4GiBLimit {
size: bufs[0].len(),
}
.into_io()
})?,
bufs[1].len().try_into().map_err(|_e| {
Error::WinUDP4GiBLimit {
size: bufs[1].len(),
}
.into_io()
})?,
];
let mut wsabufs: [WSABUF; VEC_SIZE] = [
WSABUF {
len: bufs_lens[0],
buf: bufs[0].as_mut_ptr(),
},
WSABUF {
len: bufs_lens[1],
buf: bufs[1].as_mut_ptr(),
},
];
let mut recved: u32 = 0;
let mut flags: u32 = 0;
// SAFETY: All params are setup in this function safely.
// SAFETY: Length is always VEC_SIZE.
// SAFETY: On 32 bit systems self.as_raw_socket() returns a u32.
// (https://doc.rust-lang.org/src/std/os/windows/raw.rs.html#16)
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_possible_wrap)]
let r = unsafe {
WSARecv(
self.as_raw_socket() as _,
wsabufs.as_mut_ptr(),
VEC_SIZE as _,
&mut recved,
&mut flags,
ptr::null_mut(),
None,
)
};
if r == 0 {
Ok(recved as usize)
} else {
Err(io::Error::last_os_error())
}
}
}
}