File tree 2 files changed +14
-1
lines changed
2 files changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -463,8 +463,20 @@ impl SockAddr {
463
463
}
464
464
. map ( |( _, addr) | addr)
465
465
}
466
- }
467
466
467
+ /// Returns this address VSOCK CID/port if it is in the `AF_VSOCK` family,
468
+ /// otherwise return `None`.
469
+ #[ cfg( all( feature = "all" , any( target_os = "android" , target_os = "linux" ) ) ) ]
470
+ pub fn vsock_address ( & self ) -> Option < ( u32 , u32 ) > {
471
+ if self . family ( ) == libc:: AF_VSOCK as sa_family_t {
472
+ // Safety: if the ss_family field is AF_VSOCK then storage must be a sockaddr_vm.
473
+ let addr = unsafe { & * ( self . as_ptr ( ) as * const libc:: sockaddr_vm ) } ;
474
+ Some ( ( addr. svm_cid , addr. svm_port ) )
475
+ } else {
476
+ None
477
+ }
478
+ }
479
+ }
468
480
469
481
pub ( crate ) type Socket = c_int ;
470
482
Original file line number Diff line number Diff line change @@ -141,6 +141,7 @@ fn socket_address_vsock() {
141
141
let addr = SockAddr :: vsock ( 1 , 9999 ) . unwrap ( ) ;
142
142
assert ! ( addr. as_socket_ipv4( ) . is_none( ) ) ;
143
143
assert ! ( addr. as_socket_ipv6( ) . is_none( ) ) ;
144
+ assert_eq ! ( addr. vsock_address( ) . unwrap( ) , ( 1 , 9999 ) ) ;
144
145
}
145
146
146
147
#[ test]
You can’t perform that action at this time.
0 commit comments