Skip to content

Commit

Permalink
feat: 实现unix datagram结构体
Browse files Browse the repository at this point in the history
  • Loading branch information
smallcjy committed Sep 10, 2024
1 parent 3c68fa7 commit b350aca
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
45 changes: 45 additions & 0 deletions kernel/src/net/socket/unix/datagram/inner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::net::socket::Endpoint;
use system_error::SystemError;

#[derive(Debug)]
pub enum Inner {
Unbound(Unbound),
Bound(Bound) ,
}

#[derive(Debug)]
pub struct Unbound {
local_addr: Option<Endpoint>,
}

impl Unbound {
pub fn new() -> Self {
Self {
local_addr: None,
}

}

pub fn do_bind(&self, local_endpoint: Option<Endpoint>) -> Result<Bound, SystemError> {
if self.local_addr.is_some() {
return Err(SystemError::EINVAL);
}
let bound = Bound::new(local_endpoint);
return Ok(bound);
}
}

#[derive(Debug)]
pub struct Bound {
local_addr: Option<Endpoint>,
remote_addr: Option<Endpoint>,
}

impl Bound {
pub fn new(local_addr: Option<Endpoint>) -> Self{
Self {
local_addr,
remote_addr: None,
}
}
}
155 changes: 155 additions & 0 deletions kernel/src/net/socket/unix/datagram/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use alloc::sync::Arc;
use alloc::sync::Weak;
use inner::Inner;
use inner::Unbound;
use system_error::SystemError;
use crate::libs::rwlock::RwLock;

use super::Endpoint;
use super::Socket;
use super::{buffer::Buffer, EPollItems, Shutdown, WaitQueue};

mod inner;

#[derive(Debug)]
pub struct DatagramSocket {
buffer: Arc<Buffer>,
inner: RwLock<Inner>,
shutdown: Shutdown,
epitems: EPollItems,
wait_queue: WaitQueue,
self_ref: Weak<Self>,
}

impl DatagramSocket {
pub fn new() -> Arc<Self> {
Arc::new_cyclic(|me| Self{
buffer: Buffer::new(),
inner: RwLock::new(Inner::Unbound(Unbound::new())),
shutdown: Shutdown::default(),
epitems: EPollItems::default(),
wait_queue: WaitQueue::default(),
self_ref: me.clone()
})
}

pub fn bind(&self, local_endpoint: Endpoint) -> Result<(), SystemError> {
let mut inner = self.inner.write();
match & *inner {
Inner::Bound(_) => return Err(SystemError::EINVAL),
Inner::Unbound(unbound) => {
let bound = unbound.do_bind(Some(local_endpoint))?;
*inner = Inner::Bound(bound);
return Ok(());
}
}
}
}

impl Socket for DatagramSocket {
fn accept(&self) -> Result<(Arc<super::Inode>, Endpoint), SystemError> {
Err(SystemError::ENOSYS)
}

fn bind(&self, endpoint: Endpoint) -> Result<(), SystemError> {
Err(SystemError::ENOSYS)
}

fn close(&self) -> Result<(), SystemError> {
Err(SystemError::ENOSYS)
}

fn connect(&self, endpoint: Endpoint) -> Result<(), SystemError> {
Err(SystemError::ENOSYS)
}

fn get_peer_name(&self) -> Result<Endpoint, SystemError> {
Err(SystemError::ENOSYS)
}

fn get_name(&self) -> Result<Endpoint, SystemError> {
Err(SystemError::ENOSYS)
}

fn get_option(
&self,
level: super::OptionsLevel,
name: usize,
value: &mut [u8],
) -> Result<usize, SystemError> {
log::warn!("getsockopt is not implemented");
Ok(0)
}

fn listen(&self, backlog: usize) -> Result<(), SystemError> {
Err(SystemError::ENOSYS)
}

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

fn recv(&self, buffer: &mut [u8], flags: super::MessageFlag) -> Result<usize, SystemError> {
Err(SystemError::ENOSYS)
}

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

fn recv_msg(&self, msg: &mut crate::net::syscall::MsgHdr, flags: super::MessageFlag) -> Result<usize, SystemError> {
Err(SystemError::ENOSYS)
}

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

fn send_msg(&self, msg: &crate::net::syscall::MsgHdr, flags: super::MessageFlag) -> Result<usize, SystemError> {
Err(SystemError::ENOSYS)
}

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

fn set_option(&self, level: super::OptionsLevel, name: usize, val: &[u8]) -> Result<(), SystemError> {
log::warn!("setsockopt is not implemented");
Ok(())
}

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

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

fn wait_queue(&self) -> WaitQueue {
todo!()
}

fn poll(&self) -> usize {
todo!()
}

fn send_buffer_size(&self) -> usize {
todo!()
}

fn recv_buffer_size(&self) -> usize {
todo!()
}
}

1 change: 1 addition & 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 datagram;
use crate::net::socket::*;
use system_error::SystemError::{self, *};
use alloc::sync::Arc;
Expand Down

0 comments on commit b350aca

Please sign in to comment.