Skip to content

Expose open_socket callback to Easy #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/easy/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::ptr;
use std::str;
use std::time::Duration;

use libc::c_int;
use libc::c_void;

use crate::easy::handler::{self, InfoType, ReadError, SeekResult, WriteError};
Expand Down Expand Up @@ -111,6 +112,7 @@ struct Callbacks<'a> {
header: Option<Box<dyn FnMut(&[u8]) -> bool + 'a>>,
progress: Option<Box<dyn FnMut(f64, f64, f64, f64) -> bool + 'a>>,
ssl_ctx: Option<Box<dyn FnMut(*mut c_void) -> Result<(), Error> + 'a>>,
open_socket: Option<Box<dyn FnMut(c_int, c_int, c_int) -> Option<curl_sys::curl_socket_t>>>,
}

impl Easy {
Expand Down Expand Up @@ -542,6 +544,27 @@ impl Easy {
Ok(())
}

/// Callback to open sockets for libcurl.
///
/// This callback function gets called by libcurl instead of the socket(2)
/// call. The callback function should return the newly created socket
/// or `None` in case no connection could be established or another
/// error was detected. Any additional `setsockopt(2)` calls can of course
/// be done on the socket at the user's discretion. A `None` return
/// value from the callback function will signal an unrecoverable error to
/// libcurl and it will return `is_couldnt_connect` from the function that
/// triggered this callback.
///
/// By default this function opens a standard socket and
/// corresponds to `CURLOPT_OPENSOCKETFUNCTION `.
pub fn open_socket_function<F>(&mut self, f: F) -> Result<(), Error>
where
F: FnMut(c_int, c_int, c_int) -> Option<curl_sys::curl_socket_t> + Send + 'static,
{
self.inner.get_mut().owned.open_socket = Some(Box::new(f));
Ok(())
}

// =========================================================================
// Error options

Expand Down Expand Up @@ -1568,6 +1591,20 @@ impl Handler for EasyData {
}
}
}

fn open_socket(
&mut self,
family: c_int,
socktype: c_int,
protocol: c_int,
) -> Option<curl_sys::curl_socket_t> {
unsafe {
match self.callback(|s| &mut s.open_socket) {
Some(open_socket) => open_socket(family, socktype, protocol),
None => handler::open_socket(family, socktype, protocol),
}
}
}
}

impl fmt::Debug for EasyData {
Expand Down
44 changes: 26 additions & 18 deletions src/easy/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,7 @@ pub trait Handler {
socktype: c_int,
protocol: c_int,
) -> Option<curl_sys::curl_socket_t> {
// Note that we override this to calling a function in `socket2` to
// ensure that we open all sockets with CLOEXEC. Otherwise if we rely on
// libcurl to open sockets it won't use CLOEXEC.
return Socket::new(family.into(), socktype.into(), Some(protocol.into()))
.ok()
.map(cvt);

#[cfg(unix)]
fn cvt(socket: Socket) -> curl_sys::curl_socket_t {
use std::os::unix::prelude::*;
socket.into_raw_fd()
}

#[cfg(windows)]
fn cvt(socket: Socket) -> curl_sys::curl_socket_t {
use std::os::windows::prelude::*;
socket.into_raw_socket()
}
open_socket(family, socktype, protocol)
}
}

Expand All @@ -321,6 +304,31 @@ pub fn ssl_ctx(cx: *mut c_void) -> Result<(), Error> {
Ok(())
}

pub fn open_socket(
family: c_int,
socktype: c_int,
protocol: c_int,
) -> Option<curl_sys::curl_socket_t> {
// Note that we override this to calling a function in `socket2` to
// ensure that we open all sockets with CLOEXEC. Otherwise if we rely on
// libcurl to open sockets it won't use CLOEXEC.
return Socket::new(family.into(), socktype.into(), Some(protocol.into()))
.ok()
.map(cvt);

#[cfg(unix)]
fn cvt(socket: Socket) -> curl_sys::curl_socket_t {
use std::os::unix::prelude::*;
socket.into_raw_fd()
}

#[cfg(windows)]
fn cvt(socket: Socket) -> curl_sys::curl_socket_t {
use std::os::windows::prelude::*;
socket.into_raw_socket()
}
}

/// Raw bindings to a libcurl "easy session".
///
/// This type corresponds to the `CURL` type in libcurl, and is probably what
Expand Down