-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
g3-socket: support windows SO_REUSE_UNICASTPORT
- Loading branch information
Showing
6 changed files
with
92 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2024 ByteDance and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
mod unix; | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
pub(crate) use unix::set_bind_address_no_port; | ||
|
||
#[cfg(windows)] | ||
mod windows; | ||
#[cfg(windows)] | ||
pub(crate) use windows::set_reuse_unicastport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2024 ByteDance and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
use std::io; | ||
use std::os::windows::io::AsRawSocket; | ||
|
||
use libc::{c_char, c_int, SOCKET}; | ||
|
||
// windows_sys::Win32::Networking::WinSock::SOL_SOCKET | ||
const SOL_SOCKET: i32 = 65535i32; | ||
|
||
// windows_sys::Win32::Networking::WinSock::SO_REUSE_UNICASTPORT | ||
const SO_REUSE_UNICASTPORT: i32 = 12295i32; | ||
|
||
unsafe fn setsockopt<T>(socket: SOCKET, level: c_int, name: c_int, value: T) -> io::Result<()> | ||
where | ||
T: Copy, | ||
{ | ||
let payload = &value as *const T as *const c_char; | ||
let ret = libc::setsockopt(socket, level, name, payload, size_of::<T>() as c_int); | ||
if ret == -1 { | ||
return Err(io::Error::last_os_error()); | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn set_reuse_unicastport<T: AsRawSocket>(socket: &T, enable: bool) -> io::Result<()> { | ||
unsafe { | ||
setsockopt( | ||
socket.as_raw_socket(), | ||
SOL_SOCKET, | ||
SO_REUSE_UNICASTPORT, | ||
enable as c_int, | ||
)?; | ||
Ok(()) | ||
} | ||
} |