forked from DragonOS-Community/DragonOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters