8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ use cmp;
11
12
use io:: { Error , ErrorKind , Result } ;
13
+ use mem;
12
14
use net:: { SocketAddr , Shutdown } ;
13
15
use path:: Path ;
14
16
use sys:: fs:: { File , OpenOptions } ;
17
+ use sys:: syscall:: TimeSpec ;
15
18
use sys_common:: { AsInner , FromInner , IntoInner } ;
16
19
use time:: Duration ;
17
20
use vec:: Vec ;
@@ -77,15 +80,30 @@ impl TcpStream {
77
80
}
78
81
79
82
pub fn ttl ( & self ) -> Result < u32 > {
80
- Err ( Error :: new ( ErrorKind :: Other , "TcpStream::ttl not implemented" ) )
83
+ let mut ttl = [ 0 ] ;
84
+ let file = self . 0 . dup ( b"ttl" ) ?;
85
+ file. read ( & mut ttl) ?;
86
+ Ok ( ttl[ 0 ] as u32 )
81
87
}
82
88
83
89
pub fn read_timeout ( & self ) -> Result < Option < Duration > > {
84
- Err ( Error :: new ( ErrorKind :: Other , "TcpStream::read_timeout not implemented" ) )
90
+ let mut time = TimeSpec :: default ( ) ;
91
+ let file = self . 0 . dup ( b"read_timeout" ) ?;
92
+ if file. read ( & mut time) ? >= mem:: size_of :: < TimeSpec > ( ) {
93
+ Ok ( Some ( Duration :: new ( time. tv_sec as u64 , time. tv_nsec as u32 ) ) )
94
+ } else {
95
+ Ok ( None )
96
+ }
85
97
}
86
98
87
99
pub fn write_timeout ( & self ) -> Result < Option < Duration > > {
88
- Err ( Error :: new ( ErrorKind :: Other , "TcpStream::write_timeout not implemented" ) )
100
+ let mut time = TimeSpec :: default ( ) ;
101
+ let file = self . 0 . dup ( b"write_timeout" ) ?;
102
+ if file. read ( & mut time) ? >= mem:: size_of :: < TimeSpec > ( ) {
103
+ Ok ( Some ( Duration :: new ( time. tv_sec as u64 , time. tv_nsec as u32 ) ) )
104
+ } else {
105
+ Ok ( None )
106
+ }
89
107
}
90
108
91
109
pub fn set_nodelay ( & self , _nodelay : bool ) -> Result < ( ) > {
@@ -100,16 +118,36 @@ impl TcpStream {
100
118
Err ( Error :: new ( ErrorKind :: Other , "TcpStream::set_only_v6 not implemented" ) )
101
119
}
102
120
103
- pub fn set_ttl ( & self , _ttl : u32 ) -> Result < ( ) > {
104
- Err ( Error :: new ( ErrorKind :: Other , "TcpStream::set_ttl not implemented" ) )
105
- }
106
-
107
- pub fn set_read_timeout ( & self , _dur : Option < Duration > ) -> Result < ( ) > {
108
- Err ( Error :: new ( ErrorKind :: Other , "TcpStream::set_read_timeout not implemented" ) )
109
- }
110
-
111
- pub fn set_write_timeout ( & self , _dur : Option < Duration > ) -> Result < ( ) > {
112
- Err ( Error :: new ( ErrorKind :: Other , "TcpStream::set_write_timeout not implemented" ) )
121
+ pub fn set_ttl ( & self , ttl : u32 ) -> Result < ( ) > {
122
+ let file = self . 0 . dup ( b"ttl" ) ?;
123
+ file. write ( & [ cmp:: min ( ttl, 255 ) as u8 ] ) ?;
124
+ Ok ( ( ) )
125
+ }
126
+
127
+ pub fn set_read_timeout ( & self , duration_option : Option < Duration > ) -> Result < ( ) > {
128
+ let file = self . 0 . dup ( b"read_timeout" ) ?;
129
+ if let Some ( duration) = duration_option {
130
+ file. write ( & TimeSpec {
131
+ tv_sec : duration. as_secs ( ) as i64 ,
132
+ tv_nsec : duration. subsec_nanos ( ) as i32
133
+ } ) ?;
134
+ } else {
135
+ file. write ( & [ ] ) ?;
136
+ }
137
+ Ok ( ( ) )
138
+ }
139
+
140
+ pub fn set_write_timeout ( & self , duration_option : Option < Duration > ) -> Result < ( ) > {
141
+ let file = self . 0 . dup ( b"write_timeout" ) ?;
142
+ if let Some ( duration) = duration_option {
143
+ file. write ( & TimeSpec {
144
+ tv_sec : duration. as_secs ( ) as i64 ,
145
+ tv_nsec : duration. subsec_nanos ( ) as i32
146
+ } ) ?;
147
+ } else {
148
+ file. write ( & [ ] ) ?;
149
+ }
150
+ Ok ( ( ) )
113
151
}
114
152
}
115
153
@@ -168,7 +206,10 @@ impl TcpListener {
168
206
}
169
207
170
208
pub fn ttl ( & self ) -> Result < u32 > {
171
- Err ( Error :: new ( ErrorKind :: Other , "TcpListener::ttl not implemented" ) )
209
+ let mut ttl = [ 0 ] ;
210
+ let file = self . 0 . dup ( b"ttl" ) ?;
211
+ file. read ( & mut ttl) ?;
212
+ Ok ( ttl[ 0 ] as u32 )
172
213
}
173
214
174
215
pub fn set_nonblocking ( & self , _nonblocking : bool ) -> Result < ( ) > {
@@ -179,8 +220,10 @@ impl TcpListener {
179
220
Err ( Error :: new ( ErrorKind :: Other , "TcpListener::set_only_v6 not implemented" ) )
180
221
}
181
222
182
- pub fn set_ttl ( & self , _ttl : u32 ) -> Result < ( ) > {
183
- Err ( Error :: new ( ErrorKind :: Other , "TcpListener::set_ttl not implemented" ) )
223
+ pub fn set_ttl ( & self , ttl : u32 ) -> Result < ( ) > {
224
+ let file = self . 0 . dup ( b"ttl" ) ?;
225
+ file. write ( & [ cmp:: min ( ttl, 255 ) as u8 ] ) ?;
226
+ Ok ( ( ) )
184
227
}
185
228
}
186
229
0 commit comments