-
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.
Signed-off-by: rayylee <[email protected]>
- Loading branch information
1 parent
efd46be
commit 92b6174
Showing
7 changed files
with
161 additions
and
15 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
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 |
---|---|---|
@@ -1,12 +1,87 @@ | ||
extern crate alloc; | ||
use alloc::sync::Arc; | ||
use alloc::collections::VecDeque; | ||
use core::cmp::min; | ||
|
||
use axsync::Mutex; | ||
use axerrno::AxResult; | ||
|
||
struct NetlinkBuffer<T> { | ||
buffer: VecDeque<T>, | ||
capacity: usize, | ||
} | ||
|
||
impl<T> NetlinkBuffer<T> { | ||
fn new(capacity: usize) -> Self { | ||
NetlinkBuffer { | ||
buffer: VecDeque::with_capacity(capacity), | ||
capacity, | ||
} | ||
} | ||
|
||
fn push(&mut self, element: T) { | ||
if self.buffer.len() == self.capacity { | ||
self.buffer.pop_front(); | ||
} | ||
self.buffer.push_back(element); | ||
} | ||
|
||
fn pop(&mut self) -> Option<T> { | ||
self.buffer.pop_front() | ||
} | ||
|
||
fn len(&self) -> usize { | ||
self.buffer.len() | ||
} | ||
|
||
fn capacity(&self) -> usize { | ||
self.capacity | ||
} | ||
} | ||
|
||
pub struct NlSocket { | ||
rx_buffer: Arc<Mutex<NetlinkBuffer<u8>>>, | ||
tx_buffer: Arc<Mutex<NetlinkBuffer<u8>>>, | ||
pub nl_groups: u32, | ||
} | ||
|
||
impl NlSocket { | ||
pub fn new() -> Self { | ||
NlSocket {} | ||
NlSocket { | ||
rx_buffer: Arc::new(Mutex::new(NetlinkBuffer::new(1024))), | ||
tx_buffer: Arc::new(Mutex::new(NetlinkBuffer::new(1024))), | ||
nl_groups: 0, | ||
} | ||
} | ||
|
||
pub fn bind(&mut self, groups: u32) -> AxResult { | ||
self.nl_groups = groups; | ||
Ok(()) | ||
} | ||
|
||
pub fn send(&self, buf: &[u8]) -> AxResult<usize> { | ||
let mut rx = self.rx_buffer.lock(); | ||
for byte in buf.iter() { | ||
rx.push(*byte); | ||
} | ||
Ok(buf.len()) | ||
} | ||
|
||
pub fn fill_tx(&self, buf: &[u8]) -> AxResult<usize> { | ||
let mut tx = self.tx_buffer.lock(); | ||
let length = min(buf.len(), tx.capacity() - tx.len()); | ||
for i in 0..length { | ||
tx.push(buf[i]); | ||
} | ||
Ok(length) | ||
} | ||
|
||
pub fn recv_from(&self, buf: &mut [u8]) -> AxResult<usize> { | ||
let mut tx = self.tx_buffer.lock(); | ||
let length = min(buf.len(), tx.len()); | ||
for i in 0..length { | ||
buf[i] = tx.pop().unwrap(); | ||
} | ||
Ok(length) | ||
} | ||
} |
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
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
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
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,36 @@ | ||
use axprocess::current_process; | ||
use super::socket::{Socket, SocketInner}; | ||
|
||
pub fn netlink_unicast() | ||
{ | ||
let process = current_process(); | ||
let fd_table = process.fd_manager.fd_table.lock(); | ||
|
||
// # define NLMSG_OK(nlh,len) ((len) >= (int)sizeof(struct nlmsghdr) && \ | ||
// (nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \ | ||
// (nlh)->nlmsg_len <= (len)) | ||
|
||
let mut buffer: [u8; 20] = [0; 20]; | ||
buffer[0] = 16; // nlmsg_len: sizeof(struct nlmsghdr) | ||
buffer[4] = 16; // nlmsg_type: RTM_NEWLINK | ||
buffer[6] = 65; // 'A' | ||
buffer[7] = 66; // 'B' | ||
buffer[8] = 67; // 'C' | ||
buffer[9] = 68; // 'D' | ||
buffer[10] = 69; // 'E' | ||
|
||
for i in 0..fd_table.len() { | ||
if let Some(file) = fd_table[i].as_ref() { | ||
if let Some(s_file) = file.as_any().downcast_ref::<Socket>() { | ||
let inner = s_file.inner.lock(); | ||
if let SocketInner::Netlink(s) = &*inner { | ||
let _ = s.fill_tx(&buffer); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
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