@@ -443,55 +443,34 @@ impl rustls_server_config {
443
443
}
444
444
}
445
445
446
- /// Copy the server name from the server name indication (SNI) extension to `buf`.
447
- ///
448
- /// `buf` can hold up to `count` bytes, and the length of that server name in `out_n` .
446
+
447
+ /// Returns a `rustls_str` reference to the server name sent by the client in a server name
448
+ /// indication (SNI) extension .
449
449
///
450
- /// The string is stored in UTF-8 with no terminating NUL byte.
450
+ /// The returned `rustls_str` is valid until the next mutating function call affecting the
451
+ /// connection. A mutating function call is one where the first argument has type
452
+ /// `struct rustls_connection *` (as opposed to `const struct rustls_connection *`). The caller
453
+ /// does not need to free the `rustls_str`.
451
454
///
452
- /// Returns RUSTLS_RESULT_INSUFFICIENT_SIZE if the SNI hostname is longer than `count`.
455
+ /// Returns a zero-length `rustls_str` if:
453
456
///
454
- /// Returns Ok with *out_n == 0 if there is no SNI hostname available on this connection
455
- /// because it hasn't been processed yet, or because the client did not send SNI.
456
- /// <https://docs.rs/rustls/latest/rustls/server/struct.ServerConnection.html#method.server_name>
457
+ /// - the connection is not a server connection.
458
+ /// - the connection is a server connection but the SNI extension in the client hello has not
459
+ /// been processed during the handshake yet. Check `rustls_connection_is_handshaking`.
460
+ /// - the SNI value contains null bytes.
457
461
#[ no_mangle]
458
462
pub extern "C" fn rustls_server_connection_get_server_name (
459
463
conn : * const rustls_connection ,
460
- buf : * mut u8 ,
461
- count : size_t ,
462
- out_n : * mut size_t ,
463
- ) -> rustls_result {
464
+ ) -> rustls_str < ' static > {
464
465
ffi_panic_boundary ! {
465
- let conn = try_ref_from_ptr!( conn) ;
466
- if buf. is_null( ) {
467
- return rustls_result:: NullParameter ;
468
- }
469
- if out_n. is_null( ) {
470
- return rustls_result:: NullParameter ;
471
- }
472
- let server_connection = match conn. as_server( ) {
473
- Some ( s) => s,
474
- _ => return rustls_result:: InvalidParameter ,
466
+ let Some ( server_connection) = try_ref_from_ptr!( conn) . as_server( ) else {
467
+ return rustls_str:: default ( ) ;
475
468
} ;
476
- let sni_hostname = match server_connection. server_name( ) {
477
- Some ( sni_hostname) => sni_hostname,
478
- None => {
479
- unsafe {
480
- * out_n = 0 ;
481
- }
482
- return rustls_result:: Ok ;
483
- }
469
+ let Some ( sni_hostname) = server_connection. server_name( ) else {
470
+ return rustls_str:: default ( ) ;
484
471
} ;
485
- let len = sni_hostname. len( ) ;
486
- if len > count {
487
- unsafe { * out_n = 0 }
488
- return rustls_result:: InsufficientSize ;
489
- }
490
- unsafe {
491
- std:: ptr:: copy_nonoverlapping( sni_hostname. as_ptr( ) , buf, len) ;
492
- * out_n = len;
493
- }
494
- rustls_result:: Ok
472
+ let res = rustls_str:: try_from( sni_hostname) . unwrap_or_default( ) ;
473
+ unsafe { res. into_static( ) }
495
474
}
496
475
}
497
476
0 commit comments