1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#[cfg(unix)]
use libc as c;

#[cfg(windows)]
use winapi as c;

/// Socket Type
///
/// Cross platform enum of common Socket Types. For missing types use
/// the `libc` and `ws2_32` crates, depending on platform.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum SockType {
  /// Sequenced, reliable, connection-based byte streams.
  Stream,
  /// Connectionless, unreliable datagrams of fixed max length.
  DGram,
  /// Raw protocol interface.
  Raw,
  /// Reliably-delivered messages.
  RDM,
}

impl From<SockType> for c::c_int {
  fn from(sock: SockType) -> c::c_int {
    match sock {
      SockType::Stream => c::SOCK_STREAM,
      SockType::DGram => c::SOCK_DGRAM,
      SockType::Raw => c::SOCK_RAW,
      SockType::RDM => c::SOCK_RDM,
    }
  }
}

impl PartialEq<c::c_int> for SockType {
  fn eq(&self, other: &c::c_int) -> bool {
    let int: c::c_int = (*self).into();
    *other == int
  }
}

impl PartialEq<SockType> for c::c_int {
  fn eq(&self, other: &SockType) -> bool {
    let int: c::c_int = (*other).into();
    *self == int
  }
}

/// Socket Protocol
///
/// Cross platform enum of common Socket Protocols. For missing types use
/// the `libc` and `ws2_32` crates, depending on platform.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Protocol {
  /// Internet Control Message Protocol.
  ICMP,
  /// Transmission Control Protocol.
  TCP,
  /// User Datagram Protocol.
  UDP,
}

impl From<Protocol> for c::c_int {
  #[cfg(unix)]
  fn from(sock: Protocol) -> c::c_int {
    match sock {
      Protocol::ICMP => c::IPPROTO_ICMP,
      Protocol::TCP => c::IPPROTO_TCP,
      Protocol::UDP => c::IPPROTO_UDP,
    }
  }

  #[cfg(windows)]
  fn from(sock: Protocol) -> c::c_int {
    match sock {
      Protocol::ICMP => c::IPPROTO_ICMP.0 as c::c_int,
      Protocol::TCP => c::IPPROTO_TCP.0 as c::c_int,
      Protocol::UDP => c::IPPROTO_UDP.0 as c::c_int,
    }
  }
}

impl PartialEq<c::c_int> for Protocol {
  fn eq(&self, other: &c::c_int) -> bool {
    let int: c::c_int = (*self).into();
    *other == int
  }
}

impl PartialEq<Protocol> for c::c_int {
  fn eq(&self, other: &Protocol) -> bool {
    let int: c::c_int = (*other).into();
    *self == int
  }
}

/// Address Family
///
/// Cross platform enum of common Address Families. For missing types use
/// the `libc` and `ws2_32` crates, depending on platform.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum AddrFamily {
  /// Local to host (pipes and file-domain)
  Unix,
  /// IP protocol family.
  Inet,
  /// IP version 6.
  Inet6
}

impl From<AddrFamily> for c::c_int {
  fn from(sock: AddrFamily) -> c::c_int {
    match sock {
      AddrFamily::Unix => c::AF_UNIX,
      AddrFamily::Inet => c::AF_INET,
      AddrFamily::Inet6 => c::AF_INET6,
    }
  }
}

impl PartialEq<c::c_int> for AddrFamily {
  fn eq(&self, other: &c::c_int) -> bool {
    let int: c::c_int = (*self).into();
    *other == int
  }
}

impl PartialEq<AddrFamily> for c::c_int {
  fn eq(&self, other: &AddrFamily) -> bool {
    let int: c::c_int = (*other).into();
    *self == int
  }
}