Skip to content

Commit 7a57682

Browse files
authored
Merge pull request #9 from DoumanAsh/protocol_consts
Add protocol 'constants'
2 parents 09457de + 66c7950 commit 7a57682

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/socket.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,28 @@ impl Type {
762762
}
763763
}
764764

765+
impl ::Protocol {
766+
/// Protocol corresponding to `ICMPv4`
767+
pub fn icmpv4() -> Self {
768+
::Protocol(sys::IPPROTO_ICMP)
769+
}
770+
771+
/// Protocol corresponding to `ICMPv6`
772+
pub fn icmpv6() -> Self {
773+
::Protocol(sys::IPPROTO_ICMPV6)
774+
}
775+
776+
/// Protocol corresponding to `TCP`
777+
pub fn tcp() -> Self {
778+
::Protocol(sys::IPPROTO_TCP)
779+
}
780+
781+
/// Protocol corresponding to `UDP`
782+
pub fn udp() -> Self {
783+
::Protocol(sys::IPPROTO_UDP)
784+
}
785+
}
786+
765787
impl From<i32> for Type {
766788
fn from(a: i32) -> Type {
767789
Type(a)

src/sys/unix/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ cfg_if! {
6262
use SockAddr;
6363
use utils::One;
6464

65+
pub const IPPROTO_ICMP: i32 = libc::IPPROTO_ICMP;
66+
pub const IPPROTO_ICMPV6: i32 = libc::IPPROTO_ICMPV6;
67+
pub const IPPROTO_TCP: i32 = libc::IPPROTO_TCP;
68+
pub const IPPROTO_UDP: i32 = libc::IPPROTO_UDP;
69+
6570
#[macro_use]
6671
#[cfg(target_os = "linux")]
6772
mod weak;

src/sys/windows.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const SD_SEND: c_int = 1;
3434
const SIO_KEEPALIVE_VALS: DWORD = 0x98000004;
3535
const WSA_FLAG_OVERLAPPED: DWORD = 0x01;
3636

37+
pub const IPPROTO_ICMP: i32 = ws2def::IPPROTO_ICMP.0 as i32;
38+
pub const IPPROTO_ICMPV6: i32 = ws2def::IPPROTO_ICMPV6.0 as i32;
39+
pub const IPPROTO_TCP: i32 = ws2def::IPPROTO_TCP.0 as i32;
40+
pub const IPPROTO_UDP: i32 = ws2def::IPPROTO_UDP.0 as i32;
41+
3742
#[repr(C)]
3843
struct tcp_keepalive {
3944
onoff: c_ulong,
@@ -420,15 +425,15 @@ impl Socket {
420425

421426
pub fn nodelay(&self) -> io::Result<bool> {
422427
unsafe {
423-
let raw: c_int = self.getsockopt(IPPROTO_TCP.0 as c_int,
428+
let raw: c_int = self.getsockopt(IPPROTO_TCP,
424429
TCP_NODELAY)?;
425430
Ok(raw != 0)
426431
}
427432
}
428433

429434
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
430435
unsafe {
431-
self.setsockopt(IPPROTO_TCP.0 as c_int,
436+
self.setsockopt(IPPROTO_TCP,
432437
TCP_NODELAY,
433438
nodelay as c_int)
434439
}

0 commit comments

Comments
 (0)