1
1
//! This crate provides high level SCTP networking.
2
2
//! Currently it only supports basic SCTP features like multi-homing
3
3
//! in one-to-one and one-to-many associations.
4
- //! SCTP notifications and working directly on associtaion is not supported yet
4
+ //! SCTP notifications and working directly on association is not supported yet
5
5
//! but is in the TODO list.
6
6
7
7
extern crate sctp_sys;
@@ -20,19 +20,26 @@ use std::os::unix::io::{AsRawFd, RawFd, FromRawFd};
20
20
#[ cfg( target_os="windows" ) ]
21
21
use std:: os:: windows:: io:: { AsRawHandle , RawHandle , FromRawHandle } ;
22
22
23
- /// Socket buffer type
24
- pub enum SoBuffer {
25
- /// RCV buffer
23
+ /// Socket direction
24
+ pub enum SoDirection {
25
+ /// RCV direction
26
26
Receive ,
27
- /// SND buffer
27
+ /// SND direction
28
28
Send
29
29
}
30
30
31
- impl SoBuffer {
32
- fn optname ( & self ) -> libc:: c_int {
31
+ impl SoDirection {
32
+ fn buffer_opt ( & self ) -> libc:: c_int {
33
33
return match * self {
34
- SoBuffer :: Receive => libc:: SO_RCVBUF ,
35
- SoBuffer :: Send => libc:: SO_SNDBUF
34
+ SoDirection :: Receive => libc:: SO_RCVBUF ,
35
+ SoDirection :: Send => libc:: SO_SNDBUF
36
+ } ;
37
+ }
38
+
39
+ fn timeout_opt ( & self ) -> libc:: c_int {
40
+ return match * self {
41
+ SoDirection :: Receive => libc:: SO_RCVTIMEO ,
42
+ SoDirection :: Send => libc:: SO_SNDTIMEO
36
43
} ;
37
44
}
38
45
}
@@ -108,18 +115,24 @@ impl SctpStream {
108
115
return Ok ( val == 1 ) ;
109
116
}
110
117
111
- /// Set the socket buffer size for the buffer specifid by `buf `.
112
- /// Linux system will double the provided size
113
- pub fn set_buffer_size ( & self , buf : SoBuffer , size : usize ) -> Result < ( ) > {
114
- return self . 0 . setsockopt ( libc:: SOL_SOCKET , buf . optname ( ) , & ( size as libc:: c_int ) ) ;
118
+ /// Set the socket buffer size for the direction specified by `dir `.
119
+ /// Linux systems will double the provided size
120
+ pub fn set_buffer_size ( & self , dir : SoDirection , size : usize ) -> Result < ( ) > {
121
+ return self . 0 . setsockopt ( libc:: SOL_SOCKET , dir . buffer_opt ( ) , & ( size as libc:: c_int ) ) ;
115
122
}
116
123
117
- /// Get the socket buffer size for the buffer specifid by `buf `
118
- pub fn get_buffer_size ( & self , buf : SoBuffer ) -> Result < ( usize ) > {
119
- let val: u32 = try!( self . 0 . getsockopt ( libc:: SOL_SOCKET , buf . optname ( ) ) ) ;
124
+ /// Get the socket buffer size for the direction specified by `dir `
125
+ pub fn get_buffer_size ( & self , dir : SoDirection ) -> Result < ( usize ) > {
126
+ let val: u32 = try!( self . 0 . getsockopt ( libc:: SOL_SOCKET , dir . buffer_opt ( ) ) ) ;
120
127
return Ok ( val as usize ) ;
121
128
}
122
129
130
+ /// Set `timeout` in seconds for operation `dir` (either receive or send)
131
+ pub fn set_timeout ( & self , dir : SoDirection , timeout : i32 ) -> Result < ( ) > {
132
+ let tval = libc:: timeval { tv_sec : timeout as libc:: c_int , tv_usec : 0 } ;
133
+ return self . 0 . setsockopt ( libc:: SOL_SOCKET , dir. timeout_opt ( ) , & tval) ;
134
+ }
135
+
123
136
/// Try to clone the SctpStream. On success, returns a new stream
124
137
/// wrapping a new socket handler
125
138
pub fn try_clone ( & self ) -> Result < SctpStream > {
0 commit comments