Skip to content

Commit d6f97e8

Browse files
committed
Rename sys::Socket to RawSocket
Unfortunately Windows uses std::os::windows::io::RawSocket (u64), which is different from the RawSocket that we define that uses windows_sys::Win32::Networking::WinSock::SOCKET (usize). So the std lib version gets ranamed to StdRawSocket.
1 parent 18cc89b commit d6f97e8

File tree

3 files changed

+105
-87
lines changed

3 files changed

+105
-87
lines changed

src/socket.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Socket {
8686
/// this should really be marked `unsafe`, but this being an internal
8787
/// function, often passed as mapping function, it's makes it very
8888
/// inconvenient to mark it as `unsafe`.
89-
pub(crate) fn from_raw(raw: sys::Socket) -> Socket {
89+
pub(crate) fn from_raw(raw: sys::RawSocket) -> Socket {
9090
Socket {
9191
inner: unsafe {
9292
// SAFETY: the caller must ensure that `raw` is a valid file
@@ -109,11 +109,11 @@ impl Socket {
109109
}
110110
}
111111

112-
pub(crate) fn as_raw(&self) -> sys::Socket {
112+
pub(crate) fn as_raw(&self) -> sys::RawSocket {
113113
sys::socket_as_raw(&self.inner)
114114
}
115115

116-
pub(crate) fn into_raw(self) -> sys::Socket {
116+
pub(crate) fn into_raw(self) -> sys::RawSocket {
117117
sys::socket_into_raw(self.inner)
118118
}
119119

src/sys/unix.rs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -859,35 +859,35 @@ impl SockAddr {
859859
}
860860
}
861861

862-
pub(crate) type Socket = c_int;
862+
pub(crate) type RawSocket = c_int;
863863

864-
pub(crate) unsafe fn socket_from_raw(socket: Socket) -> crate::socket::Inner {
864+
pub(crate) unsafe fn socket_from_raw(socket: RawSocket) -> crate::socket::Inner {
865865
crate::socket::Inner::from_raw_fd(socket)
866866
}
867867

868-
pub(crate) fn socket_as_raw(socket: &crate::socket::Inner) -> Socket {
868+
pub(crate) fn socket_as_raw(socket: &crate::socket::Inner) -> RawSocket {
869869
socket.as_raw_fd()
870870
}
871871

872-
pub(crate) fn socket_into_raw(socket: crate::socket::Inner) -> Socket {
872+
pub(crate) fn socket_into_raw(socket: crate::socket::Inner) -> RawSocket {
873873
socket.into_raw_fd()
874874
}
875875

876-
pub(crate) fn socket(family: c_int, ty: c_int, protocol: c_int) -> io::Result<Socket> {
876+
pub(crate) fn socket(family: c_int, ty: c_int, protocol: c_int) -> io::Result<RawSocket> {
877877
syscall!(socket(family, ty, protocol))
878878
}
879879

880880
#[cfg(all(feature = "all", unix))]
881-
pub(crate) fn socketpair(family: c_int, ty: c_int, protocol: c_int) -> io::Result<[Socket; 2]> {
881+
pub(crate) fn socketpair(family: c_int, ty: c_int, protocol: c_int) -> io::Result<[RawSocket; 2]> {
882882
let mut fds = [0, 0];
883883
syscall!(socketpair(family, ty, protocol, fds.as_mut_ptr())).map(|_| fds)
884884
}
885885

886-
pub(crate) fn bind(fd: Socket, addr: &SockAddr) -> io::Result<()> {
886+
pub(crate) fn bind(fd: RawSocket, addr: &SockAddr) -> io::Result<()> {
887887
syscall!(bind(fd, addr.as_ptr().cast::<sockaddr>(), addr.len() as _)).map(|_| ())
888888
}
889889

890-
pub(crate) fn connect(fd: Socket, addr: &SockAddr) -> io::Result<()> {
890+
pub(crate) fn connect(fd: RawSocket, addr: &SockAddr) -> io::Result<()> {
891891
syscall!(connect(fd, addr.as_ptr().cast::<sockaddr>(), addr.len())).map(|_| ())
892892
}
893893

@@ -933,46 +933,46 @@ pub(crate) fn poll_connect(socket: &crate::Socket, timeout: Duration) -> io::Res
933933
}
934934
}
935935

936-
pub(crate) fn listen(fd: Socket, backlog: c_int) -> io::Result<()> {
936+
pub(crate) fn listen(fd: RawSocket, backlog: c_int) -> io::Result<()> {
937937
syscall!(listen(fd, backlog)).map(|_| ())
938938
}
939939

940-
pub(crate) fn accept(fd: Socket) -> io::Result<(Socket, SockAddr)> {
940+
pub(crate) fn accept(fd: RawSocket) -> io::Result<(RawSocket, SockAddr)> {
941941
// Safety: `accept` initialises the `SockAddr` for us.
942942
unsafe { SockAddr::try_init(|storage, len| syscall!(accept(fd, storage.cast(), len))) }
943943
}
944944

945-
pub(crate) fn getsockname(fd: Socket) -> io::Result<SockAddr> {
945+
pub(crate) fn getsockname(fd: RawSocket) -> io::Result<SockAddr> {
946946
// Safety: `accept` initialises the `SockAddr` for us.
947947
unsafe { SockAddr::try_init(|storage, len| syscall!(getsockname(fd, storage.cast(), len))) }
948948
.map(|(_, addr)| addr)
949949
}
950950

951-
pub(crate) fn getpeername(fd: Socket) -> io::Result<SockAddr> {
951+
pub(crate) fn getpeername(fd: RawSocket) -> io::Result<SockAddr> {
952952
// Safety: `accept` initialises the `SockAddr` for us.
953953
unsafe { SockAddr::try_init(|storage, len| syscall!(getpeername(fd, storage.cast(), len))) }
954954
.map(|(_, addr)| addr)
955955
}
956956

957-
pub(crate) fn try_clone(fd: Socket) -> io::Result<Socket> {
957+
pub(crate) fn try_clone(fd: RawSocket) -> io::Result<RawSocket> {
958958
syscall!(fcntl(fd, libc::F_DUPFD_CLOEXEC, 0))
959959
}
960960

961961
#[cfg(all(feature = "all", unix, not(target_os = "vita")))]
962-
pub(crate) fn nonblocking(fd: Socket) -> io::Result<bool> {
962+
pub(crate) fn nonblocking(fd: RawSocket) -> io::Result<bool> {
963963
let file_status_flags = fcntl_get(fd, libc::F_GETFL)?;
964964
Ok((file_status_flags & libc::O_NONBLOCK) != 0)
965965
}
966966

967967
#[cfg(all(feature = "all", target_os = "vita"))]
968-
pub(crate) fn nonblocking(fd: Socket) -> io::Result<bool> {
968+
pub(crate) fn nonblocking(fd: RawSocket) -> io::Result<bool> {
969969
unsafe {
970970
getsockopt::<Bool>(fd, libc::SOL_SOCKET, libc::SO_NONBLOCK).map(|non_block| non_block != 0)
971971
}
972972
}
973973

974974
#[cfg(not(target_os = "vita"))]
975-
pub(crate) fn set_nonblocking(fd: Socket, nonblocking: bool) -> io::Result<()> {
975+
pub(crate) fn set_nonblocking(fd: RawSocket, nonblocking: bool) -> io::Result<()> {
976976
if nonblocking {
977977
fcntl_add(fd, libc::F_GETFL, libc::F_SETFL, libc::O_NONBLOCK)
978978
} else {
@@ -981,7 +981,7 @@ pub(crate) fn set_nonblocking(fd: Socket, nonblocking: bool) -> io::Result<()> {
981981
}
982982

983983
#[cfg(target_os = "vita")]
984-
pub(crate) fn set_nonblocking(fd: Socket, nonblocking: bool) -> io::Result<()> {
984+
pub(crate) fn set_nonblocking(fd: RawSocket, nonblocking: bool) -> io::Result<()> {
985985
unsafe {
986986
setsockopt(
987987
fd,
@@ -992,7 +992,7 @@ pub(crate) fn set_nonblocking(fd: Socket, nonblocking: bool) -> io::Result<()> {
992992
}
993993
}
994994

995-
pub(crate) fn shutdown(fd: Socket, how: Shutdown) -> io::Result<()> {
995+
pub(crate) fn shutdown(fd: RawSocket, how: Shutdown) -> io::Result<()> {
996996
let how = match how {
997997
Shutdown::Write => libc::SHUT_WR,
998998
Shutdown::Read => libc::SHUT_RD,
@@ -1001,7 +1001,7 @@ pub(crate) fn shutdown(fd: Socket, how: Shutdown) -> io::Result<()> {
10011001
syscall!(shutdown(fd, how)).map(|_| ())
10021002
}
10031003

1004-
pub(crate) fn recv(fd: Socket, buf: &mut [MaybeUninit<u8>], flags: c_int) -> io::Result<usize> {
1004+
pub(crate) fn recv(fd: RawSocket, buf: &mut [MaybeUninit<u8>], flags: c_int) -> io::Result<usize> {
10051005
syscall!(recv(
10061006
fd,
10071007
buf.as_mut_ptr().cast(),
@@ -1012,7 +1012,7 @@ pub(crate) fn recv(fd: Socket, buf: &mut [MaybeUninit<u8>], flags: c_int) -> io:
10121012
}
10131013

10141014
pub(crate) fn recv_from(
1015-
fd: Socket,
1015+
fd: RawSocket,
10161016
buf: &mut [MaybeUninit<u8>],
10171017
flags: c_int,
10181018
) -> io::Result<(usize, SockAddr)> {
@@ -1032,7 +1032,7 @@ pub(crate) fn recv_from(
10321032
}
10331033
}
10341034

1035-
pub(crate) fn peek_sender(fd: Socket) -> io::Result<SockAddr> {
1035+
pub(crate) fn peek_sender(fd: RawSocket) -> io::Result<SockAddr> {
10361036
// Unix-like platforms simply truncate the returned data, so this implementation is trivial.
10371037
// However, for Windows this requires suppressing the `WSAEMSGSIZE` error,
10381038
// so that requires a different approach.
@@ -1043,7 +1043,7 @@ pub(crate) fn peek_sender(fd: Socket) -> io::Result<SockAddr> {
10431043

10441044
#[cfg(not(target_os = "redox"))]
10451045
pub(crate) fn recv_vectored(
1046-
fd: Socket,
1046+
fd: RawSocket,
10471047
bufs: &mut [crate::MaybeUninitSlice<'_>],
10481048
flags: c_int,
10491049
) -> io::Result<(usize, RecvFlags)> {
@@ -1054,7 +1054,7 @@ pub(crate) fn recv_vectored(
10541054

10551055
#[cfg(not(target_os = "redox"))]
10561056
pub(crate) fn recv_from_vectored(
1057-
fd: Socket,
1057+
fd: RawSocket,
10581058
bufs: &mut [crate::MaybeUninitSlice<'_>],
10591059
flags: c_int,
10601060
) -> io::Result<(usize, RecvFlags, SockAddr)> {
@@ -1076,14 +1076,14 @@ pub(crate) fn recv_from_vectored(
10761076

10771077
#[cfg(not(target_os = "redox"))]
10781078
pub(crate) fn recvmsg(
1079-
fd: Socket,
1079+
fd: RawSocket,
10801080
msg: &mut MsgHdrMut<'_, '_, '_>,
10811081
flags: c_int,
10821082
) -> io::Result<usize> {
10831083
syscall!(recvmsg(fd, &mut msg.inner, flags)).map(|n| n as usize)
10841084
}
10851085

1086-
pub(crate) fn send(fd: Socket, buf: &[u8], flags: c_int) -> io::Result<usize> {
1086+
pub(crate) fn send(fd: RawSocket, buf: &[u8], flags: c_int) -> io::Result<usize> {
10871087
syscall!(send(
10881088
fd,
10891089
buf.as_ptr().cast(),
@@ -1094,12 +1094,21 @@ pub(crate) fn send(fd: Socket, buf: &[u8], flags: c_int) -> io::Result<usize> {
10941094
}
10951095

10961096
#[cfg(not(target_os = "redox"))]
1097-
pub(crate) fn send_vectored(fd: Socket, bufs: &[IoSlice<'_>], flags: c_int) -> io::Result<usize> {
1097+
pub(crate) fn send_vectored(
1098+
fd: RawSocket,
1099+
bufs: &[IoSlice<'_>],
1100+
flags: c_int,
1101+
) -> io::Result<usize> {
10981102
let msg = MsgHdr::new().with_buffers(bufs);
10991103
sendmsg(fd, &msg, flags)
11001104
}
11011105

1102-
pub(crate) fn send_to(fd: Socket, buf: &[u8], addr: &SockAddr, flags: c_int) -> io::Result<usize> {
1106+
pub(crate) fn send_to(
1107+
fd: RawSocket,
1108+
buf: &[u8],
1109+
addr: &SockAddr,
1110+
flags: c_int,
1111+
) -> io::Result<usize> {
11031112
syscall!(sendto(
11041113
fd,
11051114
buf.as_ptr().cast(),
@@ -1113,7 +1122,7 @@ pub(crate) fn send_to(fd: Socket, buf: &[u8], addr: &SockAddr, flags: c_int) ->
11131122

11141123
#[cfg(not(target_os = "redox"))]
11151124
pub(crate) fn send_to_vectored(
1116-
fd: Socket,
1125+
fd: RawSocket,
11171126
bufs: &[IoSlice<'_>],
11181127
addr: &SockAddr,
11191128
flags: c_int,
@@ -1123,12 +1132,12 @@ pub(crate) fn send_to_vectored(
11231132
}
11241133

11251134
#[cfg(not(target_os = "redox"))]
1126-
pub(crate) fn sendmsg(fd: Socket, msg: &MsgHdr<'_, '_, '_>, flags: c_int) -> io::Result<usize> {
1135+
pub(crate) fn sendmsg(fd: RawSocket, msg: &MsgHdr<'_, '_, '_>, flags: c_int) -> io::Result<usize> {
11271136
syscall!(sendmsg(fd, &msg.inner, flags)).map(|n| n as usize)
11281137
}
11291138

11301139
/// Wrapper around `getsockopt` to deal with platform specific timeouts.
1131-
pub(crate) fn timeout_opt(fd: Socket, opt: c_int, val: c_int) -> io::Result<Option<Duration>> {
1140+
pub(crate) fn timeout_opt(fd: RawSocket, opt: c_int, val: c_int) -> io::Result<Option<Duration>> {
11321141
unsafe { getsockopt(fd, opt, val).map(from_timeval) }
11331142
}
11341143

@@ -1144,7 +1153,7 @@ const fn from_timeval(duration: libc::timeval) -> Option<Duration> {
11441153

11451154
/// Wrapper around `setsockopt` to deal with platform specific timeouts.
11461155
pub(crate) fn set_timeout_opt(
1147-
fd: Socket,
1156+
fd: RawSocket,
11481157
opt: c_int,
11491158
val: c_int,
11501159
duration: Option<Duration>,
@@ -1172,15 +1181,15 @@ fn into_timeval(duration: Option<Duration>) -> libc::timeval {
11721181
feature = "all",
11731182
not(any(target_os = "haiku", target_os = "openbsd", target_os = "vita"))
11741183
))]
1175-
pub(crate) fn tcp_keepalive_time(fd: Socket) -> io::Result<Duration> {
1184+
pub(crate) fn tcp_keepalive_time(fd: RawSocket) -> io::Result<Duration> {
11761185
unsafe {
11771186
getsockopt::<c_int>(fd, IPPROTO_TCP, KEEPALIVE_TIME)
11781187
.map(|secs| Duration::from_secs(secs as u64))
11791188
}
11801189
}
11811190

11821191
#[allow(unused_variables)]
1183-
pub(crate) fn set_tcp_keepalive(fd: Socket, keepalive: &TcpKeepalive) -> io::Result<()> {
1192+
pub(crate) fn set_tcp_keepalive(fd: RawSocket, keepalive: &TcpKeepalive) -> io::Result<()> {
11841193
#[cfg(not(any(
11851194
target_os = "haiku",
11861195
target_os = "openbsd",
@@ -1241,13 +1250,13 @@ fn into_secs(duration: Duration) -> c_int {
12411250

12421251
/// Get the flags using `cmd`.
12431252
#[cfg(not(target_os = "vita"))]
1244-
fn fcntl_get(fd: Socket, cmd: c_int) -> io::Result<c_int> {
1253+
fn fcntl_get(fd: RawSocket, cmd: c_int) -> io::Result<c_int> {
12451254
syscall!(fcntl(fd, cmd))
12461255
}
12471256

12481257
/// Add `flag` to the current set flags of `F_GETFD`.
12491258
#[cfg(not(target_os = "vita"))]
1250-
fn fcntl_add(fd: Socket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> {
1259+
fn fcntl_add(fd: RawSocket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> {
12511260
let previous = fcntl_get(fd, get_cmd)?;
12521261
let new = previous | flag;
12531262
if new != previous {
@@ -1260,7 +1269,7 @@ fn fcntl_add(fd: Socket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Res
12601269

12611270
/// Remove `flag` to the current set flags of `F_GETFD`.
12621271
#[cfg(not(target_os = "vita"))]
1263-
fn fcntl_remove(fd: Socket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> {
1272+
fn fcntl_remove(fd: RawSocket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> {
12641273
let previous = fcntl_get(fd, get_cmd)?;
12651274
let new = previous & !flag;
12661275
if new != previous {
@@ -1272,7 +1281,7 @@ fn fcntl_remove(fd: Socket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::
12721281
}
12731282

12741283
/// Caller must ensure `T` is the correct type for `opt` and `val`.
1275-
pub(crate) unsafe fn getsockopt<T>(fd: Socket, opt: c_int, val: c_int) -> io::Result<T> {
1284+
pub(crate) unsafe fn getsockopt<T>(fd: RawSocket, opt: c_int, val: c_int) -> io::Result<T> {
12761285
let mut payload: MaybeUninit<T> = MaybeUninit::uninit();
12771286
let mut len = size_of::<T>() as libc::socklen_t;
12781287
syscall!(getsockopt(
@@ -1291,7 +1300,7 @@ pub(crate) unsafe fn getsockopt<T>(fd: Socket, opt: c_int, val: c_int) -> io::Re
12911300

12921301
/// Caller must ensure `T` is the correct type for `opt` and `val`.
12931302
pub(crate) unsafe fn setsockopt<T>(
1294-
fd: Socket,
1303+
fd: RawSocket,
12951304
opt: c_int,
12961305
val: c_int,
12971306
payload: T,
@@ -1365,7 +1374,7 @@ pub(crate) const fn to_mreqn(
13651374
feature = "all",
13661375
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
13671376
))]
1368-
pub(crate) fn original_dst_v4(fd: Socket) -> io::Result<SockAddr> {
1377+
pub(crate) fn original_dst_v4(fd: RawSocket) -> io::Result<SockAddr> {
13691378
// Safety: `getsockopt` initialises the `SockAddr` for us.
13701379
unsafe {
13711380
SockAddr::try_init(|storage, len| {
@@ -1386,7 +1395,7 @@ pub(crate) fn original_dst_v4(fd: Socket) -> io::Result<SockAddr> {
13861395
/// This value contains the original destination IPv6 address of the connection
13871396
/// redirected using `ip6tables` `REDIRECT` or `TPROXY`.
13881397
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
1389-
pub(crate) fn original_dst_v6(fd: Socket) -> io::Result<SockAddr> {
1398+
pub(crate) fn original_dst_v6(fd: RawSocket) -> io::Result<SockAddr> {
13901399
// Safety: `getsockopt` initialises the `SockAddr` for us.
13911400
unsafe {
13921401
SockAddr::try_init(|storage, len| {

0 commit comments

Comments
 (0)