Pārlūkot izejas kodu

Merge pull request #86 from dlrobertson/add_headers

Add some of the basics for netinet/in.h and sys/socket.h
Jeremy Soller 7 gadi atpakaļ
vecāks
revīzija
5cec358a45

+ 25 - 0
Cargo.lock

@@ -139,6 +139,14 @@ dependencies = [
  "platform 0.1.0",
 ]
 
+[[package]]
+name = "in_h"
+version = "0.1.0"
+dependencies = [
+ "cbindgen 0.5.2",
+ "platform 0.1.0",
+]
+
 [[package]]
 name = "itoa"
 version = "0.3.4"
@@ -182,6 +190,13 @@ dependencies = [
  "platform 0.1.0",
 ]
 
+[[package]]
+name = "netinet"
+version = "0.1.0"
+dependencies = [
+ "in_h 0.1.0",
+]
+
 [[package]]
 name = "num-traits"
 version = "0.2.1"
@@ -259,10 +274,12 @@ dependencies = [
  "float 0.1.0",
  "grp 0.1.0",
  "mman 0.1.0",
+ "netinet 0.1.0",
  "platform 0.1.0",
  "resource 0.1.0",
  "semaphore 0.1.0",
  "signal 0.1.0",
+ "socket 0.1.0",
  "stat 0.1.0",
  "stdio 0.1.0",
  "stdlib 0.1.0",
@@ -351,6 +368,14 @@ dependencies = [
  "platform 0.1.0",
 ]
 
+[[package]]
+name = "socket"
+version = "0.1.0"
+dependencies = [
+ "cbindgen 0.5.2",
+ "platform 0.1.0",
+]
+
 [[package]]
 name = "standalone-quote"
 version = "0.5.0"

+ 2 - 0
Cargo.toml

@@ -19,10 +19,12 @@ fenv = { path = "src/fenv" }
 float = { path = "src/float" }
 grp = { path = "src/grp" }
 mman = { path = "src/mman" }
+netinet = { path = "src/netinet" }
 platform = { path = "src/platform" }
 resource = { path = "src/resource" }
 semaphore = { path = "src/semaphore" }
 signal = { path = "src/signal" }
+socket = { path = "src/socket" }
 stat = { path = "src/stat" }
 stdio = { path = "src/stdio" }
 stdlib = { path = "src/stdlib" }

+ 2 - 0
src/lib.rs

@@ -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 - 0
src/netinet/Cargo.toml

@@ -0,0 +1,7 @@
+[package]
+name = "netinet"
+version = "0.1.0"
+authors = ["Dan Robertson <danlrobertson89@gmail.com>"]
+
+[dependencies]
+in_h = { path = "in" }

+ 12 - 0
src/netinet/in/Cargo.toml

@@ -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 - 0
src/netinet/in/build.rs

@@ -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 - 0
src/netinet/in/cbindgen.toml

@@ -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 - 0
src/netinet/in/src/lib.rs

@@ -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 - 0
src/netinet/src/lib.rs

@@ -0,0 +1,3 @@
+#![no_std]
+
+extern crate in_h;

+ 11 - 0
src/socket/Cargo.toml

@@ -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 - 0
src/socket/build.rs

@@ -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 - 0
src/socket/cbindgen.toml

@@ -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 - 0
src/socket/src/lib.rs

@@ -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!();
+}