@@ -1457,6 +1457,81 @@ impl Socket {
1457
1457
. map ( |recv_tos| recv_tos > 0 )
1458
1458
}
1459
1459
}
1460
+
1461
+ /// Set `TCP_FASTOPEN` option for this socket.
1462
+ ///
1463
+ /// ## Windows
1464
+ ///
1465
+ /// Windows supports TCP Fast Open since Windows 10.
1466
+ ///
1467
+ /// <https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options>
1468
+ ///
1469
+ /// `value` is a boolean with only `0` and `1`.
1470
+ ///
1471
+ /// ## Linux
1472
+ ///
1473
+ /// Linux supports TCP Fast Open since 3.7.
1474
+ ///
1475
+ /// <https://lwn.net/Articles/508865/>
1476
+ ///
1477
+ /// The option `value`, `qlen`, specifies this server's limit on the size of the queue of TFO requests that have
1478
+ /// not yet completed the three-way handshake (see the remarks on prevention of resource-exhaustion attacks above).
1479
+ ///
1480
+ /// It was recommended to be `5` in this document.
1481
+ ///
1482
+ /// ## macOS
1483
+ ///
1484
+ /// `value` is a boolean with only `0` and `1`.
1485
+ ///
1486
+ /// ## FreeBSD
1487
+ ///
1488
+ /// FreeBSD supports TCP Fast Open since 12.0.
1489
+ ///
1490
+ /// Example program: <https://people.freebsd.org/~pkelsey/tfo-tools/tfo-srv.c>
1491
+ ///
1492
+ /// `value` is a boolean with only `0` and `1`.
1493
+ #[ cfg( any(
1494
+ target_os = "linux" ,
1495
+ target_os = "android" ,
1496
+ target_os = "freebsd" ,
1497
+ target_os = "macos" ,
1498
+ target_os = "ios" ,
1499
+ target_os = "watchos" ,
1500
+ target_os = "tvos" ,
1501
+ target_os = "windows"
1502
+ ) ) ]
1503
+ pub fn set_tcp_fastopen ( & self , value : u32 ) -> io:: Result < ( ) > {
1504
+ unsafe {
1505
+ setsockopt :: < c_int > (
1506
+ self . as_raw ( ) ,
1507
+ sys:: IPPROTO_TCP ,
1508
+ sys:: TCP_FASTOPEN ,
1509
+ value as c_int ,
1510
+ )
1511
+ }
1512
+ }
1513
+
1514
+ /// Get the value of `TCP_FASTOPEN` option for this socket.
1515
+ ///
1516
+ /// For more information about this option, see [`set_tcp_fastopen`].
1517
+ ///
1518
+ /// [`set_tcp_fastopen`]: Socket::set_tcp_fastopen
1519
+ #[ cfg( any(
1520
+ target_os = "linux" ,
1521
+ target_os = "android" ,
1522
+ target_os = "freebsd" ,
1523
+ target_os = "macos" ,
1524
+ target_os = "ios" ,
1525
+ target_os = "watchos" ,
1526
+ target_os = "tvos" ,
1527
+ target_os = "windows"
1528
+ ) ) ]
1529
+ pub fn tcp_fastopen ( & self ) -> io:: Result < u32 > {
1530
+ unsafe {
1531
+ getsockopt :: < c_int > ( self . as_raw ( ) , sys:: IPPROTO_TCP , sys:: TCP_FASTOPEN )
1532
+ . map ( |c| c as u32 )
1533
+ }
1534
+ }
1460
1535
}
1461
1536
1462
1537
/// Socket options for IPv6 sockets, get/set using `IPPROTO_IPV6`.
0 commit comments