Merged master into master

This commit is contained in:
Tom Almeida
2018-03-20 23:48:07 +08:00
29 changed files with 509 additions and 42 deletions
+2
View File
@@ -11,8 +11,10 @@ extern crate fenv;
extern crate float;
extern crate grp;
extern crate mman;
extern crate netinet;
extern crate resource;
extern crate semaphore;
extern crate socket;
extern crate stat;
extern crate stdio;
extern crate stdlib;
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "netinet"
version = "0.1.0"
authors = ["Dan Robertson <danlrobertson89@gmail.com>"]
[dependencies]
in_h = { path = "in" }
+12
View File
@@ -0,0 +1,12 @@
[package]
name = "in_h"
version = "0.1.0"
authors = ["Dan Robertson <danlrobertson89@gmail.com>"]
build = "build.rs"
[build-dependencies]
cbindgen = { path = "../../../cbindgen" }
[dependencies]
platform = { path = "../../platform" }
socket = { path = "../../socket" }
+12
View File
@@ -0,0 +1,12 @@
extern crate cbindgen;
use std::{env, fs};
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
fs::create_dir_all("../../../target/include").expect("failed to create include directory");
fs::create_dir_all("../../../target/include/netinet").expect("failed to create include directory");
cbindgen::generate(crate_dir)
.expect("failed to generate bindings")
.write_to_file("../../../target/include/netinet/in.h");
}
+10
View File
@@ -0,0 +1,10 @@
sys_includes = ["sys/types.h", "sys/socket.h"]
include_guard = "_NETINET_IN_H"
style = "Tag"
language = "C"
[export]
include = ["sockaddr_in6", "sockaddr_in", "ipv6_mreq"]
[enum]
prefix_with_name = true
+58
View File
@@ -0,0 +1,58 @@
#![no_std]
#![allow(non_camel_case_types)]
extern crate platform;
extern crate socket;
use platform::types::*;
use socket::sa_family_t;
pub type in_addr_t = u32;
pub type in_port_t = u16;
#[repr(C)]
#[derive(Debug)]
pub struct in_addr {
pub s_addr: in_addr_t
}
#[repr(C)]
pub struct in6_addr {
pub s6_addr: [u8; 16]
}
#[repr(C)]
pub struct sockaddr_in {
pub sa_family: sa_family_t,
pub sin_port: in_port_t,
pub sin_addr: in_addr
}
#[repr(C)]
pub struct sockaddr_in6 {
pub sin6_family: sa_family_t,
pub sin6_port: in_port_t,
pub sin6_flowinfo: u32,
pub sin6_addr: in6_addr,
pub sin6_scope_id: u32
}
#[repr(C)]
pub struct ipv6_mreq {
pub ipv6mr_multiaddr: in6_addr,
pub ipv6mr_interface: u32,
}
// Address String Lengths
pub const INET_ADDRSTRLEN: c_int = 16;
pub const INET6_ADDRSTRLEN: c_int = 46;
// Protocol Numbers
pub const IPPROTO_IP: u8 = 0x00;
pub const IPPROTO_ICMP: u8 = 0x01;
pub const IPPROTO_TCP: u8 = 0x06;
pub const IPPROTO_UDP: u8 = 0x11;
pub const IPPROTO_IPV6: u8 = 0x29;
pub const IPPROTO_RAW: u8 = 0xff;
pub const IPPROTO_MAX: u8 = 0xff;
+3
View File
@@ -0,0 +1,3 @@
#![no_std]
extern crate in_h;
+16 -20
View File
@@ -146,27 +146,23 @@ pub fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
}
pub fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
unsafe {
let redox_rqtp = redox_timespec {
tv_sec: (*rqtp).tv_sec,
tv_nsec: (*rqtp).tv_nsec as i32,
};
let mut redox_rmtp: redox_timespec;
if rmtp.is_null() {
redox_rmtp = redox_timespec::default();
} else {
redox_rmtp = redox_timespec {
tv_sec: (*rmtp).tv_sec,
tv_nsec: (*rmtp).tv_nsec as i32,
};
}
match e(syscall::nanosleep(&redox_rqtp, &mut redox_rmtp)) as c_int {
-1 => -1,
_ => {
(*rmtp).tv_sec = redox_rmtp.tv_sec;
(*rmtp).tv_nsec = redox_rmtp.tv_nsec as i64;
0
let redox_rqtp = unsafe { redox_timespec::from(&*rqtp) };
let mut redox_rmtp: redox_timespec;
if rmtp.is_null() {
redox_rmtp = redox_timespec::default();
} else {
redox_rmtp = unsafe { redox_timespec::from(&*rmtp) };
}
match e(syscall::nanosleep(&redox_rqtp, &mut redox_rmtp)) as c_int {
-1 => -1,
_ => {
unsafe {
if !rmtp.is_null() {
(*rmtp).tv_sec = redox_rmtp.tv_sec;
(*rmtp).tv_nsec = redox_rmtp.tv_nsec as i64;
}
}
0
}
}
}
+12
View File
@@ -1,3 +1,5 @@
#[cfg(target_os = "redox")]
use syscall::data::TimeSpec as redox_timespec;
// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
// more optimization opportunities around it recognizing things like
// malloc/free.
@@ -70,3 +72,13 @@ pub struct timespec {
pub tv_sec: time_t,
pub tv_nsec: c_long,
}
#[cfg(target_os = "redox")]
impl<'a> From<&'a timespec> for redox_timespec {
fn from(tp: &timespec) -> redox_timespec {
redox_timespec {
tv_sec: tp.tv_sec,
tv_nsec: tp.tv_nsec as i32,
}
}
}
+11
View File
@@ -0,0 +1,11 @@
[package]
name = "socket"
version = "0.1.0"
authors = ["Dan Robertson <danlrobertson89@gmail.com>"]
build = "build.rs"
[build-dependencies]
cbindgen = { path = "../../cbindgen" }
[dependencies]
platform = { path = "../platform" }
+11
View File
@@ -0,0 +1,11 @@
extern crate cbindgen;
use std::{env, fs};
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
fs::create_dir_all("../../target/include").expect("failed to create include directory");
cbindgen::generate(crate_dir)
.expect("failed to generate bindings")
.write_to_file("../../target/include/sys/socket.h");
}
+11
View File
@@ -0,0 +1,11 @@
sys_includes = ["sys/types.h"]
include_guard = "_SYS_SOCKET_H"
style = "Tag"
language = "C"
[defines]
"target_os=linux" = "__linux__"
"target_os=redox" = "__redox__"
[enum]
prefix_with_name = true
+153
View File
@@ -0,0 +1,153 @@
//! socket implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xns/syssocket.h.html
#![no_std]
#![allow(non_camel_case_types)]
extern crate platform;
use platform::types::*;
pub type sa_family_t = u16;
pub type socklen_t = u32;
#[repr(C)]
pub struct sockaddr {
pub sa_family: sa_family_t,
pub sa_data: [c_char; 14],
}
#[no_mangle]
pub unsafe extern "C" fn accept(
socket: c_int,
address: *mut sockaddr,
address_len: *mut socklen_t,
) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn bind(
socket: c_int,
address: *const sockaddr,
address_len: socklen_t,
) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn connect(
socket: c_int,
address: *const sockaddr,
address_len: socklen_t,
) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn getpeername(
socket: c_int,
address: *const sockaddr,
address_len: socklen_t,
) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn getsockname(
socket: c_int,
address: *mut sockaddr,
address_len: *mut socklen_t,
) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn getsockopt(
socket: c_int,
level: c_int,
option_name: c_int,
option_value: *mut c_void,
option_len: *mut socklen_t,
) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn listen(socket: c_int, backlog: c_int) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn recv(
socket: c_int,
buffer: *mut c_void,
length: size_t,
flags: c_int,
) -> ssize_t {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn recvfrom(
socket: c_int,
buffer: *mut c_void,
length: size_t,
flags: c_int,
address: *mut sockaddr,
address_len: *mut socklen_t,
) -> ssize_t {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn send(
socket: c_int,
message: *const c_void,
length: size_t,
flags: c_int,
) -> ssize_t {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn sento(
socket: c_int,
message: *const c_void,
length: size_t,
flags: c_int,
dest_addr: *const sockaddr,
dest_len: socklen_t,
) -> ssize_t {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn setsockopt(
socket: c_int,
level: c_int,
option_name: c_int,
option_value: *const c_void,
option_len: socklen_t,
) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn shutdown(socket: c_int, how: c_int) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn socket(domain: c_int, _type: c_int, protocol: c_int) -> c_int {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn socketpair(
domain: c_int,
_type: c_int,
protocol: c_int,
socket_vector: [c_int; 2],
) -> c_int {
unimplemented!();
}
+21 -3
View File
@@ -130,15 +130,33 @@ pub extern "C" fn atol(s: *const c_char) -> c_long {
dec_num_from_ascii!(s, c_long)
}
unsafe extern "C" fn void_cmp(a: *const c_void, b: *const c_void) -> c_int {
return *(a as *const i32) - *(b as *const i32) as c_int;
}
#[no_mangle]
pub extern "C" fn bsearch(
pub unsafe extern "C" fn bsearch(
key: *const c_void,
base: *const c_void,
nel: size_t,
width: size_t,
compar: Option<extern "C" fn(*const c_void, *const c_void) -> c_int>,
compar: Option<unsafe extern "C" fn(*const c_void, *const c_void) -> c_int>,
) -> *mut c_void {
unimplemented!();
let mut start = base;
let mut len = nel;
let cmp_fn = compar.unwrap_or(void_cmp);
while len > 0 {
let med = (start as size_t + (len >> 1) * width) as *const c_void;
let diff = cmp_fn(key, med);
if diff == 0 {
return med as *mut c_void;
} else if diff > 0 {
start = (med as usize + width) as *const c_void;
len -= 1;
}
len >>= 1;
}
ptr::null_mut()
}
#[no_mangle]
+36 -4
View File
@@ -323,17 +323,49 @@ pub unsafe extern "C" fn strstr(s1: *const c_char, s2: *const c_char) -> *mut c_
}
#[no_mangle]
pub extern "C" fn strtok(s1: *mut c_char, s2: *const c_char) -> *mut c_char {
unimplemented!();
pub extern "C" fn strtok(s1: *mut c_char, delimiter: *const c_char) -> *mut c_char {
static mut HAYSTACK: *mut c_char = ptr::null_mut();
unsafe {
return strtok_r(s1, delimiter, &mut HAYSTACK);
}
}
#[no_mangle]
pub extern "C" fn strtok_r(
s: *mut c_char,
sep: *const c_char,
delimiter: *const c_char,
lasts: *mut *mut c_char,
) -> *mut c_char {
unimplemented!();
// Loosely based on GLIBC implementation
unsafe {
let mut haystack = s;
if haystack.is_null() {
if (*lasts).is_null() {
return ptr::null_mut();
}
haystack = *lasts;
}
// Skip past any extra delimiter left over from previous call
haystack = haystack.add(strspn(haystack, delimiter));
if *haystack == 0 {
*lasts = ptr::null_mut();
return ptr::null_mut();
}
// Build token by injecting null byte into delimiter
let token = haystack;
haystack = strpbrk(token, delimiter);
if !haystack.is_null() {
haystack.write(0);
haystack = haystack.add(1);
*lasts = haystack;
} else {
*lasts = ptr::null_mut();
}
return token;
}
}
#[no_mangle]