[][src]Type Definition mosquitto_rs::PasswdCallback

type PasswdCallback = unsafe extern "C" fn(buf: *mut c_char, size: c_int, _: c_int, _: *mut c_void) -> c_int;

An OpenSSL password callback (see man SSL_CTX_set_default_passwd_cb_userdata).

buf is the destination for the password string. The size of buf is specified by size.

The goal of the callback is to obtain the password from the user (or some credential store) and store it into buf.

The password must be NUL terminated. The length of the password must be returned.

The other parameters to this function must be ignored as they cannot be used safely in the Rust client binding.

use std::os::raw::{c_char, c_int, c_void};
use std::convert::TryInto;

unsafe extern "C" fn my_callback(
    buf: *mut c_char,
    size: c_int,
    _: c_int,
    _: *mut c_void
) -> c_int {
  let buf = std::slice::from_raw_parts_mut(buf as *mut u8, size as usize);

  let pw = b"secret";
  buf[..pw.len()].copy_from_slice(pw);
  buf[pw.len()] = 0;
  pw.len().try_into().unwrap()
}