Skip to content

Commit 43ed207

Browse files
authored
Getifaddrs (#137)
* new * getifaddrs * getifaddrs * getifaddrs * getifaddrs * getifaddrs * cleanup * cleanup * cleanup * PR fix * PR fix
1 parent e739403 commit 43ed207

File tree

6 files changed

+82
-18
lines changed

6 files changed

+82
-18
lines changed

gen_netdevs.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <arpa/inet.h>
2+
#include <sys/socket.h>
3+
#include <ifaddrs.h>
4+
#include <stdio.h>
5+
#include <sys/ioctl.h>
6+
#include <net/if.h>
7+
8+
int main ()
9+
{
10+
struct ifaddrs *ifap, *ifa;
11+
struct sockaddr_in *sa, *ba, *na, *da;
12+
char *addr, *baddr, *naddr, *daddr;
13+
14+
getifaddrs (&ifap);
15+
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
16+
if (ifa->ifa_addr && ifa->ifa_addr->sa_family==AF_INET) {
17+
sa = (struct sockaddr_in *) ifa->ifa_addr;
18+
addr = inet_ntoa(sa->sin_addr);
19+
na = (struct sockaddr_in *) ifa->ifa_netmask;
20+
naddr = inet_ntoa(na->sin_addr);
21+
ba = (struct sockaddr_in *) ifa->ifa_broadaddr;
22+
baddr = inet_ntoa(ba->sin_addr);
23+
24+
printf("%s %d %s %s %s\n", ifa->ifa_name, ifa->ifa_flags, addr, naddr, baddr);
25+
}
26+
}
27+
28+
freeifaddrs(ifap);
29+
return 0;
30+
}

gen_netdevs.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
#!/bin/bash
2-
shopt -u expand_aliases
3-
ip a | grep 'inet ' | awk '{print $2}' | sed 's/\/.*$//g' > net_devices
4-
echo '0.0.0.0' >> net_devices
2+
FILE=gen_netdevs
3+
if [ -f "$FILE" ]; then
4+
echo "$FILE exists."
5+
else
6+
echo "$FILE does not exist. Compiling."
7+
gcc /home/lind/lind_project/src/safeposix-rust/gen_netdevs.c -o gen_netdevs
8+
fi
9+
10+
echo "Generating netdevs"
11+
12+
./gen_netdevs > net_devices

src/interface/comm.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,6 @@ impl Drop for Socket {
352352
}
353353
}
354354

355-
pub fn read_netdevs() -> Vec<GenIpaddr> {
356-
let mut ips = vec!();
357-
for net_device in read_to_string(NET_DEV_FILENAME).expect("No net_devices file present!").split('\n') {
358-
if net_device == "" {continue;}
359-
let genipopt = GenIpaddr::from_string(net_device);
360-
ips.push(genipopt.expect("Could not parse device ip address from net_devices file"));
361-
}
362-
return ips;
355+
pub fn getifaddrs_from_file() -> String {
356+
read_to_string(NET_DEV_FILENAME).expect("No net_devices file present!").to_owned()
363357
}

src/safeposix/dispatcher.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const SOCKET_SYSCALL: i32 = 136;
7575

7676
const GETSOCKNAME_SYSCALL: i32 = 144;
7777
const GETPEERNAME_SYSCALL: i32 = 145;
78+
const GETIFADDRS_SYSCALL: i32 = 146;
7879

7980

8081
use crate::interface;
@@ -266,6 +267,9 @@ pub extern "C" fn dispatcher(cageid: u64, callnum: i32, arg1: Arg, arg2: Arg, ar
266267
}
267268
rv
268269
}
270+
GETIFADDRS_SYSCALL => {
271+
check_and_dispatch!(cage.getifaddrs_syscall, interface::get_mutcbuf(arg1), interface::get_usize(arg2))
272+
}
269273
GETSOCKOPT_SYSCALL => {
270274
let mut sockval = 0;
271275
if interface::arg_nullity(&arg4) || interface::arg_nullity(&arg5) {

src/safeposix/net.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
4359
pub 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

src/safeposix/syscalls/net_calls.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,4 +1707,16 @@ impl Cage {
17071707
}
17081708
return 0;
17091709
}
1710+
1711+
// all this does is send the net_devs data in a string to libc, where we will later parse and
1712+
// alloc into getifaddrs structs
1713+
pub fn getifaddrs_syscall(&self, buf: *mut u8, count: usize) -> i32 {
1714+
if NET_IFADDRS_STR.len() < count {
1715+
interface::fill(buf, NET_IFADDRS_STR.len(), &NET_IFADDRS_STR.as_bytes().to_vec());
1716+
0 // return success
1717+
}
1718+
else {
1719+
return syscall_error(Errno::EOPNOTSUPP, "getifaddrs", "invalid ifaddrs length");
1720+
}
1721+
}
17101722
}

0 commit comments

Comments
 (0)