@@ -277,15 +277,27 @@ fn keepalive() {
277
277
#[ cfg( all( feature = "all" , target_os = "linux" ) ) ]
278
278
#[ test]
279
279
fn device ( ) {
280
- const INTERFACE : & str = "lo0\0 " ;
281
- let interface = CStr :: from_bytes_with_nul ( INTERFACE . as_bytes ( ) ) . unwrap ( ) ;
282
- let socket = Socket :: new ( Domain :: IPV4 , Type :: STREAM , None ) . unwrap ( ) ;
280
+ // Some common network interface on Linux.
281
+ const INTERFACES : & [ & str ] = & [ "lo\0 " , "lo0\0 " , "eth0\0 " , "wlan0\0 " ] ;
283
282
283
+ let socket = Socket :: new ( Domain :: IPV4 , Type :: STREAM , None ) . unwrap ( ) ;
284
284
assert_eq ! ( socket. device( ) . unwrap( ) , None ) ;
285
285
286
- socket. bind_device ( Some ( interface) ) . unwrap ( ) ;
287
- assert_eq ! ( socket. device( ) . unwrap( ) . as_deref( ) , Some ( interface) ) ;
288
-
289
- socket. bind_device ( None ) . unwrap ( ) ;
290
- assert_eq ! ( socket. device( ) . unwrap( ) , None ) ;
286
+ for interface in INTERFACES . iter ( ) {
287
+ let interface = CStr :: from_bytes_with_nul ( interface. as_bytes ( ) ) . unwrap ( ) ;
288
+ if let Err ( err) = socket. bind_device ( Some ( interface) ) {
289
+ // Network interface is not available try another.
290
+ if let Some ( libc:: ENODEV ) = err. raw_os_error ( ) {
291
+ continue ;
292
+ } else {
293
+ panic ! ( "unexpected error binding device: {}" , err) ;
294
+ }
295
+ }
296
+ assert_eq ! ( socket. device( ) . unwrap( ) . as_deref( ) , Some ( interface) ) ;
297
+
298
+ socket. bind_device ( None ) . unwrap ( ) ;
299
+ assert_eq ! ( socket. device( ) . unwrap( ) , None ) ;
300
+ // Just need to do it with one interface.
301
+ break ;
302
+ }
291
303
}
0 commit comments