|
5 | 5 |
|
6 | 6 | use crate::backend::c;
|
7 | 7 | use crate::backend::conv::{msg_control_len, msg_iov_len};
|
8 |
| -#[cfg(target_os = "linux")] |
9 |
| -use crate::backend::net::write_sockaddr::encode_sockaddr_xdp; |
10 |
| -use crate::backend::net::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6}; |
11 | 8 |
|
12 | 9 | use crate::io::{self, IoSlice, IoSliceMut};
|
13 |
| -#[cfg(target_os = "linux")] |
14 |
| -use crate::net::xdp::SocketAddrXdp; |
15 |
| -use crate::net::{RecvAncillaryBuffer, SendAncillaryBuffer, SocketAddrV4, SocketAddrV6}; |
16 |
| -use crate::utils::as_ptr; |
| 10 | +use crate::net::{RecvAncillaryBuffer, SendAncillaryBuffer, SockAddr}; |
17 | 11 |
|
18 | 12 | use core::mem::{size_of, zeroed, MaybeUninit};
|
19 | 13 |
|
@@ -66,87 +60,24 @@ pub(crate) fn with_noaddr_msghdr<R>(
|
66 | 60 | })
|
67 | 61 | }
|
68 | 62 |
|
69 |
| -/// Create a message header intended to send with an IPv4 address. |
70 |
| -pub(crate) fn with_v4_msghdr<R>( |
71 |
| - addr: &SocketAddrV4, |
| 63 | +/// Create a message header intended to send with the specified address. |
| 64 | +pub(crate) fn with_msghdr<R>( |
| 65 | + addr: &impl SockAddr, |
72 | 66 | iov: &[IoSlice<'_>],
|
73 | 67 | control: &mut SendAncillaryBuffer<'_, '_, '_>,
|
74 | 68 | f: impl FnOnce(c::msghdr) -> R,
|
75 | 69 | ) -> R {
|
76 |
| - let encoded = encode_sockaddr_v4(addr); |
77 |
| - |
78 |
| - f({ |
79 |
| - let mut h = zero_msghdr(); |
80 |
| - h.msg_name = as_ptr(&encoded) as _; |
81 |
| - h.msg_namelen = size_of::<SocketAddrV4>() as _; |
82 |
| - h.msg_iov = iov.as_ptr() as _; |
83 |
| - h.msg_iovlen = msg_iov_len(iov.len()); |
84 |
| - h.msg_control = control.as_control_ptr().cast(); |
85 |
| - h.msg_controllen = msg_control_len(control.control_len()); |
86 |
| - h |
87 |
| - }) |
88 |
| -} |
89 |
| - |
90 |
| -/// Create a message header intended to send with an IPv6 address. |
91 |
| -pub(crate) fn with_v6_msghdr<R>( |
92 |
| - addr: &SocketAddrV6, |
93 |
| - iov: &[IoSlice<'_>], |
94 |
| - control: &mut SendAncillaryBuffer<'_, '_, '_>, |
95 |
| - f: impl FnOnce(c::msghdr) -> R, |
96 |
| -) -> R { |
97 |
| - let encoded = encode_sockaddr_v6(addr); |
98 |
| - |
99 |
| - f({ |
100 |
| - let mut h = zero_msghdr(); |
101 |
| - h.msg_name = as_ptr(&encoded) as _; |
102 |
| - h.msg_namelen = size_of::<SocketAddrV6>() as _; |
103 |
| - h.msg_iov = iov.as_ptr() as _; |
104 |
| - h.msg_iovlen = msg_iov_len(iov.len()); |
105 |
| - h.msg_control = control.as_control_ptr().cast(); |
106 |
| - h.msg_controllen = msg_control_len(control.control_len()); |
107 |
| - h |
108 |
| - }) |
109 |
| -} |
110 |
| - |
111 |
| -/// Create a message header intended to send with a Unix address. |
112 |
| -#[cfg(all(unix, not(target_os = "redox")))] |
113 |
| -pub(crate) fn with_unix_msghdr<R>( |
114 |
| - addr: &crate::net::SocketAddrUnix, |
115 |
| - iov: &[IoSlice<'_>], |
116 |
| - control: &mut SendAncillaryBuffer<'_, '_, '_>, |
117 |
| - f: impl FnOnce(c::msghdr) -> R, |
118 |
| -) -> R { |
119 |
| - f({ |
120 |
| - let mut h = zero_msghdr(); |
121 |
| - h.msg_name = as_ptr(&addr.unix) as _; |
122 |
| - h.msg_namelen = addr.addr_len(); |
123 |
| - h.msg_iov = iov.as_ptr() as _; |
124 |
| - h.msg_iovlen = msg_iov_len(iov.len()); |
125 |
| - h.msg_control = control.as_control_ptr().cast(); |
126 |
| - h.msg_controllen = msg_control_len(control.control_len()); |
127 |
| - h |
128 |
| - }) |
129 |
| -} |
130 |
| - |
131 |
| -/// Create a message header intended to send with an IPv6 address. |
132 |
| -#[cfg(target_os = "linux")] |
133 |
| -pub(crate) fn with_xdp_msghdr<R>( |
134 |
| - addr: &SocketAddrXdp, |
135 |
| - iov: &[IoSlice<'_>], |
136 |
| - control: &mut SendAncillaryBuffer<'_, '_, '_>, |
137 |
| - f: impl FnOnce(c::msghdr) -> R, |
138 |
| -) -> R { |
139 |
| - let encoded = encode_sockaddr_xdp(addr); |
140 |
| - |
141 |
| - f({ |
142 |
| - let mut h = zero_msghdr(); |
143 |
| - h.msg_name = as_ptr(&encoded) as _; |
144 |
| - h.msg_namelen = size_of::<SocketAddrXdp>() as _; |
145 |
| - h.msg_iov = iov.as_ptr() as _; |
146 |
| - h.msg_iovlen = msg_iov_len(iov.len()); |
147 |
| - h.msg_control = control.as_control_ptr().cast(); |
148 |
| - h.msg_controllen = msg_control_len(control.control_len()); |
149 |
| - h |
| 70 | + addr.with_sockaddr(|addr_ptr, addr_len| { |
| 71 | + f({ |
| 72 | + let mut h = zero_msghdr(); |
| 73 | + h.msg_name = addr_ptr as *mut _; |
| 74 | + h.msg_namelen = addr_len as c::socklen_t; |
| 75 | + h.msg_iov = iov.as_ptr() as _; |
| 76 | + h.msg_iovlen = msg_iov_len(iov.len()); |
| 77 | + h.msg_control = control.as_control_ptr().cast(); |
| 78 | + h.msg_controllen = msg_control_len(control.control_len()); |
| 79 | + h |
| 80 | + }) |
150 | 81 | })
|
151 | 82 | }
|
152 | 83 |
|
|
0 commit comments