Skip to content

Commit

Permalink
ffi: add optional qlog enablement
Browse files Browse the repository at this point in the history
Example usage:

  quiche_config_set_qlog_file(config, scid, LOCAL_CONN_ID_LEN,
                            "/var/log/qlog",
                            "client","client qlog title",
                            "client qlog desc");

create a file client-<scid>.qlog
  • Loading branch information
LPardue committed Mar 11, 2020
1 parent 9300e74 commit 7b0fbb2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/quiche.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ enum quiche_cc_algorithm {
// Sets the congestion control algorithm used.
void quiche_config_set_cc_algorithm(quiche_config *config, enum quiche_cc_algorithm algo);

// Enables qlogging to the specified path. Filename includes the
// specified Connection ID.
int quiche_config_set_qlog_file(quiche_config *config,
const uint8_t *cid, size_t cid_len,
const char * dir, const char * role,
const char * title,const char * description);

// Frees the config object.
void quiche_config_free(quiche_config *config);

Expand Down
47 changes: 47 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,53 @@ pub extern fn quiche_config_set_cc_algorithm(
config.set_cc_algorithm(algo);
}

#[allow(unused_variables)]
#[no_mangle]
pub extern fn quiche_config_set_qlog_file(
config: &mut Config, cid: *const u8, cid_len: size_t, dir: *const c_char,
role: *const c_char, title: *const c_char, description: *const c_char,
) -> c_int {
#[cfg(feature = "qlogging")]
{
let role = unsafe { ffi::CStr::from_ptr(role).to_str().unwrap() };

let cid = unsafe { slice::from_raw_parts(cid, cid_len) };
let vec: Vec<String> = cid.iter().map(|b| format!("{:02x}", b)).collect();
let cid = vec.join("");

let dir = unsafe { ffi::CStr::from_ptr(dir).to_str().unwrap() };
let mut path = std::path::PathBuf::from(dir);
let filename = format!("{}-{}.qlog", role, cid);
path.push(filename);

let title = unsafe { ffi::CStr::from_ptr(title).to_str().unwrap() };
let description =
unsafe { ffi::CStr::from_ptr(description).to_str().unwrap() };

let writer = match std::fs::File::create(&path) {
Ok(f) => Some(std::io::BufWriter::new(f)),

Err(e) => None,
};

if let Some(writer) = writer {
let qlog = ConnectionQlog::new(
std::time::Instant::now(),
std::boxed::Box::new(writer),
title.to_string(),
description.to_string(),
);

config.set_qlog(qlog);
return 0;
} else {
return -2;
}
}

-1
}

#[no_mangle]
pub extern fn quiche_config_free(config: *mut Config) {
unsafe { Box::from_raw(config) };
Expand Down

0 comments on commit 7b0fbb2

Please sign in to comment.