Skip to content

Commit 6aaf1b8

Browse files
author
pierresy
committed
Added more methods to SctpEndpoint and SctpListener
1 parent 2ae0cf8 commit 6aaf1b8

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

examples/listener.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ fn main() {
66
match SctpListener::bindx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
77
Ok(serv) => {
88
println!("bound to {:?}", serv.local_addrs().unwrap());
9+
// serv.set_timeout(5).unwrap();
910
match serv.accept() {
1011
Err(e) => println!("{:?}", e.kind()),
1112
Ok((peer, _)) => {

src/lib.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This crate provides high level SCTP networking.
22
//! Currently it only supports basic SCTP features like multi-homing
33
//! in one-to-one and one-to-many associations.
4-
//! SCTP notifications and working directly on association is not supported yet
4+
//! SCTP notifications and working directly on associations is not supported yet
55
//! but is in the TODO list.
66
77
extern crate sctp_sys;
@@ -129,7 +129,7 @@ impl SctpStream {
129129

130130
/// Set `timeout` in seconds for operation `dir` (either receive or send)
131131
pub fn set_timeout(&self, dir: SoDirection, timeout: i32) -> Result<()> {
132-
// Workaround: Use onf long instead of time_t which does not compile in windows x86_64
132+
// Workaround: Use of long instead of libc::time_t which does not compile in windows x86_64
133133
let tval = libc::timeval { tv_sec: timeout as libc::c_long, tv_usec: 0 };
134134
return self.0.setsockopt(libc::SOL_SOCKET, dir.timeout_opt(), &tval);
135135
}
@@ -229,11 +229,47 @@ impl SctpEndpoint {
229229
return self.0.sendmsg(msg, Some(address), stream, 0);
230230
}
231231

232-
/// Get local socket addresses on which this socket is bound
232+
/// Get local socket addresses to which this socket is bound
233233
pub fn local_addrs(&self) -> Result<Vec<SocketAddr>> {
234234
return self.0.local_addrs(0);
235235
}
236236

237+
/// Shuts down the read, write, or both halves of this connection
238+
pub fn shutdown(&self, how: Shutdown) -> Result<()> {
239+
return self.0.shutdown(how);
240+
}
241+
242+
/// Set or unset SCTP_NODELAY option
243+
pub fn set_nodelay(&self, nodelay: bool) -> Result<()> {
244+
let val: libc::c_int = if nodelay { 1 } else { 0 };
245+
return self.0.setsockopt(SOL_SCTP, sctp_sys::SCTP_NODELAY, &val);
246+
}
247+
248+
/// Verify if SCTP_NODELAY option is activated for this socket
249+
pub fn has_nodelay(&self) -> Result<bool> {
250+
let val: libc::c_int = try!(self.0.sctp_opt_info(sctp_sys::SCTP_NODELAY, 0));
251+
return Ok(val == 1);
252+
}
253+
254+
/// Set the socket buffer size for the direction specified by `dir`.
255+
/// Linux systems will double the provided size
256+
pub fn set_buffer_size(&self, dir: SoDirection, size: usize) -> Result<()> {
257+
return self.0.setsockopt(libc::SOL_SOCKET, dir.buffer_opt(), &(size as libc::c_int));
258+
}
259+
260+
/// Get the socket buffer size for the direction specified by `dir`
261+
pub fn get_buffer_size(&self, dir: SoDirection) -> Result<(usize)> {
262+
let val: u32 = try!(self.0.getsockopt(libc::SOL_SOCKET, dir.buffer_opt()));
263+
return Ok(val as usize);
264+
}
265+
266+
/// Set `timeout` in seconds for operation `dir` (either receive or send)
267+
pub fn set_timeout(&self, dir: SoDirection, timeout: i32) -> Result<()> {
268+
// Workaround: Use of long instead of libc::time_t which does not compile in windows x86_64
269+
let tval = libc::timeval { tv_sec: timeout as libc::c_long, tv_usec: 0 };
270+
return self.0.setsockopt(libc::SOL_SOCKET, dir.timeout_opt(), &tval);
271+
}
272+
237273
/// Try to clone this socket
238274
pub fn try_clone(&self) -> Result<SctpEndpoint> {
239275
return Ok(SctpEndpoint(try!(self.0.try_clone())));
@@ -321,16 +357,23 @@ impl SctpListener {
321357
return Ok((SctpStream(sock), addr));
322358
}
323359

324-
/// Iterate over new connections.
360+
/// Iterate over new connections
325361
pub fn incoming(&self) -> Incoming {
326362
return Incoming(self);
327363
}
328364

329-
/// Get the listener ocal addresses
365+
/// Get the listener local addresses
330366
pub fn local_addrs(&self) -> Result<Vec<SocketAddr>> {
331367
return self.0.local_addrs(0);
332368
}
333369

370+
/// Set `timeout` in seconds on accept
371+
pub fn set_timeout(&self, timeout: i32) -> Result<()> {
372+
// Workaround: Use of long instead of libc::time_t which does not compile in windows x86_64
373+
let tval = libc::timeval { tv_sec: timeout as libc::c_long, tv_usec: 0 };
374+
return self.0.setsockopt(libc::SOL_SOCKET, libc::SO_RCVTIMEO, &tval);
375+
}
376+
334377
/// Try to clone this listener
335378
pub fn try_clone(&self) -> Result<SctpListener> {
336379
return Ok(SctpListener(try!(self.0.try_clone())));

src/sctpsock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl RawSocketAddr for SocketAddr {
156156
}
157157

158158

159-
/// An SCTP socket, of any kind
159+
/// A High level wrapper around SCTP socket, of any kind
160160
pub struct SctpSocket(SOCKET);
161161

162162
impl SctpSocket {

0 commit comments

Comments
 (0)