Skip to content

Commit

Permalink
初版Seqpacket socket (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saga1718 authored Sep 11, 2024
1 parent 06ac60f commit e1da5bc
Show file tree
Hide file tree
Showing 6 changed files with 614 additions and 19 deletions.
64 changes: 45 additions & 19 deletions kernel/src/net/socket/base.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,67 @@

#![allow(unused_variables)]

use crate::net::socket::*;
use crate::net::syscall_util::MsgHdr;
use alloc::sync::Arc;
use core::any::Any;
use core::fmt::Debug;
use alloc::sync::Arc;
use system_error::SystemError::{self, *};
use crate::net::socket::*;
use crate::net::syscall_util::MsgHdr;

/// # `Socket` methods
/// ## Reference
/// - [Posix standard](https://pubs.opengroup.org/onlinepubs/9699919799/)
pub trait Socket: Sync + Send + Debug {
pub trait Socket: Sync + Send + Debug + Any{
/// # `wait_queue`
/// 获取socket的wait queue
fn wait_queue(&self) -> WaitQueue;
/// # `socket_poll`
/// 获取socket的事件。
fn poll(&self) -> usize;

fn send_buffer_size(&self) -> usize;
fn recv_buffer_size(&self) -> usize;
/// # `accept`
/// 接受连接,仅用于listening stream socket
/// ## Block
/// 如果没有连接到来,会阻塞
fn accept(&self) -> Result<(Arc<Inode>, Endpoint), SystemError> {
Err(ENOSYS)
}
/// # `bind`

/// # `bind`
/// 对应于POSIX的bind函数,用于绑定到本机指定的端点
fn bind(&self, endpoint: Endpoint) -> Result<(), SystemError> {
Err(ENOSYS)
}

/// # `close`
/// 关闭socket
fn close(&self) -> Result<(), SystemError> {
Err(ENOSYS)
}
/// # `connect`

/// # `connect`
/// 对应于POSIX的connect函数,用于连接到指定的远程服务器端点
fn connect(&self, endpoint: Endpoint) -> Result<(), SystemError> {
Err(ENOSYS)
}

// fnctl
// freeaddrinfo
// getaddrinfo
// getnameinfo

/// # `get_peer_name`
/// 获取对端的地址
fn get_peer_name(&self) -> Result<Endpoint, SystemError> {
Err(ENOSYS)
}

/// # `get_name`
/// 获取socket的地址
fn get_name(&self) -> Result<Endpoint, SystemError> {
Err(ENOSYS)
}

/// # `get_option`
/// 对应于 Posix `getsockopt` ,获取socket选项
fn get_option(
Expand All @@ -66,53 +73,60 @@ pub trait Socket: Sync + Send + Debug {
log::warn!("getsockopt is not implemented");
Ok(0)
}

/// # `listen`
/// 监听socket,仅用于stream socket
fn listen(&self, backlog: usize) -> Result<(), SystemError> {
Err(ENOSYS)
}

// poll
// pselect

/// # `read`
fn read(&self, buffer: &mut [u8]) -> Result<usize, SystemError> {
self.recv(buffer, MessageFlag::empty())
}

/// # `recv`
/// 接收数据,`read` = `recv` with flags = 0
fn recv(&self, buffer: &mut [u8], flags: MessageFlag) -> Result<usize, SystemError> {
Err(ENOSYS)
}

/// # `recv_from`
fn recv_from(
&self,
&self,
buffer: &mut [u8],
flags: MessageFlag,
address: Option<Endpoint>,
) -> Result<(usize, Endpoint), SystemError> {
Err(ENOSYS)
) -> Result<(usize, Endpoint), SystemError>
{
Err(ENOSYS)
}

/// # `recv_msg`
fn recv_msg(&self, msg: &mut MsgHdr, flags: MessageFlag) -> Result<usize, SystemError> {
Err(ENOSYS)
}

// select

/// # `send`
fn send(&self, buffer: &[u8], flags: MessageFlag) -> Result<usize, SystemError> {
Err(ENOSYS)
}

/// # `send_msg`
fn send_msg(&self, msg: &MsgHdr, flags: MessageFlag) -> Result<usize, SystemError> {
Err(ENOSYS)
}

/// # `send_to`
fn send_to(
&self,
buffer: &[u8],
flags: MessageFlag,
address: Endpoint,
) -> Result<usize, SystemError> {
fn send_to(&self, buffer: &[u8], flags: MessageFlag, address: Endpoint) -> Result<usize, SystemError> {
Err(ENOSYS)
}

/// # `set_option`
/// Posix `setsockopt` ,设置socket选项
/// ## Parameters
Expand All @@ -121,22 +135,34 @@ pub trait Socket: Sync + Send + Debug {
/// - value 选项的值
/// ## Reference
/// https://code.dragonos.org.cn/s?refs=sk_setsockopt&project=linux-6.6.21
fn set_option(&self, level: OptionsLevel, name: usize, val: &[u8]) -> Result<(), SystemError> {
fn set_option(
&self,
level: OptionsLevel,
name: usize,
val: &[u8],
) -> Result<(), SystemError> {
log::warn!("setsockopt is not implemented");
Ok(())
}

/// # `shutdown`
fn shutdown(&self, how: ShutdownTemp) -> Result<(), SystemError> {
Err(ENOSYS)
}

// sockatmark
// socket
// socketpair

/// # `write`
fn write(&self, buffer: &[u8]) -> Result<usize, SystemError> {
self.send(buffer, MessageFlag::empty())
}

fn send_buffer_size(&self) -> usize;
fn recv_buffer_size(&self) -> usize;

// fn write_buffer(&self, _buf: &[u8]) -> Result<usize, SystemError> {
// todo!()
// }
}
}
4 changes: 4 additions & 0 deletions kernel/src/net/socket/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl Buffer {
return self.read_buffer.lock().is_empty();
}

pub fn is_read_buf_full(&self) ->bool {
return self.metadata.buf_size-self.read_buffer.lock().len()==0
}

pub fn is_write_buf_empty(&self) -> bool {
return self.write_buffer.lock().is_empty();
}
Expand Down
4 changes: 4 additions & 0 deletions kernel/src/net/socket/inode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,8 @@ impl Inode {
pub fn set_close_on_exec(&self, close_on_exec: bool) {
todo!()
}

pub fn inner(&self)->Arc<dyn Socket>{
return self.inner.clone();
}
}
4 changes: 4 additions & 0 deletions kernel/src/net/socket/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod stream;
mod seqpacket;
use crate::net::socket::*;
use system_error::SystemError::{self, *};
use alloc::sync::Arc;
Expand All @@ -10,6 +11,9 @@ fn create_unix_socket(
match sock_type {
Type::Stream => {
Ok(stream::StreamSocket::new())
},
Type::SeqPacket =>{
Ok(seqpacket::SeqpacketSocket::new(false))
}
_ => {
Err(EPROTONOSUPPORT)
Expand Down
Loading

0 comments on commit e1da5bc

Please sign in to comment.