Skip to content

Commit 8a7cd56

Browse files
committed
Large cleanup, mostly of socket functions
1 parent fcc952a commit 8a7cd56

File tree

14 files changed

+372
-180
lines changed

14 files changed

+372
-180
lines changed

nix-test/src/sizes.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "sys/socket.h"
12
#include "sys/uio.h"
23

34
#define SIZE_OF_T(TYPE) \
@@ -16,6 +17,9 @@
1617

1718
size_t
1819
size_of(const char* type) {
20+
// sys/socket
21+
SIZE_OF_S(sockaddr_storage);
22+
1923
// sys/uio
2024
SIZE_OF_S(iovec);
2125

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Rust friendly bindings to the various *nix system functions.
2+
//!
3+
//! Modules are structured according to the C header file that they would be
4+
//! defined in.
15
#![crate_name = "nix"]
26

37
#![feature(collections, core, net, linkage, libc, os, path, std_misc)]
@@ -18,6 +22,7 @@ pub use libc::{c_int, c_void};
1822
mod nix;
1923
pub use nix::{NixResult, NixError, NixPath, from_ffi};
2024

25+
2126
#[cfg(unix)]
2227
pub mod errno;
2328

@@ -36,8 +41,5 @@ pub mod sched;
3641
#[cfg(unix)]
3742
pub mod sys;
3843

39-
#[cfg(target_os = "linux")]
40-
pub mod syscall;
41-
4244
#[cfg(unix)]
4345
pub mod unistd;

src/nix.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub enum NixError {
1212
}
1313

1414
impl NixError {
15+
pub fn last() -> NixError {
16+
NixError::Sys(Errno::last())
17+
}
18+
1519
pub fn invalid_argument() -> NixError {
1620
NixError::Sys(EINVAL)
1721
}

src/sys/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ pub mod socket;
1717

1818
pub mod stat;
1919

20+
#[cfg(target_os = "linux")]
21+
pub mod syscall;
22+
2023
#[cfg(not(target_os = "ios"))]
2124
pub mod termios;
2225

src/sys/socket/addr.rs

Lines changed: 115 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
use {NixResult, NixError};
2-
use super::{sa_family_t, in_addr, sockaddr_in, sockaddr_in6, sockaddr_un, AF_UNIX, AF_INET};
2+
use super::{consts, sa_family_t, in_addr, sockaddr_in, sockaddr_in6, sockaddr_un, AF_UNIX, AF_INET};
33
use errno::Errno;
44
use libc;
5-
use std::{mem, net, path, ptr};
5+
use std::{fmt, mem, net, path, ptr};
66
use std::ffi::{AsOsStr, CStr, OsStr};
77
use std::os::unix::OsStrExt;
88

9+
/*
10+
*
11+
* ===== AddressFamily =====
12+
*
13+
*/
14+
15+
#[repr(i32)]
16+
#[derive(Copy, PartialEq, Eq, Debug)]
17+
pub enum AddressFamily {
18+
Unix = consts::AF_UNIX,
19+
Inet = consts::AF_INET,
20+
Inet6 = consts::AF_INET6,
21+
}
22+
923
/*
1024
*
1125
* ===== Sock addr =====
@@ -15,12 +29,46 @@ use std::os::unix::OsStrExt;
1529
/// Represents a socket address
1630
#[derive(Copy)]
1731
pub enum SockAddr {
18-
// TODO: Rename these variants IpV4, IpV6, Unix
1932
IpV4(sockaddr_in),
2033
IpV6(sockaddr_in6),
2134
Unix(sockaddr_un)
2235
}
2336

37+
impl SockAddr {
38+
pub fn family(&self) -> AddressFamily {
39+
match *self {
40+
SockAddr::IpV4(..) => AddressFamily::Inet,
41+
SockAddr::IpV6(..) => AddressFamily::Inet6,
42+
SockAddr::Unix(..) => AddressFamily::Unix,
43+
}
44+
}
45+
46+
pub fn to_str(&self) -> String {
47+
format!("{}", self)
48+
}
49+
}
50+
51+
impl fmt::Display for SockAddr {
52+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53+
use std::num::Int;
54+
55+
match *self {
56+
SockAddr::IpV4(sin) => {
57+
let ip = Int::from_be(sin.sin_addr.s_addr);
58+
let port = Int::from_be(sin.sin_port);
59+
60+
write!(f, "{}.{}.{}.{}:{}",
61+
(ip >> 24) & 0xff,
62+
(ip >> 16) & 0xff,
63+
(ip >> 8) & 0xff,
64+
(ip) & 0xff,
65+
port)
66+
}
67+
_ => write!(f, "[some sock addr type... Debug is not implemented :(]"),
68+
}
69+
}
70+
}
71+
2472
/// A trait for values which can be converted or resolved to a SockAddr.
2573
pub trait ToSockAddr {
2674
/// Converts the value to a SockAddr
@@ -69,10 +117,18 @@ impl ToSockAddr for path::Path {
69117
}
70118
}
71119

120+
/// Convert a path buf into a unix domain socket address
121+
impl ToSockAddr for path::PathBuf {
122+
fn to_sock_addr(&self) -> NixResult<SockAddr> {
123+
(**self).to_sock_addr()
124+
}
125+
}
126+
72127
/// Convert an inet address into a socket address
73128
impl ToSockAddr for net::SocketAddr {
74129
fn to_sock_addr(&self) -> NixResult<SockAddr> {
75130
use std::net::IpAddr;
131+
use std::num::Int;
76132

77133
match self.ip() {
78134
IpAddr::V4(ip) => {
@@ -81,7 +137,7 @@ impl ToSockAddr for net::SocketAddr {
81137

82138
Ok(SockAddr::IpV4(sockaddr_in {
83139
sin_family: AF_INET as sa_family_t,
84-
sin_port: self.port(),
140+
sin_port: self.port().to_be(),
85141
sin_addr: addr,
86142
.. unsafe { mem::zeroed() }
87143
}))
@@ -110,7 +166,7 @@ impl FromSockAddr for net::SocketAddr {
110166
((ip >> 8) as u8) & 0xff,
111167
((ip >> 0) as u8) & 0xff);
112168

113-
Some(net::SocketAddr::new(IpAddr::V4(ip), addr.sin_port))
169+
Some(net::SocketAddr::new(IpAddr::V4(ip), Int::from_be(addr.sin_port)))
114170
}
115171
SockAddr::IpV6(_) => unimplemented!(),
116172
_ => None,
@@ -132,12 +188,60 @@ impl FromSockAddr for path::PathBuf {
132188
}
133189
}
134190

191+
/*
192+
*
193+
* ===== IpAddr =====
194+
*
195+
*/
196+
197+
/// Convert to an IpAddr
198+
pub trait ToIpAddr {
199+
fn to_ip_addr(self) -> Option<net::IpAddr>;
200+
}
201+
202+
impl ToIpAddr for net::IpAddr {
203+
fn to_ip_addr(self) -> Option<net::IpAddr> {
204+
Some(self)
205+
}
206+
}
207+
208+
impl<'a> ToIpAddr for &'a net::IpAddr {
209+
fn to_ip_addr(self) -> Option<net::IpAddr> {
210+
Some(*self)
211+
}
212+
}
213+
214+
impl ToIpAddr for net::Ipv4Addr {
215+
fn to_ip_addr(self) -> Option<net::IpAddr> {
216+
Some(net::IpAddr::V4(self))
217+
}
218+
}
219+
220+
impl<'a> ToIpAddr for &'a net::Ipv4Addr {
221+
fn to_ip_addr(self) -> Option<net::IpAddr> {
222+
(*self).to_ip_addr()
223+
}
224+
}
225+
226+
impl ToIpAddr for net::Ipv6Addr {
227+
fn to_ip_addr(self) -> Option<net::IpAddr> {
228+
Some(net::IpAddr::V6(self))
229+
}
230+
}
231+
232+
impl<'a> ToIpAddr for &'a net::Ipv6Addr {
233+
fn to_ip_addr(self) -> Option<net::IpAddr> {
234+
(*self).to_ip_addr()
235+
}
236+
}
237+
135238
/*
136239
*
137240
* ===== InAddr =====
138241
*
139242
*/
140243

244+
/// Convert to an in_addr
141245
pub trait ToInAddr {
142246
fn to_in_addr(self) -> Option<libc::in_addr>;
143247
}
@@ -183,13 +287,12 @@ impl ToInAddr for net::Ipv4Addr {
183287
use std::num::Int;
184288

185289
let addr = self.octets();
186-
Some(in_addr {
187-
s_addr: Int::from_be(
188-
((addr[0] as u32) << 24) |
189-
((addr[1] as u32) << 16) |
190-
((addr[2] as u32) << 8) |
191-
((addr[3] as u32) << 0))
192-
})
290+
let ip = (((addr[0] as u32) << 24) |
291+
((addr[1] as u32) << 16) |
292+
((addr[2] as u32) << 8) |
293+
((addr[3] as u32) << 0)).to_be();
294+
295+
Some(in_addr { s_addr: ip })
193296
}
194297
}
195298

src/sys/socket/consts.rs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ pub use self::os::*;
44
mod os {
55
use libc::{c_int, uint8_t};
66

7-
pub type AddressFamily = c_int;
7+
pub const AF_UNIX: c_int = 1;
8+
pub const AF_LOCAL: c_int = AF_UNIX;
9+
pub const AF_INET: c_int = 2;
10+
pub const AF_INET6: c_int = 10;
811

9-
pub const AF_UNIX: AddressFamily = 1;
10-
pub const AF_LOCAL: AddressFamily = AF_UNIX;
11-
pub const AF_INET: AddressFamily = 2;
12-
pub const AF_INET6: AddressFamily = 10;
13-
14-
pub type SockType = c_int;
15-
16-
pub const SOCK_STREAM: SockType = 1;
17-
pub const SOCK_DGRAM: SockType = 2;
18-
pub const SOCK_SEQPACKET: SockType = 5;
19-
pub const SOCK_RAW: SockType = 3;
20-
pub const SOCK_RDM: SockType = 4;
12+
pub const SOCK_STREAM: c_int = 1;
13+
pub const SOCK_DGRAM: c_int = 2;
14+
pub const SOCK_SEQPACKET: c_int = 5;
15+
pub const SOCK_RAW: c_int = 3;
16+
pub const SOCK_RDM: c_int = 4;
2117

2218
pub const SOL_IP: c_int = 0;
2319
pub const SOL_SOCKET: c_int = 1;
@@ -94,20 +90,16 @@ mod os {
9490
mod os {
9591
use libc::{c_int, uint8_t};
9692

97-
pub type AddressFamily = c_int;
98-
99-
pub const AF_UNIX: AddressFamily = 1;
100-
pub const AF_LOCAL: AddressFamily = AF_UNIX;
101-
pub const AF_INET: AddressFamily = 2;
102-
pub const AF_INET6: AddressFamily = 30;
103-
104-
pub type SockType = c_int;
93+
pub const AF_UNIX: c_int = 1;
94+
pub const AF_LOCAL: c_int = AF_UNIX;
95+
pub const AF_INET: c_int = 2;
96+
pub const AF_INET6: c_int = 30;
10597

106-
pub const SOCK_STREAM: SockType = 1;
107-
pub const SOCK_DGRAM: SockType = 2;
108-
pub const SOCK_SEQPACKET: SockType = 5;
109-
pub const SOCK_RAW: SockType = 3;
110-
pub const SOCK_RDM: SockType = 4;
98+
pub const SOCK_STREAM: c_int = 1;
99+
pub const SOCK_DGRAM: c_int = 2;
100+
pub const SOCK_SEQPACKET: c_int = 5;
101+
pub const SOCK_RAW: c_int = 3;
102+
pub const SOCK_RDM: c_int = 4;
111103

112104
pub const SOL_SOCKET: c_int = 0xffff;
113105
pub const IPPROTO_IP: c_int = 0;

0 commit comments

Comments
 (0)