@@ -5,33 +5,186 @@ use std::{fmt, hash, mem, net, ptr};
5
5
use std:: ffi:: OsStr ;
6
6
use std:: path:: Path ;
7
7
use std:: os:: unix:: ffi:: OsStrExt ;
8
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
8
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
9
9
use :: sys:: socket:: addr:: netlink:: NetlinkAddr ;
10
- #[ cfg( any( target_os = "macos " , target_os = "ios " ) ) ]
10
+ #[ cfg( any( target_os = "ios " , target_os = "macos " ) ) ]
11
11
use std:: os:: unix:: io:: RawFd ;
12
- #[ cfg( any( target_os = "macos " , target_os = "ios " ) ) ]
12
+ #[ cfg( any( target_os = "ios " , target_os = "macos " ) ) ]
13
13
use :: sys:: socket:: addr:: sys_control:: SysControlAddr ;
14
14
15
- // TODO: uncomment out IpAddr functions: rust-lang/rfcs#988
16
-
17
- /*
18
- *
19
- * ===== AddressFamily =====
20
- *
21
- */
22
-
23
15
#[ repr( i32 ) ]
24
16
#[ derive( Copy , Clone , PartialEq , Eq , Debug , Hash ) ]
25
17
pub enum AddressFamily {
26
18
Unix = libc:: AF_UNIX ,
27
19
Inet = libc:: AF_INET ,
28
20
Inet6 = libc:: AF_INET6 ,
29
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
21
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
30
22
Netlink = libc:: AF_NETLINK ,
31
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
23
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
32
24
Packet = libc:: AF_PACKET ,
33
- #[ cfg( any( target_os = "macos " , target_os = "ios " ) ) ]
25
+ #[ cfg( any( target_os = "ios " , target_os = "macos " ) ) ]
34
26
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 ,
35
188
}
36
189
37
190
#[ derive( Copy ) ]
@@ -252,7 +405,7 @@ impl Ipv4Addr {
252
405
}
253
406
254
407
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 } )
256
409
}
257
410
258
411
pub fn octets ( & self ) -> [ u8 ; 4 ] {
@@ -480,9 +633,9 @@ impl fmt::Display for UnixAddr {
480
633
pub enum SockAddr {
481
634
Inet ( InetAddr ) ,
482
635
Unix ( UnixAddr ) ,
483
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
636
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
484
637
Netlink ( NetlinkAddr ) ,
485
- #[ cfg( any( target_os = "macos " , target_os = "ios " ) ) ]
638
+ #[ cfg( any( target_os = "ios " , target_os = "macos " ) ) ]
486
639
SysControl ( SysControlAddr ) ,
487
640
}
488
641
@@ -495,12 +648,12 @@ impl SockAddr {
495
648
Ok ( SockAddr :: Unix ( try!( UnixAddr :: new ( path) ) ) )
496
649
}
497
650
498
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
651
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
499
652
pub fn new_netlink ( pid : u32 , groups : u32 ) -> SockAddr {
500
653
SockAddr :: Netlink ( NetlinkAddr :: new ( pid, groups) )
501
654
}
502
655
503
- #[ cfg( any( target_os = "macos " , target_os = "ios " ) ) ]
656
+ #[ cfg( any( target_os = "ios " , target_os = "macos " ) ) ]
504
657
pub fn new_sys_control ( sockfd : RawFd , name : & str , unit : u32 ) -> Result < SockAddr > {
505
658
SysControlAddr :: from_name ( sockfd, name, unit) . map ( |a| SockAddr :: SysControl ( a) )
506
659
}
@@ -510,9 +663,9 @@ impl SockAddr {
510
663
SockAddr :: Inet ( InetAddr :: V4 ( ..) ) => AddressFamily :: Inet ,
511
664
SockAddr :: Inet ( InetAddr :: V6 ( ..) ) => AddressFamily :: Inet6 ,
512
665
SockAddr :: Unix ( ..) => AddressFamily :: Unix ,
513
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
666
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
514
667
SockAddr :: Netlink ( ..) => AddressFamily :: Netlink ,
515
- #[ cfg( any( target_os = "macos " , target_os = "ios " ) ) ]
668
+ #[ cfg( any( target_os = "ios " , target_os = "macos " ) ) ]
516
669
SockAddr :: SysControl ( ..) => AddressFamily :: System ,
517
670
}
518
671
}
@@ -526,9 +679,9 @@ impl SockAddr {
526
679
SockAddr :: Inet ( InetAddr :: V4 ( ref addr) ) => ( mem:: transmute ( addr) , mem:: size_of :: < libc:: sockaddr_in > ( ) as libc:: socklen_t ) ,
527
680
SockAddr :: Inet ( InetAddr :: V6 ( ref addr) ) => ( mem:: transmute ( addr) , mem:: size_of :: < libc:: sockaddr_in6 > ( ) as libc:: socklen_t ) ,
528
681
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 " ) ) ]
530
683
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 " ) ) ]
532
685
SockAddr :: SysControl ( SysControlAddr ( ref sa) ) => ( mem:: transmute ( sa) , mem:: size_of :: < sys_control:: sockaddr_ctl > ( ) as libc:: socklen_t ) ,
533
686
}
534
687
}
@@ -543,7 +696,7 @@ impl PartialEq for SockAddr {
543
696
( SockAddr :: Unix ( ref a) , SockAddr :: Unix ( ref b) ) => {
544
697
a == b
545
698
}
546
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
699
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
547
700
( SockAddr :: Netlink ( ref a) , SockAddr :: Netlink ( ref b) ) => {
548
701
a == b
549
702
}
@@ -560,9 +713,9 @@ impl hash::Hash for SockAddr {
560
713
match * self {
561
714
SockAddr :: Inet ( ref a) => a. hash ( s) ,
562
715
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 " ) ) ]
564
717
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 " ) ) ]
566
719
SockAddr :: SysControl ( ref a) => a. hash ( s) ,
567
720
}
568
721
}
@@ -579,15 +732,15 @@ impl fmt::Display for SockAddr {
579
732
match * self {
580
733
SockAddr :: Inet ( ref inet) => inet. fmt ( f) ,
581
734
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 " ) ) ]
583
736
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 " ) ) ]
585
738
SockAddr :: SysControl ( ref sc) => sc. fmt ( f) ,
586
739
}
587
740
}
588
741
}
589
742
590
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
743
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
591
744
pub mod netlink {
592
745
use :: sys:: socket:: addr:: { AddressFamily } ;
593
746
use libc:: { sa_family_t, sockaddr_nl} ;
@@ -642,7 +795,7 @@ pub mod netlink {
642
795
}
643
796
}
644
797
645
- #[ cfg( any( target_os = "macos " , target_os = "ios " ) ) ]
798
+ #[ cfg( any( target_os = "ios " , target_os = "macos " ) ) ]
646
799
pub mod sys_control {
647
800
use :: sys:: socket:: addr:: { AddressFamily } ;
648
801
use libc;
0 commit comments