1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 
11 #![allow(non_camel_case_types)]
12 use libc::{self, c_int, __wasi_fd_t};
13 use std::io;
14 use std::mem;
15 use std::net::{TcpListener, TcpStream, UdpSocket};
16 use std::os::wasi::io::FromRawFd;
17 
18 mod impls;
19 
20 pub mod c {
21     pub use libc::*;
22 
23     pub type sa_family_t = u16;
24     pub type socklen_t = u32;
25     pub type in_port_t = u16;
26 
27     pub const SOCK_DGRAM: c_int = 0x00;
28     pub const SOL_SOCKET: c_int = 0x00;
29     pub const SO_RCVBUF: c_int = 0x00;
30     pub const SO_SNDBUF: c_int = 0x00;
31     pub const TCP_NODELAY: c_int = 0x00;
32     pub const IPPROTO_TCP: c_int = 0x00;
33     pub const SO_RCVTIMEO: c_int = 0x00;
34     pub const SO_SNDTIMEO: c_int = 0x00;
35     pub const IPPROTO_IP: c_int = 0x00;
36     pub const IP_TTL: c_int = 0x00;
37     pub const IPPROTO_IPV6: c_int = 0x00;
38     pub const IPV6_V6ONLY: c_int = 0x00;
39     pub const SO_ERROR: c_int = 0x00;
40     pub const SO_LINGER: c_int = 0x00;
41     pub const SO_BROADCAST: c_int = 0x00;
42     pub const IP_MULTICAST_LOOP: c_int = 0x00;
43     pub const IP_MULTICAST_TTL: c_int = 0x00;
44     pub const IPV6_MULTICAST_HOPS: c_int = 0x00;
45     pub const IPV6_MULTICAST_LOOP: c_int = 0x00;
46     pub const IP_MULTICAST_IF: c_int = 0x00;
47     pub const IPV6_MULTICAST_IF: c_int = 0x00;
48     pub const IPV6_UNICAST_HOPS: c_int = 0x00;
49     pub const IP_ADD_MEMBERSHIP: c_int = 0x00;
50     pub const IPV6_ADD_MEMBERSHIP: c_int = 0x00;
51     pub const IP_DROP_MEMBERSHIP: c_int = 0x00;
52     pub const IPV6_DROP_MEMBERSHIP: c_int = 0x00;
53     pub const SO_REUSEADDR: c_int = 0x00;
54     pub const SOCK_STREAM: c_int = 0x00;
55     pub const AF_INET: c_int = 0x00;
56     pub const AF_INET6: c_int = 0x01;
57 
58     #[repr(C)]
59     pub struct sockaddr_storage {
60         pub ss_family: sa_family_t,
61     }
62     #[repr(C)]
63     pub struct sockaddr {
64         pub sa_family: sa_family_t,
65         pub sa_data: [c_char; 14],
66     }
67 
68     #[repr(C)]
69     pub struct sockaddr_in6 {
70         pub sin6_family: sa_family_t,
71         pub sin6_port: in_port_t,
72         pub sin6_flowinfo: u32,
73         pub sin6_addr: in6_addr,
74         pub sin6_scope_id: u32,
75     }
76 
77     #[repr(C)]
78     pub struct sockaddr_in {
79         pub sin_family: sa_family_t,
80         pub sin_port: in_port_t,
81         pub sin_addr: in_addr,
82         pub sin_zero: [u8; 8],
83     }
84 
85     #[repr(align(4))]
86     #[derive(Copy, Clone)]
87     pub struct in6_addr {
88         pub s6_addr: [u8; 16],
89     }
90 
91     #[derive(Copy, Clone)]
92     pub struct ipv6_mreq {
93         pub ipv6mr_multiaddr: in6_addr,
94         pub ipv6mr_interface: c_uint,
95     }
96 
97     pub type in_addr_t = u32;
98     #[derive(Copy, Clone)]
99     pub struct in_addr {
100         pub s_addr: in_addr_t,
101     }
102 
103     #[derive(Copy, Clone)]
104     pub struct linger {
105         pub l_onoff: c_int,
106         pub l_linger: c_int,
107     }
108 
109     #[derive(Copy, Clone)]
110     pub struct ip_mreq {
111         pub imr_multiaddr: in_addr,
112         pub imr_interface: in_addr,
113     }
114 
getsockname(_socket: __wasi_fd_t, _address: *mut sockaddr, _address_len: *mut socklen_t) -> c_int115     pub unsafe fn getsockname(_socket: __wasi_fd_t, _address: *mut sockaddr,
116                     _address_len: *mut socklen_t) -> c_int {
117         unimplemented!()
118     }
connect(_socket: __wasi_fd_t, _address: *const sockaddr, _len: socklen_t) -> c_int119     pub unsafe fn connect(_socket: __wasi_fd_t, _address: *const sockaddr,
120                 _len: socklen_t) -> c_int {
121         unimplemented!()
122     }
listen(_socket: __wasi_fd_t, _backlog: c_int) -> c_int123     pub unsafe fn listen(_socket: __wasi_fd_t, _backlog: c_int) -> c_int {
124         unimplemented!()
125     }
bind(_socket: __wasi_fd_t, _address: *const sockaddr, _address_len: socklen_t) -> c_int126     pub unsafe fn bind(_socket: __wasi_fd_t, _address: *const sockaddr,
127             _address_len: socklen_t) -> c_int {
128         unimplemented!()
129     }
130 
sockaddr_in_u32(sa: &sockaddr_in) -> u32131     pub fn sockaddr_in_u32(sa: &sockaddr_in) -> u32 {
132         ::ntoh((*sa).sin_addr.s_addr)
133     }
134 
in_addr_to_u32(addr: &in_addr) -> u32135     pub fn in_addr_to_u32(addr: &in_addr) -> u32 {
136         ::ntoh(addr.s_addr)
137     }
138 }
139 
140 pub struct Socket {
141     fd: __wasi_fd_t,
142 }
143 
144 impl Socket {
new(_family: c_int, _ty: c_int) -> io::Result<Socket>145     pub fn new(_family: c_int, _ty: c_int) -> io::Result<Socket> {
146         unimplemented!()
147     }
148 
raw(&self) -> libc::__wasi_fd_t149     pub fn raw(&self) -> libc::__wasi_fd_t {
150         self.fd
151     }
152 
into_fd(self) -> libc::__wasi_fd_t153     fn into_fd(self) -> libc::__wasi_fd_t {
154         let fd = self.fd;
155         mem::forget(self);
156         fd
157     }
158 
into_tcp_listener(self) -> TcpListener159     pub fn into_tcp_listener(self) -> TcpListener {
160         unsafe { TcpListener::from_raw_fd(self.into_fd()) }
161     }
162 
into_tcp_stream(self) -> TcpStream163     pub fn into_tcp_stream(self) -> TcpStream {
164         unsafe { TcpStream::from_raw_fd(self.into_fd()) }
165     }
166 
into_udp_socket(self) -> UdpSocket167     pub fn into_udp_socket(self) -> UdpSocket {
168         unsafe { UdpSocket::from_raw_fd(self.into_fd()) }
169     }
170 }
171 
172 impl ::FromInner for Socket {
173     type Inner = libc::__wasi_fd_t;
from_inner(fd: libc::__wasi_fd_t) -> Socket174     fn from_inner(fd: libc::__wasi_fd_t) -> Socket {
175         Socket { fd: fd }
176     }
177 }
178 
179 impl Drop for Socket {
drop(&mut self)180     fn drop(&mut self) {
181         // unsafe {
182         //     let _ = libc::close(self.fd);
183         // }
184     }
185 }