Skip to content

Commit b70a603

Browse files
committed
add socket constants already present in libc
1 parent c2baaab commit b70a603

File tree

2 files changed

+184
-33
lines changed

2 files changed

+184
-33
lines changed

src/sys/socket/addr.rs

Lines changed: 183 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,186 @@ use std::{fmt, hash, mem, net, ptr};
55
use std::ffi::OsStr;
66
use std::path::Path;
77
use std::os::unix::ffi::OsStrExt;
8-
#[cfg(any(target_os = "linux", target_os = "android"))]
8+
#[cfg(any(target_os = "android", target_os = "linux"))]
99
use ::sys::socket::addr::netlink::NetlinkAddr;
10-
#[cfg(any(target_os = "macos", target_os = "ios"))]
10+
#[cfg(any(target_os = "ios", target_os = "macos"))]
1111
use std::os::unix::io::RawFd;
12-
#[cfg(any(target_os = "macos", target_os = "ios"))]
12+
#[cfg(any(target_os = "ios", target_os = "macos"))]
1313
use ::sys::socket::addr::sys_control::SysControlAddr;
1414

15-
// TODO: uncomment out IpAddr functions: rust-lang/rfcs#988
16-
17-
/*
18-
*
19-
* ===== AddressFamily =====
20-
*
21-
*/
22-
2315
#[repr(i32)]
2416
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
2517
pub enum AddressFamily {
2618
Unix = libc::AF_UNIX,
2719
Inet = libc::AF_INET,
2820
Inet6 = libc::AF_INET6,
29-
#[cfg(any(target_os = "linux", target_os = "android"))]
21+
#[cfg(any(target_os = "android", target_os = "linux"))]
3022
Netlink = libc::AF_NETLINK,
31-
#[cfg(any(target_os = "linux", target_os = "android"))]
23+
#[cfg(any(target_os = "android", target_os = "linux"))]
3224
Packet = libc::AF_PACKET,
33-
#[cfg(any(target_os = "macos", target_os = "ios"))]
25+
#[cfg(any(target_os = "ios", target_os = "macos"))]
3426
System = libc::AF_SYSTEM,
27+
#[cfg(any(target_os = "android", target_os = "linux"))]
28+
Ax25 = libc::AF_AX25,
29+
Ipx = libc::AF_IPX,
30+
AppleTalk = libc::AF_APPLETALK,
31+
#[cfg(any(target_os = "android", target_os = "linux"))]
32+
NetRom = libc::AF_NETROM,
33+
#[cfg(any(target_os = "android", target_os = "linux"))]
34+
Bridge = libc::AF_BRIDGE,
35+
#[cfg(any(target_os = "android", target_os = "linux"))]
36+
AtmPvc = libc::AF_ATMPVC,
37+
#[cfg(any(target_os = "android", target_os = "linux"))]
38+
X25 = libc::AF_X25,
39+
#[cfg(any(target_os = "android", target_os = "linux"))]
40+
Rose = libc::AF_ROSE,
41+
Decnet = libc::AF_DECnet,
42+
#[cfg(any(target_os = "android", target_os = "linux"))]
43+
NetBeui = libc::AF_NETBEUI,
44+
#[cfg(any(target_os = "android", target_os = "linux"))]
45+
Security = libc::AF_SECURITY,
46+
#[cfg(any(target_os = "android", target_os = "linux"))]
47+
Key = libc::AF_KEY,
48+
#[cfg(any(target_os = "android", target_os = "linux"))]
49+
Ash = libc::AF_ASH,
50+
#[cfg(any(target_os = "android", target_os = "linux"))]
51+
Econet = libc::AF_ECONET,
52+
#[cfg(any(target_os = "android", target_os = "linux"))]
53+
AtmSvc = libc::AF_ATMSVC,
54+
#[cfg(any(target_os = "android", target_os = "linux"))]
55+
Rds = libc::AF_RDS,
56+
Sna = libc::AF_SNA,
57+
#[cfg(any(target_os = "android", target_os = "linux"))]
58+
Irda = libc::AF_IRDA,
59+
#[cfg(any(target_os = "android", target_os = "linux"))]
60+
Pppox = libc::AF_PPPOX,
61+
#[cfg(any(target_os = "android", target_os = "linux"))]
62+
Wanpipe = libc::AF_WANPIPE,
63+
#[cfg(any(target_os = "android", target_os = "linux"))]
64+
Llc = libc::AF_LLC,
65+
#[cfg(target_os = "linux")]
66+
Ib = libc::AF_IB,
67+
#[cfg(target_os = "linux")]
68+
Mpls = libc::AF_MPLS,
69+
#[cfg(any(target_os = "android", target_os = "linux"))]
70+
Can = libc::AF_CAN,
71+
#[cfg(any(target_os = "android", target_os = "linux"))]
72+
Tipc = libc::AF_TIPC,
73+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
74+
Bluetooth = libc::AF_BLUETOOTH,
75+
#[cfg(any(target_os = "android", target_os = "linux"))]
76+
Iucv = libc::AF_IUCV,
77+
#[cfg(any(target_os = "android", target_os = "linux"))]
78+
RxRpc = libc::AF_RXRPC,
79+
Isdn = libc::AF_ISDN,
80+
#[cfg(any(target_os = "android", target_os = "linux"))]
81+
Phonet = libc::AF_PHONET,
82+
#[cfg(any(target_os = "android", target_os = "linux"))]
83+
Ieee802154 = libc::AF_IEEE802154,
84+
#[cfg(any(target_os = "android", target_os = "linux"))]
85+
Caif = libc::AF_CAIF,
86+
#[cfg(any(target_os = "android", target_os = "linux"))]
87+
Alg = libc::AF_ALG,
88+
#[cfg(target_os = "linux")]
89+
Nfc = libc::AF_NFC,
90+
#[cfg(target_os = "linux")]
91+
Vsock = libc::AF_VSOCK,
92+
#[cfg(any(target_os = "dragonfly",
93+
target_os = "freebsd",
94+
target_os = "ios",
95+
target_os = "macos",
96+
target_os = "netbsd",
97+
target_os = "openbsd"))]
98+
ImpLink = libc::AF_IMPLINK,
99+
#[cfg(any(target_os = "dragonfly",
100+
target_os = "freebsd",
101+
target_os = "ios",
102+
target_os = "macos",
103+
target_os = "netbsd",
104+
target_os = "openbsd"))]
105+
Pup = libc::AF_PUP,
106+
#[cfg(any(target_os = "dragonfly",
107+
target_os = "freebsd",
108+
target_os = "ios",
109+
target_os = "macos",
110+
target_os = "netbsd",
111+
target_os = "openbsd"))]
112+
Chaos = libc::AF_CHAOS,
113+
#[cfg(any(target_os = "ios",
114+
target_os = "macos",
115+
target_os = "netbsd",
116+
target_os = "openbsd"))]
117+
Ns = libc::AF_NS,
118+
#[cfg(any(target_os = "dragonfly",
119+
target_os = "freebsd",
120+
target_os = "ios",
121+
target_os = "macos",
122+
target_os = "netbsd",
123+
target_os = "openbsd"))]
124+
Iso = libc::AF_ISO,
125+
#[cfg(any(target_os = "dragonfly",
126+
target_os = "freebsd",
127+
target_os = "ios",
128+
target_os = "macos",
129+
target_os = "netbsd",
130+
target_os = "openbsd"))]
131+
Datakit = libc::AF_DATAKIT,
132+
#[cfg(any(target_os = "dragonfly",
133+
target_os = "freebsd",
134+
target_os = "ios",
135+
target_os = "macos",
136+
target_os = "netbsd",
137+
target_os = "openbsd"))]
138+
Ccitt = libc::AF_CCITT,
139+
#[cfg(any(target_os = "dragonfly",
140+
target_os = "freebsd",
141+
target_os = "ios",
142+
target_os = "macos",
143+
target_os = "netbsd",
144+
target_os = "openbsd"))]
145+
Dli = libc::AF_DLI,
146+
#[cfg(any(target_os = "dragonfly",
147+
target_os = "freebsd",
148+
target_os = "ios",
149+
target_os = "macos",
150+
target_os = "netbsd",
151+
target_os = "openbsd"))]
152+
Lat = libc::AF_LAT,
153+
#[cfg(any(target_os = "dragonfly",
154+
target_os = "freebsd",
155+
target_os = "ios",
156+
target_os = "macos",
157+
target_os = "netbsd",
158+
target_os = "openbsd"))]
159+
Hylink = libc::AF_HYLINK,
160+
#[cfg(any(target_os = "dragonfly",
161+
target_os = "freebsd",
162+
target_os = "ios",
163+
target_os = "macos",
164+
target_os = "netbsd",
165+
target_os = "openbsd"))]
166+
Link = libc::AF_LINK,
167+
#[cfg(any(target_os = "dragonfly",
168+
target_os = "freebsd",
169+
target_os = "ios",
170+
target_os = "macos",
171+
target_os = "netbsd",
172+
target_os = "openbsd"))]
173+
Coip = libc::AF_COIP,
174+
#[cfg(any(target_os = "dragonfly",
175+
target_os = "freebsd",
176+
target_os = "ios",
177+
target_os = "macos",
178+
target_os = "netbsd",
179+
target_os = "openbsd"))]
180+
Cnt = libc::AF_CNT,
181+
#[cfg(any(target_os = "dragonfly",
182+
target_os = "freebsd",
183+
target_os = "ios",
184+
target_os = "macos",
185+
target_os = "netbsd",
186+
target_os = "openbsd"))]
187+
Natm = libc::AF_NATM,
35188
}
36189

37190
#[derive(Copy)]
@@ -252,7 +405,7 @@ impl Ipv4Addr {
252405
}
253406

254407
pub fn any() -> Ipv4Addr {
255-
Ipv4Addr(libc::in_addr { s_addr: 0/*consts::INADDR_ANY*/ }) // TODO: define INADDR_ANY in libc
408+
Ipv4Addr(libc::in_addr { s_addr: libc::INADDR_ANY })
256409
}
257410

258411
pub fn octets(&self) -> [u8; 4] {
@@ -480,9 +633,9 @@ impl fmt::Display for UnixAddr {
480633
pub enum SockAddr {
481634
Inet(InetAddr),
482635
Unix(UnixAddr),
483-
#[cfg(any(target_os = "linux", target_os = "android"))]
636+
#[cfg(any(target_os = "android", target_os = "linux"))]
484637
Netlink(NetlinkAddr),
485-
#[cfg(any(target_os = "macos", target_os = "ios"))]
638+
#[cfg(any(target_os = "ios", target_os = "macos"))]
486639
SysControl(SysControlAddr),
487640
}
488641

@@ -495,12 +648,12 @@ impl SockAddr {
495648
Ok(SockAddr::Unix(try!(UnixAddr::new(path))))
496649
}
497650

498-
#[cfg(any(target_os = "linux", target_os = "android"))]
651+
#[cfg(any(target_os = "android", target_os = "linux"))]
499652
pub fn new_netlink(pid: u32, groups: u32) -> SockAddr {
500653
SockAddr::Netlink(NetlinkAddr::new(pid, groups))
501654
}
502655

503-
#[cfg(any(target_os = "macos", target_os = "ios"))]
656+
#[cfg(any(target_os = "ios", target_os = "macos"))]
504657
pub fn new_sys_control(sockfd: RawFd, name: &str, unit: u32) -> Result<SockAddr> {
505658
SysControlAddr::from_name(sockfd, name, unit).map(|a| SockAddr::SysControl(a))
506659
}
@@ -510,9 +663,9 @@ impl SockAddr {
510663
SockAddr::Inet(InetAddr::V4(..)) => AddressFamily::Inet,
511664
SockAddr::Inet(InetAddr::V6(..)) => AddressFamily::Inet6,
512665
SockAddr::Unix(..) => AddressFamily::Unix,
513-
#[cfg(any(target_os = "linux", target_os = "android"))]
666+
#[cfg(any(target_os = "android", target_os = "linux"))]
514667
SockAddr::Netlink(..) => AddressFamily::Netlink,
515-
#[cfg(any(target_os = "macos", target_os = "ios"))]
668+
#[cfg(any(target_os = "ios", target_os = "macos"))]
516669
SockAddr::SysControl(..) => AddressFamily::System,
517670
}
518671
}
@@ -526,9 +679,9 @@ impl SockAddr {
526679
SockAddr::Inet(InetAddr::V4(ref addr)) => (mem::transmute(addr), mem::size_of::<libc::sockaddr_in>() as libc::socklen_t),
527680
SockAddr::Inet(InetAddr::V6(ref addr)) => (mem::transmute(addr), mem::size_of::<libc::sockaddr_in6>() as libc::socklen_t),
528681
SockAddr::Unix(UnixAddr(ref addr, len)) => (mem::transmute(addr), (len + offset_of!(libc::sockaddr_un, sun_path)) as libc::socklen_t),
529-
#[cfg(any(target_os = "linux", target_os = "android"))]
682+
#[cfg(any(target_os = "android", target_os = "linux"))]
530683
SockAddr::Netlink(NetlinkAddr(ref sa)) => (mem::transmute(sa), mem::size_of::<libc::sockaddr_nl>() as libc::socklen_t),
531-
#[cfg(any(target_os = "macos", target_os = "ios"))]
684+
#[cfg(any(target_os = "ios", target_os = "macos"))]
532685
SockAddr::SysControl(SysControlAddr(ref sa)) => (mem::transmute(sa), mem::size_of::<sys_control::sockaddr_ctl>() as libc::socklen_t),
533686
}
534687
}
@@ -543,7 +696,7 @@ impl PartialEq for SockAddr {
543696
(SockAddr::Unix(ref a), SockAddr::Unix(ref b)) => {
544697
a == b
545698
}
546-
#[cfg(any(target_os = "linux", target_os = "android"))]
699+
#[cfg(any(target_os = "android", target_os = "linux"))]
547700
(SockAddr::Netlink(ref a), SockAddr::Netlink(ref b)) => {
548701
a == b
549702
}
@@ -560,9 +713,9 @@ impl hash::Hash for SockAddr {
560713
match *self {
561714
SockAddr::Inet(ref a) => a.hash(s),
562715
SockAddr::Unix(ref a) => a.hash(s),
563-
#[cfg(any(target_os = "linux", target_os = "android"))]
716+
#[cfg(any(target_os = "android", target_os = "linux"))]
564717
SockAddr::Netlink(ref a) => a.hash(s),
565-
#[cfg(any(target_os = "macos", target_os = "ios"))]
718+
#[cfg(any(target_os = "ios", target_os = "macos"))]
566719
SockAddr::SysControl(ref a) => a.hash(s),
567720
}
568721
}
@@ -579,15 +732,15 @@ impl fmt::Display for SockAddr {
579732
match *self {
580733
SockAddr::Inet(ref inet) => inet.fmt(f),
581734
SockAddr::Unix(ref unix) => unix.fmt(f),
582-
#[cfg(any(target_os = "linux", target_os = "android"))]
735+
#[cfg(any(target_os = "android", target_os = "linux"))]
583736
SockAddr::Netlink(ref nl) => nl.fmt(f),
584-
#[cfg(any(target_os = "macos", target_os = "ios"))]
737+
#[cfg(any(target_os = "ios", target_os = "macos"))]
585738
SockAddr::SysControl(ref sc) => sc.fmt(f),
586739
}
587740
}
588741
}
589742

590-
#[cfg(any(target_os = "linux", target_os = "android"))]
743+
#[cfg(any(target_os = "android", target_os = "linux"))]
591744
pub mod netlink {
592745
use ::sys::socket::addr::{AddressFamily};
593746
use libc::{sa_family_t, sockaddr_nl};
@@ -642,7 +795,7 @@ pub mod netlink {
642795
}
643796
}
644797

645-
#[cfg(any(target_os = "macos", target_os = "ios"))]
798+
#[cfg(any(target_os = "ios", target_os = "macos"))]
646799
pub mod sys_control {
647800
use ::sys::socket::addr::{AddressFamily};
648801
use libc;

src/sys/socket/sockopt.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use libc;
33
use {Errno, Result};
44
use sys::time::TimeVal;
55
use libc::{c_int, uint8_t, c_void, socklen_t};
6-
#[cfg(target_os = "linux")]
7-
use libc::sockaddr_in;
86
use std::mem;
97
use std::os::unix::io::RawFd;
108

@@ -173,7 +171,7 @@ sockopt_impl!(SetOnly, SndBufForce, libc::SOL_SOCKET, libc::SO_SNDBUFFORCE, usiz
173171
sockopt_impl!(GetOnly, SockType, libc::SOL_SOCKET, libc::SO_TYPE, super::SockType);
174172
sockopt_impl!(GetOnly, AcceptConn, libc::SOL_SOCKET, libc::SO_ACCEPTCONN, bool);
175173
#[cfg(any(target_os = "linux", target_os = "android"))]
176-
sockopt_impl!(GetOnly, OriginalDst, libc::SOL_IP, libc::SO_ORIGINAL_DST, sockaddr_in);
174+
sockopt_impl!(GetOnly, OriginalDst, libc::SOL_IP, libc::SO_ORIGINAL_DST, libc::sockaddr_in);
177175

178176
/*
179177
*

0 commit comments

Comments
 (0)