@@ -37,7 +37,23 @@ pub static NET_METADATA: interface::RustLazyGlobal<interface::RustRfc<NetMetadat
3737//A list of all network devices present on the machine
3838//It is populated from a file that should be present prior to running rustposix, see
3939//the implementation of read_netdevs for specifics
40- pub static NET_DEVICES_LIST : interface:: RustLazyGlobal < Vec < interface:: GenIpaddr > > = interface:: RustLazyGlobal :: new ( || interface:: read_netdevs ( ) ) ;
40+ pub static NET_IFADDRS_STR : interface:: RustLazyGlobal < String > = interface:: RustLazyGlobal :: new ( || interface:: getifaddrs_from_file ( ) ) ;
41+
42+ pub static NET_DEVICE_IPLIST : interface:: RustLazyGlobal < Vec < interface:: GenIpaddr > > = interface:: RustLazyGlobal :: new ( || ips_from_ifaddrs ( ) ) ;
43+
44+ fn ips_from_ifaddrs ( ) -> Vec < interface:: GenIpaddr > {
45+ let mut ips = vec ! [ ] ;
46+ for net_device in NET_IFADDRS_STR . as_str ( ) . split ( '\n' ) {
47+ if net_device == "" { continue ; }
48+ let ifaddrstr: Vec < & str > = net_device. split ( ' ' ) . collect ( ) ;
49+ let genipopt = interface:: GenIpaddr :: from_string ( ifaddrstr[ 2 ] ) ;
50+ ips. push ( genipopt. expect ( "Could not parse device ip address from net_devices file" ) ) ;
51+ }
52+
53+ let genipopt0 = interface:: GenIpaddr :: from_string ( "0.0.0.0" ) ;
54+ ips. push ( genipopt0. expect ( "Could not parse device ip address from net_devices file" ) ) ;
55+ return ips;
56+ }
4157
4258#[ derive( Debug , Hash , Eq , PartialEq , Clone ) ]
4359pub enum PortType {
@@ -77,7 +93,7 @@ impl NetMetadata {
7793 }
7894 interface:: RustHashEntry :: Vacant ( v) => {
7995 let mut intervec = vec ! ( ) ;
80- for interface_addr in & * NET_DEVICES_LIST {
96+ for interface_addr in & * NET_DEVICE_IPLIST {
8197 intervec. push ( ( interface_addr. clone ( ) , rebindability) ) ;
8298 }
8399 v. insert ( intervec) ;
@@ -103,7 +119,7 @@ impl NetMetadata {
103119 }
104120 }
105121 pub fn _get_available_udp_port ( & self , addr : interface:: GenIpaddr , domain : i32 , rebindability : bool ) -> Result < u16 , i32 > {
106- if !NET_DEVICES_LIST . contains ( & addr) {
122+ if !NET_DEVICE_IPLIST . contains ( & addr) {
107123 return Err ( syscall_error ( Errno :: EADDRNOTAVAIL , "bind" , "Specified network device is not set up for lind or does not exist!" ) ) ;
108124 }
109125 let mut porttuple = mux_port ( addr, 0 , domain, UDPPORT ) ;
@@ -132,7 +148,7 @@ impl NetMetadata {
132148 return Err ( syscall_error ( Errno :: EADDRINUSE , "bind" , "No available ephemeral port could be found" ) ) ;
133149 }
134150 pub fn _get_available_tcp_port ( & self , addr : interface:: GenIpaddr , domain : i32 , rebindability : bool ) -> Result < u16 , i32 > {
135- if !NET_DEVICES_LIST . contains ( & addr) {
151+ if !NET_DEVICE_IPLIST . contains ( & addr) {
136152 return Err ( syscall_error ( Errno :: EADDRNOTAVAIL , "bind" , "Specified network device is not set up for lind or does not exist!" ) ) ;
137153 }
138154 let mut porttuple = mux_port ( addr. clone ( ) , 0 , domain, TCPPORT ) ;
@@ -173,7 +189,7 @@ impl NetMetadata {
173189 }
174190
175191 pub fn _reserve_localport ( & self , addr : interface:: GenIpaddr , port : u16 , protocol : i32 , domain : i32 , rebindability : bool ) -> Result < u16 , i32 > {
176- if !NET_DEVICES_LIST . contains ( & addr) {
192+ if !NET_DEVICE_IPLIST . contains ( & addr) {
177193 return Err ( syscall_error ( Errno :: EADDRNOTAVAIL , "bind" , "Specified network device is not set up for lind or does not exist!" ) ) ;
178194 }
179195
@@ -202,7 +218,7 @@ impl NetMetadata {
202218 return Err ( syscall_error ( Errno :: EADDRINUSE , "reserve port" , "port is already in use" ) ) ;
203219 }
204220 interface:: RustHashEntry :: Vacant ( v) => {
205- v. insert ( NET_DEVICES_LIST . iter ( ) . map ( |x| ( x. clone ( ) , if rebindability { 1 } else { 0 } ) ) . collect ( ) ) ;
221+ v. insert ( NET_DEVICE_IPLIST . iter ( ) . map ( |x| ( x. clone ( ) , if rebindability { 1 } else { 0 } ) ) . collect ( ) ) ;
206222 }
207223 }
208224 } else {
@@ -228,7 +244,7 @@ impl NetMetadata {
228244 }
229245
230246 pub fn _release_localport ( & self , addr : interface:: GenIpaddr , port : u16 , protocol : i32 , domain : i32 ) -> Result < ( ) , i32 > {
231- if !NET_DEVICES_LIST . contains ( & addr) {
247+ if !NET_DEVICE_IPLIST . contains ( & addr) {
232248 return Err ( syscall_error ( Errno :: EADDRNOTAVAIL , "bind" , "Specified network device is not set up for lind or does not exist!" ) ) ;
233249 }
234250
0 commit comments