3
3
use std:: {
4
4
io:: { Error , Result } ,
5
5
mem,
6
- os:: unix:: io:: { AsRawFd , FromRawFd , RawFd } ,
6
+ os:: {
7
+ fd:: { AsFd , BorrowedFd } ,
8
+ unix:: io:: { AsRawFd , RawFd } ,
9
+ } ,
7
10
} ;
8
11
9
12
use crate :: SocketAddr ;
@@ -51,7 +54,7 @@ use crate::SocketAddr;
51
54
/// }
52
55
/// }
53
56
/// ```
54
- #[ derive( Clone , Debug ) ]
57
+ #[ derive( Debug ) ]
55
58
pub struct Socket ( RawFd ) ;
56
59
57
60
impl AsRawFd for Socket {
@@ -60,15 +63,15 @@ impl AsRawFd for Socket {
60
63
}
61
64
}
62
65
63
- impl FromRawFd for Socket {
64
- unsafe fn from_raw_fd ( fd : RawFd ) -> Self {
65
- Socket ( fd )
66
+ impl AsFd for Socket {
67
+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
68
+ unsafe { BorrowedFd :: borrow_raw ( self . 0 ) }
66
69
}
67
70
}
68
71
69
72
impl Drop for Socket {
70
73
fn drop ( & mut self ) {
71
- unsafe { libc:: close ( self . 0 ) } ;
74
+ unsafe { libc:: close ( self . as_raw_fd ( ) ) } ;
72
75
}
73
76
}
74
77
@@ -94,7 +97,7 @@ impl Socket {
94
97
/// Bind the socket to the given address
95
98
pub fn bind ( & mut self , addr : & SocketAddr ) -> Result < ( ) > {
96
99
let ( addr_ptr, addr_len) = addr. as_raw ( ) ;
97
- let res = unsafe { libc:: bind ( self . 0 , addr_ptr, addr_len) } ;
100
+ let res = unsafe { libc:: bind ( self . as_raw_fd ( ) , addr_ptr, addr_len) } ;
98
101
if res < 0 {
99
102
return Err ( Error :: last_os_error ( ) ) ;
100
103
}
@@ -115,7 +118,9 @@ impl Socket {
115
118
let ( addr_ptr, mut addr_len) = addr. as_raw_mut ( ) ;
116
119
let addr_len_copy = addr_len;
117
120
let addr_len_ptr = & mut addr_len as * mut libc:: socklen_t ;
118
- let res = unsafe { libc:: getsockname ( self . 0 , addr_ptr, addr_len_ptr) } ;
121
+ let res = unsafe {
122
+ libc:: getsockname ( self . as_raw_fd ( ) , addr_ptr, addr_len_ptr)
123
+ } ;
119
124
if res < 0 {
120
125
return Err ( Error :: last_os_error ( ) ) ;
121
126
}
@@ -128,8 +133,9 @@ impl Socket {
128
133
/// Make this socket non-blocking
129
134
pub fn set_non_blocking ( & self , non_blocking : bool ) -> Result < ( ) > {
130
135
let mut non_blocking = non_blocking as libc:: c_int ;
131
- let res =
132
- unsafe { libc:: ioctl ( self . 0 , libc:: FIONBIO , & mut non_blocking) } ;
136
+ let res = unsafe {
137
+ libc:: ioctl ( self . as_raw_fd ( ) , libc:: FIONBIO , & mut non_blocking)
138
+ } ;
133
139
if res < 0 {
134
140
return Err ( Error :: last_os_error ( ) ) ;
135
141
}
@@ -152,7 +158,7 @@ impl Socket {
152
158
/// 2. connect it to the kernel with [`Socket::connect`]
153
159
/// 3. send a request to the kernel with [`Socket::send`]
154
160
/// 4. read the response (which can span over several messages)
155
- /// [`Socket::recv`]
161
+ /// [`Socket::recv`]
156
162
///
157
163
/// ```rust
158
164
/// use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};
@@ -216,7 +222,7 @@ impl Socket {
216
222
// - https://stackoverflow.com/a/14046386/1836144
217
223
// - https://lists.isc.org/pipermail/bind-users/2009-August/077527.html
218
224
let ( addr, addr_len) = remote_addr. as_raw ( ) ;
219
- let res = unsafe { libc:: connect ( self . 0 , addr, addr_len) } ;
225
+ let res = unsafe { libc:: connect ( self . as_raw_fd ( ) , addr, addr_len) } ;
220
226
if res < 0 {
221
227
return Err ( Error :: last_os_error ( ) ) ;
222
228
}
@@ -293,7 +299,7 @@ impl Socket {
293
299
294
300
let res = unsafe {
295
301
libc:: recvfrom (
296
- self . 0 ,
302
+ self . as_raw_fd ( ) ,
297
303
buf_ptr,
298
304
buf_len,
299
305
flags,
@@ -324,7 +330,8 @@ impl Socket {
324
330
let buf_ptr = chunk. as_mut_ptr ( ) as * mut libc:: c_void ;
325
331
let buf_len = chunk. len ( ) as libc:: size_t ;
326
332
327
- let res = unsafe { libc:: recv ( self . 0 , buf_ptr, buf_len, flags) } ;
333
+ let res =
334
+ unsafe { libc:: recv ( self . as_raw_fd ( ) , buf_ptr, buf_len, flags) } ;
328
335
if res < 0 {
329
336
return Err ( Error :: last_os_error ( ) ) ;
330
337
} else {
@@ -367,7 +374,14 @@ impl Socket {
367
374
let buf_len = buf. len ( ) as libc:: size_t ;
368
375
369
376
let res = unsafe {
370
- libc:: sendto ( self . 0 , buf_ptr, buf_len, flags, addr_ptr, addr_len)
377
+ libc:: sendto (
378
+ self . as_raw_fd ( ) ,
379
+ buf_ptr,
380
+ buf_len,
381
+ flags,
382
+ addr_ptr,
383
+ addr_len,
384
+ )
371
385
} ;
372
386
if res < 0 {
373
387
return Err ( Error :: last_os_error ( ) ) ;
@@ -382,7 +396,8 @@ impl Socket {
382
396
let buf_ptr = buf. as_ptr ( ) as * const libc:: c_void ;
383
397
let buf_len = buf. len ( ) as libc:: size_t ;
384
398
385
- let res = unsafe { libc:: send ( self . 0 , buf_ptr, buf_len, flags) } ;
399
+ let res =
400
+ unsafe { libc:: send ( self . as_raw_fd ( ) , buf_ptr, buf_len, flags) } ;
386
401
if res < 0 {
387
402
return Err ( Error :: last_os_error ( ) ) ;
388
403
}
@@ -391,12 +406,17 @@ impl Socket {
391
406
392
407
pub fn set_pktinfo ( & mut self , value : bool ) -> Result < ( ) > {
393
408
let value: libc:: c_int = value. into ( ) ;
394
- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_PKTINFO , value)
409
+ setsockopt (
410
+ self . as_raw_fd ( ) ,
411
+ libc:: SOL_NETLINK ,
412
+ libc:: NETLINK_PKTINFO ,
413
+ value,
414
+ )
395
415
}
396
416
397
417
pub fn get_pktinfo ( & self ) -> Result < bool > {
398
418
let res = getsockopt :: < libc:: c_int > (
399
- self . 0 ,
419
+ self . as_raw_fd ( ) ,
400
420
libc:: SOL_NETLINK ,
401
421
libc:: NETLINK_PKTINFO ,
402
422
) ?;
@@ -405,7 +425,7 @@ impl Socket {
405
425
406
426
pub fn add_membership ( & mut self , group : u32 ) -> Result < ( ) > {
407
427
setsockopt (
408
- self . 0 ,
428
+ self . as_raw_fd ( ) ,
409
429
libc:: SOL_NETLINK ,
410
430
libc:: NETLINK_ADD_MEMBERSHIP ,
411
431
group,
@@ -414,7 +434,7 @@ impl Socket {
414
434
415
435
pub fn drop_membership ( & mut self , group : u32 ) -> Result < ( ) > {
416
436
setsockopt (
417
- self . 0 ,
437
+ self . as_raw_fd ( ) ,
418
438
libc:: SOL_NETLINK ,
419
439
libc:: NETLINK_DROP_MEMBERSHIP ,
420
440
group,
@@ -434,7 +454,7 @@ impl Socket {
434
454
pub fn set_broadcast_error ( & mut self , value : bool ) -> Result < ( ) > {
435
455
let value: libc:: c_int = value. into ( ) ;
436
456
setsockopt (
437
- self . 0 ,
457
+ self . as_raw_fd ( ) ,
438
458
libc:: SOL_NETLINK ,
439
459
libc:: NETLINK_BROADCAST_ERROR ,
440
460
value,
@@ -443,7 +463,7 @@ impl Socket {
443
463
444
464
pub fn get_broadcast_error ( & self ) -> Result < bool > {
445
465
let res = getsockopt :: < libc:: c_int > (
446
- self . 0 ,
466
+ self . as_raw_fd ( ) ,
447
467
libc:: SOL_NETLINK ,
448
468
libc:: NETLINK_BROADCAST_ERROR ,
449
469
) ?;
@@ -454,12 +474,17 @@ impl Socket {
454
474
/// unicast and broadcast listeners to avoid receiving `ENOBUFS` errors.
455
475
pub fn set_no_enobufs ( & mut self , value : bool ) -> Result < ( ) > {
456
476
let value: libc:: c_int = value. into ( ) ;
457
- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_NO_ENOBUFS , value)
477
+ setsockopt (
478
+ self . as_raw_fd ( ) ,
479
+ libc:: SOL_NETLINK ,
480
+ libc:: NETLINK_NO_ENOBUFS ,
481
+ value,
482
+ )
458
483
}
459
484
460
485
pub fn get_no_enobufs ( & self ) -> Result < bool > {
461
486
let res = getsockopt :: < libc:: c_int > (
462
- self . 0 ,
487
+ self . as_raw_fd ( ) ,
463
488
libc:: SOL_NETLINK ,
464
489
libc:: NETLINK_NO_ENOBUFS ,
465
490
) ?;
@@ -474,7 +499,7 @@ impl Socket {
474
499
pub fn set_listen_all_namespaces ( & mut self , value : bool ) -> Result < ( ) > {
475
500
let value: libc:: c_int = value. into ( ) ;
476
501
setsockopt (
477
- self . 0 ,
502
+ self . as_raw_fd ( ) ,
478
503
libc:: SOL_NETLINK ,
479
504
libc:: NETLINK_LISTEN_ALL_NSID ,
480
505
value,
@@ -483,7 +508,7 @@ impl Socket {
483
508
484
509
pub fn get_listen_all_namespaces ( & self ) -> Result < bool > {
485
510
let res = getsockopt :: < libc:: c_int > (
486
- self . 0 ,
511
+ self . as_raw_fd ( ) ,
487
512
libc:: SOL_NETLINK ,
488
513
libc:: NETLINK_LISTEN_ALL_NSID ,
489
514
) ?;
@@ -498,12 +523,17 @@ impl Socket {
498
523
/// acknowledgment.
499
524
pub fn set_cap_ack ( & mut self , value : bool ) -> Result < ( ) > {
500
525
let value: libc:: c_int = value. into ( ) ;
501
- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_CAP_ACK , value)
526
+ setsockopt (
527
+ self . as_raw_fd ( ) ,
528
+ libc:: SOL_NETLINK ,
529
+ libc:: NETLINK_CAP_ACK ,
530
+ value,
531
+ )
502
532
}
503
533
504
534
pub fn get_cap_ack ( & self ) -> Result < bool > {
505
535
let res = getsockopt :: < libc:: c_int > (
506
- self . 0 ,
536
+ self . as_raw_fd ( ) ,
507
537
libc:: SOL_NETLINK ,
508
538
libc:: NETLINK_CAP_ACK ,
509
539
) ?;
@@ -515,12 +545,17 @@ impl Socket {
515
545
/// NLMSG_ERROR and NLMSG_DONE messages.
516
546
pub fn set_ext_ack ( & mut self , value : bool ) -> Result < ( ) > {
517
547
let value: libc:: c_int = value. into ( ) ;
518
- setsockopt ( self . 0 , libc:: SOL_NETLINK , libc:: NETLINK_EXT_ACK , value)
548
+ setsockopt (
549
+ self . as_raw_fd ( ) ,
550
+ libc:: SOL_NETLINK ,
551
+ libc:: NETLINK_EXT_ACK ,
552
+ value,
553
+ )
519
554
}
520
555
521
556
pub fn get_ext_ack ( & self ) -> Result < bool > {
522
557
let res = getsockopt :: < libc:: c_int > (
523
- self . 0 ,
558
+ self . as_raw_fd ( ) ,
524
559
libc:: SOL_NETLINK ,
525
560
libc:: NETLINK_EXT_ACK ,
526
561
) ?;
@@ -534,13 +569,13 @@ impl Socket {
534
569
/// the maximum allowed value is set by the /proc/sys/net/core/rmem_max
535
570
/// file. The minimum (doubled) value for this option is 256.
536
571
pub fn set_rx_buf_sz < T > ( & self , size : T ) -> Result < ( ) > {
537
- setsockopt ( self . 0 , libc:: SOL_SOCKET , libc:: SO_RCVBUF , size)
572
+ setsockopt ( self . as_raw_fd ( ) , libc:: SOL_SOCKET , libc:: SO_RCVBUF , size)
538
573
}
539
574
540
575
/// Gets socket receive buffer in bytes
541
576
pub fn get_rx_buf_sz ( & self ) -> Result < usize > {
542
577
let res = getsockopt :: < libc:: c_int > (
543
- self . 0 ,
578
+ self . as_raw_fd ( ) ,
544
579
libc:: SOL_SOCKET ,
545
580
libc:: SO_RCVBUF ,
546
581
) ?;
@@ -552,7 +587,7 @@ impl Socket {
552
587
pub fn set_netlink_get_strict_chk ( & self , value : bool ) -> Result < ( ) > {
553
588
let value: u32 = value. into ( ) ;
554
589
setsockopt (
555
- self . 0 ,
590
+ self . as_raw_fd ( ) ,
556
591
libc:: SOL_NETLINK ,
557
592
libc:: NETLINK_GET_STRICT_CHK ,
558
593
value,
0 commit comments