MCPcopy Index your code
hub / github.com/RustPython/RustPython / _servername_callback

Function _servername_callback

crates/stdlib/src/openssl.rs:649–750  ·  view source on GitHub ↗

SNI callback function called by OpenSSL

(
        ssl_ptr: *mut sys::SSL,
        al: *mut libc::c_int,
        arg: *mut libc::c_void,
    )

Source from the content-addressed store, hash-verified

647
648 // SNI callback function called by OpenSSL
649 unsafe extern "C" fn _servername_callback(
650 ssl_ptr: *mut sys::SSL,
651 al: *mut libc::c_int,
652 arg: *mut libc::c_void,
653 ) -> libc::c_int {
654 const SSL_TLSEXT_ERR_OK: libc::c_int = 0;
655 const SSL_TLSEXT_ERR_ALERT_FATAL: libc::c_int = 2;
656 const SSL_AD_INTERNAL_ERROR: libc::c_int = 80;
657 const TLSEXT_NAMETYPE_host_name: libc::c_int = 0;
658
659 if arg.is_null() {
660 return SSL_TLSEXT_ERR_OK;
661 }
662
663 unsafe {
664 let ctx = &*(arg as *const PySslContext);
665
666 // Get the callback
667 let callback_opt = ctx.sni_callback.lock().clone();
668 let Some(callback) = callback_opt else {
669 return SSL_TLSEXT_ERR_OK;
670 };
671
672 // Get callback data from SSL ex_data
673 let idx = get_sni_ex_data_index();
674 let data_ptr = sys::SSL_get_ex_data(ssl_ptr, idx);
675 if data_ptr.is_null() {
676 return SSL_TLSEXT_ERR_ALERT_FATAL;
677 }
678
679 let callback_data = &*(data_ptr as *const SniCallbackData);
680
681 // Get VM from thread-local storage (set by HandshakeVmGuard in do_handshake)
682 let Some(vm_ptr) = HANDSHAKE_VM.with(|cell| cell.get()) else {
683 // VM not available - this shouldn't happen during handshake
684 *al = SSL_AD_INTERNAL_ERROR;
685 return SSL_TLSEXT_ERR_ALERT_FATAL;
686 };
687 let vm = &*vm_ptr;
688
689 // Get server name
690 let servername = sys::SSL_get_servername(ssl_ptr, TLSEXT_NAMETYPE_host_name);
691 let server_name_arg = if servername.is_null() {
692 vm.ctx.none()
693 } else {
694 let name_cstr = std::ffi::CStr::from_ptr(servername);
695 match name_cstr.to_str() {
696 Ok(name_str) => vm.ctx.new_str(name_str).into(),
697 Err(_) => vm.ctx.none(),
698 }
699 };
700
701 // Get SSL socket from callback data via weak reference
702 let ssl_socket_obj = callback_data
703 .ssl_socket_weak
704 .upgrade()
705 .unwrap_or_else(|| vm.ctx.none());
706

Callers

nothing calls this directly

Calls 13

get_sni_ex_data_indexFunction · 0.85
withMethod · 0.80
noneMethod · 0.80
to_strMethod · 0.80
run_unraisableMethod · 0.80
cloneMethod · 0.45
lockMethod · 0.45
getMethod · 0.45
new_strMethod · 0.45
upgradeMethod · 0.45
callMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected