|
|
|
@ -82,6 +82,18 @@ struct SockaddrIn {
|
|
|
|
|
sin_zero: [u8; 8],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_handle(proc: &mut MutexGuard<'static, Process>, fd: usize) -> Result<SocketHandle, SysError> {
|
|
|
|
|
if let Some(file) = proc.files.get(&fd) {
|
|
|
|
|
if let FileLike::Socket(handle) = file {
|
|
|
|
|
return Ok((*handle).clone());
|
|
|
|
|
} else {
|
|
|
|
|
Err(SysError::ENOTSOCK)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Err(SysError::EBADF)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_connect(fd: usize, addr: *const u8, addrlen: usize) -> SysResult {
|
|
|
|
|
info!(
|
|
|
|
|
"sys_connect: fd: {}, addr: {:?}, addrlen: {}",
|
|
|
|
@ -110,9 +122,8 @@ pub fn sys_connect(fd: usize, addr: *const u8, addrlen: usize) -> SysResult {
|
|
|
|
|
let iface = &mut *NET_DRIVERS.lock()[0];
|
|
|
|
|
iface.poll(&mut proc.sockets);
|
|
|
|
|
|
|
|
|
|
if let Some(FileLike::Socket(handle)) = proc.files.get(&fd) {
|
|
|
|
|
// TODO: check its type
|
|
|
|
|
let tcp_handle = (*handle).clone();
|
|
|
|
|
let tcp_handle = get_handle(&mut proc, fd)?;
|
|
|
|
|
let mut socket = proc.sockets.get::<TcpSocket>(tcp_handle);
|
|
|
|
|
|
|
|
|
|
// TODO selects non-conflict high port
|
|
|
|
@ -130,9 +141,6 @@ pub fn sys_connect(fd: usize, addr: *const u8, addrlen: usize) -> SysResult {
|
|
|
|
|
Ok(()) => Ok(0),
|
|
|
|
|
Err(_) => Err(SysError::EISCONN),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Err(SysError::ENOTSOCK)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_write_socket(
|
|
|
|
@ -145,9 +153,8 @@ pub fn sys_write_socket(
|
|
|
|
|
let iface = &mut *NET_DRIVERS.lock()[0];
|
|
|
|
|
iface.poll(&mut proc.sockets);
|
|
|
|
|
|
|
|
|
|
if let Some(FileLike::Socket(handle)) = proc.files.get(&fd) {
|
|
|
|
|
// TODO: check its type
|
|
|
|
|
let tcp_handle = (*handle).clone();
|
|
|
|
|
let tcp_handle = get_handle(proc, fd)?;
|
|
|
|
|
let mut socket = proc.sockets.get::<TcpSocket>(tcp_handle);
|
|
|
|
|
let slice = unsafe { slice::from_raw_parts(base, len) };
|
|
|
|
|
if socket.is_open() {
|
|
|
|
@ -162,9 +169,6 @@ pub fn sys_write_socket(
|
|
|
|
|
} else {
|
|
|
|
|
Err(SysError::ECONNREFUSED)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Err(SysError::ENOTSOCK)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_select(
|
|
|
|
|