Skip to content

Instantly share code, notes, and snippets.

@sajattack
Last active November 24, 2017 05:03
Show Gist options
  • Save sajattack/62ed1c91f8ebc357b7792d0d3166c148 to your computer and use it in GitHub Desktop.
Save sajattack/62ed1c91f8ebc357b7792d0d3166c148 to your computer and use it in GitHub Desktop.
libc_fn!(unsafe gethostent() -> Result<*const hostent> {
if hostdb == 0 {
hostdb = syscall::open("/etc/hosts", syscall::O_RDONLY).unwrap();
}
let mut line = RawLineBuffer::new(hostdb);
let mut host_aliases: [*const u8; MAXALIASES] = [0 as *const u8; MAXALIASES];
let hostaddr: [usize; (MAXADDRS+mem::size_of::<u64>()-1)/mem::size_of::<u64>()] = [0; (MAXADDRS+mem::size_of::<u64>()-1)/mem::size_of::<u64>()];
let mut host_addrs: [*mut libc::c_char;2] = [0 as *mut libc::c_char; 2];
let mut currentBlock = 0;
let mut p : *mut u8 = null_mut();
let mut cp : *mut u8 = null_mut();
let mut q : *mut *mut u8;
'loop1: loop {
let mut r = line.next();
//let _ = syscall::write(2, "loop 1\n".as_bytes());
if r == None {
//let _ = syscall::write(2, "r == None\n".as_bytes());
currentBlock = 18;
break;
} else {
//let _ = syscall::write(2, "unwrapping line\n".as_bytes());
p = r.unwrap().unwrap().as_bytes().as_ptr() as *mut u8;
//let _ = syscall::write(2, "line unwrapped successfully\n".as_bytes());
//let _ = syscall::write(2, slice::from_raw_parts(p as *const u8, 1));
}
if *p as i32 == b'#' as i32 {
continue;
}
cp = libc::strpbrk(p as *mut i8, ['#' as i8,'\n' as i8, '\0' as i8].as_ptr()) as *mut u8;
if cp == 0 as *mut u8 {
continue;
}
*cp = b'\0';
cp = libc::strpbrk(p as *mut i8, [' ' as i8,'\t' as i8,'\0' as i8].as_ptr()) as *mut u8;
if !(cp == 0 as *mut u8) {
currentBlock = 5;
break;
}
}
if currentBlock == 5 {
//let _ = syscall::write(2, "currentBlock == 5\n".as_bytes());
*{
let _old = cp;
cp = cp.offset(1);
_old
} = b'\0';
let h_addr_list = host_addrs.as_mut_ptr();
*h_addr_list.offset(0) = hostaddr.as_ptr() as *mut libc::c_char;
*(*h_addr_list.offset(0) as *mut usize) =
{
let mut addr = mem::uninitialized();
::socket::inet_aton(p as *const i8, &mut addr) as usize;
addr.s_addr.as_ptr() as usize
};
let h_length = mem::size_of::<usize> as i32;
let h_addrtype = ::socket::AF_INET;
'loop6: loop {
// let _ = syscall::write(2, "loop 6\n".as_bytes());
if !*cp as i32 == b' ' as i32 || *cp as i32 == b'\t' as i32 {
break;
}
cp = cp.offset(1);
}
let h_name = cp as *mut i8;
let mut h_aliases: *mut *mut i8 = mem::uninitialized();
q = {
h_aliases = host_aliases.as_mut_ptr() as *mut *mut i8;
h_aliases as *mut *mut u8
};
if cp != 0 as *mut u8{
*{
let _old = cp;
cp = cp.offset(1);
_old
} = b'\0';
}
'loop9: loop {
// let _ = syscall::write(2, "loop 9\n".as_bytes());
if !(!cp.is_null() && (*cp != 0)) {
break;
}
if *cp as i32 == b' ' as i32 || cp as (i32) == b'\t' as i32 {
cp = cp.offset(1);
} else {
if q < &host_aliases[MAXALIASES - 1 as usize] as *const *const u8 as *mut *mut u8{
*{
let _old = q;
q = q.offset(1);
_old
} = cp;
}
if !(cp != 0 as *mut u8) {
continue;
}
*{
let _old = cp;
cp = cp.offset(1);
_old
} = b'\0';
}
}
*q = 0 as *mut u8;
let _ = syscall::write(2, ::cstr_to_slice(h_name as *const c_char));
let host = hostent {
h_name: h_name as *const c_char,
h_aliases: h_aliases as *const *const c_char,
h_addrtype: h_addrtype,
h_length: h_length,
h_addr_list: h_addr_list as *const *const i8
};
Ok(&host as (*const hostent))
} else {
Err(Error::new(EIO))
}
});
@dlrobertson
Copy link

While it is quite clever

*{
    let _old = cp;
    cp = cp.offset(1);
    _old
} = b'\0';

Should be able to be replaced with *cp.offset(1) = b'\0' right? Or is there another reason for the temporary variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment